Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sqlite-async

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sqlite-async - npm Package Compare versions

Comparing version 1.0.5 to 1.0.6

11

package.json
{
"name": "sqlite-async",
"version": "1.0.5",
"version": "1.0.6",
"description": "ES6 Promise-based interface to the sqlite3 module.",
"main": "sqlite-async.js",
"directories": {
"test": "test"
"test": "mocha"
},
"scripts": {
"test": "test/sqlite-async-test.js"
"test": "mocha"
},

@@ -28,3 +28,6 @@ "repository": {

"sqlite3": "^3.1.8"
},
"devDependencies": {
"mocha": "^3.2.0"
}
}
}

@@ -23,6 +23,40 @@ # sqlite-async

## Transactions
## API
The `transaction` method allows a function returning a promise to be wrapped in a transaction. The function is passed the `Database` instance as its parameter.
### Database.open(filename, mode)
Static method that instantiates a new `Database` object and calls `Database#open(filename, mode)`. Returns a promise that is resolved with the Database instance.
### Database#open(filename, mode)
Opens the database with the same arguments as the sqlite3 `Database` constructor. Returns a promise that is resolved with the Database instance.
### Database#close()
Closes the database and returns a promise.
### Database#run(sql, [param, ...])
Equivalent to the sqlite3 `Database#run` method. Returns a promise that is resolved with the `this` parameter from the sqlite3 callback function.
### Database#get(sql, [param, ...])
Equivalent to the sqlite3 `Database#get` method. Returns a promise that is resolved with the row.
### Database#all(sql, [param, ...])
Equivalent to the sqlite3 `Database#all` method. Returns a promise that is resolved with the rows.
### Database#each(sql, [param, ...], callback)
Equivalent to the sqlite3 `Database#each` method. The per-row callback function is requied. Returns a promise that is resolved with the Database instance.
### Database#exec(sql)
Equivalent to the sqlite3 `Database#exec` method. Returns a promise that is resolved with the Database instance.
### Database#transaction(fn)
The `transaction` method allows a function returning a promise to be wrapped in a transaction. The function is passed the `Database` instance as its parameter. Returns a promise that is resolved with the functions promise value.
```javascript

@@ -37,2 +71,34 @@ db.transaction(db => {

### Database#prepare(sql, [param, ...])
Equivalent to the sqlite3 `Database#prepare` method. Returns a promise that is resolved with the Statement instance.
### Statement#bind(sql, [param, ...])
Equivalent to the sqlite3 `Statement#bind` method. Returns a promise that is resolved with the Statement instance.
### Statement#reset()
Equivalent to the sqlite3 `Statement#reset` method. Returns a promise that is resolved with the Statement instance.
### Statement#finalize()
Equivalent to the sqlite3 `Statement#finalize` method. Returns a promise that is resolved with no value because the statement can no longer be used.
### Statement#run([param, ...])
Equivalent to the sqlite3 `Statement#run` method. Returns a promise that is resolved with the `this` parameter from the sqlite3 callback function.
### Statement#get([param, ...])
Equivalent to the sqlite3 `Statement#get` method. Returns a promise that is resolved with the row.
### Statement#all([param, ...])
Equivalent to the sqlite3 `Statement#all` method. Returns a promise that is resolved with the rows.
### Statement#each([param, ...], callback)
Equivalent to the sqlite3 `Statement#each` method. The per-row callback function is requied. Returns a promise that is resolved with the Statement instance.
## License

@@ -39,0 +105,0 @@

