db-wrapper 0.0.7 → 2.0.0

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.
Files changed (40) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +24 -24
  3. package/esm2020/db-wrapper.mjs +5 -0
  4. package/esm2020/lib/db-wrapper.models.mjs +45 -0
  5. package/esm2020/lib/db-wrapper.module.mjs +22 -0
  6. package/esm2020/lib/db-wrapper.service.mjs +405 -0
  7. package/esm2020/lib/window.service.mjs +17 -0
  8. package/esm2020/public-api.mjs +7 -0
  9. package/fesm2015/db-wrapper.mjs +498 -0
  10. package/fesm2015/db-wrapper.mjs.map +1 -0
  11. package/fesm2020/db-wrapper.mjs +495 -0
  12. package/fesm2020/db-wrapper.mjs.map +1 -0
  13. package/{db-wrapper.d.ts → index.d.ts} +5 -4
  14. package/lib/db-wrapper.models.d.ts +119 -119
  15. package/lib/db-wrapper.module.d.ts +9 -5
  16. package/lib/db-wrapper.service.d.ts +39 -37
  17. package/lib/window.service.d.ts +6 -3
  18. package/package.json +26 -16
  19. package/public-api.d.ts +3 -3
  20. package/bundles/db-wrapper.umd.js +0 -1063
  21. package/bundles/db-wrapper.umd.js.map +0 -1
  22. package/bundles/db-wrapper.umd.min.js +0 -2
  23. package/bundles/db-wrapper.umd.min.js.map +0 -1
  24. package/db-wrapper.metadata.json +0 -1
  25. package/esm2015/db-wrapper.js +0 -9
  26. package/esm2015/lib/db-wrapper.models.js +0 -206
  27. package/esm2015/lib/db-wrapper.module.js +0 -27
  28. package/esm2015/lib/db-wrapper.service.js +0 -779
  29. package/esm2015/lib/window.service.js +0 -24
  30. package/esm2015/public-api.js +0 -11
  31. package/esm5/db-wrapper.js +0 -9
  32. package/esm5/lib/db-wrapper.models.js +0 -206
  33. package/esm5/lib/db-wrapper.module.js +0 -36
  34. package/esm5/lib/db-wrapper.service.js +0 -895
  35. package/esm5/lib/window.service.js +0 -32
  36. package/esm5/public-api.js +0 -11
  37. package/fesm2015/db-wrapper.js +0 -863
  38. package/fesm2015/db-wrapper.js.map +0 -1
  39. package/fesm5/db-wrapper.js +0 -993
  40. package/fesm5/db-wrapper.js.map +0 -1
