import { Filter } from '@loopback/filter'; import { Connector, juggler, ModelDefinition, Options, PositionalParameters } from '@loopback/repository'; export interface SqlConnector extends Connector { getModelDefinition(modelName: string): ModelDefinition | undefined; idNames(model: string): string[]; escapeName(name: string): string; table(modelName: string): string; tableEscaped(modelName: string): string; column(modelName: string, prop: string): string; columnEscaped(modelName: string, property: string): string; buildColumnNames(modelName: string, filter: Filter): string | string[]; fromRow(model: string, rowData: Record): Record; } /** * Object-Persistence Mapping */ export interface Orm { getModelDefinition(modelName: string): ModelDefinition | undefined; idNames(model: string): string[]; escapeName(name: string): string; /** * Get the table name for the given model. The table name can be customized * at model settings as `table` or `tableName`. For example, * * ```json * "Customer": { * "name": "Customer", * "mysql": { * "table": "CUSTOMER" * } * } * ``` * * Returns the table name (String). * @param {String} model The model name */ table(model: string): string; /** * Get the escaped table name * @param {String} model The model name * @returns {String} the escaped table name */ tableEscaped(model: string): string; /** * Return the database name of the property of the model if it exists. * Otherwise return the property name. * Some connectors allow the column/field name to be customized * at the model property definition level as `column`, * `columnName`, or `field`. For example, * * ```json * "name": { * "type": "string", * "mysql": { * "column": "NAME" * } * } * ``` * @param {String} model The target model name * @param {String} prop The property name * * @returns {String} The database mapping name of the property of the model if it exists */ column(model: string, prop: string): string; /** * Get the escaped column name for a given model property * @param {String} model The model name * @param {String} property The property name * @param {String} [withTable] if true prepend the table name (default= false) * @param {String} [prefix] add a prefix to column (for alias) * @returns {String} The escaped column name */ columnEscaped(model: string, property: string, withTable?: boolean, prefix?: string): string; /** * Build a list of escaped column names for the given model and fields filter * @param {string} model Model name * @param {object} filter The filter object * @returns {string[]} Escaped column names */ buildColumnNames(model: string, filter: Filter): string[]; fromRow(model: string, rowData: Record): Record; /** * Execute a SQL command. * * **WARNING:** In general, it is always better to perform database actions * through repository methods. Directly executing SQL may lead to unexpected * results, corrupted data, security vulnerabilities and other issues. * * @example * * ```ts * // MySQL * const result = await db.execute( * 'SELECT * FROM Products WHERE size > ?', * [42] * ); * * // PostgreSQL * const result = await db.execute( * 'SELECT * FROM Products WHERE size > $1', * [42] * ); * ``` * * @param command A parameterized SQL command or query. * Check your database documentation for information on which characters to * use as parameter placeholders. * @param parameters List of parameter values to use. * @param options Additional options, for example `transaction`. * @returns A promise which resolves to the command output as returned by the * database driver. The output type (data structure) is database specific and * often depends on the command executed. */ execute(command: string | object, parameters?: any[] | object, options?: Options): Promise; /** * Execute a MongoDB command. * * **WARNING:** In general, it is always better to perform database actions * through repository methods. Directly executing MongoDB commands may lead * to unexpected results and other issues. * * @example * * ```ts * const result = await db.execute('MyCollection', 'aggregate', [ * {$lookup: { * // ... * }}, * {$unwind: '$data'}, * {$out: 'tempData'} * ]); * ``` * * @param collectionName The name of the collection to execute the command on. * @param command The command name. See * [Collection API docs](http://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html) * for the list of commands supported by the MongoDB client. * @param parameters Command parameters (arguments), as described in MongoDB API * docs for individual collection methods. * @returns A promise which resolves to the command output as returned by the * database driver. */ execute(collectionName: string, command: string, ...parameters: any[]): Promise; /** * Execute a raw database command using a connector that's not described * by LoopBack's `execute` API yet. * * **WARNING:** In general, it is always better to perform database actions * through repository methods. Directly executing database commands may lead * to unexpected results and other issues. * * @param args Command and parameters, please consult your connector's * documentation to learn about supported commands and their parameters. * @returns A promise which resolves to the command output as returned by the * database driver. */ execute(...args: any[]): Promise; } export declare class DefaultOrm implements Orm { protected ds?: juggler.DataSource | undefined; constructor(ds?: juggler.DataSource | undefined); get connector(): SqlConnector; getModelDefinition(modelName: string): ModelDefinition | undefined; idNames(model: string): string[]; escapeName(name: string): string; column(model: string, prop: string): string; columnEscaped(model: string, property: string, withTable?: boolean, prefix?: string): string; table(model: string): string; tableEscaped(model: string): string; buildColumnNames(model: string, filter: Filter): string[]; fromRow(model: string, rowData: Record): Record; execute(...args: PositionalParameters): Promise; protected ensureModelHasBeenDefined(model: string): void; }