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

Handle larger changes #22

Merged
merged 1 commit into from
Nov 25, 2023
Merged
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
115 changes: 69 additions & 46 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ export type RowOf<T extends Table<any>> = T extends Table<infer R> ? FullRow<R>
const defaultEndpoint = "https://api.glideapp.io/api/function";
const defaultEndpointREST = "https://functions.prod.internal.glideapps.com/api";

const MAX_MUTATIONS = 500;

async function chunked<TItem, TResult>(
array: TItem[],
chunkSize: number,
callback: (chunk: TItem[]) => Promise<TResult>
): Promise<TResult[]> {
const results: TResult[] = [];
for (let i = 0; i < array.length; i += chunkSize) {
const result = await callback(array.slice(i, i + chunkSize));
results.push(result);
}
return results;
}

export class Table<T extends ColumnSchema> {
private props: TableProps<T>;

Expand Down Expand Up @@ -125,24 +140,28 @@ export class Table<T extends ColumnSchema> {

const renamedRows = this.renameOutgoing(rows);

const response = await fetch(this.endpoint("/mutateTables"), {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
appID: app,
mutations: renamedRows.map(row => ({
kind: "add-row-to-table",
tableName: table,
columnValues: row,
})),
}),
const addedIds = await chunked(renamedRows, MAX_MUTATIONS, async chunk => {
const response = await fetch(this.endpoint("/mutateTables"), {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
appID: app,
mutations: chunk.map(row => ({
kind: "add-row-to-table",
tableName: table,
columnValues: row,
})),
}),
});

const added = await response.json();
return added.map((row: any) => row.rowID) as string[];
});

const added = await response.json();
return added.map((row: any) => row.rowID);
return addedIds.flat();
}

/**
Expand Down Expand Up @@ -172,23 +191,25 @@ export class Table<T extends ColumnSchema> {
async setRows(rows: { id: RowIdentifiable<T>; row: Row<T> }[]): Promise<void> {
const { token, app, table } = this.props;

await fetch(this.endpoint("/mutateTables"), {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
appID: app,
mutations: rows.map(({ id, row }) => {
return {
kind: "set-columns-in-row",
tableName: table,
columnValues: this.renameOutgoing([row])[0],
rowID: rowID(id),
};
await chunked(rows, MAX_MUTATIONS, async chunk => {
await fetch(this.endpoint("/mutateTables"), {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
appID: app,
mutations: chunk.map(({ id, row }) => {
return {
kind: "set-columns-in-row",
tableName: table,
columnValues: this.renameOutgoing([row])[0],
rowID: rowID(id),
};
}),
}),
}),
});
});
}

Expand All @@ -212,20 +233,22 @@ export class Table<T extends ColumnSchema> {
public async deleteRows(rows: RowIdentifiable<T>[]): Promise<void> {
const { token, app, table } = this.props;

await fetch(this.endpoint("/mutateTables"), {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
appID: app,
mutations: rows.map(row => ({
kind: "delete-row",
tableName: table,
rowID: rowID(row),
})),
}),
await chunked(rows, MAX_MUTATIONS, async chunk => {
await fetch(this.endpoint("/mutateTables"), {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
appID: app,
mutations: chunk.map(row => ({
kind: "delete-row",
tableName: table,
rowID: rowID(row),
})),
}),
});
});
}

Expand Down
Loading