From 7cfab1067fae46c1ec9e38a91668e43be71d948b Mon Sep 17 00:00:00 2001 From: Gioele Cantoni Date: Wed, 19 Feb 2025 18:09:24 +0100 Subject: [PATCH 1/4] new removeDoubleArray --- package.json | 2 +- src/drivers/utilities.ts | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index a5917fb..be0401d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sqlitecloud/drivers", - "version": "1.0.413", + "version": "1.0.415", "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..3284aba 100644 --- a/src/drivers/utilities.ts +++ b/src/drivers/utilities.ts @@ -125,6 +125,17 @@ export function getUpdateResults(results?: any): Record | undefined return undefined } +/** + * Allow compatibility with ORMs that call methods such as all(), get(), etc. + * by passing parameters in an array + */ +function removeDoubleArray(array: any[]): SQLiteCloudDataTypes[] { + if (Array.isArray(array) && array.length == 1 && Array.isArray(array[0])) { + return array[0] + } + return array +} + /** * Many of the methods in our API may contain a callback as their last argument. * This method will take the arguments array passed to the method and return an object @@ -140,9 +151,9 @@ 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: removeDoubleArray(remaining.slice(0, -2)), 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: removeDoubleArray(remaining.slice(0, -1)), callback: args[args.length - 1] as T } } return { args: remaining } } From f76cdb5a0b43230374b0045ef68654858854994c Mon Sep 17 00:00:00 2001 From: Gioele Cantoni Date: Thu, 20 Feb 2025 12:56:07 +0100 Subject: [PATCH 2/4] fix flat instead of custom function --- package.json | 2 +- src/drivers/utilities.ts | 19 ++++---------- test/database.test.ts | 56 ++++++++++++++++++++++++++++++++++++++++ test/statement.test.ts | 27 +++++++++++++++++++ 4 files changed, 89 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index be0401d..f6d3861 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sqlitecloud/drivers", - "version": "1.0.415", + "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 3284aba..d0fe859 100644 --- a/src/drivers/utilities.ts +++ b/src/drivers/utilities.ts @@ -125,23 +125,14 @@ export function getUpdateResults(results?: any): Record | undefined return undefined } -/** - * Allow compatibility with ORMs that call methods such as all(), get(), etc. - * by passing parameters in an array - */ -function removeDoubleArray(array: any[]): SQLiteCloudDataTypes[] { - if (Array.isArray(array) && array.length == 1 && Array.isArray(array[0])) { - return array[0] - } - return array -} - /** * Many of the methods in our API may contain a callback as their last argument. * This method will take the arguments array passed to the method and return an object * 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)[] @@ -151,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: removeDoubleArray(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: removeDoubleArray(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() From 051bf9f9a093d9207ace64d7d59c540b394a9771 Mon Sep 17 00:00:00 2001 From: Gioele Cantoni Date: Thu, 20 Feb 2025 12:56:17 +0100 Subject: [PATCH 3/4] up package lock --- package-lock.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3618a17..5ee5f79 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sqlitecloud/drivers", - "version": "1.0.413", + "version": "1.0.415", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@sqlitecloud/drivers", - "version": "1.0.413", + "version": "1.0.415", "license": "MIT", "dependencies": { "buffer": "^6.0.3", From b33043d2e3381d252f2545d922c3acb0f7cfcada Mon Sep 17 00:00:00 2001 From: Gioele Cantoni Date: Thu, 20 Feb 2025 12:56:25 +0100 Subject: [PATCH 4/4] up package lock --- package-lock.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5ee5f79..0785b63 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sqlitecloud/drivers", - "version": "1.0.415", + "version": "1.0.416", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@sqlitecloud/drivers", - "version": "1.0.415", + "version": "1.0.416", "license": "MIT", "dependencies": { "buffer": "^6.0.3",