backend-plus 2.0.0-rc.23 → 2.0.0-rc.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +263 -263
- package/for-client/my-localdb.d.ts +59 -59
- package/for-client/my-localdb.js +688 -688
- package/for-client/my-localdb.js.map +1 -1
- package/for-client/my-websqldb.d.ts +36 -36
- package/for-client/my-websqldb.js +599 -599
- package/for-client/my-websqldb.js.map +1 -1
- package/lib/backend-plus.d.ts +2 -1
- package/lib/table-def-adapt.js +358 -358
- package/lib/tables/table-tokens.js +28 -28
- package/package.json +134 -134
- package/unlogged/compatibilidad.js +161 -161
|
@@ -1,600 +1,600 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
12
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
-
function step(op) {
|
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
-
switch (op[0]) {
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
(function (factory) {
|
|
38
|
-
if (typeof module === "object" && typeof module.exports === "object") {
|
|
39
|
-
var v = factory(require, exports);
|
|
40
|
-
if (v !== undefined) module.exports = v;
|
|
41
|
-
}
|
|
42
|
-
else if (typeof define === "function" && define.amd) {
|
|
43
|
-
define(["require", "exports", "like-ar", "best-globals", "sql-tools"], factory);
|
|
44
|
-
}
|
|
45
|
-
})(function (require, exports) {
|
|
46
|
-
//import { TableDefinition } from "backend-plus";
|
|
47
|
-
"use strict";
|
|
48
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
49
|
-
exports.WebsqlDb = exports.detectedFeatures = void 0;
|
|
50
|
-
//x// <amd-dependency path="../../../node_modules/like-ar/like-ar.d.ts" name="likeAr"/>
|
|
51
|
-
//x// <reference path="../../node_modules/like-ar/like-ar.d.ts" />
|
|
52
|
-
/// <reference path="../node_modules/types.d.ts/modules/myOwn/in-myOwn.d.ts" />
|
|
53
|
-
/// <reference path="../../../lib/in-backend-plus.d.ts" />
|
|
54
|
-
/// <reference path="../../../lib/backend-plus.d.ts" />
|
|
55
|
-
/// <reference path="../../lib/in-backend-plus.d.ts" />
|
|
56
|
-
/// <reference path="../../lib/backend-plus.d.ts" />
|
|
57
|
-
/// <reference path="../lib/in-backend-plus.d.ts" />
|
|
58
|
-
/// <reference path="../lib/backend-plus.d.ts" />
|
|
59
|
-
/// <reference path="lib/in-backend-plus.d.ts" />
|
|
60
|
-
/// <reference path="lib/backend-plus.d.ts" />
|
|
61
|
-
var likeAr = require("like-ar");
|
|
62
|
-
var best_globals_1 = require("best-globals");
|
|
63
|
-
var SqlTools = require("sql-tools");
|
|
64
|
-
exports.detectedFeatures = {
|
|
65
|
-
needToCopyResults: null
|
|
66
|
-
};
|
|
67
|
-
var WebsqlDb = /** @class */ (function () {
|
|
68
|
-
function WebsqlDb(name) {
|
|
69
|
-
this.name = name;
|
|
70
|
-
var version = 1.0;
|
|
71
|
-
var dbName = this.name;
|
|
72
|
-
var dbDisplayName = this.name;
|
|
73
|
-
var dbSize = 2 * 1024 * 1024;
|
|
74
|
-
try {
|
|
75
|
-
this.db = openDatabase(dbName, version, dbDisplayName, dbSize);
|
|
76
|
-
this.wait4detectedFeatures = this.detectFeatures();
|
|
77
|
-
}
|
|
78
|
-
catch (err) {
|
|
79
|
-
console.log(err.message);
|
|
80
|
-
throw err;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
WebsqlDb.prototype.detectFeatures = function () {
|
|
84
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
85
|
-
var detectedFeatures, detectElement, tableDef, err_1;
|
|
86
|
-
return __generator(this, function (_a) {
|
|
87
|
-
switch (_a.label) {
|
|
88
|
-
case 0:
|
|
89
|
-
detectedFeatures = { needToCopyResults: false };
|
|
90
|
-
detectElement = { feature: 'need_to_copy_results', value: false };
|
|
91
|
-
_a.label = 1;
|
|
92
|
-
case 1:
|
|
93
|
-
_a.trys.push([1, 5, , 7]);
|
|
94
|
-
tableDef = {
|
|
95
|
-
name: '_detect',
|
|
96
|
-
fields: [
|
|
97
|
-
{ name: 'feature', typeName: 'text' },
|
|
98
|
-
{ name: 'value', typeName: 'boolean' }
|
|
99
|
-
],
|
|
100
|
-
primaryKey: ['feature']
|
|
101
|
-
};
|
|
102
|
-
return [4 /*yield*/, this.registerStructure(tableDef)];
|
|
103
|
-
case 2:
|
|
104
|
-
_a.sent();
|
|
105
|
-
return [4 /*yield*/, this.putOne('_detect', detectElement)];
|
|
106
|
-
case 3:
|
|
107
|
-
_a.sent();
|
|
108
|
-
return [4 /*yield*/, this.getOne('_detect', ['need_to_copy_results'], true)];
|
|
109
|
-
case 4:
|
|
110
|
-
_a.sent();
|
|
111
|
-
return [3 /*break*/, 7];
|
|
112
|
-
case 5:
|
|
113
|
-
err_1 = _a.sent();
|
|
114
|
-
detectElement.value = true;
|
|
115
|
-
detectedFeatures.needToCopyResults = true;
|
|
116
|
-
return [4 /*yield*/, this.putOne('_detect', detectElement)];
|
|
117
|
-
case 6:
|
|
118
|
-
_a.sent();
|
|
119
|
-
return [3 /*break*/, 7];
|
|
120
|
-
case 7: return [2 /*return*/, detectedFeatures];
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
};
|
|
125
|
-
WebsqlDb.prototype.generateSqlForTableDef = function (tableDef) {
|
|
126
|
-
var typeDb = {
|
|
127
|
-
integer: 'integer',
|
|
128
|
-
number: 'real',
|
|
129
|
-
date: 'text',
|
|
130
|
-
boolean: 'text',
|
|
131
|
-
text: 'text',
|
|
132
|
-
jsonb: 'text',
|
|
133
|
-
json: 'text'
|
|
134
|
-
};
|
|
135
|
-
var lines = [];
|
|
136
|
-
var consLines = [];
|
|
137
|
-
lines.push('CREATE TABLE IF NOT EXISTS ' + SqlTools.quoteIdent(tableDef.name) + ' (');
|
|
138
|
-
var fields = [];
|
|
139
|
-
tableDef.fields.forEach(function (fieldDef) {
|
|
140
|
-
var fieldType = typeDb[fieldDef.typeName] || '"' + fieldDef.typeName + '"';
|
|
141
|
-
if (fieldDef.sizeByte == 4) {
|
|
142
|
-
fieldType = 'integer';
|
|
143
|
-
}
|
|
144
|
-
fields.push(' ' + SqlTools.quoteIdent(fieldDef.name) +
|
|
145
|
-
' ' + fieldType +
|
|
146
|
-
(fieldDef.defaultValue != null ? ' default ' + SqlTools.quoteLiteral(fieldDef.defaultValue) : '') +
|
|
147
|
-
(fieldDef.defaultDbValue != null ? ' default ' + fieldDef.defaultDbValue : ''));
|
|
148
|
-
if (fieldDef.typeName === 'text' && !fieldDef.allowEmptyText) {
|
|
149
|
-
consLines.push('alter table ' + SqlTools.quoteIdent(tableDef.name) +
|
|
150
|
-
' add constraint ' + SqlTools.quoteIdent(fieldDef.name + "<>''") +
|
|
151
|
-
' check (' + SqlTools.quoteIdent(fieldDef.name) + "<>'');");
|
|
152
|
-
}
|
|
153
|
-
if (fieldDef.nullable === false) {
|
|
154
|
-
consLines.push('alter table ' + SqlTools.quoteIdent(tableDef.name) +
|
|
155
|
-
' alter column ' + SqlTools.quoteIdent(fieldDef.name) + ' set not null;');
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
|
-
lines.push(fields.join(', \n'));
|
|
159
|
-
if (tableDef.primaryKey) {
|
|
160
|
-
lines.push(', primary key (' + tableDef.primaryKey.map(function (name) { return SqlTools.quoteIdent(name); }).join(', ') + ')');
|
|
161
|
-
}
|
|
162
|
-
lines.push(');');
|
|
163
|
-
return lines.join('\n'); //+'\n-- conss\n' + consLines.join('\n')
|
|
164
|
-
};
|
|
165
|
-
WebsqlDb.deleteDatabase = function (name) {
|
|
166
|
-
//TODO
|
|
167
|
-
return Promise.resolve();
|
|
168
|
-
};
|
|
169
|
-
WebsqlDb.prototype.executeQuery = function (sql, params) {
|
|
170
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
171
|
-
var db;
|
|
172
|
-
return __generator(this, function (_a) {
|
|
173
|
-
db = this.db;
|
|
174
|
-
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
175
|
-
if (!db)
|
|
176
|
-
return reject('no database.');
|
|
177
|
-
var result;
|
|
178
|
-
db.transaction(function (tx) {
|
|
179
|
-
tx.executeSql(sql, params || [], function (_tx, res) {
|
|
180
|
-
result = res.rows;
|
|
181
|
-
});
|
|
182
|
-
}, function (err) {
|
|
183
|
-
reject(err);
|
|
184
|
-
}, function success() {
|
|
185
|
-
resolve(result);
|
|
186
|
-
});
|
|
187
|
-
})];
|
|
188
|
-
});
|
|
189
|
-
});
|
|
190
|
-
};
|
|
191
|
-
;
|
|
192
|
-
WebsqlDb.prototype.executeQueries = function (sql, data) {
|
|
193
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
194
|
-
var db;
|
|
195
|
-
return __generator(this, function (_a) {
|
|
196
|
-
db = this.db;
|
|
197
|
-
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
198
|
-
if (!db)
|
|
199
|
-
return reject('no database.');
|
|
200
|
-
db.transaction(function (tx) {
|
|
201
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
202
|
-
return __generator(this, function (_a) {
|
|
203
|
-
data.forEach(function (data) {
|
|
204
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
205
|
-
return __generator(this, function (_a) {
|
|
206
|
-
tx.executeSql(sql, data);
|
|
207
|
-
return [2 /*return*/];
|
|
208
|
-
});
|
|
209
|
-
});
|
|
210
|
-
});
|
|
211
|
-
return [2 /*return*/];
|
|
212
|
-
});
|
|
213
|
-
});
|
|
214
|
-
}, function (tx, err) {
|
|
215
|
-
reject(err);
|
|
216
|
-
}, function (tx, res) {
|
|
217
|
-
resolve();
|
|
218
|
-
});
|
|
219
|
-
})];
|
|
220
|
-
});
|
|
221
|
-
});
|
|
222
|
-
};
|
|
223
|
-
;
|
|
224
|
-
WebsqlDb.prototype.registerStructure = function (tableDef) {
|
|
225
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
226
|
-
return __generator(this, function (_a) {
|
|
227
|
-
switch (_a.label) {
|
|
228
|
-
case 0: return [4 /*yield*/, this.executeQuery("CREATE TABLE IF NOT EXISTS _structures (name string primary key, def string not null);", [])];
|
|
229
|
-
case 1:
|
|
230
|
-
_a.sent();
|
|
231
|
-
return [4 /*yield*/, this.executeQuery("INSERT OR REPLACE INTO _structures (name, def) values (?, ?);", [tableDef.name, JSON.stringify(tableDef)])];
|
|
232
|
-
case 2:
|
|
233
|
-
_a.sent();
|
|
234
|
-
return [4 /*yield*/, this.executeQuery("DROP TABLE IF EXISTS " + SqlTools.quoteIdent(tableDef.name), [])];
|
|
235
|
-
case 3:
|
|
236
|
-
_a.sent();
|
|
237
|
-
return [4 /*yield*/, this.executeQuery(this.generateSqlForTableDef(tableDef), [])];
|
|
238
|
-
case 4:
|
|
239
|
-
_a.sent();
|
|
240
|
-
return [2 /*return*/];
|
|
241
|
-
}
|
|
242
|
-
});
|
|
243
|
-
});
|
|
244
|
-
};
|
|
245
|
-
WebsqlDb.prototype.getStructure = function (tableName) {
|
|
246
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
247
|
-
var result;
|
|
248
|
-
return __generator(this, function (_a) {
|
|
249
|
-
switch (_a.label) {
|
|
250
|
-
case 0: return [4 /*yield*/, this.executeQuery("SELECT * from _structures where name = ?", [tableName])];
|
|
251
|
-
case 1:
|
|
252
|
-
result = _a.sent();
|
|
253
|
-
if (result.length) {
|
|
254
|
-
return [2 /*return*/, JSON.parse(result.item(0).def)];
|
|
255
|
-
}
|
|
256
|
-
else {
|
|
257
|
-
return [2 /*return*/, undefined];
|
|
258
|
-
}
|
|
259
|
-
return [2 /*return*/];
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
});
|
|
263
|
-
};
|
|
264
|
-
WebsqlDb.prototype.existsStructure = function (tableName) {
|
|
265
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
266
|
-
var tableDef;
|
|
267
|
-
return __generator(this, function (_a) {
|
|
268
|
-
switch (_a.label) {
|
|
269
|
-
case 0: return [4 /*yield*/, this.getStructure(tableName)];
|
|
270
|
-
case 1:
|
|
271
|
-
tableDef = _a.sent();
|
|
272
|
-
return [2 /*return*/, tableDef ? true : false];
|
|
273
|
-
}
|
|
274
|
-
});
|
|
275
|
-
});
|
|
276
|
-
};
|
|
277
|
-
WebsqlDb.prototype.convertSQLResultSetRowListToArray = function (rowResultSetList, copyResults) {
|
|
278
|
-
var arr = [];
|
|
279
|
-
for (var i = 0; i < rowResultSetList.length; i++) {
|
|
280
|
-
var result = rowResultSetList.item(i);
|
|
281
|
-
if (copyResults) {
|
|
282
|
-
result = (0, best_globals_1.changing)(result, {});
|
|
283
|
-
}
|
|
284
|
-
arr.push(result);
|
|
285
|
-
}
|
|
286
|
-
return arr;
|
|
287
|
-
};
|
|
288
|
-
WebsqlDb.prototype.fieldIsNotSupported = function (fieldName, notSupportedFields) {
|
|
289
|
-
var isNotSupported = notSupportedFields.find(function (field) {
|
|
290
|
-
return field.name == fieldName;
|
|
291
|
-
});
|
|
292
|
-
return isNotSupported ? true : false;
|
|
293
|
-
};
|
|
294
|
-
WebsqlDb.prototype.getNotSupportedFields = function (tableDef) {
|
|
295
|
-
var jsonbFields = tableDef.fields.filter(function (field) {
|
|
296
|
-
return field.typeName == 'jsonb' || field.typeName == 'date' || field.typeName == 'timestamp' || field.typeName == 'boolean';
|
|
297
|
-
});
|
|
298
|
-
return jsonbFields;
|
|
299
|
-
};
|
|
300
|
-
WebsqlDb.prototype.convertNotSupportedFields = function (records, notSupportedFields) {
|
|
301
|
-
records.forEach(function (row) {
|
|
302
|
-
if (notSupportedFields) {
|
|
303
|
-
notSupportedFields.forEach(function (notSupportedField) {
|
|
304
|
-
var fieldName = notSupportedField.name;
|
|
305
|
-
row[fieldName] = JSON.parse(row[fieldName]);
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
});
|
|
309
|
-
return records;
|
|
310
|
-
};
|
|
311
|
-
WebsqlDb.prototype.getOneIfExists = function (tableName, key) {
|
|
312
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
313
|
-
var err_2;
|
|
314
|
-
return __generator(this, function (_a) {
|
|
315
|
-
switch (_a.label) {
|
|
316
|
-
case 0:
|
|
317
|
-
_a.trys.push([0, 2, , 3]);
|
|
318
|
-
return [4 /*yield*/, this.getOne(tableName, key)];
|
|
319
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
320
|
-
case 2:
|
|
321
|
-
err_2 = _a.sent();
|
|
322
|
-
return [2 /*return*/, undefined];
|
|
323
|
-
case 3: return [2 /*return*/];
|
|
324
|
-
}
|
|
325
|
-
});
|
|
326
|
-
});
|
|
327
|
-
};
|
|
328
|
-
WebsqlDb.prototype.getOne = function (tableName, key, ignoreDetectFeatures) {
|
|
329
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
330
|
-
var tableDef, jsonbFields, fieldNames, whereExpr, sql, result, detectedFeatures, copyResults, convertedResult;
|
|
331
|
-
return __generator(this, function (_a) {
|
|
332
|
-
switch (_a.label) {
|
|
333
|
-
case 0:
|
|
334
|
-
ignoreDetectFeatures = ignoreDetectFeatures || false;
|
|
335
|
-
return [4 /*yield*/, this.getStructure(tableName)];
|
|
336
|
-
case 1:
|
|
337
|
-
tableDef = _a.sent();
|
|
338
|
-
jsonbFields = this.getNotSupportedFields(tableDef);
|
|
339
|
-
fieldNames = tableDef.primaryKey;
|
|
340
|
-
whereExpr = [];
|
|
341
|
-
fieldNames.forEach(function (fieldName, i) {
|
|
342
|
-
whereExpr.push(fieldName + '=' + SqlTools.quoteLiteral(key[i]));
|
|
343
|
-
});
|
|
344
|
-
sql = "SELECT * \n from " + SqlTools.quoteIdent(tableName) + "\n where " + whereExpr.join(' and ');
|
|
345
|
-
return [4 /*yield*/, this.executeQuery(sql, [])];
|
|
346
|
-
case 2:
|
|
347
|
-
result = _a.sent();
|
|
348
|
-
if (!!ignoreDetectFeatures) return [3 /*break*/, 4];
|
|
349
|
-
return [4 /*yield*/, this.wait4detectedFeatures];
|
|
350
|
-
case 3:
|
|
351
|
-
detectedFeatures = _a.sent();
|
|
352
|
-
_a.label = 4;
|
|
353
|
-
case 4:
|
|
354
|
-
copyResults = !ignoreDetectFeatures && detectedFeatures.needToCopyResults;
|
|
355
|
-
convertedResult = this.convertNotSupportedFields(this.convertSQLResultSetRowListToArray(result, copyResults), jsonbFields);
|
|
356
|
-
if (convertedResult[0]) {
|
|
357
|
-
return [2 /*return*/, convertedResult[0]];
|
|
358
|
-
}
|
|
359
|
-
else {
|
|
360
|
-
throw Error('no existe el elemento');
|
|
361
|
-
}
|
|
362
|
-
return [2 /*return*/];
|
|
363
|
-
}
|
|
364
|
-
});
|
|
365
|
-
});
|
|
366
|
-
};
|
|
367
|
-
WebsqlDb.prototype.getChild = function (tableName, parentKey) {
|
|
368
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
369
|
-
var tableDef, jsonbFields, fieldNames, whereExpr, sql, result, detectedFeatures;
|
|
370
|
-
return __generator(this, function (_a) {
|
|
371
|
-
switch (_a.label) {
|
|
372
|
-
case 0: return [4 /*yield*/, this.getStructure(tableName)];
|
|
373
|
-
case 1:
|
|
374
|
-
tableDef = _a.sent();
|
|
375
|
-
jsonbFields = this.getNotSupportedFields(tableDef);
|
|
376
|
-
fieldNames = tableDef.primaryKey;
|
|
377
|
-
whereExpr = [];
|
|
378
|
-
parentKey.forEach(function (key, i) {
|
|
379
|
-
whereExpr.push(fieldNames[i] + '=' + SqlTools.quoteLiteral(key));
|
|
380
|
-
});
|
|
381
|
-
sql = "SELECT * from " + SqlTools.quoteIdent(tableName);
|
|
382
|
-
if (parentKey.length) {
|
|
383
|
-
sql += " where " + whereExpr.join(' and ');
|
|
384
|
-
}
|
|
385
|
-
return [4 /*yield*/, this.executeQuery(sql, [])];
|
|
386
|
-
case 2:
|
|
387
|
-
result = _a.sent();
|
|
388
|
-
return [4 /*yield*/, this.wait4detectedFeatures];
|
|
389
|
-
case 3:
|
|
390
|
-
detectedFeatures = _a.sent();
|
|
391
|
-
return [2 /*return*/, this.convertNotSupportedFields(this.convertSQLResultSetRowListToArray(result, detectedFeatures.needToCopyResults), jsonbFields)];
|
|
392
|
-
}
|
|
393
|
-
});
|
|
394
|
-
});
|
|
395
|
-
};
|
|
396
|
-
WebsqlDb.prototype.getAll = function (tableName) {
|
|
397
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
398
|
-
var tableDef, jsonbFields, results, detectedFeatures;
|
|
399
|
-
return __generator(this, function (_a) {
|
|
400
|
-
switch (_a.label) {
|
|
401
|
-
case 0: return [4 /*yield*/, this.getStructure(tableName)];
|
|
402
|
-
case 1:
|
|
403
|
-
tableDef = _a.sent();
|
|
404
|
-
jsonbFields = this.getNotSupportedFields(tableDef);
|
|
405
|
-
return [4 /*yield*/, this.executeQuery("SELECT * from " + SqlTools.quoteIdent(tableName), [])];
|
|
406
|
-
case 2:
|
|
407
|
-
results = _a.sent();
|
|
408
|
-
return [4 /*yield*/, this.wait4detectedFeatures];
|
|
409
|
-
case 3:
|
|
410
|
-
detectedFeatures = _a.sent();
|
|
411
|
-
return [2 /*return*/, this.convertNotSupportedFields(this.convertSQLResultSetRowListToArray(results, detectedFeatures.needToCopyResults), jsonbFields)];
|
|
412
|
-
}
|
|
413
|
-
});
|
|
414
|
-
});
|
|
415
|
-
};
|
|
416
|
-
WebsqlDb.prototype.getAllStructures = function () {
|
|
417
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
418
|
-
var results, structures, i;
|
|
419
|
-
return __generator(this, function (_a) {
|
|
420
|
-
switch (_a.label) {
|
|
421
|
-
case 0: return [4 /*yield*/, this.executeQuery("SELECT * from _structures where name <> '_detect'", [])];
|
|
422
|
-
case 1:
|
|
423
|
-
results = _a.sent();
|
|
424
|
-
structures = [];
|
|
425
|
-
for (i = 0; i < results.length; i++) {
|
|
426
|
-
structures.push(JSON.parse(results.item(i).def));
|
|
427
|
-
}
|
|
428
|
-
return [2 /*return*/, structures];
|
|
429
|
-
}
|
|
430
|
-
});
|
|
431
|
-
});
|
|
432
|
-
};
|
|
433
|
-
WebsqlDb.prototype.isEmpty = function (tableName) {
|
|
434
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
435
|
-
var result;
|
|
436
|
-
return __generator(this, function (_a) {
|
|
437
|
-
switch (_a.label) {
|
|
438
|
-
case 0: return [4 /*yield*/, this.executeQuery("SELECT count(*) as cantidad from " + SqlTools.quoteIdent(tableName), [])];
|
|
439
|
-
case 1:
|
|
440
|
-
result = _a.sent();
|
|
441
|
-
return [2 /*return*/, result.item(0).cantidad == 0];
|
|
442
|
-
}
|
|
443
|
-
});
|
|
444
|
-
});
|
|
445
|
-
};
|
|
446
|
-
WebsqlDb.prototype.putOne = function (tableName, element) {
|
|
447
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
448
|
-
var ldb, createPromiseForFK, tableDef, jsonbFields, promisesArray;
|
|
449
|
-
return __generator(this, function (_a) {
|
|
450
|
-
switch (_a.label) {
|
|
451
|
-
case 0:
|
|
452
|
-
ldb = this;
|
|
453
|
-
createPromiseForFK = function createPromiseForFK(fk) {
|
|
454
|
-
return Promise.resolve().then(function () {
|
|
455
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
456
|
-
var fkTableDef, pk, isFKCompleteInSource, fkRecord;
|
|
457
|
-
return __generator(this, function (_a) {
|
|
458
|
-
switch (_a.label) {
|
|
459
|
-
case 0: return [4 /*yield*/, ldb.getStructure(fk.references)];
|
|
460
|
-
case 1:
|
|
461
|
-
fkTableDef = _a.sent();
|
|
462
|
-
if (!fkTableDef) return [3 /*break*/, 3];
|
|
463
|
-
pk = [];
|
|
464
|
-
fk.fields.forEach(function (field) {
|
|
465
|
-
pk.push(element[field.source]);
|
|
466
|
-
});
|
|
467
|
-
isFKCompleteInSource = fk.fields.filter(function (field) {
|
|
468
|
-
return element[field.source] == null;
|
|
469
|
-
}).length == 0;
|
|
470
|
-
if (!isFKCompleteInSource) return [3 /*break*/, 3];
|
|
471
|
-
return [4 /*yield*/, ldb.getOneIfExists(fk.references, pk)];
|
|
472
|
-
case 2:
|
|
473
|
-
fkRecord = _a.sent();
|
|
474
|
-
if (fkRecord) {
|
|
475
|
-
fk.displayFields.forEach(function (field) {
|
|
476
|
-
element[fk.alias + '__' + field] = fkRecord[field];
|
|
477
|
-
});
|
|
478
|
-
}
|
|
479
|
-
_a.label = 3;
|
|
480
|
-
case 3: return [2 /*return*/];
|
|
481
|
-
}
|
|
482
|
-
});
|
|
483
|
-
});
|
|
484
|
-
});
|
|
485
|
-
};
|
|
486
|
-
return [4 /*yield*/, ldb.getStructure(tableName)];
|
|
487
|
-
case 1:
|
|
488
|
-
tableDef = _a.sent();
|
|
489
|
-
jsonbFields = this.getNotSupportedFields(tableDef);
|
|
490
|
-
promisesArray = [];
|
|
491
|
-
if (tableDef.foreignKeys) {
|
|
492
|
-
tableDef.foreignKeys.forEach(function (fk) {
|
|
493
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
494
|
-
return __generator(this, function (_a) {
|
|
495
|
-
promisesArray.push(createPromiseForFK(fk));
|
|
496
|
-
return [2 /*return*/];
|
|
497
|
-
});
|
|
498
|
-
});
|
|
499
|
-
});
|
|
500
|
-
}
|
|
501
|
-
if (tableDef.softForeignKeys) {
|
|
502
|
-
tableDef.softForeignKeys.forEach(function (fk) {
|
|
503
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
504
|
-
return __generator(this, function (_a) {
|
|
505
|
-
promisesArray.push(createPromiseForFK(fk));
|
|
506
|
-
return [2 /*return*/];
|
|
507
|
-
});
|
|
508
|
-
});
|
|
509
|
-
});
|
|
510
|
-
}
|
|
511
|
-
return [4 /*yield*/, Promise.all(promisesArray).then(function () {
|
|
512
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
513
|
-
var fieldNames, fieldValues, unquotedFieldValues;
|
|
514
|
-
return __generator(this, function (_a) {
|
|
515
|
-
switch (_a.label) {
|
|
516
|
-
case 0:
|
|
517
|
-
fieldNames = [];
|
|
518
|
-
fieldValues = [];
|
|
519
|
-
unquotedFieldValues = [];
|
|
520
|
-
likeAr(element).forEach(function (value, key) {
|
|
521
|
-
fieldNames.push(SqlTools.quoteIdent(key));
|
|
522
|
-
fieldValues.push(SqlTools.quoteLiteral(ldb.fieldIsNotSupported(key, jsonbFields) ? JSON.stringify(value) : value));
|
|
523
|
-
unquotedFieldValues.push(value);
|
|
524
|
-
});
|
|
525
|
-
return [4 /*yield*/, ldb.executeQuery("INSERT OR REPLACE INTO " + SqlTools.quoteIdent(tableName) +
|
|
526
|
-
" (" + fieldNames.join(',') + ") values (" + fieldValues.join(',') + ");", [])];
|
|
527
|
-
case 1:
|
|
528
|
-
_a.sent();
|
|
529
|
-
return [2 /*return*/, element];
|
|
530
|
-
}
|
|
531
|
-
});
|
|
532
|
-
});
|
|
533
|
-
})];
|
|
534
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
535
|
-
}
|
|
536
|
-
});
|
|
537
|
-
});
|
|
538
|
-
};
|
|
539
|
-
WebsqlDb.prototype.putMany = function (tableName, elements) {
|
|
540
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
541
|
-
var sql, ldb, data, tableDef, jsonbFields;
|
|
542
|
-
return __generator(this, function (_a) {
|
|
543
|
-
switch (_a.label) {
|
|
544
|
-
case 0:
|
|
545
|
-
ldb = this;
|
|
546
|
-
data = [];
|
|
547
|
-
return [4 /*yield*/, this.getStructure(tableName)];
|
|
548
|
-
case 1:
|
|
549
|
-
tableDef = _a.sent();
|
|
550
|
-
jsonbFields = this.getNotSupportedFields(tableDef);
|
|
551
|
-
elements.forEach(function (element) {
|
|
552
|
-
if (!sql) {
|
|
553
|
-
var fieldNames = [];
|
|
554
|
-
}
|
|
555
|
-
var values = [];
|
|
556
|
-
likeAr(element).forEach(function (value, key) {
|
|
557
|
-
if (!sql) {
|
|
558
|
-
fieldNames.push(SqlTools.quoteIdent(key));
|
|
559
|
-
}
|
|
560
|
-
values.push(ldb.fieldIsNotSupported(key, jsonbFields) ? JSON.stringify(value) : value);
|
|
561
|
-
});
|
|
562
|
-
if (!sql) {
|
|
563
|
-
sql = "INSERT OR REPLACE INTO " + SqlTools.quoteIdent(tableName) +
|
|
564
|
-
" (" + fieldNames.join(',') + ") values (" + values.map(function (_) { return '?'; }).join(',') + ");";
|
|
565
|
-
}
|
|
566
|
-
data.push(values);
|
|
567
|
-
});
|
|
568
|
-
return [4 /*yield*/, this.executeQueries(sql, data)];
|
|
569
|
-
case 2:
|
|
570
|
-
_a.sent();
|
|
571
|
-
return [2 /*return*/];
|
|
572
|
-
}
|
|
573
|
-
});
|
|
574
|
-
});
|
|
575
|
-
};
|
|
576
|
-
WebsqlDb.prototype.close = function () {
|
|
577
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
578
|
-
return __generator(this, function (_a) {
|
|
579
|
-
//TODO
|
|
580
|
-
return [2 /*return*/, Promise.resolve()];
|
|
581
|
-
});
|
|
582
|
-
});
|
|
583
|
-
};
|
|
584
|
-
WebsqlDb.prototype.clear = function (tableName) {
|
|
585
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
586
|
-
return __generator(this, function (_a) {
|
|
587
|
-
switch (_a.label) {
|
|
588
|
-
case 0: return [4 /*yield*/, this.executeQuery("DELETE FROM " + SqlTools.quoteIdent(tableName) + ";", [])];
|
|
589
|
-
case 1:
|
|
590
|
-
_a.sent();
|
|
591
|
-
return [2 /*return*/];
|
|
592
|
-
}
|
|
593
|
-
});
|
|
594
|
-
});
|
|
595
|
-
};
|
|
596
|
-
return WebsqlDb;
|
|
597
|
-
}());
|
|
598
|
-
exports.WebsqlDb = WebsqlDb;
|
|
599
|
-
});
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
12
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
+
function step(op) {
|
|
15
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
17
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
+
switch (op[0]) {
|
|
20
|
+
case 0: case 1: t = op; break;
|
|
21
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
+
default:
|
|
25
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
+
if (t[2]) _.ops.pop();
|
|
30
|
+
_.trys.pop(); continue;
|
|
31
|
+
}
|
|
32
|
+
op = body.call(thisArg, _);
|
|
33
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
(function (factory) {
|
|
38
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
39
|
+
var v = factory(require, exports);
|
|
40
|
+
if (v !== undefined) module.exports = v;
|
|
41
|
+
}
|
|
42
|
+
else if (typeof define === "function" && define.amd) {
|
|
43
|
+
define(["require", "exports", "like-ar", "best-globals", "sql-tools"], factory);
|
|
44
|
+
}
|
|
45
|
+
})(function (require, exports) {
|
|
46
|
+
//import { TableDefinition } from "backend-plus";
|
|
47
|
+
"use strict";
|
|
48
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
49
|
+
exports.WebsqlDb = exports.detectedFeatures = void 0;
|
|
50
|
+
//x// <amd-dependency path="../../../node_modules/like-ar/like-ar.d.ts" name="likeAr"/>
|
|
51
|
+
//x// <reference path="../../node_modules/like-ar/like-ar.d.ts" />
|
|
52
|
+
/// <reference path="../node_modules/types.d.ts/modules/myOwn/in-myOwn.d.ts" />
|
|
53
|
+
/// <reference path="../../../lib/in-backend-plus.d.ts" />
|
|
54
|
+
/// <reference path="../../../lib/backend-plus.d.ts" />
|
|
55
|
+
/// <reference path="../../lib/in-backend-plus.d.ts" />
|
|
56
|
+
/// <reference path="../../lib/backend-plus.d.ts" />
|
|
57
|
+
/// <reference path="../lib/in-backend-plus.d.ts" />
|
|
58
|
+
/// <reference path="../lib/backend-plus.d.ts" />
|
|
59
|
+
/// <reference path="lib/in-backend-plus.d.ts" />
|
|
60
|
+
/// <reference path="lib/backend-plus.d.ts" />
|
|
61
|
+
var likeAr = require("like-ar");
|
|
62
|
+
var best_globals_1 = require("best-globals");
|
|
63
|
+
var SqlTools = require("sql-tools");
|
|
64
|
+
exports.detectedFeatures = {
|
|
65
|
+
needToCopyResults: null
|
|
66
|
+
};
|
|
67
|
+
var WebsqlDb = /** @class */ (function () {
|
|
68
|
+
function WebsqlDb(name) {
|
|
69
|
+
this.name = name;
|
|
70
|
+
var version = 1.0;
|
|
71
|
+
var dbName = this.name;
|
|
72
|
+
var dbDisplayName = this.name;
|
|
73
|
+
var dbSize = 2 * 1024 * 1024;
|
|
74
|
+
try {
|
|
75
|
+
this.db = openDatabase(dbName, version, dbDisplayName, dbSize);
|
|
76
|
+
this.wait4detectedFeatures = this.detectFeatures();
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
console.log(err.message);
|
|
80
|
+
throw err;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
WebsqlDb.prototype.detectFeatures = function () {
|
|
84
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
85
|
+
var detectedFeatures, detectElement, tableDef, err_1;
|
|
86
|
+
return __generator(this, function (_a) {
|
|
87
|
+
switch (_a.label) {
|
|
88
|
+
case 0:
|
|
89
|
+
detectedFeatures = { needToCopyResults: false };
|
|
90
|
+
detectElement = { feature: 'need_to_copy_results', value: false };
|
|
91
|
+
_a.label = 1;
|
|
92
|
+
case 1:
|
|
93
|
+
_a.trys.push([1, 5, , 7]);
|
|
94
|
+
tableDef = {
|
|
95
|
+
name: '_detect',
|
|
96
|
+
fields: [
|
|
97
|
+
{ name: 'feature', typeName: 'text' },
|
|
98
|
+
{ name: 'value', typeName: 'boolean' }
|
|
99
|
+
],
|
|
100
|
+
primaryKey: ['feature']
|
|
101
|
+
};
|
|
102
|
+
return [4 /*yield*/, this.registerStructure(tableDef)];
|
|
103
|
+
case 2:
|
|
104
|
+
_a.sent();
|
|
105
|
+
return [4 /*yield*/, this.putOne('_detect', detectElement)];
|
|
106
|
+
case 3:
|
|
107
|
+
_a.sent();
|
|
108
|
+
return [4 /*yield*/, this.getOne('_detect', ['need_to_copy_results'], true)];
|
|
109
|
+
case 4:
|
|
110
|
+
_a.sent();
|
|
111
|
+
return [3 /*break*/, 7];
|
|
112
|
+
case 5:
|
|
113
|
+
err_1 = _a.sent();
|
|
114
|
+
detectElement.value = true;
|
|
115
|
+
detectedFeatures.needToCopyResults = true;
|
|
116
|
+
return [4 /*yield*/, this.putOne('_detect', detectElement)];
|
|
117
|
+
case 6:
|
|
118
|
+
_a.sent();
|
|
119
|
+
return [3 /*break*/, 7];
|
|
120
|
+
case 7: return [2 /*return*/, detectedFeatures];
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
};
|
|
125
|
+
WebsqlDb.prototype.generateSqlForTableDef = function (tableDef) {
|
|
126
|
+
var typeDb = {
|
|
127
|
+
integer: 'integer',
|
|
128
|
+
number: 'real',
|
|
129
|
+
date: 'text',
|
|
130
|
+
boolean: 'text',
|
|
131
|
+
text: 'text',
|
|
132
|
+
jsonb: 'text',
|
|
133
|
+
json: 'text'
|
|
134
|
+
};
|
|
135
|
+
var lines = [];
|
|
136
|
+
var consLines = [];
|
|
137
|
+
lines.push('CREATE TABLE IF NOT EXISTS ' + SqlTools.quoteIdent(tableDef.name) + ' (');
|
|
138
|
+
var fields = [];
|
|
139
|
+
tableDef.fields.forEach(function (fieldDef) {
|
|
140
|
+
var fieldType = typeDb[fieldDef.typeName] || '"' + fieldDef.typeName + '"';
|
|
141
|
+
if (fieldDef.sizeByte == 4) {
|
|
142
|
+
fieldType = 'integer';
|
|
143
|
+
}
|
|
144
|
+
fields.push(' ' + SqlTools.quoteIdent(fieldDef.name) +
|
|
145
|
+
' ' + fieldType +
|
|
146
|
+
(fieldDef.defaultValue != null ? ' default ' + SqlTools.quoteLiteral(fieldDef.defaultValue) : '') +
|
|
147
|
+
(fieldDef.defaultDbValue != null ? ' default ' + fieldDef.defaultDbValue : ''));
|
|
148
|
+
if (fieldDef.typeName === 'text' && !fieldDef.allowEmptyText) {
|
|
149
|
+
consLines.push('alter table ' + SqlTools.quoteIdent(tableDef.name) +
|
|
150
|
+
' add constraint ' + SqlTools.quoteIdent(fieldDef.name + "<>''") +
|
|
151
|
+
' check (' + SqlTools.quoteIdent(fieldDef.name) + "<>'');");
|
|
152
|
+
}
|
|
153
|
+
if (fieldDef.nullable === false) {
|
|
154
|
+
consLines.push('alter table ' + SqlTools.quoteIdent(tableDef.name) +
|
|
155
|
+
' alter column ' + SqlTools.quoteIdent(fieldDef.name) + ' set not null;');
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
lines.push(fields.join(', \n'));
|
|
159
|
+
if (tableDef.primaryKey) {
|
|
160
|
+
lines.push(', primary key (' + tableDef.primaryKey.map(function (name) { return SqlTools.quoteIdent(name); }).join(', ') + ')');
|
|
161
|
+
}
|
|
162
|
+
lines.push(');');
|
|
163
|
+
return lines.join('\n'); //+'\n-- conss\n' + consLines.join('\n')
|
|
164
|
+
};
|
|
165
|
+
WebsqlDb.deleteDatabase = function (name) {
|
|
166
|
+
//TODO
|
|
167
|
+
return Promise.resolve();
|
|
168
|
+
};
|
|
169
|
+
WebsqlDb.prototype.executeQuery = function (sql, params) {
|
|
170
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
171
|
+
var db;
|
|
172
|
+
return __generator(this, function (_a) {
|
|
173
|
+
db = this.db;
|
|
174
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
175
|
+
if (!db)
|
|
176
|
+
return reject('no database.');
|
|
177
|
+
var result;
|
|
178
|
+
db.transaction(function (tx) {
|
|
179
|
+
tx.executeSql(sql, params || [], function (_tx, res) {
|
|
180
|
+
result = res.rows;
|
|
181
|
+
});
|
|
182
|
+
}, function (err) {
|
|
183
|
+
reject(err);
|
|
184
|
+
}, function success() {
|
|
185
|
+
resolve(result);
|
|
186
|
+
});
|
|
187
|
+
})];
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
};
|
|
191
|
+
;
|
|
192
|
+
WebsqlDb.prototype.executeQueries = function (sql, data) {
|
|
193
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
194
|
+
var db;
|
|
195
|
+
return __generator(this, function (_a) {
|
|
196
|
+
db = this.db;
|
|
197
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
198
|
+
if (!db)
|
|
199
|
+
return reject('no database.');
|
|
200
|
+
db.transaction(function (tx) {
|
|
201
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
202
|
+
return __generator(this, function (_a) {
|
|
203
|
+
data.forEach(function (data) {
|
|
204
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
205
|
+
return __generator(this, function (_a) {
|
|
206
|
+
tx.executeSql(sql, data);
|
|
207
|
+
return [2 /*return*/];
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
return [2 /*return*/];
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
}, function (tx, err) {
|
|
215
|
+
reject(err);
|
|
216
|
+
}, function (tx, res) {
|
|
217
|
+
resolve();
|
|
218
|
+
});
|
|
219
|
+
})];
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
};
|
|
223
|
+
;
|
|
224
|
+
WebsqlDb.prototype.registerStructure = function (tableDef) {
|
|
225
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
226
|
+
return __generator(this, function (_a) {
|
|
227
|
+
switch (_a.label) {
|
|
228
|
+
case 0: return [4 /*yield*/, this.executeQuery("CREATE TABLE IF NOT EXISTS _structures (name string primary key, def string not null);", [])];
|
|
229
|
+
case 1:
|
|
230
|
+
_a.sent();
|
|
231
|
+
return [4 /*yield*/, this.executeQuery("INSERT OR REPLACE INTO _structures (name, def) values (?, ?);", [tableDef.name, JSON.stringify(tableDef)])];
|
|
232
|
+
case 2:
|
|
233
|
+
_a.sent();
|
|
234
|
+
return [4 /*yield*/, this.executeQuery("DROP TABLE IF EXISTS " + SqlTools.quoteIdent(tableDef.name), [])];
|
|
235
|
+
case 3:
|
|
236
|
+
_a.sent();
|
|
237
|
+
return [4 /*yield*/, this.executeQuery(this.generateSqlForTableDef(tableDef), [])];
|
|
238
|
+
case 4:
|
|
239
|
+
_a.sent();
|
|
240
|
+
return [2 /*return*/];
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
};
|
|
245
|
+
WebsqlDb.prototype.getStructure = function (tableName) {
|
|
246
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
247
|
+
var result;
|
|
248
|
+
return __generator(this, function (_a) {
|
|
249
|
+
switch (_a.label) {
|
|
250
|
+
case 0: return [4 /*yield*/, this.executeQuery("SELECT * from _structures where name = ?", [tableName])];
|
|
251
|
+
case 1:
|
|
252
|
+
result = _a.sent();
|
|
253
|
+
if (result.length) {
|
|
254
|
+
return [2 /*return*/, JSON.parse(result.item(0).def)];
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
return [2 /*return*/, undefined];
|
|
258
|
+
}
|
|
259
|
+
return [2 /*return*/];
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
};
|
|
264
|
+
WebsqlDb.prototype.existsStructure = function (tableName) {
|
|
265
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
266
|
+
var tableDef;
|
|
267
|
+
return __generator(this, function (_a) {
|
|
268
|
+
switch (_a.label) {
|
|
269
|
+
case 0: return [4 /*yield*/, this.getStructure(tableName)];
|
|
270
|
+
case 1:
|
|
271
|
+
tableDef = _a.sent();
|
|
272
|
+
return [2 /*return*/, tableDef ? true : false];
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
};
|
|
277
|
+
WebsqlDb.prototype.convertSQLResultSetRowListToArray = function (rowResultSetList, copyResults) {
|
|
278
|
+
var arr = [];
|
|
279
|
+
for (var i = 0; i < rowResultSetList.length; i++) {
|
|
280
|
+
var result = rowResultSetList.item(i);
|
|
281
|
+
if (copyResults) {
|
|
282
|
+
result = (0, best_globals_1.changing)(result, {});
|
|
283
|
+
}
|
|
284
|
+
arr.push(result);
|
|
285
|
+
}
|
|
286
|
+
return arr;
|
|
287
|
+
};
|
|
288
|
+
WebsqlDb.prototype.fieldIsNotSupported = function (fieldName, notSupportedFields) {
|
|
289
|
+
var isNotSupported = notSupportedFields.find(function (field) {
|
|
290
|
+
return field.name == fieldName;
|
|
291
|
+
});
|
|
292
|
+
return isNotSupported ? true : false;
|
|
293
|
+
};
|
|
294
|
+
WebsqlDb.prototype.getNotSupportedFields = function (tableDef) {
|
|
295
|
+
var jsonbFields = tableDef.fields.filter(function (field) {
|
|
296
|
+
return field.typeName == 'jsonb' || field.typeName == 'date' || field.typeName == 'timestamp' || field.typeName == 'boolean';
|
|
297
|
+
});
|
|
298
|
+
return jsonbFields;
|
|
299
|
+
};
|
|
300
|
+
WebsqlDb.prototype.convertNotSupportedFields = function (records, notSupportedFields) {
|
|
301
|
+
records.forEach(function (row) {
|
|
302
|
+
if (notSupportedFields) {
|
|
303
|
+
notSupportedFields.forEach(function (notSupportedField) {
|
|
304
|
+
var fieldName = notSupportedField.name;
|
|
305
|
+
row[fieldName] = JSON.parse(row[fieldName]);
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
return records;
|
|
310
|
+
};
|
|
311
|
+
WebsqlDb.prototype.getOneIfExists = function (tableName, key) {
|
|
312
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
313
|
+
var err_2;
|
|
314
|
+
return __generator(this, function (_a) {
|
|
315
|
+
switch (_a.label) {
|
|
316
|
+
case 0:
|
|
317
|
+
_a.trys.push([0, 2, , 3]);
|
|
318
|
+
return [4 /*yield*/, this.getOne(tableName, key)];
|
|
319
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
320
|
+
case 2:
|
|
321
|
+
err_2 = _a.sent();
|
|
322
|
+
return [2 /*return*/, undefined];
|
|
323
|
+
case 3: return [2 /*return*/];
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
};
|
|
328
|
+
WebsqlDb.prototype.getOne = function (tableName, key, ignoreDetectFeatures) {
|
|
329
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
330
|
+
var tableDef, jsonbFields, fieldNames, whereExpr, sql, result, detectedFeatures, copyResults, convertedResult;
|
|
331
|
+
return __generator(this, function (_a) {
|
|
332
|
+
switch (_a.label) {
|
|
333
|
+
case 0:
|
|
334
|
+
ignoreDetectFeatures = ignoreDetectFeatures || false;
|
|
335
|
+
return [4 /*yield*/, this.getStructure(tableName)];
|
|
336
|
+
case 1:
|
|
337
|
+
tableDef = _a.sent();
|
|
338
|
+
jsonbFields = this.getNotSupportedFields(tableDef);
|
|
339
|
+
fieldNames = tableDef.primaryKey;
|
|
340
|
+
whereExpr = [];
|
|
341
|
+
fieldNames.forEach(function (fieldName, i) {
|
|
342
|
+
whereExpr.push(fieldName + '=' + SqlTools.quoteLiteral(key[i]));
|
|
343
|
+
});
|
|
344
|
+
sql = "SELECT * \n from " + SqlTools.quoteIdent(tableName) + "\n where " + whereExpr.join(' and ');
|
|
345
|
+
return [4 /*yield*/, this.executeQuery(sql, [])];
|
|
346
|
+
case 2:
|
|
347
|
+
result = _a.sent();
|
|
348
|
+
if (!!ignoreDetectFeatures) return [3 /*break*/, 4];
|
|
349
|
+
return [4 /*yield*/, this.wait4detectedFeatures];
|
|
350
|
+
case 3:
|
|
351
|
+
detectedFeatures = _a.sent();
|
|
352
|
+
_a.label = 4;
|
|
353
|
+
case 4:
|
|
354
|
+
copyResults = !ignoreDetectFeatures && detectedFeatures.needToCopyResults;
|
|
355
|
+
convertedResult = this.convertNotSupportedFields(this.convertSQLResultSetRowListToArray(result, copyResults), jsonbFields);
|
|
356
|
+
if (convertedResult[0]) {
|
|
357
|
+
return [2 /*return*/, convertedResult[0]];
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
throw Error('no existe el elemento');
|
|
361
|
+
}
|
|
362
|
+
return [2 /*return*/];
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
});
|
|
366
|
+
};
|
|
367
|
+
WebsqlDb.prototype.getChild = function (tableName, parentKey) {
|
|
368
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
369
|
+
var tableDef, jsonbFields, fieldNames, whereExpr, sql, result, detectedFeatures;
|
|
370
|
+
return __generator(this, function (_a) {
|
|
371
|
+
switch (_a.label) {
|
|
372
|
+
case 0: return [4 /*yield*/, this.getStructure(tableName)];
|
|
373
|
+
case 1:
|
|
374
|
+
tableDef = _a.sent();
|
|
375
|
+
jsonbFields = this.getNotSupportedFields(tableDef);
|
|
376
|
+
fieldNames = tableDef.primaryKey;
|
|
377
|
+
whereExpr = [];
|
|
378
|
+
parentKey.forEach(function (key, i) {
|
|
379
|
+
whereExpr.push(fieldNames[i] + '=' + SqlTools.quoteLiteral(key));
|
|
380
|
+
});
|
|
381
|
+
sql = "SELECT * from " + SqlTools.quoteIdent(tableName);
|
|
382
|
+
if (parentKey.length) {
|
|
383
|
+
sql += " where " + whereExpr.join(' and ');
|
|
384
|
+
}
|
|
385
|
+
return [4 /*yield*/, this.executeQuery(sql, [])];
|
|
386
|
+
case 2:
|
|
387
|
+
result = _a.sent();
|
|
388
|
+
return [4 /*yield*/, this.wait4detectedFeatures];
|
|
389
|
+
case 3:
|
|
390
|
+
detectedFeatures = _a.sent();
|
|
391
|
+
return [2 /*return*/, this.convertNotSupportedFields(this.convertSQLResultSetRowListToArray(result, detectedFeatures.needToCopyResults), jsonbFields)];
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
};
|
|
396
|
+
WebsqlDb.prototype.getAll = function (tableName) {
|
|
397
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
398
|
+
var tableDef, jsonbFields, results, detectedFeatures;
|
|
399
|
+
return __generator(this, function (_a) {
|
|
400
|
+
switch (_a.label) {
|
|
401
|
+
case 0: return [4 /*yield*/, this.getStructure(tableName)];
|
|
402
|
+
case 1:
|
|
403
|
+
tableDef = _a.sent();
|
|
404
|
+
jsonbFields = this.getNotSupportedFields(tableDef);
|
|
405
|
+
return [4 /*yield*/, this.executeQuery("SELECT * from " + SqlTools.quoteIdent(tableName), [])];
|
|
406
|
+
case 2:
|
|
407
|
+
results = _a.sent();
|
|
408
|
+
return [4 /*yield*/, this.wait4detectedFeatures];
|
|
409
|
+
case 3:
|
|
410
|
+
detectedFeatures = _a.sent();
|
|
411
|
+
return [2 /*return*/, this.convertNotSupportedFields(this.convertSQLResultSetRowListToArray(results, detectedFeatures.needToCopyResults), jsonbFields)];
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
});
|
|
415
|
+
};
|
|
416
|
+
WebsqlDb.prototype.getAllStructures = function () {
|
|
417
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
418
|
+
var results, structures, i;
|
|
419
|
+
return __generator(this, function (_a) {
|
|
420
|
+
switch (_a.label) {
|
|
421
|
+
case 0: return [4 /*yield*/, this.executeQuery("SELECT * from _structures where name <> '_detect'", [])];
|
|
422
|
+
case 1:
|
|
423
|
+
results = _a.sent();
|
|
424
|
+
structures = [];
|
|
425
|
+
for (i = 0; i < results.length; i++) {
|
|
426
|
+
structures.push(JSON.parse(results.item(i).def));
|
|
427
|
+
}
|
|
428
|
+
return [2 /*return*/, structures];
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
});
|
|
432
|
+
};
|
|
433
|
+
WebsqlDb.prototype.isEmpty = function (tableName) {
|
|
434
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
435
|
+
var result;
|
|
436
|
+
return __generator(this, function (_a) {
|
|
437
|
+
switch (_a.label) {
|
|
438
|
+
case 0: return [4 /*yield*/, this.executeQuery("SELECT count(*) as cantidad from " + SqlTools.quoteIdent(tableName), [])];
|
|
439
|
+
case 1:
|
|
440
|
+
result = _a.sent();
|
|
441
|
+
return [2 /*return*/, result.item(0).cantidad == 0];
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
});
|
|
445
|
+
};
|
|
446
|
+
WebsqlDb.prototype.putOne = function (tableName, element) {
|
|
447
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
448
|
+
var ldb, createPromiseForFK, tableDef, jsonbFields, promisesArray;
|
|
449
|
+
return __generator(this, function (_a) {
|
|
450
|
+
switch (_a.label) {
|
|
451
|
+
case 0:
|
|
452
|
+
ldb = this;
|
|
453
|
+
createPromiseForFK = function createPromiseForFK(fk) {
|
|
454
|
+
return Promise.resolve().then(function () {
|
|
455
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
456
|
+
var fkTableDef, pk, isFKCompleteInSource, fkRecord;
|
|
457
|
+
return __generator(this, function (_a) {
|
|
458
|
+
switch (_a.label) {
|
|
459
|
+
case 0: return [4 /*yield*/, ldb.getStructure(fk.references)];
|
|
460
|
+
case 1:
|
|
461
|
+
fkTableDef = _a.sent();
|
|
462
|
+
if (!fkTableDef) return [3 /*break*/, 3];
|
|
463
|
+
pk = [];
|
|
464
|
+
fk.fields.forEach(function (field) {
|
|
465
|
+
pk.push(element[field.source]);
|
|
466
|
+
});
|
|
467
|
+
isFKCompleteInSource = fk.fields.filter(function (field) {
|
|
468
|
+
return element[field.source] == null;
|
|
469
|
+
}).length == 0;
|
|
470
|
+
if (!isFKCompleteInSource) return [3 /*break*/, 3];
|
|
471
|
+
return [4 /*yield*/, ldb.getOneIfExists(fk.references, pk)];
|
|
472
|
+
case 2:
|
|
473
|
+
fkRecord = _a.sent();
|
|
474
|
+
if (fkRecord) {
|
|
475
|
+
fk.displayFields.forEach(function (field) {
|
|
476
|
+
element[fk.alias + '__' + field] = fkRecord[field];
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
_a.label = 3;
|
|
480
|
+
case 3: return [2 /*return*/];
|
|
481
|
+
}
|
|
482
|
+
});
|
|
483
|
+
});
|
|
484
|
+
});
|
|
485
|
+
};
|
|
486
|
+
return [4 /*yield*/, ldb.getStructure(tableName)];
|
|
487
|
+
case 1:
|
|
488
|
+
tableDef = _a.sent();
|
|
489
|
+
jsonbFields = this.getNotSupportedFields(tableDef);
|
|
490
|
+
promisesArray = [];
|
|
491
|
+
if (tableDef.foreignKeys) {
|
|
492
|
+
tableDef.foreignKeys.forEach(function (fk) {
|
|
493
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
494
|
+
return __generator(this, function (_a) {
|
|
495
|
+
promisesArray.push(createPromiseForFK(fk));
|
|
496
|
+
return [2 /*return*/];
|
|
497
|
+
});
|
|
498
|
+
});
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
if (tableDef.softForeignKeys) {
|
|
502
|
+
tableDef.softForeignKeys.forEach(function (fk) {
|
|
503
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
504
|
+
return __generator(this, function (_a) {
|
|
505
|
+
promisesArray.push(createPromiseForFK(fk));
|
|
506
|
+
return [2 /*return*/];
|
|
507
|
+
});
|
|
508
|
+
});
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
return [4 /*yield*/, Promise.all(promisesArray).then(function () {
|
|
512
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
513
|
+
var fieldNames, fieldValues, unquotedFieldValues;
|
|
514
|
+
return __generator(this, function (_a) {
|
|
515
|
+
switch (_a.label) {
|
|
516
|
+
case 0:
|
|
517
|
+
fieldNames = [];
|
|
518
|
+
fieldValues = [];
|
|
519
|
+
unquotedFieldValues = [];
|
|
520
|
+
likeAr(element).forEach(function (value, key) {
|
|
521
|
+
fieldNames.push(SqlTools.quoteIdent(key));
|
|
522
|
+
fieldValues.push(SqlTools.quoteLiteral(ldb.fieldIsNotSupported(key, jsonbFields) ? JSON.stringify(value) : value));
|
|
523
|
+
unquotedFieldValues.push(value);
|
|
524
|
+
});
|
|
525
|
+
return [4 /*yield*/, ldb.executeQuery("INSERT OR REPLACE INTO " + SqlTools.quoteIdent(tableName) +
|
|
526
|
+
" (" + fieldNames.join(',') + ") values (" + fieldValues.join(',') + ");", [])];
|
|
527
|
+
case 1:
|
|
528
|
+
_a.sent();
|
|
529
|
+
return [2 /*return*/, element];
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
});
|
|
533
|
+
})];
|
|
534
|
+
case 2: return [2 /*return*/, _a.sent()];
|
|
535
|
+
}
|
|
536
|
+
});
|
|
537
|
+
});
|
|
538
|
+
};
|
|
539
|
+
WebsqlDb.prototype.putMany = function (tableName, elements) {
|
|
540
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
541
|
+
var sql, ldb, data, tableDef, jsonbFields;
|
|
542
|
+
return __generator(this, function (_a) {
|
|
543
|
+
switch (_a.label) {
|
|
544
|
+
case 0:
|
|
545
|
+
ldb = this;
|
|
546
|
+
data = [];
|
|
547
|
+
return [4 /*yield*/, this.getStructure(tableName)];
|
|
548
|
+
case 1:
|
|
549
|
+
tableDef = _a.sent();
|
|
550
|
+
jsonbFields = this.getNotSupportedFields(tableDef);
|
|
551
|
+
elements.forEach(function (element) {
|
|
552
|
+
if (!sql) {
|
|
553
|
+
var fieldNames = [];
|
|
554
|
+
}
|
|
555
|
+
var values = [];
|
|
556
|
+
likeAr(element).forEach(function (value, key) {
|
|
557
|
+
if (!sql) {
|
|
558
|
+
fieldNames.push(SqlTools.quoteIdent(key));
|
|
559
|
+
}
|
|
560
|
+
values.push(ldb.fieldIsNotSupported(key, jsonbFields) ? JSON.stringify(value) : value);
|
|
561
|
+
});
|
|
562
|
+
if (!sql) {
|
|
563
|
+
sql = "INSERT OR REPLACE INTO " + SqlTools.quoteIdent(tableName) +
|
|
564
|
+
" (" + fieldNames.join(',') + ") values (" + values.map(function (_) { return '?'; }).join(',') + ");";
|
|
565
|
+
}
|
|
566
|
+
data.push(values);
|
|
567
|
+
});
|
|
568
|
+
return [4 /*yield*/, this.executeQueries(sql, data)];
|
|
569
|
+
case 2:
|
|
570
|
+
_a.sent();
|
|
571
|
+
return [2 /*return*/];
|
|
572
|
+
}
|
|
573
|
+
});
|
|
574
|
+
});
|
|
575
|
+
};
|
|
576
|
+
WebsqlDb.prototype.close = function () {
|
|
577
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
578
|
+
return __generator(this, function (_a) {
|
|
579
|
+
//TODO
|
|
580
|
+
return [2 /*return*/, Promise.resolve()];
|
|
581
|
+
});
|
|
582
|
+
});
|
|
583
|
+
};
|
|
584
|
+
WebsqlDb.prototype.clear = function (tableName) {
|
|
585
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
586
|
+
return __generator(this, function (_a) {
|
|
587
|
+
switch (_a.label) {
|
|
588
|
+
case 0: return [4 /*yield*/, this.executeQuery("DELETE FROM " + SqlTools.quoteIdent(tableName) + ";", [])];
|
|
589
|
+
case 1:
|
|
590
|
+
_a.sent();
|
|
591
|
+
return [2 /*return*/];
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
});
|
|
595
|
+
};
|
|
596
|
+
return WebsqlDb;
|
|
597
|
+
}());
|
|
598
|
+
exports.WebsqlDb = WebsqlDb;
|
|
599
|
+
});
|
|
600
600
|
//# sourceMappingURL=my-websqldb.js.map
|