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

feat(client-sdk): allow to pass synchronous flag for kwil executions #367

Merged
merged 5 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const useDeleteCredentialMutation = () => {

return useMutation<{ id: string }, DefaultError, { id: string; credential_type: string }, Ctx>({
mutationFn: ({ id, credential_type }) =>
sdk.data.delete("credentials", id, `Delete credential ${credential_type} from idOS`, true),
sdk.data.delete("credentials", id, `Delete credential ${credential_type} from idOS`),
async onMutate({ id }) {
await queryClient.cancelQueries({ queryKey: ["credentials"] });
const previousCredentials = queryClient.getQueryData<idOSCredential[]>(["credentials"]) ?? [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ const useAddWalletMutation = () => {
mutationFn: async ({ address, publicKeys }) => {
const payload = publicKeys.map((public_key) => createWalletFactory({ address, public_key }));

if (payload.length > 0) return await sdk.data.createMultiple("wallets", payload, true);
if (payload.length > 0) return await sdk.data.createMultiple("wallets", payload);

return await sdk.data.create("wallets", createWalletFactory({ address }), true);
return await sdk.data.create("wallets", createWalletFactory({ address }));
},

onMutate: async ({ address, publicKeys }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const useDeleteWalletMutation = () => {
"wallets",
wallets.map((wallet) => wallet.id),
"Delete wallet from idOS",
true,
),
async onMutate(wallets) {
await queryClient.cancelQueries({ queryKey: ["wallets"] });
Expand Down
43 changes: 6 additions & 37 deletions packages/idos-sdk-js/src/lib/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ export class Data {
return this.enclave.filterCredentialsByCountries(credentialsWithContent, countries);
}

async createMultiple<T extends AnyRecord>(
tableName: string,
records: T[],
synchronous?: boolean,
) {
async createMultiple<T extends AnyRecord>(tableName: string, records: T[]) {
let receiverPublicKey: string | undefined;

if (tableName === "credentials") {
Expand All @@ -103,7 +99,6 @@ export class Data {
`add_${this.singularize(tableName)}`,
newRecords,
`Create new ${this.singularize(tableName)} in your idOS profile`,
synchronous,
);

return newRecords;
Expand All @@ -112,7 +107,6 @@ export class Data {
async create<T extends { id: string }>(
tableName: string,
record: Omit<T, "id">,
synchronous?: boolean,
): Promise<Omit<T, "id"> & { id: string }> {
const name = `add_${this.singularize(
tableName === "human_attributes" ? "attributes" : tableName,
Expand Down Expand Up @@ -148,7 +142,6 @@ export class Data {
`add_${this.singularize(tableName)}`,
[newRecord],
`Create new ${this.singularize(tableName)} in your idOS profile`,
synchronous,
);

return newRecord;
Expand Down Expand Up @@ -223,32 +216,16 @@ export class Data {
tableName: string,
recordIds: string[],
description?: string,
synchronous?: boolean,
): Promise<{ id: string }[]> {
const records = recordIds.map((id) => ({ id }));
await this.kwilWrapper.execute(
`remove_${this.singularize(tableName)}`,
records,
description,
synchronous,
);
await this.kwilWrapper.execute(`remove_${this.singularize(tableName)}`, records, description);

return records;
}

async delete(
tableName: string,
recordId: string,
description?: string,
synchronous?: boolean,
): Promise<{ id: string }> {
async delete(tableName: string, recordId: string, description?: string): Promise<{ id: string }> {
const record = { id: recordId };
await this.kwilWrapper.execute(
`remove_${this.singularize(tableName)}`,
[record],
description,
synchronous,
);
await this.kwilWrapper.execute(`remove_${this.singularize(tableName)}`, [record], description);

return record;
}
Expand All @@ -257,7 +234,6 @@ export class Data {
tableName: string,
recordLike: T,
description?: string,
synchronous?: boolean,
): Promise<T> {
if (!this.enclave.encryptionPublicKey) await this.enclave.ready();

Expand All @@ -278,12 +254,7 @@ export class Data {
);
}

await this.kwilWrapper.execute(
`edit_${this.singularize(tableName)}`,
[record],
description,
synchronous,
);
await this.kwilWrapper.execute(`edit_${this.singularize(tableName)}`, [record], description);

return record;
}
Expand Down Expand Up @@ -322,14 +293,13 @@ export class Data {
},
],
`Share a ${name} on idOS`,
true,
);

return { id };
}

async unshare(tableName: string, recordId: string): Promise<{ id: string }> {
return await this.delete(tableName, recordId);
return await this.delete(tableName, recordId, undefined);
}

async addWriteGrant(grantee: string) {
Expand All @@ -341,7 +311,6 @@ export class Data {
},
],
`Grant ${grantee} write access to your idOS credentials`,
true,
);
}

Expand Down
9 changes: 2 additions & 7 deletions packages/idos-sdk-js/src/lib/kwil-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,11 @@ export class KwilWrapper {
return res.data?.result;
}

async execute(
actionName: string,
actionInputs: Record<string, unknown>[],
description?: string,
synchronous?: boolean,
) {
async execute(actionName: string, actionInputs: Record<string, unknown>[], description?: string) {
if (!this.signer) throw new Error("No signer set");

const action = await this.buildAction(actionName, actionInputs, description);
const res = await this.client.execute(action, this.signer, synchronous);
const res = await this.client.execute(action, this.signer, true);
return res.data?.tx_hash;
}

Expand Down