UNPKG

12.3 kBTypeScriptView Raw
1// Copyright IBM Corp. 2018,2019. All Rights Reserved.
2// Node module: loopback-datasource-juggler
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6import {AnyObject, Callback, Options} from './common';
7import {Connector} from './connector';
8import {
9 ModelBaseClass,
10 ModelBuilder,
11 ModelDefinition,
12 PropertyDefinition
13} from './model';
14import {EventEmitter} from 'events';
15import {IsolationLevel, Transaction} from './transaction-mixin';
16
17/**
18 * LoopBack models can manipulate data via the DataSource object.
19 * Attaching a `DataSource` to a `Model` adds instance methods and static methods to the `Model`.
20 *
21 * Define a data source to persist model data.
22 * To create a DataSource programmatically, call `createDataSource()` on the LoopBack object; for example:
23 * ```js
24 * var oracle = loopback.createDataSource({
25 * connector: 'oracle',
26 * host: '111.22.333.44',
27 * database: 'MYDB',
28 * username: 'username',
29 * password: 'password'
30 * });
31 * ```
32 *
33 * All classes in single dataSource share same the connector type and
34 * one database connection.
35 *
36 * For example, the following creates a DataSource, and waits for a connection callback.
37 *
38 * ```
39 * var dataSource = new DataSource('mysql', { database: 'myapp_test' });
40 * dataSource.define(...);
41 * dataSource.on('connected', function () {
42 * // work with database
43 * });
44 * ```
45 * @class DataSource
46 * @param {String} [name] Optional name for datasource.
47 * @options {Object} settings Database-specific settings to establish connection (settings depend on specific connector).
48 * The table below lists a typical set for a relational database.
49 * @property {String} connector Database connector to use. For any supported connector, can be any of:
50 *
51 * - The connector module from `require(connectorName)`.
52 * - The full name of the connector module, such as 'loopback-connector-oracle'.
53 * - The short name of the connector module, such as 'oracle'.
54 * - A local module under `./connectors/` folder.
55 * @property {String} host Database server host name.
56 * @property {String} port Database server port number.
57 * @property {String} username Database user name.
58 * @property {String} password Database password.
59 * @property {String} database Name of the database to use.
60 * @property {Boolean} debug Display debugging information. Default is false.
61 *
62 * The constructor allows the following styles:
63 *
64 * 1. new DataSource(dataSourceName, settings). For example:
65 * - new DataSource('myDataSource', {connector: 'memory'});
66 * - new DataSource('myDataSource', {name: 'myDataSource', connector: 'memory'});
67 * - new DataSource('myDataSource', {name: 'anotherDataSource', connector: 'memory'});
68 *
69 * 2. new DataSource(settings). For example:
70 * - new DataSource({name: 'myDataSource', connector: 'memory'});
71 * - new DataSource({connector: 'memory'});
72 *
73 * 3. new DataSource(connectorModule, settings). For example:
74 * - new DataSource(connectorModule, {name: 'myDataSource})
75 * - new DataSource(connectorModule)
76 */
77export declare class DataSource extends EventEmitter {
78 name: string;
79 settings: Options;
80
81 initialized?: boolean;
82 connected?: boolean;
83 connecting?: boolean;
84
85 connector?: Connector;
86
87 definitions: {[modelName: string]: ModelDefinition};
88
89 DataAccessObject: AnyObject & {prototype: AnyObject};
90
91 constructor(name: string, settings?: Options, modelBuilder?: ModelBuilder);
92
93 constructor(settings?: Options, modelBuilder?: ModelBuilder);
94
95 constructor(
96 connectorModule: Connector,
97 settings?: Options,
98 modelBuilder?: ModelBuilder,
99 );
100
101 /**
102 * Create a model class
103 * @param name Name of the model
104 * @param properties An object of property definitions
105 * @param options Options for model settings
106 */
107 createModel<T extends ModelBaseClass>(
108 name: string,
109 properties?: AnyObject,
110 options?: Options,
111 ): T;
112
113 /**
114 * Look up a model class by name
115 * @param modelName Model name
116 * @param forceCreate A flag to force creation of a model if not found
117 */
118 getModel(
119 modelName: string,
120 forceCreate?: boolean,
121 ): ModelBaseClass | undefined;
122
123 /**
124 * Remove a model from the registry.
125 *
126 * @param modelName
127 */
128 deleteModelByName(modelName: string): void;
129
130 /**
131 * Remove all models from the registry, but keep the connector instance
132 * (including the pool of database connections).
133 */
134 deleteAllModels(): void;
135
136 /**
137 * Attach an existing model to a data source.
138 * This will mixin all of the data access object functions (DAO) into your
139 * modelClass definition.
140 * @param {ModelBaseClass} modelClass The model constructor that will be enhanced
141 * by DataAccessObject mixins.
142 */
143 attach(modelClass: ModelBaseClass): ModelBaseClass;
144
145 automigrate(models?: string | string[]): Promise<void>;
146 // legacy callback style
147 automigrate(models: string | string[] | undefined, callback: Callback): void;
148
149 autoupdate(models?: string | string[]): Promise<void>;
150 // legacy callback style
151 autoupdate(models: string | string[] | undefined, callback: Callback): void;
152
153 discoverModelDefinitions(
154 options?: Options,
155 ): Promise<ModelDefinition[]>;
156 // legacy callback style (no options)
157 discoverModelDefinitions(
158 callback: Callback<ModelDefinition[]>,
159 ): void;
160 // legacy callback style (with options)
161 discoverModelDefinitions(
162 options: Options,
163 callback: Callback<ModelDefinition[]>,
164 ): void;
165
166 discoverModelProperties(
167 modelName: string,
168 options?: Options,
169 ): Promise<PropertyDefinition[]>;
170 // legacy callback style (no options)
171 discoverModelProperties(
172 modelName: string,
173 callback: Callback<PropertyDefinition[]>,
174 ): void;
175 // legacy callback style (with options)
176 discoverModelProperties(
177 modelName: string,
178 options: Options,
179 callback: Callback<PropertyDefinition[]>,
180 ): void;
181
182 discoverPrimaryKeys(
183 modelName: string,
184 options?: Options,
185 ): Promise<PropertyDefinition[]>;
186 // legacy callback style (no options)
187 discoverPrimaryKeys(
188 modelName: string,
189 callback: Callback<PropertyDefinition[]>,
190 ): void;
191 // legacy callback style (with options)
192 discoverPrimaryKeys(
193 modelName: string,
194 options: Options,
195 callback: Callback<PropertyDefinition[]>,
196 ): void;
197
198 discoverForeignKeys(
199 modelName: string,
200 options?: Options,
201 ): Promise<PropertyDefinition[]>;
202 // legacy callback style (no options)
203 discoverForeignKeys(
204 modelName: string,
205 callback: Callback<PropertyDefinition[]>,
206 ): void;
207 // legacy callback style (with options)
208 discoverForeignKeys(
209 modelName: string,
210 options: Options,
211 callback: Callback<PropertyDefinition[]>,
212 ): void;
213
214 discoverExportedForeignKeys(
215 modelName: string,
216 options?: Options,
217 ): Promise<PropertyDefinition[]>;
218 // legacy callback style (no options)
219 discoverExportedForeignKeys(
220 modelName: string,
221 callback: Callback<PropertyDefinition[]>,
222 ): void;
223 // legacy callback style (with options)
224 discoverExportedForeignKeys(
225 modelName: string,
226 options: Options,
227 callback: Callback<PropertyDefinition[]>,
228 ): void;
229
230 discoverAndBuildModels(
231 modelName: string,
232 options?: Options,
233 ): Promise<{[name: string]: ModelBaseClass}>;
234 // legacy callback style (no options)
235 discoverAndBuildModels(
236 modelName: string,
237 callback: Callback<{[name: string]: ModelBaseClass}>,
238 ): void;
239 // legacy callback style (with options)
240 discoverAndBuildModels(
241 modelName: string,
242 options: Options,
243 callback: Callback<{[name: string]: ModelBaseClass}>,
244 ): void;
245
246 discoverSchema(
247 tableName: string,
248 options?: Options,
249 ): Promise<AnyObject>;
250 // legacy callback style (no options)
251 discoverSchema(
252 tableName: string,
253 callback: Callback<AnyObject>,
254 ): void;
255 // legacy callback style (with options)
256 discoverSchema(
257 tableName: string,
258 options: Options,
259 callback: Callback<AnyObject>,
260 ): void;
261
262 discoverSchemas(
263 tableName: string,
264 options?: Options,
265 ): Promise<AnyObject[]>;
266 // legacy callback style (no options)
267 discoverSchemas(
268 tableName: string,
269 callback: Callback<AnyObject[]>,
270 ): void;
271 // legacy callback style (with options)
272 discoverSchemas(
273 tableName: string,
274 options: Options,
275 callback: Callback<AnyObject[]>,
276 ): void;
277
278 buildModelFromInstance(
279 modelName: string,
280 jsonObject: AnyObject,
281 options?: Options,
282 ): ModelBaseClass;
283
284 connect(): Promise<void>;
285 // legacy callback style
286 connect(callback: Callback): void;
287
288 disconnect(): Promise<void>;
289 // legacy callback style
290 disconnect(callback: Callback): void;
291
292 // Only promise variant, callback is intentionally not described.
293 // Note we must use `void | PromiseLike<void>` to avoid breaking
294 // existing LoopBack 4 applications.
295 // TODO(semver-major): change the return type to `Promise<void>`
296 stop(): void | PromiseLike<void>;
297
298 ping(): Promise<void>;
299 // legacy callback style
300 ping(callback: Callback): void;
301
302 // Only promise variant, callback is intentionally not supported.
303
304 /**
305 * Execute a SQL command.
306 *
307 * **WARNING:** In general, it is always better to perform database actions
308 * through repository methods. Directly executing SQL may lead to unexpected
309 * results, corrupted data, security vulnerabilities and other issues.
310 *
311 * @example
312 *
313 * ```ts
314 * // MySQL
315 * const result = await db.execute(
316 * 'SELECT * FROM Products WHERE size > ?',
317 * [42]
318 * );
319 *
320 * // PostgreSQL
321 * const result = await db.execute(
322 * 'SELECT * FROM Products WHERE size > $1',
323 * [42]
324 * );
325 * ```
326 *
327 * @param command A parameterized SQL command or query.
328 * Check your database documentation for information on which characters to
329 * use as parameter placeholders.
330 * @param parameters List of parameter values to use.
331 * @param options Additional options, for example `transaction`.
332 * @returns A promise which resolves to the command output as returned by the
333 * database driver. The output type (data structure) is database specific and
334 * often depends on the command executed.
335 */
336 execute(
337 command: string | object,
338 parameters?: any[] | object,
339 options?: Options,
340 ): Promise<any>;
341
342 /**
343 * Execute a MongoDB command.
344 *
345 * **WARNING:** In general, it is always better to perform database actions
346 * through repository methods. Directly executing MongoDB commands may lead
347 * to unexpected results and other issues.
348 *
349 * @example
350 *
351 * ```ts
352 * const result = await db.execute('MyCollection', 'aggregate', [
353 * {$lookup: {
354 * // ...
355 * }},
356 * {$unwind: '$data'},
357 * {$out: 'tempData'}
358 * ]);
359 * ```
360 *
361 * @param collectionName The name of the collection to execute the command on.
362 * @param command The command name. See
363 * [Collection API docs](http://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html)
364 * for the list of commands supported by the MongoDB client.
365 * @param parameters Command parameters (arguments), as described in MongoDB API
366 * docs for individual collection methods.
367 * @returns A promise which resolves to the command output as returned by the
368 * database driver.
369 */
370 execute(
371 collectionName: string,
372 command: string,
373 ...parameters: any[],
374 ): Promise<any>;
375
376 /**
377 * Execute a raw database command using a connector that's not described
378 * by LoopBack's `execute` API yet.
379 *
380 * **WARNING:** In general, it is always better to perform database actions
381 * through repository methods. Directly executing database commands may lead
382 * to unexpected results and other issues.
383 *
384 * @param args Command and parameters, please consult your connector's
385 * documentation to learn about supported commands and their parameters.
386 * @returns A promise which resolves to the command output as returned by the
387 * database driver.
388 */
389 execute(...args: any[]): Promise<any>;
390
391 /**
392 * Begin a new transaction.
393 *
394 *
395 * @param [options] Options {isolationLevel: '...', timeout: 1000}
396 * @returns Promise A promise which resolves to a Transaction object
397 */
398 beginTransaction(options?: IsolationLevel | Options): Promise<Transaction>;
399}
400
\No newline at end of file