db-wrapper 0.0.7 → 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 +718 -1027
- package/bundles/db-wrapper.umd.js.map +1 -1
- package/bundles/db-wrapper.umd.min.js +15 -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 +5 -9
- package/esm2015/lib/db-wrapper.models.js +45 -206
- package/esm2015/lib/db-wrapper.module.js +21 -27
- package/esm2015/lib/db-wrapper.service.js +410 -779
- package/esm2015/lib/window.service.js +16 -24
- package/esm2015/public-api.js +7 -11
- package/esm5/db-wrapper.js +5 -9
- package/esm5/lib/db-wrapper.models.js +45 -206
- package/esm5/lib/db-wrapper.module.js +25 -36
- package/esm5/lib/db-wrapper.service.js +436 -895
- package/esm5/lib/window.service.js +23 -32
- package/esm5/public-api.js +7 -11
- package/fesm2015/db-wrapper.js +483 -851
- package/fesm2015/db-wrapper.js.map +1 -1
- package/fesm5/db-wrapper.js +518 -980
- 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 -37
- package/lib/window.service.d.ts +3 -3
- package/package.json +7 -7
- package/public-api.d.ts +3 -3
package/fesm5/db-wrapper.js
CHANGED
|
@@ -1,993 +1,531 @@
|
|
|
1
|
-
import { __awaiter, __generator } from 'tslib';
|
|
1
|
+
import { __decorate, __awaiter, __generator } from 'tslib';
|
|
2
2
|
import { Injectable, InjectionToken, NgModule } from '@angular/core';
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
{ type: Injectable }
|
|
23
|
-
];
|
|
24
|
-
return WindowService;
|
|
25
|
-
}());
|
|
26
|
-
/**
|
|
27
|
-
* @return {?}
|
|
28
|
-
*/
|
|
29
|
-
function _window() {
|
|
30
|
-
// Return the global native browser window object
|
|
31
|
-
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;
|
|
32
22
|
}
|
|
33
23
|
|
|
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
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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
|
-
return Promise
|
|
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
|
-
DbWrapperService.prototype.
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
return
|
|
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
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
* @return {?}
|
|
465
|
-
*/
|
|
466
|
-
function (query, bindings) {
|
|
467
|
-
var _this = this;
|
|
468
|
-
if (bindings === void 0) { bindings = []; }
|
|
469
|
-
return this.query(query, bindings).then((/**
|
|
470
|
-
* @param {?} rows
|
|
471
|
-
* @return {?}
|
|
472
|
-
*/
|
|
473
|
-
function (rows) {
|
|
474
|
-
return _this.rowsCount(rows);
|
|
475
|
-
}));
|
|
476
|
-
};
|
|
477
|
-
/**
|
|
478
|
-
* @param {?} table
|
|
479
|
-
* @param {?} bindings
|
|
480
|
-
* @return {?}
|
|
481
|
-
*/
|
|
482
|
-
DbWrapperService.prototype.insert = /**
|
|
483
|
-
* @param {?} table
|
|
484
|
-
* @param {?} bindings
|
|
485
|
-
* @return {?}
|
|
486
|
-
*/
|
|
487
|
-
function (table, bindings) {
|
|
488
|
-
var _this = this;
|
|
489
|
-
/** @type {?} */
|
|
490
|
-
var sql = '';
|
|
491
|
-
/** @type {?} */
|
|
492
|
-
var sqlBinds = [];
|
|
493
|
-
/** @type {?} */
|
|
494
|
-
var sqlKeys = [];
|
|
495
|
-
/** @type {?} */
|
|
496
|
-
var sqlValues = [];
|
|
497
|
-
sql = sql + 'insert into ' + table + ' ';
|
|
498
|
-
for (var index in bindings) {
|
|
499
|
-
if (bindings.hasOwnProperty(index)) {
|
|
500
|
-
sqlKeys.push(index);
|
|
501
|
-
sqlValues.push(bindings[index]);
|
|
502
|
-
sqlBinds.push('?');
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
sql = sql + '(' + sqlKeys.join(',') + ') values (' + sqlBinds.join(',') + ')';
|
|
506
|
-
return new Promise((/**
|
|
507
|
-
* @param {?} resolve
|
|
508
|
-
* @param {?} reject
|
|
509
|
-
* @return {?}
|
|
510
|
-
*/
|
|
511
|
-
function (resolve, reject) {
|
|
512
|
-
_this.db.transaction((/**
|
|
513
|
-
* @param {?} transaction
|
|
514
|
-
* @return {?}
|
|
515
|
-
*/
|
|
516
|
-
function (transaction) {
|
|
517
|
-
transaction.executeSql(sql, sqlValues, (/**
|
|
518
|
-
* @param {?} transaction
|
|
519
|
-
* @param {?} result
|
|
520
|
-
* @return {?}
|
|
521
|
-
*/
|
|
522
|
-
function (transaction, result) {
|
|
523
|
-
resolve(result);
|
|
524
|
-
}), (/**
|
|
525
|
-
* @param {?} transaction
|
|
526
|
-
* @param {?} error
|
|
527
|
-
* @return {?}
|
|
528
|
-
*/
|
|
529
|
-
function (transaction, error) {
|
|
530
|
-
reject(error);
|
|
531
|
-
}));
|
|
532
|
-
}));
|
|
533
|
-
}));
|
|
534
|
-
};
|
|
535
|
-
/**
|
|
536
|
-
* @param {?} table
|
|
537
|
-
* @param {?} bindings
|
|
538
|
-
* @param {?} where
|
|
539
|
-
* @return {?}
|
|
540
|
-
*/
|
|
541
|
-
DbWrapperService.prototype.update = /**
|
|
542
|
-
* @param {?} table
|
|
543
|
-
* @param {?} bindings
|
|
544
|
-
* @param {?} where
|
|
545
|
-
* @return {?}
|
|
546
|
-
*/
|
|
547
|
-
function (table, bindings, where) {
|
|
548
|
-
var _this = this;
|
|
549
|
-
/** @type {?} */
|
|
550
|
-
var sql = '';
|
|
551
|
-
/** @type {?} */
|
|
552
|
-
var sqlBinds = [];
|
|
553
|
-
/** @type {?} */
|
|
554
|
-
var sqlKeys = [];
|
|
555
|
-
/** @type {?} */
|
|
556
|
-
var sqlValues = [];
|
|
557
|
-
sql = sql + 'update ' + table + ' set ';
|
|
558
|
-
for (var index in bindings) {
|
|
559
|
-
if (bindings.hasOwnProperty(index)) {
|
|
560
|
-
/** @type {?} */
|
|
561
|
-
var newObj = bindings[index];
|
|
562
|
-
if (typeof newObj === 'object' && newObj !== null) {
|
|
563
|
-
sqlValues.push(newObj.value);
|
|
564
|
-
if (newObj.ifnull) {
|
|
565
|
-
sqlBinds.push(index + ' = ifnull( ' + index + ',0) + ?');
|
|
566
|
-
}
|
|
567
|
-
else if (newObj.operator) {
|
|
568
|
-
sqlBinds.push(index + ' = ' + index + newObj.operator + ' ?');
|
|
569
|
-
}
|
|
570
|
-
else {
|
|
571
|
-
sqlBinds.push(index + ' = ?');
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
else {
|
|
575
|
-
sqlBinds.push(index + ' = ?');
|
|
576
|
-
sqlValues.push(bindings[index]);
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
for (var index in where) {
|
|
581
|
-
if (where.hasOwnProperty(index)) {
|
|
582
|
-
if (typeof where[index] === 'object' && where[index] !== null) {
|
|
583
|
-
sqlKeys.push(index + ' ' + where[index].operator + ' ?');
|
|
584
|
-
sqlValues.push(where[index].value);
|
|
585
|
-
}
|
|
586
|
-
else {
|
|
587
|
-
sqlKeys.push(index + ' = ?');
|
|
588
|
-
sqlValues.push(where[index]);
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
sql = sql + sqlBinds.join(',') + ' where ' + sqlKeys.join(' and ');
|
|
593
|
-
return new Promise((/**
|
|
594
|
-
* @param {?} resolve
|
|
595
|
-
* @param {?} reject
|
|
596
|
-
* @return {?}
|
|
597
|
-
*/
|
|
598
|
-
function (resolve, reject) {
|
|
599
|
-
_this.db.transaction((/**
|
|
600
|
-
* @param {?} transaction
|
|
601
|
-
* @return {?}
|
|
602
|
-
*/
|
|
603
|
-
function (transaction) {
|
|
604
|
-
transaction.executeSql(sql, sqlValues, (/**
|
|
605
|
-
* @param {?} transaction
|
|
606
|
-
* @param {?} result
|
|
607
|
-
* @return {?}
|
|
608
|
-
*/
|
|
609
|
-
function (transaction, result) {
|
|
610
|
-
resolve(result);
|
|
611
|
-
}), (/**
|
|
612
|
-
* @param {?} transaction
|
|
613
|
-
* @param {?} error
|
|
614
|
-
* @return {?}
|
|
615
|
-
*/
|
|
616
|
-
function (transaction, error) {
|
|
617
|
-
reject(error);
|
|
618
|
-
}));
|
|
619
|
-
}));
|
|
620
|
-
}));
|
|
621
|
-
};
|
|
622
|
-
/**
|
|
623
|
-
* @param {?} tables
|
|
624
|
-
* @return {?}
|
|
625
|
-
*/
|
|
626
|
-
DbWrapperService.prototype.dropTable = /**
|
|
627
|
-
* @param {?} tables
|
|
628
|
-
* @return {?}
|
|
629
|
-
*/
|
|
630
|
-
function (tables) {
|
|
631
|
-
var _this = this;
|
|
632
|
-
/** @type {?} */
|
|
633
|
-
var cont = 1;
|
|
634
|
-
cont = tables.length;
|
|
635
|
-
return new Promise((/**
|
|
636
|
-
* @param {?} resolve
|
|
637
|
-
* @param {?} reject
|
|
638
|
-
* @return {?}
|
|
639
|
-
*/
|
|
640
|
-
function (resolve, reject) {
|
|
641
|
-
tables.some((/**
|
|
642
|
-
* @param {?} table
|
|
643
|
-
* @return {?}
|
|
644
|
-
*/
|
|
645
|
-
function (table) {
|
|
646
|
-
if (cont !== 0) {
|
|
647
|
-
_this.query('DROP TABLE ' + table)
|
|
648
|
-
.then((/**
|
|
649
|
-
* @return {?}
|
|
650
|
-
*/
|
|
651
|
-
function () {
|
|
652
|
-
if (cont === 1) {
|
|
653
|
-
resolve();
|
|
654
|
-
}
|
|
655
|
-
else {
|
|
656
|
-
cont--;
|
|
657
|
-
}
|
|
658
|
-
}), (/**
|
|
659
|
-
* @param {?} err
|
|
660
|
-
* @return {?}
|
|
661
|
-
*/
|
|
662
|
-
function (err) {
|
|
663
|
-
cont = 0;
|
|
664
|
-
reject(err);
|
|
665
|
-
return true;
|
|
666
|
-
}));
|
|
667
|
-
return false;
|
|
668
|
-
}
|
|
669
|
-
}));
|
|
670
|
-
}));
|
|
671
|
-
};
|
|
672
|
-
/**
|
|
673
|
-
* @param {?} result
|
|
674
|
-
* @return {?}
|
|
675
|
-
*/
|
|
676
|
-
DbWrapperService.prototype.fetchAll = /**
|
|
677
|
-
* @param {?} result
|
|
678
|
-
* @return {?}
|
|
679
|
-
*/
|
|
680
|
-
function (result) {
|
|
681
|
-
/** @type {?} */
|
|
682
|
-
var output = [];
|
|
683
|
-
for (var i = 0; i < result.rows.length; i++) {
|
|
684
|
-
output.push(result.rows.item(i));
|
|
685
|
-
}
|
|
686
|
-
return output;
|
|
687
|
-
};
|
|
688
|
-
/**
|
|
689
|
-
* @param {?} result
|
|
690
|
-
* @return {?}
|
|
691
|
-
*/
|
|
692
|
-
DbWrapperService.prototype.fetch = /**
|
|
693
|
-
* @param {?} result
|
|
694
|
-
* @return {?}
|
|
695
|
-
*/
|
|
696
|
-
function (result) {
|
|
697
|
-
/** @type {?} */
|
|
698
|
-
var objeto = undefined;
|
|
699
|
-
/** @type {?} */
|
|
700
|
-
var newObj = undefined;
|
|
701
|
-
if (result.rows.length !== 0) {
|
|
702
|
-
objeto = result.rows.item(0);
|
|
703
|
-
newObj = {};
|
|
704
|
-
/** @type {?} */
|
|
705
|
-
var keys = Object.keys(objeto);
|
|
706
|
-
for (var key in keys) {
|
|
707
|
-
newObj[keys[key]] = objeto[keys[key]];
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
return newObj;
|
|
711
|
-
};
|
|
712
|
-
/**
|
|
713
|
-
* @param {?} result
|
|
714
|
-
* @return {?}
|
|
715
|
-
*/
|
|
716
|
-
DbWrapperService.prototype.rowsCount = /**
|
|
717
|
-
* @param {?} result
|
|
718
|
-
* @return {?}
|
|
719
|
-
*/
|
|
720
|
-
function (result) {
|
|
721
|
-
return result.rows.length;
|
|
722
|
-
};
|
|
723
|
-
/* Private */
|
|
724
|
-
/* Private */
|
|
725
|
-
/**
|
|
726
|
-
* @private
|
|
727
|
-
* @param {?} col
|
|
728
|
-
* @return {?}
|
|
729
|
-
*/
|
|
730
|
-
DbWrapperService.prototype.defColumn = /* Private */
|
|
731
|
-
/**
|
|
732
|
-
* @private
|
|
733
|
-
* @param {?} col
|
|
734
|
-
* @return {?}
|
|
735
|
-
*/
|
|
736
|
-
function (col) {
|
|
737
|
-
/** @type {?} */
|
|
738
|
-
var colSql;
|
|
739
|
-
colSql = col.name + ' ' + col.type;
|
|
740
|
-
if (!col.isNullable) {
|
|
741
|
-
colSql += ' NOT NULL ';
|
|
742
|
-
}
|
|
743
|
-
if (col.default) {
|
|
744
|
-
colSql += ' DEFAULT ' + col.default + ' ';
|
|
745
|
-
}
|
|
746
|
-
if (col.type === 'text' && col.length) {
|
|
747
|
-
colSql += " CONSTRAINT " + col.name + "_length CHECK ( ";
|
|
748
|
-
if (col.isNullable) {
|
|
749
|
-
colSql += col.name + " is null OR ";
|
|
750
|
-
}
|
|
751
|
-
colSql += "length(" + col.name + ") <= " + col.length + " )";
|
|
752
|
-
}
|
|
753
|
-
return colSql;
|
|
754
|
-
};
|
|
755
|
-
/**
|
|
756
|
-
* @private
|
|
757
|
-
* @param {?} unique
|
|
758
|
-
* @return {?}
|
|
759
|
-
*/
|
|
760
|
-
DbWrapperService.prototype.defUnique = /**
|
|
761
|
-
* @private
|
|
762
|
-
* @param {?} unique
|
|
763
|
-
* @return {?}
|
|
764
|
-
*/
|
|
765
|
-
function (unique) {
|
|
766
|
-
/** @type {?} */
|
|
767
|
-
var uniqSql;
|
|
768
|
-
// depende del entorno, producción o desarrollo
|
|
769
|
-
/** @type {?} */
|
|
770
|
-
var onConflictUnique = 'on conflict fail';
|
|
771
|
-
if (Array.isArray(unique.fields)) {
|
|
772
|
-
uniqSql = " CONSTRAINT " + unique.name + " UNIQUE ( " + unique.fields.join(',') + " ) " + onConflictUnique + " ";
|
|
773
|
-
}
|
|
774
|
-
if (typeof unique.fields === 'string') {
|
|
775
|
-
uniqSql = " CONSTRAINT " + unique.name + " UNIQUE ( " + unique.fields + " ) " + onConflictUnique + " ";
|
|
776
|
-
}
|
|
777
|
-
return uniqSql;
|
|
778
|
-
};
|
|
779
|
-
/**
|
|
780
|
-
* @private
|
|
781
|
-
* @param {?} fore
|
|
782
|
-
* @return {?}
|
|
783
|
-
*/
|
|
784
|
-
DbWrapperService.prototype.defForeign = /**
|
|
785
|
-
* @private
|
|
786
|
-
* @param {?} fore
|
|
787
|
-
* @return {?}
|
|
788
|
-
*/
|
|
789
|
-
function (fore) {
|
|
790
|
-
/** @type {?} */
|
|
791
|
-
var foreSql;
|
|
792
|
-
if (Array.isArray(fore.foreignKey)) {
|
|
793
|
-
foreSql = " FOREIGN KEY ( " + fore.foreignKey.join(',') + " ) ";
|
|
794
|
-
}
|
|
795
|
-
if (typeof fore.foreignKey === 'string') {
|
|
796
|
-
foreSql = " FOREIGN KEY ( " + fore.foreignKey + " ) ";
|
|
797
|
-
}
|
|
798
|
-
if (Array.isArray(fore.primaryKey)) {
|
|
799
|
-
foreSql = " REFERENCES " + fore.referenceTable + " ( " + fore.primaryKey.join(',') + " ) ";
|
|
800
|
-
}
|
|
801
|
-
if (typeof fore.primaryKey === 'string') {
|
|
802
|
-
foreSql = " REFERENCES " + fore.referenceTable + " ( " + fore.primaryKey + " ) ";
|
|
803
|
-
}
|
|
804
|
-
return foreSql;
|
|
805
|
-
};
|
|
806
|
-
/**
|
|
807
|
-
* @private
|
|
808
|
-
* @param {?} table
|
|
809
|
-
* @return {?}
|
|
810
|
-
*/
|
|
811
|
-
DbWrapperService.prototype.tableExists = /**
|
|
812
|
-
* @private
|
|
813
|
-
* @param {?} table
|
|
814
|
-
* @return {?}
|
|
815
|
-
*/
|
|
816
|
-
function (table) {
|
|
817
|
-
var _this = this;
|
|
818
|
-
return this.query('select sql from sqlite_master ' +
|
|
819
|
-
'where type=\'table\' and name=\'' + table + '\'')
|
|
820
|
-
.then((/**
|
|
821
|
-
* @param {?} result
|
|
822
|
-
* @return {?}
|
|
823
|
-
*/
|
|
824
|
-
function (result) {
|
|
825
|
-
if (_this.rowsCount(result) === 1) {
|
|
826
|
-
return (/** @type {?} */ (((/** @type {?} */ (_this.fetch(result)))).sql));
|
|
827
|
-
}
|
|
828
|
-
else {
|
|
829
|
-
return false;
|
|
830
|
-
}
|
|
831
|
-
}));
|
|
832
|
-
};
|
|
833
|
-
/**
|
|
834
|
-
* @private
|
|
835
|
-
* @param {?} tableDDL
|
|
836
|
-
* @param {?} column
|
|
837
|
-
* @return {?}
|
|
838
|
-
*/
|
|
839
|
-
DbWrapperService.prototype.columnExists = /**
|
|
840
|
-
* @private
|
|
841
|
-
* @param {?} tableDDL
|
|
842
|
-
* @param {?} column
|
|
843
|
-
* @return {?}
|
|
844
|
-
*/
|
|
845
|
-
function (tableDDL, column) {
|
|
846
|
-
if (tableDDL.indexOf(column) === -1) {
|
|
847
|
-
return false;
|
|
848
|
-
}
|
|
849
|
-
else {
|
|
850
|
-
return true;
|
|
851
|
-
}
|
|
852
|
-
};
|
|
853
|
-
/**
|
|
854
|
-
* @private
|
|
855
|
-
* @param {?} table
|
|
856
|
-
* @param {?} col
|
|
857
|
-
* @return {?}
|
|
858
|
-
*/
|
|
859
|
-
DbWrapperService.prototype.addColumn = /**
|
|
860
|
-
* @private
|
|
861
|
-
* @param {?} table
|
|
862
|
-
* @param {?} col
|
|
863
|
-
* @return {?}
|
|
864
|
-
*/
|
|
865
|
-
function (table, col) {
|
|
866
|
-
/** @type {?} */
|
|
867
|
-
var colSql = this.defColumn(col);
|
|
868
|
-
/** @type {?} */
|
|
869
|
-
var query = 'alter TABLE ' + table + ' add COLUMN ' + colSql;
|
|
870
|
-
return this.query(query);
|
|
871
|
-
};
|
|
872
|
-
return DbWrapperService;
|
|
873
|
-
}());
|
|
874
|
-
/** @type {?} */
|
|
875
|
-
var DB_CONFIG = new InjectionToken('dbWrapperConfig');
|
|
876
|
-
/** @type {?} */
|
|
877
|
-
var DB_SCHEMA = new InjectionToken('dbWrapperSchema');
|
|
878
|
-
/**
|
|
879
|
-
* @return {?}
|
|
880
|
-
*/
|
|
881
|
-
function getDefaultDbConfig() {
|
|
882
|
-
return {
|
|
883
|
-
dbName: 'db.sqlite',
|
|
884
|
-
location: 'default',
|
|
885
|
-
iosDatabaseLocation: 'Library'
|
|
886
|
-
};
|
|
887
|
-
}
|
|
888
|
-
/**
|
|
889
|
-
* @param {?} dbConfig
|
|
890
|
-
* @param {?=} dbSchema
|
|
891
|
-
* @return {?}
|
|
892
|
-
*/
|
|
893
|
-
function provideDbWrapper(dbConfig, dbSchema) {
|
|
894
|
-
/** @type {?} */
|
|
895
|
-
var config = !!dbConfig ? dbConfig : getDefaultDbConfig();
|
|
896
|
-
return new DbWrapperService(config, dbSchema);
|
|
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);
|
|
897
454
|
}
|
|
898
455
|
|
|
899
|
-
/**
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
ngModule: DbWrapperModule,
|
|
919
|
-
providers: [
|
|
920
|
-
{ provide: DB_CONFIG, useValue: dbConfig },
|
|
921
|
-
{ provide: DB_SCHEMA, useValue: schema },
|
|
922
|
-
{ provide: DbWrapperService, useFactory: provideDbWrapper, deps: [DB_CONFIG, DB_SCHEMA] }
|
|
923
|
-
]
|
|
924
|
-
};
|
|
925
|
-
};
|
|
926
|
-
DbWrapperModule.decorators = [
|
|
927
|
-
{ type: NgModule }
|
|
928
|
-
];
|
|
929
|
-
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;
|
|
930
475
|
}());
|
|
931
476
|
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
Boolean: '0',
|
|
977
|
-
DateTime: '(datetime(\'now\', \'localtime\'))',
|
|
978
|
-
Date: '(date(\'now\', \'localtime\'))',
|
|
979
|
-
};
|
|
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'))";
|
|
520
|
+
})(DefaultType || (DefaultType = {}));
|
|
980
521
|
|
|
981
|
-
|
|
982
|
-
*
|
|
983
|
-
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
|
|
522
|
+
/*
|
|
523
|
+
* Public API Surface of db-wrapper
|
|
984
524
|
*/
|
|
985
525
|
|
|
986
|
-
/**
|
|
987
|
-
*
|
|
988
|
-
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
|
|
526
|
+
/**
|
|
527
|
+
* Generated bundle index. Do not edit.
|
|
989
528
|
*/
|
|
990
529
|
|
|
991
|
-
export {
|
|
992
|
-
|
|
993
|
-
//# sourceMappingURL=db-wrapper.js.map
|
|
530
|
+
export { DB_CONFIG, DB_SCHEMA, DataType, DbWrapperModule, DbWrapperService, DefaultType, getDefaultDbConfig, provideDbWrapper };
|
|
531
|
+
//# sourceMappingURL=db-wrapper.js.map
|