Skip to content
Draft
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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@types/jest": "^29.5.0",
"@types/lodash": "^4.14.202",
"@types/prompt-sync": "^4.2.3",
"@types/uuid": "^10.0.0",
"dotenv": "^16.3.1",
"esbuild": "^0.19.7",
"jest": "^29.5.0",
Expand All @@ -35,6 +36,7 @@
"dependencies": {
"cross-fetch": "^3.1.5",
"lodash": "^4.17.21",
"prompt-sync": "^4.2.0"
"prompt-sync": "^4.2.0",
"uuid": "^10.0.0"
}
}
2 changes: 1 addition & 1 deletion src/App.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Glide } from "./Glide";
import { Table } from "./Table";
import type { TableProps, ColumnSchema, AppProps, IDName, AppManifest } from "./types";
import type { TableProps, ColumnSchema, AppProps, IDName, AppManifest, Row, RowID } from "./types";

import fetch from "cross-fetch";

Expand Down
176 changes: 176 additions & 0 deletions src/BigTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import { QueryBuilder } from "./QueryBuilder";
import { v4 as uuidv4 } from "uuid";
import type {
TableProps,
Row,
ColumnSchema,
RowID,
FullRow,
Query,
ToSQL,
RowIdentifiable,
NullableRow,
NullableFullRow,
APITableSchema,
} from "./types";
import { MAX_MUTATIONS } from "./constants";
import { throwError } from "./common";
import { Glide } from "./Glide";
import { mapChunks } from "./Table";
import { Stash } from "./Stash";

/**
* Class to interact with the Glide API v2 with functionalities reserved for Big Tables.
*/
export class BigTable<T extends ColumnSchema = {}> {
private displayNameToName: Record<keyof FullRow<T>, string>;

/**
* @returns The table id.
*/
public get id(): string {
return this.props.table;
}

/**
* @returns The display name
*/
public get name() {
return this.props.name;
}
constructor(private props: Omit<TableProps<T>, "app">, private glide: Glide) {
const { columns } = props;
this.displayNameToName = Object.fromEntries(
Object.entries(columns).map(([displayName, value]) =>
typeof value !== "string" && typeof value.name === "string"
? [displayName, value.name /* internal name */]
: [displayName, displayName]
)
) as Record<keyof T, string>;
this.displayNameToName["$rowID"] = "$rowID";
}

private renameOutgoing(rows: NullableRow<T>[]): NullableRow<T>[] {
const rename = this.displayNameToName;
return rows.map(
row =>
Object.fromEntries(
Object.entries(row).map(([key, value]) => [
rename[key] ?? key,
// null is sent as an empty string
value === null ? "" : value,
])
) as NullableRow<T>
);
}

/**
* Add a row to the table.
*
* @param row A row to add.
*/
public async add(row: Row<T>): Promise<RowID>;

/**
* Adds rows to the table.
*
* @param rows An array of rows to add to the table.
*/
public async add(rows: Row<T>[]): Promise<RowID[]>;

async add(rowOrRows: Row<T> | Row<T>[]): Promise<RowID | RowID[]> {
const { table } = this.props;

const rows = Array.isArray(rowOrRows) ? rowOrRows : [rowOrRows];
const renamedRows = this.renameOutgoing(rows);

const addedIds = await mapChunks(renamedRows, MAX_MUTATIONS, async chunk => {
const response = await this.glide.post(`/tables/${table}/rows`, chunk);
await throwError(response);

const {
data: { rowIDs },
} = await response.json();
return rowIDs;
});

const rowIDs = addedIds.flat();
return Array.isArray(rowOrRows) ? rowIDs : rowIDs[0];
}

/**
* Creates a new Stash object for the BigTable.
*
* @returns The newly created Stash object.
*/
createStash(): Stash<T> {
// const stashId: string = "20240215-job32";
// const stashId: string = Math.random().toString(36).substring(2, 15);
// TODO: use a better stash id (for now using uuid v4 because no other stashId seems to work)
const stashId: string = uuidv4();
return new Stash({ stashId, bigTable: this }, this.glide);
}

/**
* Adds a Stash to the BigTable.
*
* @param stash The Stash to add.
* @returns A promise that resolves to an array of row IDs if successful, or undefined.
*/
async addStash(stash: Stash<T>): Promise<RowID[]> {
const response = await this.glide.post(`/tables/${this.id}/rows`, {
$stashID: stash.stashId,
});
await throwError(response);

const {
data: { rowIDs },
} = await response.json();
return rowIDs;
}

/**
* Overwrites a row or rows in the BigTable.
*
* @param rowOrRows The row or rows to overwrite.
* @returns A promise that resolves to the row ID or an array of row IDs if successful, or undefined.
*/
async overwrite(rowOrRows: Row<T> | Row<T>[]): Promise<RowID | RowID[]> {
const { table } = this.props;

const rows = Array.isArray(rowOrRows) ? rowOrRows : [rowOrRows];
const renamedRows = this.renameOutgoing(rows);

const addedIds = await mapChunks(renamedRows, MAX_MUTATIONS, async chunk => {
// TODO see if the chunk should be in the "rows" key in the docs
const response = await this.glide.put(`/tables/${table}/`, chunk);
await throwError(response);

const {
data: { rowIDs },
} = await response.json();
return rowIDs;
});

const rowIDs = addedIds.flat();
return Array.isArray(rowOrRows) ? rowIDs : rowIDs[0];
}

/**
* Overwrites a Stash in the BigTable.
*
* @param stash The Stash to overwrite.
* @returns A promise that resolves to an array of row IDs if successful, or undefined.
*/
async overwriteStash(stash: Stash<T>): Promise<RowID[]> {
const response = await this.glide.post(`/tables/${this.id}`, {
$stashID: stash.stashId,
});
await throwError(response);

const {
data: { rowIDs },
} = await response.json();
return rowIDs;
}
}
83 changes: 82 additions & 1 deletion src/Glide.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { App } from "./App";
import { BigTable } from "./BigTable";
import { Stash } from "./Stash";
import { Table } from "./Table";
import { defaultEndpoint, defaultEndpointREST } from "./constants";
import type { TableProps, ColumnSchema, AppProps, IDName, GlideProps, Tokened } from "./types";
import type {
TableProps,
ColumnSchema,
AppProps,
IDName,
GlideProps,
Tokened,
Row,
RowID,
} from "./types";
import fetch from "cross-fetch";

