db-wrapper 0.0.8 → 0.0.9
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 +201 -201
- package/README.md +24 -24
- package/bundles/db-wrapper.umd.js +510 -508
- package/bundles/db-wrapper.umd.js.map +1 -1
- package/bundles/db-wrapper.umd.min.js +1 -1
- package/bundles/db-wrapper.umd.min.js.map +1 -1
- package/db-wrapper.d.ts +4 -4
- package/db-wrapper.metadata.json +1 -1
- package/esm2015/db-wrapper.js +4 -4
- package/esm2015/lib/db-wrapper.models.js +45 -45
- package/esm2015/lib/db-wrapper.module.js +21 -21
- package/esm2015/lib/db-wrapper.service.js +410 -408
- package/esm2015/lib/window.service.js +16 -16
- package/esm2015/public-api.js +7 -7
- package/esm5/db-wrapper.js +4 -4
- package/esm5/lib/db-wrapper.models.js +45 -45
- package/esm5/lib/db-wrapper.module.js +25 -25
- package/esm5/lib/db-wrapper.service.js +436 -434
- package/esm5/lib/window.service.js +23 -23
- package/esm5/public-api.js +7 -7
- package/fesm2015/db-wrapper.js +478 -476
- package/fesm2015/db-wrapper.js.map +1 -1
- package/fesm5/db-wrapper.js +514 -512
- package/fesm5/db-wrapper.js.map +1 -1
- package/lib/db-wrapper.models.d.ts +123 -119
- package/lib/db-wrapper.module.d.ts +5 -5
- package/lib/db-wrapper.service.d.ts +40 -39
- package/lib/window.service.d.ts +3 -3
- package/package.json +1 -1
- package/public-api.d.ts +3 -3
package/fesm5/db-wrapper.js
CHANGED
|
@@ -1,528 +1,530 @@
|
|
|
1
1
|
import { __decorate, __awaiter, __generator } from 'tslib';
|
|
2
2
|
import { Injectable, InjectionToken, NgModule } from '@angular/core';
|
|
3
3
|
|
|
4
|
-
var WindowService = /** @class */ (function () {
|
|
5
|
-
function WindowService() {
|
|
6
|
-
}
|
|
7
|
-
Object.defineProperty(WindowService.prototype, "nativeWindow", {
|
|
8
|
-
get: function () {
|
|
9
|
-
return _window();
|
|
10
|
-
},
|
|
11
|
-
enumerable: true,
|
|
12
|
-
configurable: true
|
|
13
|
-
});
|
|
14
|
-
WindowService = __decorate([
|
|
15
|
-
Injectable()
|
|
16
|
-
], WindowService);
|
|
17
|
-
return WindowService;
|
|
18
|
-
}());
|
|
19
|
-
function _window() {
|
|
20
|
-
// Return the global native browser window object
|
|
21
|
-
return typeof window !== 'undefined' ? window : undefined;
|
|
4
|
+
var WindowService = /** @class */ (function () {
|
|
5
|
+
function WindowService() {
|
|
6
|
+
}
|
|
7
|
+
Object.defineProperty(WindowService.prototype, "nativeWindow", {
|
|
8
|
+
get: function () {
|
|
9
|
+
return _window();
|
|
10
|
+
},
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true
|
|
13
|
+
});
|
|
14
|
+
WindowService = __decorate([
|
|
15
|
+
Injectable()
|
|
16
|
+
], WindowService);
|
|
17
|
+
return WindowService;
|
|
18
|
+
}());
|
|
19
|
+
function _window() {
|
|
20
|
+
// Return the global native browser window object
|
|
21
|
+
return typeof window !== 'undefined' ? window : undefined;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
var DbWrapperService = /** @class */ (function () {
|
|
25
|
-
function DbWrapperService(dbConfig, dbSchema) {
|
|
26
|
-
var _this = this;
|
|
27
|
-
this._dbName = dbConfig.dbName;
|
|
28
|
-
this._location = dbConfig.location || 'default';
|
|
29
|
-
this._iosDatabaseLocation = dbConfig.iosDatabaseLocation || 'Library';
|
|
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
|
-
var
|
|
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
|
-
console.
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
console.
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
})
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
})
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
})
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
})
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
var
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
24
|
+
var DbWrapperService = /** @class */ (function () {
|
|
25
|
+
function DbWrapperService(dbConfig, dbSchema) {
|
|
26
|
+
var _this = this;
|
|
27
|
+
this._dbName = dbConfig.dbName;
|
|
28
|
+
this._location = dbConfig.location || 'default';
|
|
29
|
+
this._iosDatabaseLocation = dbConfig.iosDatabaseLocation || 'Library';
|
|
30
|
+
this._androidDatabaseProvider = dbConfig.androidDatabaseProvider || 'default';
|
|
31
|
+
var window = (new WindowService()).nativeWindow;
|
|
32
|
+
if (dbSchema) {
|
|
33
|
+
this.schema = dbSchema;
|
|
34
|
+
}
|
|
35
|
+
this._readyPromise = new Promise(function (resolve, reject) {
|
|
36
|
+
if (typeof window.cordova === 'undefined') {
|
|
37
|
+
setTimeout(function () {
|
|
38
|
+
_this.init(dbSchema).then(function () {
|
|
39
|
+
resolve();
|
|
40
|
+
})
|
|
41
|
+
.catch(function (err) {
|
|
42
|
+
reject(err);
|
|
43
|
+
});
|
|
44
|
+
}, 500);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
document.addEventListener('deviceready', function () {
|
|
48
|
+
console.log('dbWrapper: device ready.');
|
|
49
|
+
_this.initDevice(dbSchema).then(function () { return resolve(); }).catch(function (err) { return reject(err); });
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
DbWrapperService.prototype.ready = function () {
|
|
55
|
+
return this._readyPromise;
|
|
56
|
+
};
|
|
57
|
+
DbWrapperService.prototype.initDevice = function (dbSchema) {
|
|
58
|
+
var _this = this;
|
|
59
|
+
var window = (new WindowService()).nativeWindow;
|
|
60
|
+
return new Promise(function (resolve, reject) {
|
|
61
|
+
_this.db = window.sqlitePlugin.openDatabase({
|
|
62
|
+
name: _this._dbName,
|
|
63
|
+
location: _this._location,
|
|
64
|
+
androidLockWorkaround: 1,
|
|
65
|
+
androidDatabaseProvider: _this._androidDatabaseProvider,
|
|
66
|
+
});
|
|
67
|
+
if (dbSchema) {
|
|
68
|
+
_this.generate(dbSchema).catch(function (err) {
|
|
69
|
+
reject(err);
|
|
70
|
+
}).then(function () { resolve(); });
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
resolve();
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
DbWrapperService.prototype.init = function (dbSchema) {
|
|
78
|
+
var _this = this;
|
|
79
|
+
var window = (new WindowService()).nativeWindow;
|
|
80
|
+
return new Promise(function (resolve, reject) {
|
|
81
|
+
_this.db = window.openDatabase(_this._dbName, '1.0', 'database', -1);
|
|
82
|
+
if (dbSchema) {
|
|
83
|
+
_this.generate(dbSchema).catch(function (err) {
|
|
84
|
+
reject(err);
|
|
85
|
+
}).then(function () {
|
|
86
|
+
resolve();
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
resolve();
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
DbWrapperService.prototype.generate = function (schema) {
|
|
95
|
+
var _this = this;
|
|
96
|
+
var sentences = [];
|
|
97
|
+
this.schema = schema;
|
|
98
|
+
if (schema.tables) {
|
|
99
|
+
schema.tables.forEach(function (table) {
|
|
100
|
+
sentences.push(_this.createTable(table));
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
if (schema.views) {
|
|
104
|
+
schema.views.forEach(function (view) {
|
|
105
|
+
sentences.push(_this.createView(view));
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
if (sentences.length === 0) {
|
|
109
|
+
throw new Error('Esquema de la base de datos vacia.');
|
|
110
|
+
}
|
|
111
|
+
return Promise.all(sentences);
|
|
112
|
+
};
|
|
113
|
+
DbWrapperService.prototype.createTable = function (table) {
|
|
114
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
115
|
+
var sql, onConflictPrimary, sentences, newColumns, tableDDL;
|
|
116
|
+
var _this = this;
|
|
117
|
+
return __generator(this, function (_a) {
|
|
118
|
+
switch (_a.label) {
|
|
119
|
+
case 0:
|
|
120
|
+
sql = '';
|
|
121
|
+
sentences = [], newColumns = [];
|
|
122
|
+
return [4 /*yield*/, this._tableExists(table.name)];
|
|
123
|
+
case 1:
|
|
124
|
+
tableDDL = _a.sent();
|
|
125
|
+
if (tableDDL) {
|
|
126
|
+
table.columns.forEach(function (col) {
|
|
127
|
+
if (!_this._columnExists(tableDDL, col.name)) {
|
|
128
|
+
newColumns.push(_this.addColumn(table.name, col));
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
table.columns.forEach(function (col) {
|
|
134
|
+
sentences.push(_this.defColumn(col));
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
onConflictPrimary = 'on conflict fail';
|
|
138
|
+
if (Array.isArray(table.primary)) {
|
|
139
|
+
sentences.push(' CONSTRAINT identificador PRIMARY KEY (' + table.primary.join(',') + ') ' + onConflictPrimary);
|
|
140
|
+
}
|
|
141
|
+
if (typeof table.primary === 'string') {
|
|
142
|
+
sentences.push(' CONSTRAINT identificador PRIMARY KEY (' + table.primary + ') ' + onConflictPrimary);
|
|
143
|
+
}
|
|
144
|
+
if (table.uniques) {
|
|
145
|
+
table.uniques.forEach(function (uniq) {
|
|
146
|
+
sentences.push(_this.defUnique(uniq));
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
if (table.foreigns) {
|
|
150
|
+
table.foreigns.forEach(function (fore) {
|
|
151
|
+
sentences.push(_this.defForeign(fore));
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
if (tableDDL && newColumns.length !== 0) {
|
|
155
|
+
return [2 /*return*/, Promise.all(newColumns)
|
|
156
|
+
.then(function () { console.log("Table " + table.name + " altered."); })
|
|
157
|
+
.catch(function (err) {
|
|
158
|
+
console.error("Table " + table.name + ": " + err.message);
|
|
159
|
+
throw new Error("Table " + table.name + ": " + err.message);
|
|
160
|
+
})];
|
|
161
|
+
}
|
|
162
|
+
else if (!tableDDL) {
|
|
163
|
+
sql = 'CREATE TABLE IF NOT EXISTS ' + table.name + ' (' + sentences.join(',') + ')';
|
|
164
|
+
return [2 /*return*/, this.query(sql)
|
|
165
|
+
.then(function () { console.log("Table " + table.name + " created."); })
|
|
166
|
+
.catch(function (err) {
|
|
167
|
+
console.error("Table " + table.name + ": " + err.message);
|
|
168
|
+
throw new Error("Table " + table.name + ": " + err.message);
|
|
169
|
+
})];
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
return [2 /*return*/, Promise.resolve().then(function () { console.log("Table " + table.name + " initialized."); })];
|
|
173
|
+
}
|
|
174
|
+
return [2 /*return*/];
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
};
|
|
179
|
+
DbWrapperService.prototype.createView = function (view) {
|
|
180
|
+
var sql;
|
|
181
|
+
sql = 'CREATE VIEW IF NOT EXISTS ' + view.name
|
|
182
|
+
+ ' AS ' + view.sql;
|
|
183
|
+
return this.query(sql);
|
|
184
|
+
};
|
|
185
|
+
DbWrapperService.prototype.getSchemaTable = function (tableName) {
|
|
186
|
+
var res;
|
|
187
|
+
this.schema.tables.forEach(function (table) {
|
|
188
|
+
if (table.name === tableName) {
|
|
189
|
+
res = table;
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
return res;
|
|
193
|
+
};
|
|
194
|
+
DbWrapperService.prototype.getUniquesTable = function (table) {
|
|
195
|
+
var res;
|
|
196
|
+
table.uniques.forEach(function (uniq) {
|
|
197
|
+
res[uniq.name] = uniq;
|
|
198
|
+
});
|
|
199
|
+
return res;
|
|
200
|
+
};
|
|
201
|
+
DbWrapperService.prototype.query = function (query, bindings) {
|
|
202
|
+
var _this = this;
|
|
203
|
+
if (bindings === void 0) { bindings = []; }
|
|
204
|
+
return new Promise(function (resolve, reject) {
|
|
205
|
+
if (query.indexOf('select') === 0) {
|
|
206
|
+
_this.db.readTransaction(function (transaction) {
|
|
207
|
+
transaction.executeSql(query, bindings, function (transaction, result) {
|
|
208
|
+
resolve(result);
|
|
209
|
+
}, function (transaction, error) {
|
|
210
|
+
reject(error);
|
|
211
|
+
});
|
|
212
|
+
}, function (err) { return reject(err); });
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
_this.db.transaction(function (transaction) {
|
|
216
|
+
transaction.executeSql(query, bindings, function (transaction, result) {
|
|
217
|
+
resolve(result);
|
|
218
|
+
}, function (transaction, error) {
|
|
219
|
+
reject(error);
|
|
220
|
+
});
|
|
221
|
+
}, function (err) { return reject(err); });
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
DbWrapperService.prototype.Count = function (query, bindings) {
|
|
226
|
+
var _this = this;
|
|
227
|
+
if (bindings === void 0) { bindings = []; }
|
|
228
|
+
return this.query(query, bindings).then(function (rows) {
|
|
229
|
+
return _this.rowsCount(rows);
|
|
230
|
+
});
|
|
231
|
+
};
|
|
232
|
+
DbWrapperService.prototype.insert = function (table, bindings) {
|
|
233
|
+
var _this = this;
|
|
234
|
+
var sql = '', sqlBinds = [], sqlKeys = [], sqlValues = [];
|
|
235
|
+
sql = sql + 'insert into ' + table + ' ';
|
|
236
|
+
for (var index in bindings) {
|
|
237
|
+
if (bindings.hasOwnProperty(index)) {
|
|
238
|
+
sqlKeys.push(index);
|
|
239
|
+
sqlValues.push(bindings[index]);
|
|
240
|
+
sqlBinds.push('?');
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
sql = sql + '(' + sqlKeys.join(',') + ') values (' + sqlBinds.join(',') + ')';
|
|
244
|
+
return new Promise(function (resolve, reject) {
|
|
245
|
+
_this.db.transaction(function (transaction) {
|
|
246
|
+
transaction.executeSql(sql, sqlValues, function (transaction, result) {
|
|
247
|
+
resolve(result);
|
|
248
|
+
}, function (transaction, error) {
|
|
249
|
+
reject(error);
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
};
|
|
254
|
+
DbWrapperService.prototype.update = function (table, bindings, where) {
|
|
255
|
+
var _this = this;
|
|
256
|
+
var sql = '', sqlBinds = [], sqlKeys = [], sqlValues = [];
|
|
257
|
+
sql = sql + 'update ' + table + ' set ';
|
|
258
|
+
for (var index in bindings) {
|
|
259
|
+
if (bindings.hasOwnProperty(index)) {
|
|
260
|
+
var newObj = bindings[index];
|
|
261
|
+
if (typeof newObj === 'object' && newObj !== null) {
|
|
262
|
+
sqlValues.push(newObj.value);
|
|
263
|
+
if (newObj.ifnull) {
|
|
264
|
+
sqlBinds.push(index + ' = ifnull( ' + index + ',0) + ?');
|
|
265
|
+
}
|
|
266
|
+
else if (newObj.operator) {
|
|
267
|
+
sqlBinds.push(index + ' = ' + index + newObj.operator + ' ?');
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
sqlBinds.push(index + ' = ?');
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
sqlBinds.push(index + ' = ?');
|
|
275
|
+
sqlValues.push(bindings[index]);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
for (var index in where) {
|
|
280
|
+
if (where.hasOwnProperty(index)) {
|
|
281
|
+
if (typeof where[index] === 'object' && where[index] !== null) {
|
|
282
|
+
sqlKeys.push(index + ' ' + where[index].operator + ' ?');
|
|
283
|
+
sqlValues.push(where[index].value);
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
sqlKeys.push(index + ' = ?');
|
|
287
|
+
sqlValues.push(where[index]);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
sql = sql + sqlBinds.join(',') + ' where ' + sqlKeys.join(' and ');
|
|
292
|
+
return new Promise(function (resolve, reject) {
|
|
293
|
+
_this.db.transaction(function (transaction) {
|
|
294
|
+
transaction.executeSql(sql, sqlValues, function (transaction, result) {
|
|
295
|
+
resolve(result);
|
|
296
|
+
}, function (transaction, error) {
|
|
297
|
+
reject(error);
|
|
298
|
+
});
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
};
|
|
302
|
+
DbWrapperService.prototype.dropTable = function (tables) {
|
|
303
|
+
var _this = this;
|
|
304
|
+
var cont = 1;
|
|
305
|
+
cont = tables.length;
|
|
306
|
+
return new Promise(function (resolve, reject) {
|
|
307
|
+
tables.some(function (table) {
|
|
308
|
+
if (cont !== 0) {
|
|
309
|
+
_this.query('DROP TABLE ' + table)
|
|
310
|
+
.then(function () {
|
|
311
|
+
if (cont === 1) {
|
|
312
|
+
resolve();
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
cont--;
|
|
316
|
+
}
|
|
317
|
+
}, function (err) {
|
|
318
|
+
cont = 0;
|
|
319
|
+
reject(err);
|
|
320
|
+
return true;
|
|
321
|
+
});
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
};
|
|
327
|
+
DbWrapperService.prototype.fetchAll = function (result) {
|
|
328
|
+
var output = [];
|
|
329
|
+
for (var i = 0; i < result.rows.length; i++) {
|
|
330
|
+
output.push(result.rows.item(i));
|
|
331
|
+
}
|
|
332
|
+
return output;
|
|
333
|
+
};
|
|
334
|
+
DbWrapperService.prototype.fetch = function (result) {
|
|
335
|
+
var objeto = undefined, newObj = undefined;
|
|
336
|
+
if (result.rows.length !== 0) {
|
|
337
|
+
objeto = result.rows.item(0);
|
|
338
|
+
newObj = {};
|
|
339
|
+
var keys = Object.keys(objeto);
|
|
340
|
+
for (var key in keys) {
|
|
341
|
+
newObj[keys[key]] = objeto[keys[key]];
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return newObj;
|
|
345
|
+
};
|
|
346
|
+
DbWrapperService.prototype.rowsCount = function (result) {
|
|
347
|
+
return result.rows.length;
|
|
348
|
+
};
|
|
349
|
+
DbWrapperService.prototype.tableExists = function (table) {
|
|
350
|
+
var _this = this;
|
|
351
|
+
return this.query('select sql from sqlite_master ' +
|
|
352
|
+
'where type=\'table\' and name=\'' + table + '\'')
|
|
353
|
+
.then(function (result) { return (_this.rowsCount(result) === 1); });
|
|
354
|
+
};
|
|
355
|
+
DbWrapperService.prototype.columnExists = function (table, column) {
|
|
356
|
+
var _this = this;
|
|
357
|
+
return this._tableExists(table)
|
|
358
|
+
.then(function (tableDDL) {
|
|
359
|
+
if (tableDDL) {
|
|
360
|
+
return _this._columnExists(tableDDL, column);
|
|
361
|
+
}
|
|
362
|
+
else {
|
|
363
|
+
return false;
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
};
|
|
367
|
+
/* Private */
|
|
368
|
+
DbWrapperService.prototype.defColumn = function (col) {
|
|
369
|
+
var colSql;
|
|
370
|
+
colSql = col.name + ' ' + col.type;
|
|
371
|
+
if (!col.isNullable) {
|
|
372
|
+
colSql += ' NOT NULL ';
|
|
373
|
+
}
|
|
374
|
+
if (col.default) {
|
|
375
|
+
colSql += ' DEFAULT ' + col.default + ' ';
|
|
376
|
+
}
|
|
377
|
+
if (col.type === 'text' && col.length) {
|
|
378
|
+
colSql += " CONSTRAINT " + col.name + "_length CHECK ( ";
|
|
379
|
+
if (col.isNullable) {
|
|
380
|
+
colSql += col.name + " is null OR ";
|
|
381
|
+
}
|
|
382
|
+
colSql += "length(" + col.name + ") <= " + col.length + " )";
|
|
383
|
+
}
|
|
384
|
+
return colSql;
|
|
385
|
+
};
|
|
386
|
+
DbWrapperService.prototype.defUnique = function (unique) {
|
|
387
|
+
var uniqSql;
|
|
388
|
+
// depende del entorno, producción o desarrollo
|
|
389
|
+
var onConflictUnique = 'on conflict replace';
|
|
390
|
+
if (Array.isArray(unique.fields)) {
|
|
391
|
+
uniqSql = " CONSTRAINT " + unique.name + " UNIQUE ( " + unique.fields.join(',') + " ) " + onConflictUnique + " ";
|
|
392
|
+
}
|
|
393
|
+
if (typeof unique.fields === 'string') {
|
|
394
|
+
uniqSql = " CONSTRAINT " + unique.name + " UNIQUE ( " + unique.fields + " ) " + onConflictUnique + " ";
|
|
395
|
+
}
|
|
396
|
+
return uniqSql;
|
|
397
|
+
};
|
|
398
|
+
DbWrapperService.prototype.defForeign = function (fore) {
|
|
399
|
+
var foreSql;
|
|
400
|
+
if (Array.isArray(fore.foreignKey)) {
|
|
401
|
+
foreSql = " FOREIGN KEY ( " + fore.foreignKey.join(',') + " ) ";
|
|
402
|
+
}
|
|
403
|
+
if (typeof fore.foreignKey === 'string') {
|
|
404
|
+
foreSql = " FOREIGN KEY ( " + fore.foreignKey + " ) ";
|
|
405
|
+
}
|
|
406
|
+
if (Array.isArray(fore.primaryKey)) {
|
|
407
|
+
foreSql = " REFERENCES " + fore.referenceTable + " ( " + fore.primaryKey.join(',') + " ) ";
|
|
408
|
+
}
|
|
409
|
+
if (typeof fore.primaryKey === 'string') {
|
|
410
|
+
foreSql = " REFERENCES " + fore.referenceTable + " ( " + fore.primaryKey + " ) ";
|
|
411
|
+
}
|
|
412
|
+
return foreSql;
|
|
413
|
+
};
|
|
414
|
+
DbWrapperService.prototype._tableExists = function (table) {
|
|
415
|
+
var _this = this;
|
|
416
|
+
return this.query('select sql from sqlite_master ' +
|
|
417
|
+
'where type=\'table\' and name=\'' + table + '\'')
|
|
418
|
+
.then(function (result) {
|
|
419
|
+
if (_this.rowsCount(result) === 1) {
|
|
420
|
+
return _this.fetch(result).sql;
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
return false;
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
};
|
|
427
|
+
DbWrapperService.prototype._columnExists = function (tableDDL, column) {
|
|
428
|
+
if (tableDDL.indexOf(column) === -1) {
|
|
429
|
+
return false;
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
return true;
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
DbWrapperService.prototype.addColumn = function (table, col) {
|
|
436
|
+
var colSql = this.defColumn(col);
|
|
437
|
+
var query = 'alter TABLE ' + table + ' add COLUMN ' + colSql;
|
|
438
|
+
return this.query(query);
|
|
439
|
+
};
|
|
440
|
+
return DbWrapperService;
|
|
441
|
+
}());
|
|
442
|
+
var DB_CONFIG = new InjectionToken('dbWrapperConfig');
|
|
443
|
+
var DB_SCHEMA = new InjectionToken('dbWrapperSchema');
|
|
444
|
+
function getDefaultDbConfig() {
|
|
445
|
+
return {
|
|
446
|
+
dbName: 'db.sqlite',
|
|
447
|
+
location: 'default',
|
|
448
|
+
iosDatabaseLocation: 'Library'
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
function provideDbWrapper(dbConfig, dbSchema) {
|
|
452
|
+
var config = !!dbConfig ? dbConfig : getDefaultDbConfig();
|
|
453
|
+
return new DbWrapperService(config, dbSchema);
|
|
452
454
|
}
|
|
453
455
|
|
|
454
|
-
var DbWrapperModule = /** @class */ (function () {
|
|
455
|
-
function DbWrapperModule() {
|
|
456
|
-
}
|
|
457
|
-
DbWrapperModule_1 = DbWrapperModule;
|
|
458
|
-
DbWrapperModule.forRoot = function (dbConfig, schema) {
|
|
459
|
-
return {
|
|
460
|
-
ngModule: DbWrapperModule_1,
|
|
461
|
-
providers: [
|
|
462
|
-
{ provide: DB_CONFIG, useValue: dbConfig },
|
|
463
|
-
{ provide: DB_SCHEMA, useValue: schema },
|
|
464
|
-
{ provide: DbWrapperService, useFactory: provideDbWrapper, deps: [DB_CONFIG, DB_SCHEMA] }
|
|
465
|
-
]
|
|
466
|
-
};
|
|
467
|
-
};
|
|
468
|
-
var DbWrapperModule_1;
|
|
469
|
-
DbWrapperModule = DbWrapperModule_1 = __decorate([
|
|
470
|
-
NgModule()
|
|
471
|
-
], DbWrapperModule);
|
|
472
|
-
return DbWrapperModule;
|
|
456
|
+
var DbWrapperModule = /** @class */ (function () {
|
|
457
|
+
function DbWrapperModule() {
|
|
458
|
+
}
|
|
459
|
+
DbWrapperModule_1 = DbWrapperModule;
|
|
460
|
+
DbWrapperModule.forRoot = function (dbConfig, schema) {
|
|
461
|
+
return {
|
|
462
|
+
ngModule: DbWrapperModule_1,
|
|
463
|
+
providers: [
|
|
464
|
+
{ provide: DB_CONFIG, useValue: dbConfig },
|
|
465
|
+
{ provide: DB_SCHEMA, useValue: schema },
|
|
466
|
+
{ provide: DbWrapperService, useFactory: provideDbWrapper, deps: [DB_CONFIG, DB_SCHEMA] }
|
|
467
|
+
]
|
|
468
|
+
};
|
|
469
|
+
};
|
|
470
|
+
var DbWrapperModule_1;
|
|
471
|
+
DbWrapperModule = DbWrapperModule_1 = __decorate([
|
|
472
|
+
NgModule()
|
|
473
|
+
], DbWrapperModule);
|
|
474
|
+
return DbWrapperModule;
|
|
473
475
|
}());
|
|
474
476
|
|
|
475
|
-
var DataType;
|
|
476
|
-
(function (DataType) {
|
|
477
|
-
DataType["Integer"] = "integer";
|
|
478
|
-
DataType["Int"] = "integer";
|
|
479
|
-
DataType["TinyInt"] = "integer";
|
|
480
|
-
DataType["SmallInt"] = "integer";
|
|
481
|
-
DataType["BigInt"] = "integer";
|
|
482
|
-
DataType["Text"] = "text";
|
|
483
|
-
DataType["Char"] = "text";
|
|
484
|
-
DataType["NChar"] = "text";
|
|
485
|
-
DataType["VarChar"] = "text";
|
|
486
|
-
DataType["NVarChar"] = "text";
|
|
487
|
-
DataType["CLOB"] = "text";
|
|
488
|
-
DataType["BLOB"] = "blob";
|
|
489
|
-
DataType["Real"] = "real";
|
|
490
|
-
DataType["Double"] = "real";
|
|
491
|
-
DataType["Decimal"] = "real";
|
|
492
|
-
DataType["Float"] = "real";
|
|
493
|
-
DataType["Boolean"] = "integer";
|
|
494
|
-
DataType["DateTime"] = "text";
|
|
495
|
-
DataType["Date"] = "text";
|
|
496
|
-
})(DataType || (DataType = {}));
|
|
497
|
-
var DefaultType;
|
|
498
|
-
(function (DefaultType) {
|
|
499
|
-
DefaultType["Integer"] = "0";
|
|
500
|
-
DefaultType["Int"] = "0";
|
|
501
|
-
DefaultType["TinyInt"] = "0";
|
|
502
|
-
DefaultType["SmallInt"] = "0";
|
|
503
|
-
DefaultType["BigInt"] = "0";
|
|
504
|
-
DefaultType["Text"] = "''";
|
|
505
|
-
DefaultType["Char"] = "''";
|
|
506
|
-
DefaultType["NChar"] = "''";
|
|
507
|
-
DefaultType["VarChar"] = "''";
|
|
508
|
-
DefaultType["NVarChar"] = "''";
|
|
509
|
-
DefaultType["CLOB"] = "''";
|
|
510
|
-
DefaultType["BLOB"] = "0";
|
|
511
|
-
DefaultType["Real"] = "0.0";
|
|
512
|
-
DefaultType["Double"] = "0.0";
|
|
513
|
-
DefaultType["Decimal"] = "0.0";
|
|
514
|
-
DefaultType["Float"] = "0.0";
|
|
515
|
-
DefaultType["Boolean"] = "0";
|
|
516
|
-
DefaultType["DateTime"] = "(datetime('now', 'localtime'))";
|
|
517
|
-
DefaultType["Date"] = "(date('now', 'localtime'))";
|
|
477
|
+
var DataType;
|
|
478
|
+
(function (DataType) {
|
|
479
|
+
DataType["Integer"] = "integer";
|
|
480
|
+
DataType["Int"] = "integer";
|
|
481
|
+
DataType["TinyInt"] = "integer";
|
|
482
|
+
DataType["SmallInt"] = "integer";
|
|
483
|
+
DataType["BigInt"] = "integer";
|
|
484
|
+
DataType["Text"] = "text";
|
|
485
|
+
DataType["Char"] = "text";
|
|
486
|
+
DataType["NChar"] = "text";
|
|
487
|
+
DataType["VarChar"] = "text";
|
|
488
|
+
DataType["NVarChar"] = "text";
|
|
489
|
+
DataType["CLOB"] = "text";
|
|
490
|
+
DataType["BLOB"] = "blob";
|
|
491
|
+
DataType["Real"] = "real";
|
|
492
|
+
DataType["Double"] = "real";
|
|
493
|
+
DataType["Decimal"] = "real";
|
|
494
|
+
DataType["Float"] = "real";
|
|
495
|
+
DataType["Boolean"] = "integer";
|
|
496
|
+
DataType["DateTime"] = "text";
|
|
497
|
+
DataType["Date"] = "text";
|
|
498
|
+
})(DataType || (DataType = {}));
|
|
499
|
+
var DefaultType;
|
|
500
|
+
(function (DefaultType) {
|
|
501
|
+
DefaultType["Integer"] = "0";
|
|
502
|
+
DefaultType["Int"] = "0";
|
|
503
|
+
DefaultType["TinyInt"] = "0";
|
|
504
|
+
DefaultType["SmallInt"] = "0";
|
|
505
|
+
DefaultType["BigInt"] = "0";
|
|
506
|
+
DefaultType["Text"] = "''";
|
|
507
|
+
DefaultType["Char"] = "''";
|
|
508
|
+
DefaultType["NChar"] = "''";
|
|
509
|
+
DefaultType["VarChar"] = "''";
|
|
510
|
+
DefaultType["NVarChar"] = "''";
|
|
511
|
+
DefaultType["CLOB"] = "''";
|
|
512
|
+
DefaultType["BLOB"] = "0";
|
|
513
|
+
DefaultType["Real"] = "0.0";
|
|
514
|
+
DefaultType["Double"] = "0.0";
|
|
515
|
+
DefaultType["Decimal"] = "0.0";
|
|
516
|
+
DefaultType["Float"] = "0.0";
|
|
517
|
+
DefaultType["Boolean"] = "0";
|
|
518
|
+
DefaultType["DateTime"] = "(datetime('now', 'localtime'))";
|
|
519
|
+
DefaultType["Date"] = "(date('now', 'localtime'))";
|
|
518
520
|
})(DefaultType || (DefaultType = {}));
|
|
519
521
|
|
|
520
|
-
/*
|
|
521
|
-
* Public API Surface of db-wrapper
|
|
522
|
+
/*
|
|
523
|
+
* Public API Surface of db-wrapper
|
|
522
524
|
*/
|
|
523
525
|
|
|
524
|
-
/**
|
|
525
|
-
* Generated bundle index. Do not edit.
|
|
526
|
+
/**
|
|
527
|
+
* Generated bundle index. Do not edit.
|
|
526
528
|
*/
|
|
527
529
|
|
|
528
530
|
export { DB_CONFIG, DB_SCHEMA, DataType, DbWrapperModule, DbWrapperService, DefaultType, getDefaultDbConfig, provideDbWrapper };
|