@@ -1,993 +0,0 @@
1
- import { __awaiter, __generator } from 'tslib';
2
- import { Injectable, InjectionToken, NgModule } from '@angular/core';
3
-
4
- /**
5
- * @fileoverview added by tsickle
6
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
7
- */
8
- var WindowService = /** @class */ (function () {
9
- function WindowService() {
10
- }
11
- Object.defineProperty(WindowService.prototype, "nativeWindow", {
12
- get: /**
13
- * @return {?}
14
- */
15
- function () {
16
- return _window();
17
- },
18
- enumerable: true,
19
- configurable: true
20
- });
21
- WindowService.decorators = [
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;
32
- }
33
-
34
- /**
35
- * @fileoverview added by tsickle
36
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
37
- */
38
- var DbWrapperService = /** @class */ (function () {
39
- function DbWrapperService(dbConfig, dbSchema) {
40
- var _this = this;
41
- this._dbName = dbConfig.dbName;
42
- this._location = dbConfig.location || 'default';
43
- this._iosDatabaseLocation = dbConfig.iosDatabaseLocation || 'Library';
44
- /** @type {?} */
45
- var window = (new WindowService()).nativeWindow;
46
- if (dbSchema) {
47
- this.schema = dbSchema;
48
- }
49
- this._readyPromise = new Promise((/**
50
- * @param {?} resolve
51
- * @param {?} reject
52
- * @return {?}
53
- */
54
- function (resolve, reject) {
55
- if (typeof window.cordova === 'undefined') {
56
- setTimeout((/**
57
- * @return {?}
58
- */
59
- function () {
60
- _this.init(dbSchema).then((/**
61
- * @return {?}
62
- */
63
- function () {
64
- resolve();
65
- }))
66
- .catch((/**
67
- * @param {?} err
68
- * @return {?}
69
- */
70
- function (err) {
71
- reject(err);
72
- }));
73
- }), 500);
74
- }
75
- else {
76
- document.addEventListener('deviceready', (/**
77
- * @return {?}
78
- */
79
- function () {
80
- console.log('dbWrapper: device ready.');
81
- _this.initDevice(dbSchema).then((/**
82
- * @return {?}
83
- */
84
- function () { return resolve(); })).catch((/**
85
- * @param {?} err
86
- * @return {?}
87
- */
88
- function (err) { return reject(err); }));
89
- }));
90
- }
91
- }));
92
- }
93
- /**
94
- * @return {?}
95
- */
96
- DbWrapperService.prototype.ready = /**
97
- * @return {?}
98
- */
99
- function () {
100
- return this._readyPromise;
101
- };
102
- /**
103
- * @param {?} dbSchema
104
- * @return {?}
105
- */
106
- DbWrapperService.prototype.initDevice = /**
107
- * @param {?} dbSchema
108
- * @return {?}
109
- */
110
- function (dbSchema) {
111
- var _this = this;
112
- /** @type {?} */
113
- var window = (new WindowService()).nativeWindow;
114
- return new Promise((/**
115
- * @param {?} resolve
116
- * @param {?} reject
117
- * @return {?}
118
- */
119
- function (resolve, reject) {
120
- _this.db = window.sqlitePlugin.openDatabase({
121
- name: _this._dbName,
122
- location: _this._location,
123
- androidLockWorkaround: 1
124
- });
125
- if (dbSchema) {
126
- _this.generate(dbSchema).catch((/**
127
- * @param {?} err
128
- * @return {?}
129
- */
130
- function (err) {
131
- reject(err);
132
- })).then((/**
133
- * @return {?}
134
- */
135
- function () { resolve(); }));
136
- }
137
- else {
138
- resolve();
139
- }
140
- }));
141
- };
142
- /**
143
- * @param {?} dbSchema
144
- * @return {?}
145
- */
146
- DbWrapperService.prototype.init = /**
147
- * @param {?} dbSchema
148
- * @return {?}
149
- */
150
- function (dbSchema) {
151
- var _this = this;
152
- /** @type {?} */
153
- var window = (new WindowService()).nativeWindow;
154
- return new Promise((/**
155
- * @param {?} resolve
156
- * @param {?} reject
157
- * @return {?}
158
- */
159
- function (resolve, reject) {
160
- _this.db = window.openDatabase(_this._dbName, '1.0', 'database', -1);
161
- if (dbSchema) {
162
- _this.generate(dbSchema).catch((/**
163
- * @param {?} err
164
- * @return {?}
165
- */
166
- function (err) {
167
- reject(err);
168
- })).then((/**
169
- * @return {?}
170
- */
171
- function () {
172
- resolve();
173
- }));
174
- }
175
- else {
176
- resolve();
177
- }
178
- }));
179
- };
180
- /**
181
- * @param {?} schema
182
- * @return {?}
183
- */
184
- DbWrapperService.prototype.generate = /**
185
- * @param {?} schema
186
- * @return {?}
187
- */
188
- function (schema) {
189
- var _this = this;
190
- /** @type {?} */
191
- var sentences = [];
192
- this.schema = schema;
193
- if (schema.tables) {
194
- schema.tables.forEach((/**
195
- * @param {?} table
196
- * @return {?}
197
- */
198
- function (table) {
199
- sentences.push(_this.createTable(table));
200
- }));
201
- }
202
- if (schema.views) {
203
- schema.views.forEach((/**
204
- * @param {?} view
205
- * @return {?}
206
- */
207
- function (view) {
208
- sentences.push(_this.createView(view));
209
- }));
210
- }
211
- if (sentences.length === 0) {
212
- throw new Error('Esquema de la base de datos vacia.');
213
- }
214
- return Promise.all(sentences);
215
- };
216
- /**
217
- * @param {?} table
218
- * @return {?}
219
- */
220
- DbWrapperService.prototype.createTable = /**
221
- * @param {?} table
222
- * @return {?}
223
- */
224
- function (table) {
225
- return __awaiter(this, void 0, void 0, function () {
226
- var sql, onConflictPrimary, sentences, newColumns, tableDDL;
227
- var _this = this;
228
- return __generator(this, function (_a) {
229
- switch (_a.label) {
230
- case 0:
231
- sql = '';
232
- sentences = [];
233
- newColumns = [];
234
- return [4 /*yield*/, this.tableExists(table.name)];
235
- case 1:
236
- tableDDL = _a.sent();
237
- if (tableDDL) {
238
- table.columns.forEach((/**
239
- * @param {?} col
240
- * @return {?}
241
- */
242
- function (col) {
243
- if (!_this.columnExists((/** @type {?} */ (tableDDL)), col.name)) {
244
- newColumns.push(_this.addColumn(table.name, col));
245
- }
246
- }));
247
- }
248
- else {
249
- table.columns.forEach((/**
250
- * @param {?} col
251
- * @return {?}
252
- */
253
- function (col) {
254
- sentences.push(_this.defColumn(col));
255
- }));
256
- }
257
- onConflictPrimary = 'on conflict fail';
258
- if (Array.isArray(table.primary)) {
259
- sentences.push(' CONSTRAINT identificador PRIMARY KEY (' + table.primary.join(',') + ') ' + onConflictPrimary);
260
- }
261
- if (typeof table.primary === 'string') {
262
- sentences.push(' CONSTRAINT identificador PRIMARY KEY (' + table.primary + ') ' + onConflictPrimary);
263
- }
264
- if (table.uniques) {
265
- table.uniques.forEach((/**
266
- * @param {?} uniq
267
- * @return {?}
268
- */
269
- function (uniq) {
270
- sentences.push(_this.defUnique(uniq));
271
- }));
272
- }
273
- if (table.foreigns) {
274
- table.foreigns.forEach((/**
275
- * @param {?} fore
276
- * @return {?}
277
- */
278
- function (fore) {
279
- sentences.push(_this.defForeign(fore));
280
- }));
281
- }
282
- if (tableDDL && newColumns.length !== 0) {
283
- return [2 /*return*/, Promise.all(newColumns)
284
- .then((/**
285
- * @return {?}
286
- */
287
- function () { console.log("Table " + table.name + " altered."); }))
288
- .catch((/**
289
- * @param {?} err
290
- * @return {?}
291
- */
292
- function (err) {
293
- console.error("Table " + table.name + ": " + err.message);
294
- throw new Error("Table " + table.name + ": " + err.message);
295
- }))];
296
- }
297
- else if (!tableDDL) {
298
- sql = 'CREATE TABLE IF NOT EXISTS ' + table.name + ' (' + sentences.join(',') + ')';
299
- return [2 /*return*/, this.query(sql)
300
- .then((/**
301
- * @return {?}
302
- */
303
- function () { console.log("Table " + table.name + " created."); }))
304
- .catch((/**
305
- * @param {?} err
306
- * @return {?}
307
- */
308
- function (err) {
309
- console.error("Table " + table.name + ": " + err.message);
310
- throw new Error("Table " + table.name + ": " + err.message);
311
- }))];
312
- }
313
- else {
314
- return [2 /*return*/, Promise.resolve().then((/**
315
- * @return {?}
316
- */
317
- function () { console.log("Table " + table.name + " initialized."); }))];
318
- }
319
- return [2 /*return*/];
320
- }
321
- });
322
- });
323
- };
324
- /**
325
- * @param {?} view
326
- * @return {?}
327
- */
328
- DbWrapperService.prototype.createView = /**
329
- * @param {?} view
330
- * @return {?}
331
- */
332
- function (view) {
333
- /** @type {?} */
334
- var sql;
335
- sql = 'CREATE VIEW IF NOT EXISTS ' + view.name
336
- + ' AS ' + view.sql;
337
- return this.query(sql);
338
- };
339
- /**
340
- * @param {?} tableName
341
- * @return {?}
342
- */
343
- DbWrapperService.prototype.getSchemaTable = /**
344
- * @param {?} tableName
345
- * @return {?}
346
- */
347
- function (tableName) {
348
- /** @type {?} */
349
- var res;
350
- this.schema.tables.forEach((/**
351
- * @param {?} table
352
- * @return {?}
353
- */
354
- function (table) {
355
- if (table.name === tableName) {
356
- res = table;
357
- }
358
- }));
359
- return res;
360
- };
361
- /**
362
- * @param {?} table
363
- * @return {?}
364
- */
365
- DbWrapperService.prototype.getUniquesTable = /**
366
- * @param {?} table
367
- * @return {?}
368
- */
369
- function (table) {
370
- /** @type {?} */
371
- var res;
372
- table.uniques.forEach((/**
373
- * @param {?} uniq
374
- * @return {?}
375
- */
376
- function (uniq) {
377
- res[uniq.name] = uniq;
378
- }));
379
- return res;
380
- };
381
- /**
382
- * @param {?} query
383
- * @param {?=} bindings
384
- * @return {?}
385
- */
386
- DbWrapperService.prototype.query = /**
387
- * @param {?} query
388
- * @param {?=} bindings
389
- * @return {?}
390
- */
391
- function (query, bindings) {
392
- var _this = this;
393
- if (bindings === void 0) { bindings = []; }
394
- return new Promise((/**
395
- * @param {?} resolve
396
- * @param {?} reject
397
- * @return {?}
398
- */
399
- function (resolve, reject) {
400
- if (query.indexOf('select') === 0) {
401
- _this.db.readTransaction((/**
402
- * @param {?} transaction
403
- * @return {?}
404
- */
405
- function (transaction) {
406
- transaction.executeSql(query, bindings, (/**
407
- * @param {?} transaction
408
- * @param {?} result
409
- * @return {?}
410
- */
411
- function (transaction, result) {
412
- resolve(result);
413
- }), (/**
414
- * @param {?} transaction
415
- * @param {?} error
416
- * @return {?}
417
- */
418
- function (transaction, error) {
419
- reject(error);
420
- }));
421
- }), (/**
422
- * @param {?} err
423
- * @return {?}
424
- */
425
- function (err) { return reject(err); }));
426
- }
427
- else {
428
- _this.db.transaction((/**
429
- * @param {?} transaction
430
- * @return {?}
431
- */
432
- function (transaction) {
433
- transaction.executeSql(query, bindings, (/**
434
- * @param {?} transaction
435
- * @param {?} result
436
- * @return {?}
437
- */
438
- function (transaction, result) {
439
- resolve(result);
440
- }), (/**
441
- * @param {?} transaction
442
- * @param {?} error
443
- * @return {?}
444
- */
445
- function (transaction, error) {
446
- reject(error);
447
- }));
448
- }), (/**
449
- * @param {?} err
450
- * @return {?}
451
- */
452
- function (err) { return reject(err); }));
453
- }
454
- }));
455
- };
456
- /**
457
- * @param {?} query
458
- * @param {?=} bindings
459
- * @return {?}
460
- */
461
- DbWrapperService.prototype.Count = /**
462
- * @param {?} query
463
- * @param {?=} bindings
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);
897
- }
898
-
899
- /**
900
- * @fileoverview added by tsickle
901
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
902
- */
903
- var DbWrapperModule = /** @class */ (function () {
904
- function DbWrapperModule() {
905
- }
906
- /**
907
- * @param {?} dbConfig
908
- * @param {?=} schema
909
- * @return {?}
910
- */
911
- DbWrapperModule.forRoot = /**
912
- * @param {?} dbConfig
913
- * @param {?=} schema
914
- * @return {?}
915
- */
916
- function (dbConfig, schema) {
917
- return {
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;
930
- }());
931
-
932
- /**
933
- * @fileoverview added by tsickle
934
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
935
- */
936
- /** @enum {string} */
937
- var DataType = {
938
- Integer: 'integer',
939
- Int: 'integer',
940
- TinyInt: 'integer',
941
- SmallInt: 'integer',
942
- BigInt: 'integer',
943
- Text: 'text',
944
- Char: 'text',
945
- NChar: 'text',
946
- VarChar: 'text',
947
- NVarChar: 'text',
948
- CLOB: 'text',
949
- BLOB: 'blob',
950
- Real: 'real',
951
- Double: 'real',
952
- Decimal: 'real',
953
- Float: 'real',
954
- Boolean: 'integer',
955
- DateTime: 'text',
956
- Date: 'text',
957
- };
958
- /** @enum {string} */
959
- var DefaultType = {
960
- Integer: '0',
961
- Int: '0',
962
- TinyInt: '0',
963
- SmallInt: '0',
964
- BigInt: '0',
965
- Text: '\'\'',
966
- Char: '\'\'',
967
- NChar: '\'\'',
968
- VarChar: '\'\'',
969
- NVarChar: '\'\'',
970
- CLOB: '\'\'',
971
- BLOB: '0',
972
- Real: '0.0',
973
- Double: '0.0',
974
- Decimal: '0.0',
975
- Float: '0.0',
976
- Boolean: '0',
977
- DateTime: '(datetime(\'now\', \'localtime\'))',
978
- Date: '(date(\'now\', \'localtime\'))',
979
- };
980
-
981
- /**
982
- * @fileoverview added by tsickle
983
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
984
- */
985
-
986
- /**
987
- * @fileoverview added by tsickle
988
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
989
- */
990
-
991
- export { getDefaultDbConfig, provideDbWrapper, DbWrapperService, DB_CONFIG, DB_SCHEMA, DbWrapperModule, DataType, DefaultType };
992
-
993
- //# sourceMappingURL=db-wrapper.js.map