@freckleface/golembase-tables
Version:
SQL-like table implementation on top of Golem Base
84 lines (83 loc) • 3.38 kB
TypeScript
import { GolemBaseCreate } from 'golem-base-sdk';
/**
* TODO: Very early on let's add some form of BSON / binary data so they can upload images and docs.
* The SQL Parser allows for a type called BINARY. In that we would store the hash of the entity containing the data.
* Here are the types the parser recognizes for CREATE TABLE:
*
* Expected "#", "--", ".", "/*", "BIGINT", "BINARY", "BIT", "CHAR", "COLLATE", "DATE", "DATETIME", "DECIMAL", "DOUBLE",
* "ENUM", "FLOAT", "GEOMETRY", "GEOMETRYCOLLECTION", "INT", "INTEGER", "JSON", "LINESTRING", "LONGTEXT", "MEDIUMINT",
* "MEDIUMTEXT", "MULTILINESTRING", "MULTIPOINT", "MULTIPOLYGON", "NUMERIC", "POINT", "POLYGON", "SET", "SMALLINT",
* "TIME", "TIMESTAMP", "TINYINT", "TINYTEXT", "VARBINARY", "VARCHAR", "YEAR", "blob", "boolean", "longblob",
* "mediumblob", "tinyblob", or [ \t\n\r] but "T" found.
*/
export type CreateTableData = {
type: 'table';
tablename: string;
indexes?: string;
[key: string]: any;
};
export type SelectData = {
select: string;
tablename: string;
where: string;
};
export type InsertData = {
type: 'tabledata';
tablename: string;
[key: string]: any;
};
export type ParseResult = {
sqlType: 'create table';
data: CreateTableData | null;
} | {
sqlType: 'select';
data: SelectData;
} | {
sqlType: 'insert';
data: InsertData;
};
export type SqlBatches = ParseResult[][];
export declare const CreateTableObjToGBCreate: (app: string, parseResult: ParseResult) => GolemBaseCreate;
export declare const SQLCreateTableToGBCreate: (app: string, createSql: string) => GolemBaseCreate;
export declare const InsertObjToGBCreate: (app: string, parseResult: ParseResult) => GolemBaseCreate;
export declare const SQLInsertToGBCreate: (app: string, insertSQL: string) => GolemBaseCreate;
/**
* Filters an object to include only specified keys, plus a set of mandatory keys.
*
* @param select A comma-delimited string of keys to include in the result.
* @param obj The source object to filter.
* @returns A new object containing only the selected and mandatory keys.
*/
export declare const filterObjectBySelect: (select: string, obj: Record<string, any>) => Record<string, any>;
export interface ParsedForeignKey {
tablename: string;
localKey: string;
viewKey: string;
}
export declare const parseForeignKeyString: (input: string) => {
tablename: string;
localKey: string;
viewKey: string;
} | null;
type FkQueryResult = {
queryString: string;
} & ParsedForeignKey;
/**
* Finds all foreign keys in a POJO and builds a query object for each one.
*
* @param pojo The data object to scan for foreign keys.
* @param fks A map of foreign key metadata.
* @returns An array of objects, one for each foreign key found.
*/
export declare const buildFkQueries: (pojo: Record<string, any>, fks: Record<string, ParsedForeignKey>) => FkQueryResult[];
/**
* Groups a list of parsed SQL statements into batches.
* 'create table' and 'insert' statements are grouped together.
* 'select' statements are always in their own batch.
*
* @param statements An array of ParseResult objects.
* @returns The statements grouped into batches.
*/
export declare const groupSqlIntoBatches: (statements: ParseResult[]) => SqlBatches;
export declare const parseSql: (sqlString: string) => ParseResult | null;
export {};