Skip to content

Commit

Permalink
Revert "feat: remove sycnhronous flags from data. use sync by default"
Browse files Browse the repository at this point in the history
This reverts commit d6b1404.
  • Loading branch information
ditoglez committed Nov 29, 2024
1 parent d6b1404 commit 0be8705
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
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`),
sdk.data.delete("credentials", id, `Delete credential ${credential_type} from idOS`, true),
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);
if (payload.length > 0) return await sdk.data.createMultiple("wallets", payload, true);

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

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

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

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

return newRecords;
Expand All @@ -107,6 +112,7 @@ 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 @@ -142,6 +148,7 @@ export class Data {
`add_${this.singularize(tableName)}`,
[newRecord],
`Create new ${this.singularize(tableName)} in your idOS profile`,
synchronous,
);

return newRecord;
Expand Down Expand Up @@ -216,16 +223,32 @@ 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);
await this.kwilWrapper.execute(
`remove_${this.singularize(tableName)}`,
records,
description,
synchronous,
);

return records;
}

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

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

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

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

return record;
}
Expand All @@ -263,6 +292,7 @@ export class Data {
tableName: string,
recordId: string,
receiverPublicKey: string,
synchronous?: boolean,
): Promise<{ id: string }> {
const name = this.singularize(tableName);

Expand Down Expand Up @@ -293,16 +323,21 @@ 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, undefined);
async unshare(
tableName: string,
recordId: string,
synchronous?: boolean,
): Promise<{ id: string }> {
return await this.delete(tableName, recordId, undefined, synchronous);
}

async addWriteGrant(grantee: string) {
async addWriteGrant(grantee: string, synchronous?: boolean) {
return await this.kwilWrapper.execute(
"add_write_grant",
[
Expand All @@ -311,6 +346,7 @@ export class Data {
},
],
`Grant ${grantee} write access to your idOS credentials`,
true,
);
}

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

async execute(actionName: string, actionInputs: Record<string, unknown>[], description?: string) {
async execute(
actionName: string,
actionInputs: Record<string, unknown>[],
description?: string,
synchronous = true,
) {
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, true);
const res = await this.client.execute(action, this.signer, synchronous);
return res.data?.tx_hash;
}

Expand Down

0 comments on commit 0be8705

Please sign in to comment.