From 4622e364e84de6a2e3ab088615ce446f2b860527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 27 Jul 2020 10:09:18 +0200 Subject: [PATCH] types: support no-SQL styles of `ds.execute` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miroslav Bajtoš --- types/__test__.ts | 27 +++++++++++++++++++++++++++ types/datasource.d.ts | 11 +++++++++++ 2 files changed, 38 insertions(+) diff --git a/types/__test__.ts b/types/__test__.ts index 6d0d1bfca..1a22b6e1a 100644 --- a/types/__test__.ts +++ b/types/__test__.ts @@ -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', + }, + }); +}); diff --git a/types/datasource.d.ts b/types/datasource.d.ts index d124ffd35..c609aca79 100644 --- a/types/datasource.d.ts +++ b/types/datasource.d.ts @@ -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; + // Execute a MongoDB command. `options` argument is not supported. + execute( + collectionName: string, + command: string, + ...args: any[], + ): Promise; + + // Free-form variant to support other connector types. + execute(...args: any[]): Promise; + /** * Begin a new transaction. *