diff --git a/package-lock.json b/package-lock.json index 3618a17..0785b63 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sqlitecloud/drivers", - "version": "1.0.413", + "version": "1.0.416", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@sqlitecloud/drivers", - "version": "1.0.413", + "version": "1.0.416", "license": "MIT", "dependencies": { "buffer": "^6.0.3", diff --git a/package.json b/package.json index a5917fb..f6d3861 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sqlitecloud/drivers", - "version": "1.0.413", + "version": "1.0.416", "description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients", "main": "./lib/index.js", "types": "./lib/index.d.ts", diff --git a/src/drivers/utilities.ts b/src/drivers/utilities.ts index 3af3034..d0fe859 100644 --- a/src/drivers/utilities.ts +++ b/src/drivers/utilities.ts @@ -131,6 +131,8 @@ export function getUpdateResults(results?: any): Record | undefined * containing the arguments array with the callbacks removed (if any), and the callback itself. * If there are multiple callbacks, the first one is returned as 'callback' and the last one * as 'completeCallback'. + * + * @returns args is a simple list of SQLiteCloudDataTypes, we flat them into a single array */ export function popCallback( args: (SQLiteCloudDataTypes | T | ErrorCallback)[] @@ -140,11 +142,11 @@ export function popCallback( if (args && args.length > 0 && typeof args[args.length - 1] === 'function') { // at least 2 callbacks? if (args.length > 1 && typeof args[args.length - 2] === 'function') { - return { args: remaining.slice(0, -2), callback: args[args.length - 2] as T, complete: args[args.length - 1] as T } + return { args: remaining.slice(0, -2).flat(), callback: args[args.length - 2] as T, complete: args[args.length - 1] as T } } - return { args: remaining.slice(0, -1), callback: args[args.length - 1] as T } + return { args: remaining.slice(0, -1).flat(), callback: args[args.length - 1] as T } } - return { args: remaining } + return { args: remaining.flat() } } // diff --git a/test/database.test.ts b/test/database.test.ts index b63d334..bdaefd2 100644 --- a/test/database.test.ts +++ b/test/database.test.ts @@ -138,6 +138,62 @@ describe('Database.all', () => { LONG_TIMEOUT ) + it( + 'select with one argument', + done => { + const chinook = getChinookDatabase() + chinook.all('SELECT * FROM tracks LIMIT ?', 1, (err: Error, rows: SQLiteCloudRowset) => { + expect(err).toBeNull() + expect(rows).toBeDefined() + expect(rows[0]).toMatchObject({ + AlbumId: 1, + Bytes: 11170334, + Composer: 'Angus Young, Malcolm Young, Brian Johnson', + GenreId: 1, + MediaTypeId: 1, + Milliseconds: 343719, + Name: 'For Those About To Rock (We Salute You)', + TrackId: 1, + UnitPrice: 0.99 + }) + + chinook.close(error => { + expect(error).toBeNull() + done() + }) + }) + }, + LONG_TIMEOUT + ) + + it( + 'select with one argument with array like ORMs', + done => { + const chinook = getChinookDatabase() + chinook.all('SELECT * FROM tracks LIMIT ?', [1], (err: Error, rows: SQLiteCloudRowset) => { + expect(err).toBeNull() + expect(rows).toBeDefined() + expect(rows[0]).toMatchObject({ + AlbumId: 1, + Bytes: 11170334, + Composer: 'Angus Young, Malcolm Young, Brian Johnson', + GenreId: 1, + MediaTypeId: 1, + Milliseconds: 343719, + Name: 'For Those About To Rock (We Salute You)', + TrackId: 1, + UnitPrice: 0.99 + }) + + chinook.close(error => { + expect(error).toBeNull() + done() + }) + }) + }, + LONG_TIMEOUT + ) + it('select with empty space after semi-colon', done => { const chinook = getChinookDatabase() chinook.all('SELECT 1; ', (err: Error, rows: SQLiteCloudRowset) => { diff --git a/test/statement.test.ts b/test/statement.test.ts index 4d80a08..96d26fc 100644 --- a/test/statement.test.ts +++ b/test/statement.test.ts @@ -110,6 +110,33 @@ it('Statement.all', done => { }) }) +it('Statement.all like ORMs where parameters are passed as an array of bindings', done => { + const chinook = getChinookDatabase() + expect(chinook).toBeDefined() + const statement = chinook.prepare('SELECT * FROM tracks WHERE albumId = ?;', (err: Error, results: any) => { + expect(err).toBeNull() + }) + + statement.all([3], (error, rows) => { + expect(error).toBeNull() + expect(rows).toBeDefined() + expect(rows).toHaveLength(3) + expect(rows).toBeInstanceOf(SQLiteCloudRowset) + + statement.all([4], (error, rows) => { + expect(error).toBeNull() + expect(rows).toBeDefined() + expect(rows).toHaveLength(8) + expect(rows).toBeInstanceOf(SQLiteCloudRowset) + + chinook.close(error => { + expect(error).toBeNull() + done() + }) + }) + }) +}) + it('Statement.all withtout bindings', done => { const chinook = getChinookDatabase() expect(chinook).toBeDefined()