Compare commits

..

4 Commits

3 changed files with 28 additions and 11 deletions

View File

@ -1,4 +1,6 @@
'use strict';
/*global Promise*/
var PromiseA = Promise;
function lowerFirst(str) {
return str.charAt(0).toLowerCase() + str.substr(1);
@ -45,7 +47,6 @@ var searchConditions = {
function wrap(db, dir, dbsMap) {
// TODO if I put a failure right here,
// why doesn't the unhandled promise rejection fire?
var PromiseA = require('bluebird');
var promises = [];
var earr = [];
var debug = false;
@ -435,15 +436,18 @@ function wrap(db, dir, dbsMap) {
params.orderBy = params.orderByDesc;
params.orderByDesc = true;
}
// IMPORTANT: " is not the same to sqlite as '.
// // " is exact and necessary
if (params.orderBy) {
sql += " ORDER BY '" + db.escape(snakeCase(params.orderBy)) + "' ";
sql += " ORDER BY \"" + db.escape(snakeCase(params.orderBy)) + "\" ";
if (params.orderByDesc) {
sql += "DESC ";
}
} else if (DB._indicesMap.updated_at) {
sql += " ORDER BY 'updated_at' DESC ";
sql += " ORDER BY \"updated_at\" DESC ";
} else if (DB._indicesMap.created_at) {
sql += " ORDER BY 'created_at' DESC ";
sql += " ORDER BY \"created_at\" DESC ";
}
if (isFinite(params.limit)) {
sql += " LIMIT " + parseInt(params.limit, 10);
@ -490,7 +494,13 @@ function wrap(db, dir, dbsMap) {
DB.save = function (data, oldId) {
if (!data[idnameCased] && !oldId) {
// NOTE saving the id both in the object and the id for now
data[idnameCased] = require('uuid').v4();
data[idnameCased] = require('crypto').randomBytes(16).toString('hex').split('');
data[idnameCased].splice(8, 0, '-');
data[idnameCased].splice(8 + 1 + 4, 0, '-');
data[idnameCased].splice(8 + 1 + 4 + 1 + 4, 0, '-');
data[idnameCased].splice(8 + 1 + 4 + 1 + 4 + 1 + 4, 0, '-');
data[idnameCased][14] = 4; // TODO look at the mock uuid in the Go code I wrote
data[idnameCased] = data[idnameCased].join('');
return DB.create(data[idnameCased], data).then(function (/*stats*/) {
//data._rowid = stats.id;
return data;
@ -673,9 +683,14 @@ function wrap(db, dir, dbsMap) {
});
}
function promisify(key) {
if ('function' !== typeof db[key] || /Async$/.test(key) || db[key + 'Async']) { return; }
db[key + 'Async'] = require('util').promisify(db[key]);
}
if (!db.__masterquest_init) {
db.__masterquest_init = true;
db = PromiseA.promisifyAll(db);
Object.keys(db).forEach(promisify);
['run', 'all'].forEach(promisify);
db.__masterquest_init = true;
db.escape = function (str) {
// TODO? literals for true,false,null

5
package-lock.json generated Normal file
View File

@ -0,0 +1,5 @@
{
"name": "masterquest-sqlite3",
"version": "1.3.0",
"lockfileVersion": 1
}

View File

@ -1,6 +1,6 @@
{
"name": "masterquest-sqlite3",
"version": "1.2.0",
"version": "1.3.1",
"description": "A NoSQL / SQLite3 Hybrid. All your indices are belong to us. Master Quest.",
"main": "lib/dbwrap",
"scripts": {
@ -13,8 +13,5 @@
"author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)",
"license": "(MIT OR Apache-2.0)",
"homepage": "https://git.coolaj86.com:coolaj86/masterquest-sqlite3.js",
"dependencies": {
"bluebird": "^3.5.1",
"uuid": "^3.2.1"
}
"dependencies": {}
}