sqlite3orm
Version:
ORM for sqlite3 and TypeScript/JavaScript
307 lines (306 loc) • 12.4 kB
TypeScript
import { SqlDatabase } from './core';
import { MetaModel, Table } from './metadata';
import { Filter, QueryModel, Where } from './query';
/**
* @export
* @enum
*/
export declare enum BaseDAOInsertMode {
/** use the provided value if defined, otherwise let sqlite generate the value automatically */
StrictSqlite = 1,
/** prevents the insertion of predefined primary key values; always let sqlite generate a value automatically */
ForceAutoGeneration = 2
}
/**
* @export
* @interface BaseDAOOptions
*/
export interface BaseDAOOptions {
insertMode?: BaseDAOInsertMode;
/**
* @description if set to `true` resolve 'updatePartialAll' and 'deleteAll' with `0` if nothing changed
*/
ignoreNoChanges?: boolean;
}
/**
* @export
* @class BaseDAO
* @template T - The class mapped to the base table
*/
export declare class BaseDAO<T extends object> {
static options?: BaseDAOOptions;
readonly type: {
new (): T;
};
readonly metaModel: MetaModel;
readonly table: Table;
readonly sqldb: SqlDatabase;
readonly queryModel: QueryModel<T>;
/**
* Creates an instance of BaseDAO.
*
* @param type - The class mapped to the base table
* @param sqldb - The database connection
*/
constructor(type: {
new (): T;
}, sqldb: SqlDatabase);
/**
* insert
*
* @param model - A model class instance
* @returns A promise of the inserted model class instance
*/
insert(model: T, mode?: BaseDAOInsertMode): Promise<T>;
/**
* insert partially - insert only columns mapped to the property keys from the partial input model
*
* for this to work:
* all columns mapped to included properties must be nullable or their properties must provide a value
* all columns mapped to excluded properties must be nullable or must have a database default value
*
* @param input - A model class instance
* @returns A promise of the inserted model class instance
*/
insertPartial(input: Partial<T>, mode?: BaseDAOInsertMode): Promise<Partial<T>>;
/**
* replace ( insert or replace )
*
* @param model - A model class instance
* @returns A promise of the inserted or updated model class instance
*/
replace(model: T): Promise<T>;
/**
* replace ( insert or replace ) partially
*
* for this to work:
* all columns mapped to included properties must be nullable or their properties must provide a value
* on insert: all columns mapped to excluded properties must be nullable or must have a database default value
* on update: all columns mapped to excluded properties are not affected by this update
*
* @param input - A model class instance
* @returns A promise of the inserted or updated model class instance
*/
replacePartial(input: Partial<T>): Promise<Partial<T>>;
/**
* update
*
* @param model - A model class instance
* @returns A promise of the updated model class instance
*/
update(model: T): Promise<T>;
/**
* update partially - update only columns mapped to the property keys from the partial input model
*
* for this to work:
* all columns mapped to included properties must be nullable or their properties must provide a value
* all other columns are not affected by this update
*
* @param input - A model class instance
* @returns A promise of the updated model class instance
*/
updatePartial(input: Partial<T>): Promise<Partial<T>>;
/**
* update all - please provide a proper sql-condition otherwise all records will be updated!
* this updates only columns mapped to the property keys from the partial input model
*
* for this to work:
* all columns mapped to included properties must be nullable or their properties must provide a value
* all other columns are not affected by this update
*
* @param input - A model class instance
* @param [where] - An optional Where-object or sql-text which will be added to the update-statement
* e.g 'WHERE <your condition>'
* @param [params] - An optional object with additional host parameter
* @returns A promise of the updated model class instance
*/
updatePartialAll(input: Partial<T>, where?: Where<T>, params?: object): Promise<number>;
/**
* delete using primary key
*
* @param model - A model class instance
* @returns A promise
*/
delete(model: T): Promise<void>;
/**
* delete using primary key
*
* @param input - A partial model class instance
* @returns A promise
*/
deleteById(input: Partial<T>): Promise<void>;
/**
* delete all - please provide a proper sql-condition otherwise all records will be deleted!
*
* @param [where] - An optional Where-object or sql-text which will be added to the delete-statement
* e.g 'WHERE <your condition>'
* @param [params] - An optional object with additional host parameter
* @returns A promise
*/
deleteAll(where?: Where<T>, params?: object): Promise<number>;
/**
* Select a given model
*
* @param model - The input/output model
* @returns A promise of the model instance
*/
select(model: T): Promise<T>;
/**
* select using primary key
*
* @param input - A partial model class instance
* @returns A promise of the model instance
*/
selectById(input: Partial<T>): Promise<T>;
/**
* select parent by using a foreign key constraint and a given child instance
*
* @template C - The class mapped to the child table
* @param constraintName - The foreign key constraint (defined in the child table)
* @param childType - The class mapped to the childtable
* @param childObj - An instance of the class mapped to the child table
* @returns A promise of model instance
*/
selectByChild<C extends object>(constraintName: string, childType: {
new (): C;
}, childObj: C): Promise<T>;
/**
* select parent by using a foreign key constraint and a given child instance
*
* @template P - The class mapped to the parent table
* @param constraintName - The foreign key constraint (defined in the child table)
* @param parentType - The class mapped to the parent table
* @param childObj - An instance of the class mapped to the child table
* @returns A promise of model instance
*/
selectParentOf<P extends object>(constraintName: string, parentType: {
new (): P;
}, childObj: T): Promise<P>;
/**
* Select one model using an optional filter
*
* @param [whereOrFilter] - An optional Where/Filter-object or
* sql-text which will be added to the select-statement
* e.g 'WHERE <your condition>'
* @param [params] - An optional object with additional host parameter
* @returns A promise of the selected model instance; rejects if result is not exactly one row
*/
selectOne(whereOrFilter?: Where<T> | Filter<T>, params?: object): Promise<T>;
/**
* Select all models using an optional filter
*
* @param [whereOrFilter] - An optional Where/Filter-object or
* sql-text which will be added to the select-statement
* e.g 'WHERE <your condition>'
* @param [params] - An optional object with additional host parameter
* @returns A promise of array of model instances
*/
selectAll(whereOrFilter?: Where<T> | Filter<T>, params?: object): Promise<T[]>;
/**
* Count all models using an optional filter
*
* @param [whereOrFilter] - An optional Where/Filter-object or
* sql-text which will be added to the select-statement
* e.g 'WHERE <your condition>'
* @param [params] - An optional object with additional host parameter
* @returns A promise of the count value
*/
countAll(whereOrFilter?: Where<T> | Filter<T>, params?: object): Promise<number>;
/**
* check if model exist using an optional filter
*
* @param [whereOrFilter] - An optional Where/Filter-object or
* sql-text which will be added to the select-statement
* e.g 'WHERE <your condition>'
* @param [params] - An optional object with additional host parameter
* @returns A promise of the count value
*/
exists(whereOrFilter?: Where<T> | Filter<T>, params?: object): Promise<boolean>;
/**
* Select all partial models using a filter
*
* @param filter - A Filter-object
* @param [params] - An optional object with additional host parameter
* @returns A promise of array of model instances
*/
selectPartialAll(filter: Filter<T>, params?: object): Promise<Partial<T>[]>;
/**
* select all childs using a foreign key constraint and a given parent instance
*
* @template P - The class mapped to the parent table
* @param constraintName - The foreign key constraint
* @param parentType - The class mapped to the parent table
* @param parentObj - An instance of the class mapped to the parent table
* @param [whereOrFilter] - An optional Where/Filter-object or sql-text which will be added to the select-statement
* e.g 'WHERE <your condition>'
* @param [params] - An optional object with additional host parameter
* @returns A promise of array of model instances
*/
selectAllOf<P extends object>(constraintName: string, parentType: {
new (): P;
}, parentObj: P, whereOrFilter?: Where<T> | Filter<T>, params?: object): Promise<T[]>;
/**
* select all childs using a foreign key constraint and a given parent instance
*
* @template C - The class mapped to the child table
* @param constraintName - The foreign key constraint (defined in the child table)
* @param childType - The class mapped to the childtable
* @param parentObj - An instance of the class mapped to the parent table
* @param [where] - An optional Where/Filter-object or sql-text which will be added to the select-statement
* e.g 'WHERE <your condition>'
* @param [params] - An optional object with additional host parameter
* @returns A promise of array of model instances
*/
selectAllChildsOf<C extends object>(constraintName: string, childType: {
new (): C;
}, parentObj: T, where?: string, params?: object): Promise<C[]>;
/**
* perform:
* select T.<col1>,.. FROM <table> T
*
* @param callback - The callback called for each row
* @param [whereOrFilter] - An optional Where/Filter-object or sql-text which will be added to the select-statement
* e.g 'WHERE <your condition>'
* @param [params] - An optional object with additional host parameter
* @returns A promise
*/
selectEach(callback: (err: Error, model: T) => void, whereOrFilter?: Where<T> | Filter<T>, params?: object): Promise<number>;
/**
* create a table in the database
*
* @returns {Promise<void>}
*/
createTable(force?: boolean): Promise<void>;
/**
* drop a table from the database
*
* @returns {Promise<void>}
*/
dropTable(): Promise<void>;
/**
* add a column/field to a database table
*
* @param colName - The column/field to add
* @returns A promise
*/
alterTableAddColumn(colName: string): Promise<void>;
/**
* create index in the database
*
* @param idxName - The name of the index
* @param [unique] - create unique index
* @returns A promise
*/
createIndex(idxName: string, unique?: boolean): Promise<void>;
/**
* drop an index from the database
*
* @param idxName - The name of the index
* @returns A promise
*/
dropIndex(idxName: string): Promise<void>;
protected toFilter(whereOrFilter?: Where<T> | Filter<T>, tableAlias?: string): Filter<T>;
private insertInternal;
private insertOrReplaceInternal;
private updateInternal;
}