export class Glide {
Expand Down Expand Up @@ -58,6 +69,10 @@ export class Glide {
return this.api(r, { method: "POST", body: JSON.stringify(body) });
}

public put(r: string, body: any) {
return this.api(r, { method: "PUT", body: JSON.stringify(body) });
}

public with(props: Partial<GlideProps> = {}) {
return new Glide({ ...this.props, ...props });
}
Expand Down Expand Up @@ -111,4 +126,70 @@ export class Glide {
const apps = await this.getApps(props);
return apps?.find(a => a.name === name);
}

/**
* Retrieves all big tables.
*
* @param props An optional object containing a token.
* @param props.token An optional token for authentication.
* @returns A promise that resolves to an array of tables if successful, or undefined.
*/
public async getBigTables(props: Tokened = {}): Promise<BigTable[] | undefined> {
const response = await this.with(props).get(`/tables`);
if (response.status !== 200) return undefined;
const { data: tables }: { data: IDName[] } = await response.json();
console.log(tables);
return tables.map(t => this.bigTable({ table: t.id, name: t.name, columns: {}, ...props }));
}

/**
* This function creates a new Table object with the provided properties.
*
* @param props The properties to create the table with.
* @returns The newly created table.
*/
public bigTable<T extends ColumnSchema>(props: Omit<TableProps<T>, "app">) {
return new BigTable<T>(props, this.with(props));
}

public async addBigTable<T extends ColumnSchema>(props: {
name: string;
schema: T;
rows: Row<T>;
}) {
const result = await this.post("/tables", props);
if (result.status != 200) return undefined;
const { data }: { data: { tableId: string; rowIDs: RowID[] } } = await result.json();
return {
table: this.bigTable({
columns: props.schema,
table: data.tableId,
name: props.name,
token: this.props.token,
}),
};
}
public async addBigTableStash<T extends ColumnSchema>(props: {
name: string;
schema: T;
stash: Stash<T>;
}) {
const result = await this.post("/tables", {
name: props.name,
schema: props.schema,
rows: {
$stashID: props.stash.stashId,
},
});
if (result.status != 200) return undefined;
const { data }: { data: { tableId: string; rowIDs: RowID[] } } = await result.json();
return {
table: this.bigTable({
columns: props.schema,
table: data.tableId,
name: props.name,
token: this.props.token,
}),
};
}
}
36 changes: 36 additions & 0 deletions src/Stash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { BigTable } from "./BigTable";
import { throwError } from "./common";
import { Glide } from "./Glide";
import { ColumnSchema, Row } from "./types";

type StashProps<T extends ColumnSchema = {}> = {
stashId: string;
bigTable: BigTable<T>;
};

export class Stash<T extends ColumnSchema = {}> {
public get stashId(): string {
return this.props.stashId;
}

indexOfLastAdd = 0;

getSerial(): string {
return `${this.indexOfLastAdd++}`;
}

constructor(private props: StashProps<T>, private glide: Glide) {}

public async add(rows: Row<T>[]) {
const serial = this.getSerial();
const url = `/stashes/${this.props.stashId}/${serial}`;
console.log(url);
const response = await this.glide.post(`/stashes/${this.props.stashId}/${serial}`, rows);
if (response.status !== 200) {
const text = await response.text();
console.log(text);
throw new Error(`Error adding to stash: ${text}`);
}
await throwError(response);
}
}
2 changes: 1 addition & 1 deletion src/Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Glide } from "./Glide";
*/
export type RowOf<T extends Table<any>> = T extends Table<infer R> ? FullRow<R> : never;

async function mapChunks<TItem, TResult>(
export async function mapChunks<TItem, TResult>(
array: TItem[],
chunkSize: number,
work: (chunk: TItem[]) => Promise<TResult>
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const defaultEndpoint = "https://api.glideapp.io/api/function";
export const defaultEndpointREST = "https://functions.prod.internal.glideapps.com/api";
export const defaultEndpointREST = "https://api.glideapps.com/";

export const MAX_MUTATIONS = 500;