Skip to content

Commit 076e1d9

Browse files
authored
fix #209 (orm): call sqlite methods with arrays (#210)
* new removeDoubleArray * fix flat instead of custom function * up package lock * up package lock
1 parent 1b77f58 commit 076e1d9

File tree

5 files changed

+91
-6
lines changed

5 files changed

+91
-6
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sqlitecloud/drivers",
3-
"version": "1.0.413",
3+
"version": "1.0.416",
44
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

src/drivers/utilities.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ export function getUpdateResults(results?: any): Record<string, any> | undefined
131131
* containing the arguments array with the callbacks removed (if any), and the callback itself.
132132
* If there are multiple callbacks, the first one is returned as 'callback' and the last one
133133
* as 'completeCallback'.
134+
*
135+
* @returns args is a simple list of SQLiteCloudDataTypes, we flat them into a single array
134136
*/
135137
export function popCallback<T extends ErrorCallback = ErrorCallback>(
136138
args: (SQLiteCloudDataTypes | T | ErrorCallback)[]
@@ -140,11 +142,11 @@ export function popCallback<T extends ErrorCallback = ErrorCallback>(
140142
if (args && args.length > 0 && typeof args[args.length - 1] === 'function') {
141143
// at least 2 callbacks?
142144
if (args.length > 1 && typeof args[args.length - 2] === 'function') {
143-
return { args: remaining.slice(0, -2), callback: args[args.length - 2] as T, complete: args[args.length - 1] as T }
145+
return { args: remaining.slice(0, -2).flat(), callback: args[args.length - 2] as T, complete: args[args.length - 1] as T }
144146
}
145-
return { args: remaining.slice(0, -1), callback: args[args.length - 1] as T }
147+
return { args: remaining.slice(0, -1).flat(), callback: args[args.length - 1] as T }
146148
}
147-
return { args: remaining }
149+
return { args: remaining.flat() }
148150
}
149151

150152
//

test/database.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,62 @@ describe('Database.all', () => {
138138
LONG_TIMEOUT
139139
)
140140

141+
it(
142+
'select with one argument',
143+
done => {
144+
const chinook = getChinookDatabase()
145+
chinook.all('SELECT * FROM tracks LIMIT ?', 1, (err: Error, rows: SQLiteCloudRowset) => {
146+
expect(err).toBeNull()
147+
expect(rows).toBeDefined()
148+
expect(rows[0]).toMatchObject({
149+
AlbumId: 1,
150+
Bytes: 11170334,
151+
Composer: 'Angus Young, Malcolm Young, Brian Johnson',
152+
GenreId: 1,
153+
MediaTypeId: 1,
154+
Milliseconds: 343719,
155+
Name: 'For Those About To Rock (We Salute You)',
156+
TrackId: 1,
157+
UnitPrice: 0.99
158+
})
159+
160+
chinook.close(error => {
161+
expect(error).toBeNull()
162+
done()
163+
})
164+
})
165+
},
166+
LONG_TIMEOUT
167+
)
168+
169+
it(
170+
'select with one argument with array like ORMs',
171+
done => {
172+
const chinook = getChinookDatabase()
173+
chinook.all('SELECT * FROM tracks LIMIT ?', [1], (err: Error, rows: SQLiteCloudRowset) => {
174+
expect(err).toBeNull()
175+
expect(rows).toBeDefined()
176+
expect(rows[0]).toMatchObject({
177+
AlbumId: 1,
178+
Bytes: 11170334,
179+
Composer: 'Angus Young, Malcolm Young, Brian Johnson',
180+
GenreId: 1,
181+
MediaTypeId: 1,
182+
Milliseconds: 343719,
183+
Name: 'For Those About To Rock (We Salute You)',
184+
TrackId: 1,
185+
UnitPrice: 0.99
186+
})
187+
188+
chinook.close(error => {
189+
expect(error).toBeNull()
190+
done()
191+
})
192+
})
193+
},
194+
LONG_TIMEOUT
195+
)
196+
141197
it('select with empty space after semi-colon', done => {
142198
const chinook = getChinookDatabase()
143199
chinook.all('SELECT 1; ', (err: Error, rows: SQLiteCloudRowset) => {

test/statement.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,33 @@ it('Statement.all', done => {
110110
})
111111
})
112112

113+
it('Statement.all like ORMs where parameters are passed as an array of bindings', done => {
114+
const chinook = getChinookDatabase()
115+
expect(chinook).toBeDefined()
116+
const statement = chinook.prepare('SELECT * FROM tracks WHERE albumId = ?;', (err: Error, results: any) => {
117+
expect(err).toBeNull()
118+
})
119+
120+
statement.all([3], (error, rows) => {
121+
expect(error).toBeNull()
122+
expect(rows).toBeDefined()
123+
expect(rows).toHaveLength(3)
124+
expect(rows).toBeInstanceOf(SQLiteCloudRowset)
125+
126+
statement.all([4], (error, rows) => {
127+
expect(error).toBeNull()
128+
expect(rows).toBeDefined()
129+
expect(rows).toHaveLength(8)
130+
expect(rows).toBeInstanceOf(SQLiteCloudRowset)
131+
132+
chinook.close(error => {
133+
expect(error).toBeNull()
134+
done()
135+
})
136+
})
137+
})
138+
})
139+
113140
it('Statement.all withtout bindings', done => {
114141
const chinook = getChinookDatabase()
115142
expect(chinook).toBeDefined()

0 commit comments

Comments
 (0)