Skip to content

Commit dab20e4

Browse files
Adding insert data results in prepared statements #71
Also adding related test to show how it can be used
1 parent 8fc5de2 commit dab20e4

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
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.120",
3+
"version": "1.0.122",
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/database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export class Database extends EventEmitter {
214214
} else {
215215
// context may include id of last row inserted, total changes, etc...
216216
const context = this.processContext(results)
217-
callback?.call(context || this, null, context ? undefined : results)
217+
callback?.call(context || this, null, context ? context : results)
218218
}
219219
})
220220
}

test/shared.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export function getTestingDatabase(callback?: ResultsCallback): Database {
195195
}
196196
expect(results).toBeDefined()
197197
expect(results[0][42]).toBe(42)
198-
callback?.call(database, null)
198+
callback?.call(database, null, database)
199199
})
200200
})
201201

test/statement.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import { SQLiteCloudRowset } from '../src'
66
import { RowCallback, RowCountCallback, SQLiteCloudError } from '../src/drivers/types'
7-
import { getChinookDatabase } from './shared'
7+
import { getChinookDatabase, getTestingDatabase } from './shared'
88

99
describe('Database.prepare', () => {
1010
it('without incorrect bindings', done => {
@@ -201,3 +201,23 @@ it('Statement.run', done => {
201201
})
202202
})
203203
})
204+
205+
it('Statement.run - with insert results', done => {
206+
// create simple "people" database that we can write in...
207+
const database = getTestingDatabase(error => {
208+
expect(error).toBeNull()
209+
210+
const statement = database.prepare('INSERT INTO people (name, hobby, age) VALUES (?, ?, ?); ')
211+
212+
// @ts-ignore
213+
statement.run('John Wayne', 73, 'Horse Riding', (error, results) => {
214+
// this is an insert statement and the result will indicate number of changes, etc.
215+
console.debug(`insert results: `, results)
216+
217+
expect(results.lastID).toBeGreaterThan(1)
218+
expect(results.changes).toBe(1)
219+
220+
done()
221+
})
222+
})
223+
})

0 commit comments

Comments
 (0)