Skip to content

Node sqlite3 migrate #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 21, 2018
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
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

SQLite wrapper for database-js
## About
Database-js-mysql is a wrapper around the [sql.js](https://github.com/kripken/sql.js) package by lovasoa. It is intended to be used with the [database-js](https://github.com/mlaanderson/database-js) package.
Database-js-mysql is a wrapper around the [node-sqlite3](https://github.com/mapbox/node-sqlite3) package. It is intended to be used with the [database-js](https://github.com/mlaanderson/database-js) package.
## Usage
~~~~
~~~~Javascript
var Database = require('database-js2').Connection;

(async () => {
let connection, statement, rows;
connection = new Database('database-js-sqlite:///test.sqlite');
connection = new Database('sqlite:///test.sqlite');

try {
statement = await connection.prepareStatement("SELECT * FROM tablea WHERE user_name = ?");
Expand All @@ -22,4 +22,9 @@ var Database = require('database-js2').Connection;
await connection.close();
}
})();
~~~~
## [sql.js](https://github.com/kripken/sql.js)
The original sql.js package can still be used as well. Pass "driver=sql.js" in the connection parameter string. e.g.
~~~~Javascript
connection = new Database('sqlite:///test.sqlite?driver=sql.js');
~~~~
122 changes: 121 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ var sqlite = require('sql.js');
var debug = require('debug')('database-js-sqlite');
const fs = require('fs');

var sqlite3 = require('sqlite3');
const Common = require('database-js-common');

var m_database = Symbol('database');
var m_filename = Symbol('filename');
var m_transaction = Symbol('transaction');
Expand Down Expand Up @@ -151,8 +154,125 @@ class SQLite {
}
}


class SQLite3 {
constructor(database) {
if (database) {
this.__database = new sqlite3.Database(database);
} else {
this.__database = new sqlite3.Database(':memory:');
}
this.__transaction = false;
}

query(sql) {
debug('Query: %s', sql);

return new Promise((resolve, reject) => {
this.__database.all(sql, (err, rows) => {
if (err) {
return reject(err);
}
debug('Data: %o', rows);
return resolve(rows);
});
});
}

execute(sql) {
debug('Execute: %s', sql);

return new Promise((resolve, reject) => {
this.__database.exec(sql, (err) => {
if (err) {
return reject(err);
}
resolve();
});
});
}

close() {
return new Promise((resolve, reject) => {
this.__database.close((err) => {
if (err) {
return reject(err);
}
resolve(true);
});
});
}

isTransactionSupported() {
return true;
}

inTransaction() {
return this.__transaction;
}

beginTransaction() {
if (this.inTransaction() == true) {
return Promise.resolve(false);
}
return new Promise((resolve, reject) => {
this.execute('BEGIN')
.then(() => {
this.__transaction = true;
resolve(true);
})
.catch(error => {
reject(error);
});
});
}

commit() {
if (this.inTransaction() == false) {
return Promise.resolve(false);
}
return new Promise((resolve, reject) => {
this.execute('COMMIT')
.then(() => {
this.__transaction = false;
resolve(true);
})
.catch(error => {
reject(error);
})
});
}

rollback() {
if (this.inTransaction() == false) {
return Promise.resolve(false);
}
return new Promise((resolve, reject) => {
this.execute('ROLLBACK')
.then(() => {
this.__transaction = false;
resolve(true);
})
.catch(error => {
reject(error);
})
});
}
}

module.exports = {
open: function(connection) {
return new SQLite(connection.Database);
let useSqlite3 = true;
if (connection.Parameters) {
let params = Common.parseConnectionParams(connection.Parameters);
if (("driver" in params == true) && (params.driver === "sql.js")) {
useSqlite3 = false;
}
}
if (useSqlite3 == true) {
return new SQLite3(connection.Database);
} else {
return new SQLite(connection.Database);
}
}
};
Loading