import { TableBuilder } from "./TableStructor"; import BulkSave from "./BulkSave"; import { IReturnMethods, IQuerySelector } from "./QuerySelector"; export type SQLQuery = { sql: string; args: any[]; parseble?: boolean; }; export type ColumnType = "Number" | "String" | "Decimal" | "Boolean" | "DateTime" | "JSON" | "BLOB"; export type NonFunctionPropertyNames = { [K in keyof T]: T[K] extends Function ? never : K; }[keyof T]; export type ITableBuilder = { /** * column can contain nullable value */ nullable: ITableBuilder; /** * isPrimary key */ primary: ITableBuilder; /** * work togather with isPrimary, autoIncrement value on insert */ autoIncrement: ITableBuilder; /** * save method will check if this column value exist in the table, if it dose then it will update insted. * this will only be check if id is not set */ unique: ITableBuilder; /** * column of type boolean */ boolean: ITableBuilder; /** * column of type integer */ number: ITableBuilder; /** * column of type decimal */ decimal: ITableBuilder; /** * column of type string */ string: ITableBuilder; /** * column of type datetime */ dateTime: ITableBuilder; /** * encrypt the column */ encrypt: (encryptionKey: string) => ITableBuilder; /** * add column to table and specify its props there after, eg boolean, number etc */ column: (colName: NonFunctionPropertyNames) => ITableBuilder; /** * add a foreign key to the table */ constrain: (columnName: NonFunctionPropertyNames, contraintTableName: D, contraintColumnName: NonFunctionPropertyNames) => ITableBuilder; /** * sqlite return json object, with this convert it to class object instead */ onItemCreate: (func: (item: T) => T) => ITableBuilder; /** * if not using onItemCreate then use this to convert json item to class * note: this will ignore the constructor * example class Test { name: String; passowrd: String; constructor(name: string, passowrd: string){ this.name = name; this.passowrd = passowrd; } get getName(){ return this.name; } } .objectPrototype(Test.prototype) */ objectPrototype: (objectProptoType: any) => ITableBuilder; }; export declare class IId { id: number; constructor(id?: number); } export declare class IBaseModule extends IId { tableName: D; constructor(tableName: D, id?: number); } export type Operation = "UPDATE" | "INSERT"; export type SOperation = "onSave" | "onDelete" | "onBulkSave"; export declare type SingleValue = string | number | boolean | Date | undefined | null; export declare type ArrayValue = any[] | undefined; export declare type NumberValue = number | undefined; export declare type StringValue = string | undefined; export type IDataBaseExtender = { tables: TableBuilder[]; dbTable: TableBuilder[]; triggerWatch: >(items: T | T[], operation: SOperation, subOperation?: Operation, tableName?: D) => Promise; } & IDatabase; export interface IChildQueryLoader, D extends string> { With: (columnName: NonFunctionPropertyNames) => IChildQueryLoader; AssignTo: (columnName: NonFunctionPropertyNames) => IQuery; } export type WatchIdentifier = "Hook" | "Other"; export type TempStore = { operation: SOperation; subOperation?: Operation; tableName: D; items: IBaseModule[]; identifier?: WatchIdentifier; }; export interface IWatcher { onSave?: (item: T[], operation: Operation) => Promise; onDelete?: (item: T[]) => Promise; onBulkSave?: () => Promise; readonly removeWatch: () => void; identifier: WatchIdentifier; } export interface IChildLoader { parentProperty: string; parentTable: D; childProperty: string; childTableName: D; assignTo: string; isArray: boolean; } export declare enum Param { StartParameter = "#(", EqualTo = "#=", EndParameter = "#)", OR = "#OR", AND = "#AND", LessThan = "#<", GreaterThan = "#>", IN = "#IN", NotIn = "#NOT IN", NULL = "#IS NULL", NotNULL = "#IS NOT NULL", NotEqualTo = "#!=", Contains = "#like", StartWith = "S#like", EndWith = "E#like", EqualAndGreaterThen = "#>=", EqualAndLessThen = "#<=", OrderByDesc = "#Order By #C DESC", OrderByAsc = "#Order By #C ASC", Limit = "#Limit #Counter" } export interface IQuaryResult { sql: string; values: any[]; children: IChildLoader[]; } export interface IQuery, D extends string> { Column: (columnName: NonFunctionPropertyNames) => IQuery; EqualTo: (value: SingleValue) => IQuery; Contains: (value: StringValue) => IQuery; StartWith: (value: StringValue) => IQuery; EndWith: (value: StringValue) => IQuery; NotEqualTo: (value: SingleValue) => IQuery; EqualAndGreaterThen: (value: NumberValue | StringValue) => IQuery; EqualAndLessThen: (value: NumberValue | StringValue) => IQuery; Start: () => IQuery; End: () => IQuery; OR: () => IQuery; AND: () => IQuery; GreaterThan: (value: NumberValue | StringValue) => IQuery; LessThan: (value: NumberValue | StringValue) => IQuery; IN: (value: ArrayValue) => IQuery; NotIn: (value: ArrayValue) => IQuery; Null: () => IQuery; NotNull: () => IQuery; OrderByDesc: (columnName: NonFunctionPropertyNames) => IQuery; OrderByAsc: (columnName: NonFunctionPropertyNames) => IQuery; Limit: (value: number) => IQuery; LoadChildren: >(childTableName: D, parentProperty: NonFunctionPropertyNames) => IChildQueryLoader; LoadChild: >(childTableName: D, parentProperty: NonFunctionPropertyNames) => IChildQueryLoader; delete: () => Promise; firstOrDefault: () => Promise | undefined>; findOrSave: (item: T & IBaseModule) => Promise>; toList: () => Promise[]>; getQueryResult: (operation?: "SELECT" | "DELETE") => IQuaryResult; } export type IQueryResultItem = T & { saveChanges: () => Promise>; delete: () => Promise; update: (...keys: NonFunctionPropertyNames[]) => Promise; }; declare const OUseQuery: , D extends string>(tableName: D, query: IQuery | SQLQuery | IReturnMethods | (() => Promise), onDbItemsChanged?: (items: T[]) => T[]) => readonly [IQueryResultItem[], boolean, () => Promise, IDatabase]; export type IUseQuery = typeof OUseQuery; export interface IDatabase { /** * This is a hook you could use in a component */ useQuery: IUseQuery; /** * Freeze all watchers, this is usefull when for example doing many changes to the db * and you dont want the watchers to be triggerd many times */ disableWatchers: () => IDatabase; /** * enabling Watchers will call all the frozen watchers that has not been called when it was frozen */ enableWatchers: () => Promise; /** * Freeze all hooks, this is usefull when for example doing many changes to the db * and you dont want the hooks to be triggerd(rerender components) many times */ disableHooks: () => IDatabase; /** * enabling Hooks will call all the frozen hooks that has not been called when it was frozen */ enableHooks: () => Promise; /** * BulkSave object * This will only watchers.onBulkSave */ bulkSave: >(tabelName: D) => Promise>; isClosed?: boolean; /** * Its importend that,createDbContext return new database after this is triggered */ tryToClose: () => Promise; /** * Its importend that,createDbContext return new database after this is triggered */ close: () => Promise; /** * begin transaction */ beginTransaction: () => Promise; /** * comit the transaction */ commitTransaction: () => Promise; /** * rollback the transaction */ rollbackTransaction: () => Promise; /** Auto close the db after every ms. The db will be able to refresh only if there is no db operation is ongoing. This is useful, so that it will use less memory as SQlite tends to store transaction in memories which causes the increase in memory over time. its best to use ms:3600000 the db has to be ideal for ms to be able to close it. */ startRefresher: (ms: number) => void; /** * return column name for the specific table */ allowedKeys: (tableName: D) => Promise; /** * convert json to IQueryResultItem object, this will add method as saveChanges, update and delete methods to an object */ asQueryable: >(item: IId | IBaseModule, tableName?: D) => Promise>; watch: >(tableName: D) => IWatcher; /** * More advanced queryBuilder * It include join and aggregators and better validations */ querySelector: >(tabelName: D) => IQuerySelector; /** * execute sql eg * query: select * from users where name = ? * args: ["test"] */ find: (query: string, args?: any[], tableName?: D) => Promise[]>; /** * trigger save, update will depend on id and unique columns */ save: >(item: T | T[], insertOnly?: Boolean, tableName?: D, saveAndForget?: boolean) => Promise; where: >(tableName: D, query?: any | T) => Promise; /** * this method translate json-sql to sqlite select. * for more info about this read json-sql documentations * https://github.com/2do2go/json-sql/tree/4be018c0662dacba06ddf033d18e71ebf93ee7c3/docs * example * { type: 'select', table: 'DetaliItems', condition:{"DetaliItems.id":{$gt: 1}, "DetaliItems.title": "Epic Of Caterpillar"}, join: { Chapters: { on: {'DetaliItems.id': 'Chapters.detaliItem_Id'} } } } */ jsonToSql: (jsonQuery: any, tableName?: D) => Promise; /** * delete object based on Id */ delete: (item: IId | IId[], tableName?: D) => Promise; /** * execute sql without returning anyting */ execute: (query: string, args?: any[]) => Promise; /** * Drop all tables */ dropTables: () => Promise; /** * Setup your table, this will only create a table if it dose not exist */ setUpDataBase: (forceCheck?: boolean) => Promise; /** * find out if there some changes between object and db table */ tableHasChanges: >(item: ITableBuilder) => Promise; /** * execute an array of sql */ executeRawSql: (queries: SQLQuery[]) => Promise; /** * migrate new added or removed columns * constrains is not supported to add */ migrateNewChanges: () => Promise; } export {};