Skip to content

Commit f76cdb5

Browse files
committed
fix flat instead of custom function
1 parent 7cfab10 commit f76cdb5

File tree

4 files changed

+89
-15
lines changed

4 files changed

+89
-15
lines changed

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.415",
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 & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,23 +125,14 @@ export function getUpdateResults(results?: any): Record<string, any> | undefined
125125
return undefined
126126
}
127127

128-
/**
129-
* Allow compatibility with ORMs that call methods such as all(), get(), etc.
130-
* by passing parameters in an array
131-
*/
132-
function removeDoubleArray(array: any[]): SQLiteCloudDataTypes[] {
133-
if (Array.isArray(array) && array.length == 1 && Array.isArray(array[0])) {
134-
return array[0]
135-
}
136-
return array
137-
}
138-
139128
/**
140129
* Many of the methods in our API may contain a callback as their last argument.
141130
* This method will take the arguments array passed to the method and return an object
142131
* containing the arguments array with the callbacks removed (if any), and the callback itself.
143132
* If there are multiple callbacks, the first one is returned as 'callback' and the last one
144133
* as 'completeCallback'.
134+
*
135+
* @returns args is a simple list of SQLiteCloudDataTypes, we flat them into a single array
145136
*/
146137
export function popCallback<T extends ErrorCallback = ErrorCallback>(
147138
args: (SQLiteCloudDataTypes | T | ErrorCallback)[]
@@ -151,11 +142,11 @@ export function popCallback<T extends ErrorCallback = ErrorCallback>(
151142
if (args && args.length > 0 && typeof args[args.length - 1] === 'function') {
152143
// at least 2 callbacks?
153144
if (args.length > 1 && typeof args[args.length - 2] === 'function') {
154-
return { args: removeDoubleArray(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 }
155146
}
156-
return { args: removeDoubleArray(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 }
157148
}
158-
return { args: remaining }
149+
return { args: remaining.flat() }
159150
}
160151

161152
//

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)