1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-30 10:36:35 +02:00
joplin/packages/lib/database-driver-node.js

85 lines
1.6 KiB
JavaScript
Raw Normal View History

const shim = require('./shim').default;
2017-06-11 23:11:14 +02:00
const Promise = require('promise');
class DatabaseDriverNode {
open(options) {
return new Promise((resolve, reject) => {
const sqlite3 = shim.nodeSqlite().verbose();
this.db_ = new sqlite3.Database(options.name, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, error => {
2017-06-11 23:11:14 +02:00
if (error) {
reject(error);
return;
}
resolve();
});
});
}
2017-07-05 23:29:00 +02:00
sqliteErrorToJsError(error, sql = null, params = null) {
const msg = [error.toString()];
2017-07-05 23:29:00 +02:00
if (sql) msg.push(sql);
if (params) msg.push(params);
const output = new Error(msg.join(': '));
2017-07-05 23:29:00 +02:00
if (error.code) output.code = error.code;
return output;
}
2017-06-11 23:11:14 +02:00
selectOne(sql, params = null) {
if (!params) params = {};
return new Promise((resolve, reject) => {
this.db_.get(sql, params, (error, row) => {
if (error) {
reject(error);
return;
}
resolve(row);
});
});
}
2020-09-06 14:07:00 +02:00
loadExtension(path) {
return new Promise((resolve, reject) => {
this.db_.loadExtension(path, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
2020-09-06 14:07:00 +02:00
}
2017-06-11 23:11:14 +02:00
selectAll(sql, params = null) {
if (!params) params = {};
return new Promise((resolve, reject) => {
this.db_.all(sql, params, (error, row) => {
if (error) {
reject(error);
return;
}
resolve(row);
});
});
}
exec(sql, params = null) {
if (!params) params = {};
return new Promise((resolve, reject) => {
this.db_.run(sql, params, error => {
2017-06-11 23:11:14 +02:00
if (error) {
reject(error);
return;
}
resolve();
});
});
}
lastInsertId() {
throw new Error('NOT IMPLEMENTED');
}
2017-06-11 23:11:14 +02:00
}
2019-07-29 15:43:53 +02:00
module.exports = { DatabaseDriverNode };