@@ -69,9 +69,6 @@ /**

} else {
if (this.lastID) {
resolve(this.lastID)
} else if (this.changes) {
resolve(this.changes)
} else {
resolve()
}
resolve({
lastID: this.lastID,
changes: this.changes
})
}

@@ -153,2 +150,16 @@ }

transaction(fn) {
return this.exec('BEGIN TRANSACTION').then(_ => {
return fn(this).then(result => {
return this.exec('END TRANSACTION').then(_ => {
return result
})
}).catch(err => {
return this.exec('ROLLBACK TRANSACTION').then(_ => {
return Promise.reject(err)
})
})
})
}
prepare(...args) {

@@ -171,16 +182,2 @@ return new Promise((resolve, reject) => {

}
transaction(fn) {
return this.exec('BEGIN TRANSACTION').then(_ => {
return fn(this).then(result => {
return this.exec('END TRANSACTION').then(_ => {
return result
})
}).catch(err => {
return this.exec('ROLLBACK TRANSACTION').then(_ => {
return Promise.reject(err)
})
})
})
}
}

@@ -242,9 +239,6 @@

} else {
if (this.lastID) {
resolve(this.lastID)
} else if (this.changes) {
resolve(this.changes)
} else {
resolve()
}
resolve({
lastID: this.lastID,
changes: this.changes
})
}

@@ -251,0 +245,0 @@ }

@@ -0,110 +1,189 @@

const assert = require('assert')
const fs = require('fs')
const Database = require('../sqlite-async')
const fs = require('fs')
const FILENAME = 'test.db'
let db
let statement
function expect(val, exp) {
val = JSON.stringify(val)
if (val != exp) {
throw new Error(`Got ${val} instead of ${exp}.`)
}
console.log('Pass:', val)
}
describe('Module', function () {
describe('Database', function () {
describe('open', function () {
it('should open the database', function () {
return Database.open('test.db').then(_db => {
db = _db
})
})
})
describe('exec', function () {
it('should create a table', function () {
return db.exec(
`CREATE TABLE test (
id INT PRIMARY KEY,
name TEXT NOT NULL
)`)
})
})
describe('run (insert)', function () {
it('should insert a row', function () {
return db.run('INSERT INTO test VALUES (1, "inserted")')
})
})
describe('run (update)', function () {
it('should update a value', function () {
return db.run('UPDATE test SET name = "test" WHERE id = 1').then(result => {
assert.strictEqual(result.changes, 1, 'Expected one change in the database')
})
})
})
describe('run (delete)', function () {
it('should delete a row', function () {
return db.run('INSERT INTO test VALUES (2, "test")').then(_ => {
return db.run('DELETE FROM test WHERE id = 2').then(result => {
assert.strictEqual(result.changes, 1, 'Expected one change in the database')
})
})
})
})
describe('get', function () {
it('should select one row', function () {
return db.get('SELECT * FROM test').then(row => {
assert.deepStrictEqual(row, {
id: 1,
name: 'test'
})
})
})
})
describe('all', function () {
it('should select all rows', function () {
return db.all('SELECT * FROM test').then(rows => {
assert.deepStrictEqual(rows, [
{
id: 1,
name: 'test'
}
])
})
})
})
describe('each', function () {
it('should select rows and pass each to a callback', function () {
return db.each('SELECT * FROM test WHERE id = 1', (err, row) => {
assert.deepStrictEqual(row, {
id: 1,
name: 'test'
})
})
})
})
Database.open(FILENAME)
.then(_db => {
db = _db
expect(db.filename, `"${FILENAME}"`)
})
.then(_ => {
return db.exec('CREATE TABLE test (id INT, name TEXT NOT NULL)')
})
.then(_ => {
return db.run('INSERT INTO test VALUES (1, "test")')
})
.then(_ => {
return db.get('SELECT * FROM test')
})
.then(row => {
expect(row, '{"id":1,"name":"test"}')
return db.all('SELECT * FROM test')
})
.then(rows => {
expect(rows, '[{"id":1,"name":"test"}]')
})
.then(_ => {
return db.each('SELECT * FROM test WHERE id = 1', (err, row) => {
expect(row, '{"id":1,"name":"test"}')
describe('transaction (success)', function () {
it('should execute and rollback a failed transaction', function () {
return db.transaction(db => {
return Promise.all([
db.run('INSERT INTO test VALUES (2, "two")'),
db.run('INSERT INTO test VALUES (3, NULL)')
])
}).then(_ => {
throw new Error('The transaction should not have succeeded.')
}, err => {
assert.strictEqual(err.code, 'SQLITE_CONSTRAINT')
})
})
it('should leave the database unchanged', function () {
return db.all('SELECT * FROM test').then(rows => {
assert.strictEqual(rows.length, 1, 'Expected only one row in the database.')
})
})
})
})
.then(_ => {
return db.prepare('SELECT * FROM test WHERE id = ?')
})
.then(_statement => {
statement = _statement
return statement.bind(1)
})
.then(statement => {
return statement.get()
})
.then(row => {
expect(row, '{"id":1,"name":"test"}')
return statement.all()
})
.then(rows => {
expect(rows, '[{"id":1,"name":"test"}]')
})
.then(_ => {
return statement.each((err, row) => {
expect(row, '{"id":1,"name":"test"}')
describe('transaction (success)', function () {
it('should execute and commit a successful transaction', function () {
return db.transaction(db => {
return Promise.all([
db.run('INSERT INTO test VALUES (2, "two")'),
db.run('INSERT INTO test VALUES (3, "three")')
])
})
})
it('should have added two rows to the database', function () {
return db.all('SELECT * FROM test').then(rows => {
assert.strictEqual(rows.length, 3, 'Expected three rows in the database.')
})
})
})
describe('prepare', function () {
it('should prepare a statement', function () {
return db.prepare('SELECT * FROM test WHERE id = ?').then(_statement => {
statement = _statement
})
})
})
})
.then(_ => {
return statement.finalize()
})
.then(_ => {
return db.transaction(db => {
return Promise.all([
db.run('INSERT INTO test VALUES (2, "two")'),
db.run('INSERT INTO test VALUES (3, NULL)')
])
describe('Statement', function () {
describe('bind', function () {
it('should bind a value to the statement', function () {
return statement.bind(1)
})
})
describe('get', function () {
it('should select one row', function () {
return statement.get().then(row => {
assert.deepStrictEqual(row, {
id: 1,
name: 'test'
})
})
})
})
describe('all', function () {
it('should select all rows', function () {
return statement.all().then(rows => {
assert.deepStrictEqual(rows, [
{
id: 1,
name: 'test'
}
])
})
})
})
describe('each', function () {
it('should select rows and pass each to a callback', function () {
return statement.each((err, row) => {
assert.deepStrictEqual(row, {
id: 1,
name: 'test'
})
})
})
})
describe('run', function () {
it('should delete all rows from the database', function () {
return db.prepare('DELETE FROM test').then(statement => {
return statement.run().then(result => {
assert.strictEqual(result.changes, 3, 'Expected three changes in the database')
return statement.finalize()
})
})
})
})
describe('finalize', function () {
it('should finalize the statement', function () {
return statement.finalize()
})
})
})
.catch(err => {
expect(err.code, '"SQLITE_CONSTRAINT"')
})
.then(_ => {
return db.all('SELECT * FROM test')
})
.then(rows => {
expect(rows, '[{"id":1,"name":"test"}]')
})
.then(_ => {
return db.close()
})
.then(_ => {
return new Promise((resolve, reject) => {
fs.unlink(FILENAME, err => {
if (err) {
reject(err)
} else {
resolve()
}
describe('Database', function () {
describe('close', function () {
it('should close database', function () {
return db.close()
})
})
})
.then(_ => {
return db.run()
after(function (done) {
fs.unlink('test.db', done)
})
.catch(err => {
expect(err.message, `"Database.run: database is not open"`)
})
.then(_ => {
console.log('All tests pass.')
})
.catch(err => {
console.error('Error', err.message)
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc