Skip to content

Commit 6e6a3df

Browse files
authored
Merge pull request #1855 from strongloop/types/execute-nosql
types: support non-SQL styles of `ds.execute`
2 parents 0ba09a1 + f13d50e commit 6e6a3df

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

types/__test__.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,30 @@ const db = new DataSource('db', {connector: 'memory'});
8888
await ctx.Model.expire('key', 100);
8989
});
9090
});
91+
92+
//-------
93+
// DataSource supports different `execute` styles
94+
//-------
95+
(async function () {
96+
// SQL style
97+
const tx = await db.beginTransaction();
98+
await db.execute('SELECT * FROM Product WHERE count > ?', [10], {
99+
transaction: tx,
100+
});
101+
await tx.commit();
102+
103+
// MongoDB style
104+
await db.execute('MyCollection', 'aggregate', [
105+
{$lookup: { /* ... */ }},
106+
{$unwind: '$data'},
107+
{$out: 'tempData'}
108+
]);
109+
110+
// Neo4J style
111+
await db.execute({
112+
query: 'MATCH (u:User {email: {email}}) RETURN u',
113+
params: {
114+
115+
},
116+
});
117+
});

types/datasource.d.ts

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,94 @@ export declare class DataSource extends EventEmitter {
300300
ping(callback: Callback): void;
301301

302302
// 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+
*/
303336
execute(
304337
command: string | object,
305-
args?: any[] | object,
338+
parameters?: any[] | object,
306339
options?: Options,
307340
): Promise<any>;
308341

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+
309391
/**
310392
* Begin a new transaction.
311393
*

0 commit comments

Comments
 (0)