Skip to content

Commit

Permalink
types: support no-SQL styles of ds.execute
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
  • Loading branch information
bajtos committed Jul 27, 2020
1 parent 8d1690e commit 4622e36
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
27 changes: 27 additions & 0 deletions types/__test__.ts
Expand Up @@ -88,3 +88,30 @@ const db = new DataSource('db', {connector: 'memory'});
await ctx.Model.expire('key', 100);
});
});

//-------
// DataSource supports different `execute` styles
//-------
(async function () {
// SQL style
const tx = await db.beginTransaction();
await db.execute('SELECT * FROM Product WHERE count > ?', [10], {
transaction: tx,
});
await tx.commit();

// MongoDB style
await db.execute('MyCollection', 'aggregate', [
{$lookup: { /* ... */ }},
{$unwind: '$data'},
{$out: 'tempData'}
]);

// Neo4J style
await db.execute({
query: 'MATCH (u:User {email: {email}}) RETURN u',
params: {
email: 'alice@example.com',
},
});
});
11 changes: 11 additions & 0 deletions types/datasource.d.ts
Expand Up @@ -298,12 +298,23 @@ export declare class DataSource extends EventEmitter {
ping(callback: Callback): void;

// Only promise variant, callback is intentionally not supported.
// Execute a SQL command
execute(
command: string | object,
args?: any[] | object,
options?: Options,
): Promise<any>;

// Execute a MongoDB command. `options` argument is not supported.
execute(
collectionName: string,
command: string,
...args: any[],
): Promise<any>;

// Free-form variant to support other connector types.
execute(...args: any[]): Promise<any>;

/**
* Begin a new transaction.
*
Expand Down

0 comments on commit 4622e36

Please sign in to comment.