Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
8 changes: 5 additions & 3 deletions src/drivers/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export function getUpdateResults(results?: any): Record<string, any> | 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<T extends ErrorCallback = ErrorCallback>(
args: (SQLiteCloudDataTypes | T | ErrorCallback)[]
Expand All @@ -140,11 +142,11 @@ export function popCallback<T extends ErrorCallback = ErrorCallback>(
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() }
}

//
Expand Down
56 changes: 56 additions & 0 deletions test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
27 changes: 27 additions & 0 deletions test/statement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading