-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteTable.js
More file actions
157 lines (157 loc) · 5.58 KB
/
SQLiteTable.js
File metadata and controls
157 lines (157 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/// <reference path="../typings/tsd.d.ts"/>
var debug = require('debug');
var log = debug('sqlite-table');
var logError = debug('sqlite-table:error');
var SQLiteTable = (function () {
function SQLiteTable(_db) {
this._db = _db;
}
SQLiteTable.prototype.joins = function (record, done) {
done(null, record);
};
SQLiteTable.prototype.all = function (params, next) {
var _this = this;
if (typeof params === 'function') {
next = params;
params = {};
}
log('get all', params);
var stmt = this.getSQLSelectStmt(params);
this.db.all(stmt.sql, stmt.objVars, function (err, records) {
if (err)
return next(err);
_this.joinMany(records, next);
});
};
SQLiteTable.prototype.count = function (where, next) {
if (where === void 0) { where = {}; }
if (next === void 0) { next = function () {
}; }
log('get count', where);
var stmt = this.getSQLSelectStmt(where, null, 'COUNT(id) as c');
this.db.get(stmt.sql, stmt.objVars, function (err, record) {
next(err, parseInt(record.c));
});
};
SQLiteTable.prototype.allLimited = function (where, limit, next) {
var _this = this;
if (limit === void 0) { limit = { limit: 1000, offset: 0 }; }
if (next === void 0) { next = function () {
}; }
log('get allLimited', where, limit);
var stmt = this.getSQLSelectStmt(where, limit);
this.db.all(stmt.sql, stmt.objVars, function (err, records) {
if (err)
return next(err);
_this.joinMany(records, next);
});
};
SQLiteTable.prototype.joinMany = function (records, next) {
var _this = this;
var joined = [];
if (records.length === 0) {
return next(null, records);
}
records.forEach(function (r) {
_this.joins(r, function (err, j) {
if (err) {
return next(err);
}
joined.push(j);
if (joined.length === records.length) {
next(null, joined);
}
});
});
};
SQLiteTable.prototype.find = function (params, next) {
var _this = this;
var typeofParams = typeof params;
if (typeofParams === 'string' || typeofParams === 'number') {
params = { id: params };
}
log('find', params);
var stmt = this.getSQLSelectStmt(params);
this.db.get(stmt.sql, stmt.objVars, function (err, record) {
if (err)
return next(err);
if (!record)
return next(null);
_this.joins(record, next);
});
};
SQLiteTable.prototype.insert = function (data, next) {
log('insert', data);
if (data.id) {
delete data.id;
}
var keys = Object.keys(data);
var keysVars = keys.map(function (key) { return '$' + key; });
var objVars = this.getObjVars(data);
var sql = 'INSERT INTO ' + this.getTableName() + '(' + keys.join(',') + ') ' + 'VALUES(' + keysVars.join(',') + ')';
this.db.run(sql, objVars, function (err) {
data.id = this.lastID;
next(err, data.id);
});
};
SQLiteTable.prototype.update = function (data, next) {
log('update', data);
if (!data.id) {
logError('id is not specified, can not update');
next(new Error('id is not specified'));
return;
}
var keys = Object.keys(data);
var updateStmts = keys.map(function (key) {
return key + '=$' + key;
});
var objVars = this.getObjVars(data);
var sql = 'UPDATE ' + this.getTableName() + ' SET ' + updateStmts.join(' , ') + ' WHERE id=$id';
this.db.run(sql, objVars, next);
};
SQLiteTable.prototype.remove = function (id, next) {
this.db.run('DELETE FROM ' + this.getTableName() + ' WHERE id=?', id, function (err) {
next(err, this.changes === 1);
});
};
SQLiteTable.prototype.getObjVars = function (obj) {
var objVars = {};
Object.keys(obj).forEach(function (key) {
objVars['$' + key] = obj[key];
});
return objVars;
};
SQLiteTable.prototype.getSQLSelectStmt = function (params, limit, select) {
if (select === void 0) { select = '*'; }
var objVars = this.getObjVars(params);
var whereStmts = Object.keys(params).map(function (key) { return key + '=$' + key; });
var where = whereStmts.length > 0 ? ' WHERE ' + whereStmts.join(' AND ') : '';
var limitSQL = '';
if (limit) {
limitSQL += ' LIMIT ' + parseInt(limit.limit).toString();
if (limit.offset) {
limitSQL += ' OFFSET ' + parseInt(limit.offset).toString();
}
}
return {
sql: 'SELECT ' + select + ' FROM ' + this.getTableName() + where + limitSQL,
objVars: objVars
};
};
SQLiteTable.prototype.getTableName = function () {
if (!this.tableName) {
throw new Error('tableName is not specified, override public get tableName():string method');
}
return this.tableName;
};
Object.defineProperty(SQLiteTable.prototype, "db", {
get: function () {
return this._db;
},
enumerable: true,
configurable: true
});
return SQLiteTable;
})();
module.exports = SQLiteTable;
//# sourceMappingURL=SQLiteTable.js.map