Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding insertBuklVectorData fuction in supabase class #346

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions JS/edgechains/vector-db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.Supabase = void 0;
var supabase_js_1 = require("./src/lib/supabase/supabase.js");
Object.defineProperty(exports, "Supabase", {
enumerable: true,
get: function () {
return supabase_js_1.Supabase;
},
});
Object.defineProperty(exports, "Supabase", { enumerable: true, get: function () { return supabase_js_1.Supabase; } });
58 changes: 28 additions & 30 deletions JS/edgechains/vector-db/src/lib/supabase/supabase.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ interface InsertVectorDataArgs {
tableName: string;
[key: string]: any;
}
interface InsertBulkVectorDataArgs {
client: SupabaseClient;
tableName: string;
data: Array<{
[key: string]: any;
}>;
}
interface GetDataFromQueryArgs {
client: SupabaseClient;
functionNameToCall: string;
Expand All @@ -18,15 +25,23 @@ export declare class Supabase {
constructor(SUPABASE_URL: string, SUPABASE_API_KEY: string);
createClient(): SupabaseClient<any, "public", any>;
/**
* Insert data into a vector database using a Supabase client.
* @param client The Supabase client instance.
* @param relation The name of the relation (table) to insert data into.
* @param content The content to insert.
* @param embedding The embedding data to insert.
* @returns The inserted data if successful.
* @throws Error if insertion fails.
*/
insertVectorData({ client, tableName, ...args }: InsertVectorDataArgs): Promise<any>;
* Insert data into a vector database using a Supabase client.
* @param client The Supabase client instance.
* @param relation The name of the relation (tableName) to insert data into.
* @param content The content to insert.
* @returns The inserted data if successful.
* @throws Error if insertion fails.
*/
insertVectorData({ client, tableName, ...args }: InsertVectorDataArgs | InsertBulkVectorDataArgs): Promise<any>;
/**
* Insert Bulk data into a vector database using a Supabase client.
* @param client The Supabase client instance.
* @param relation The name of the relation (table) to insert data into.
* @param args The array of objects containing the data to be inserted.
* @returns The inserted data if successful.
* @throws Error if insertion fails.
*/
insertBulkVectorData({ client, tableName, data }: InsertBulkVectorDataArgs): Promise<any>;
/**
* fetch data from vector database using a Supabase client
* @param client - The Supabase client instance.
Expand All @@ -43,11 +58,7 @@ export declare class Supabase {
* @returns The fetched data if successful.
* @throws Error if fetching fails.
*/
getData({
client,
tableName,
columns,
}: {
getData({ client, tableName, columns }: {
client: SupabaseClient;
tableName: string;
columns: string;
Expand All @@ -60,11 +71,7 @@ export declare class Supabase {
* @returns The fetched data if successful.
* @throws Error if fetching fails.
*/
getDataById({
client,
tableName,
id,
}: {
getDataById({ client, tableName, id }: {
client: SupabaseClient;
tableName: string;
id: number;
Expand All @@ -77,12 +84,7 @@ export declare class Supabase {
* @returns Updated data if Success
* @throws Error if fetching fails.
*/
updateById({
client,
tableName,
id,
updatedContent,
}: {
updateById({ client, tableName, id, updatedContent }: {
client: SupabaseClient;
tableName: string;
id: number;
Expand All @@ -96,11 +98,7 @@ export declare class Supabase {
* @returns Success
* @throws Error if deleting is fails.
*/
deleteById({
client,
tableName,
id,
}: {
deleteById({ client, tableName, id }: {
client: SupabaseClient;
tableName: string;
id: number;
Expand Down
120 changes: 77 additions & 43 deletions JS/edgechains/vector-db/src/lib/supabase/supabase.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"use strict";
var __importDefault =
(this && this.__importDefault) ||
function (mod) {
return mod && mod.__esModule ? mod : { default: mod };
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Supabase = void 0;
const supabase_js_1 = require("@supabase/supabase-js");
Expand All @@ -22,14 +20,13 @@ class Supabase {
return (0, supabase_js_1.createClient)(this.SUPABASE_URL, this.SUPABASE_API_KEY);
}
/**
* Insert data into a vector database using a Supabase client.
* @param client The Supabase client instance.
* @param relation The name of the relation (table) to insert data into.
* @param content The content to insert.
* @param embedding The embedding data to insert.
* @returns The inserted data if successful.
* @throws Error if insertion fails.
*/
* Insert data into a vector database using a Supabase client.
* @param client The Supabase client instance.
* @param relation The name of the relation (tableName) to insert data into.
* @param content The content to insert.
* @returns The inserted data if successful.
* @throws Error if insertion fails.
*/
async insertVectorData({ client, tableName, ...args }) {
return new Promise((resolve, reject) => {
const operation = retry_1.default.operation({
Expand All @@ -43,18 +40,55 @@ class Supabase {
try {
const res = await client.from(tableName).insert(args);
if (res.error?.message) {
if (operation.retry(new Error())) {
if (operation.retry(new Error)) {
return;
}
reject(
new Error(
`Failed to insert ${JSON.stringify(args)} with error message "${res.error.message}"`
)
);
} else {
reject(new Error(`Failed to insert ${tableName} with error message "${res.error.message}"`));
}
else {
resolve(res);
}
} catch (error) {
}
catch (error) {
if (operation.retry(error)) {
return;
}
reject(error);
}
});
});
}
/**
* Insert Bulk data into a vector database using a Supabase client.
* @param client The Supabase client instance.
* @param relation The name of the relation (table) to insert data into.
* @param args The array of objects containing the data to be inserted.
* @returns The inserted data if successful.
* @throws Error if insertion fails.
*/
async insertBulkVectorData({ client, tableName, data }) {
return new Promise((resolve, reject) => {
const operation = retry_1.default.operation({
retries: 5,
factor: 3,
minTimeout: 1 * 1000,
maxTimeout: 60 * 1000,
randomize: true,
});
operation.attempt(async (currentAttempt) => {
try {
const res = await client.from(tableName).insert(data);
if (res.error?.message) {
if (operation.retry(new Error)) {
return;
}
reject(new Error(`Failed to insert ${tableName} with error message "${res.error.message}"`));
}
else {
resolve(res);
}
}
catch (error) {
if (operation.retry(error)) {
return;
}
Expand Down Expand Up @@ -83,19 +117,18 @@ class Supabase {
operation.attempt(async (currentAttempt) => {
try {
let res = await client.rpc(functionNameToCall, args);
console.log(res);
if (res.status == 200) {
resolve(res.data);
} else {
if (operation.retry(new Error())) return;
reject(
new Error(
`Failed with ErrorCode:${res.statusText} and ErrorMessage:${res.data}`
)
);
}
} catch (error) {
if (operation.retry(error)) return;
else {
if (operation.retry(new Error))
return;
reject(new Error(`Failed with ErrorCode:${res.statusText} and ErrorMessage:${res.data}`));
}
}
catch (error) {
if (operation.retry(error))
return;
reject(error);
}
});
Expand Down Expand Up @@ -124,10 +157,13 @@ class Supabase {
if (data) {
resolve(data);
}
if (operation.retry(new Error())) return;
if (operation.retry(new Error))
return;
reject(error);
} catch (error) {
if (operation.retry(new Error())) return;
}
catch (error) {
if (operation.retry(new Error))
return;
reject(error);
}
});
Expand All @@ -149,7 +185,8 @@ class Supabase {
return data;
}
return error;
} catch (error) {
}
catch (error) {
console.error("Error inserting data into vector database:", error);
throw error;
}
Expand All @@ -165,17 +202,13 @@ class Supabase {
async updateById({ client, tableName, id, updatedContent }) {
try {
// Insert data into the specified relation
const { data, error } = await client
.from(tableName)
.update(updatedContent)
.eq("id", id)
.select()
.single();
const { data, error } = await client.from(tableName).update(updatedContent).eq("id", id).select().single();
if (data) {
return data;
}
return error;
} catch (error) {
}
catch (error) {
console.error("Error inserting data into vector database:", error);
throw error;
}
Expand All @@ -193,7 +226,8 @@ class Supabase {
// Insert data into the specified relation
const res = await client.from(tableName).delete().eq("id", id);
return { status: res.status, messages: res.statusText };
} catch (error) {
}
catch (error) {
console.error("Error inserting data into vector database:", error);
throw error;
}
Expand Down
Loading
Loading