db-wrapper 0.0.7 → 0.0.8
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/bundles/db-wrapper.umd.js +640 -951
- 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.metadata.json +1 -1
- package/esm2015/db-wrapper.js +2 -6
- package/esm2015/lib/db-wrapper.models.js +45 -206
- package/esm2015/lib/db-wrapper.module.js +10 -16
- package/esm2015/lib/db-wrapper.service.js +110 -481
- package/esm2015/lib/window.service.js +8 -16
- package/esm2015/public-api.js +4 -8
- package/esm5/db-wrapper.js +2 -6
- package/esm5/lib/db-wrapper.models.js +45 -206
- package/esm5/lib/db-wrapper.module.js +9 -20
- package/esm5/lib/db-wrapper.service.js +132 -593
- package/esm5/lib/window.service.js +6 -15
- package/esm5/public-api.js +4 -8
- package/fesm2015/db-wrapper.js +169 -539
- package/fesm2015/db-wrapper.js.map +1 -1
- package/fesm5/db-wrapper.js +189 -653
- package/fesm5/db-wrapper.js.map +1 -1
- package/lib/db-wrapper.service.d.ts +4 -2
- package/lib/window.service.d.ts +1 -1
- package/package.json +7 -7
package/fesm2015/db-wrapper.js
CHANGED
|
@@ -1,231 +1,118 @@
|
|
|
1
|
-
import { __awaiter } from 'tslib';
|
|
1
|
+
import { __decorate, __awaiter } from 'tslib';
|
|
2
2
|
import { Injectable, InjectionToken, NgModule } from '@angular/core';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
* @fileoverview added by tsickle
|
|
6
|
-
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
|
|
7
|
-
*/
|
|
8
|
-
class WindowService {
|
|
9
|
-
/**
|
|
10
|
-
* @return {?}
|
|
11
|
-
*/
|
|
4
|
+
let WindowService = class WindowService {
|
|
12
5
|
get nativeWindow() {
|
|
13
6
|
return _window();
|
|
14
7
|
}
|
|
15
|
-
}
|
|
16
|
-
WindowService
|
|
17
|
-
|
|
18
|
-
];
|
|
19
|
-
/**
|
|
20
|
-
* @return {?}
|
|
21
|
-
*/
|
|
8
|
+
};
|
|
9
|
+
WindowService = __decorate([
|
|
10
|
+
Injectable()
|
|
11
|
+
], WindowService);
|
|
22
12
|
function _window() {
|
|
23
13
|
// Return the global native browser window object
|
|
24
14
|
return typeof window !== 'undefined' ? window : undefined;
|
|
25
15
|
}
|
|
26
16
|
|
|
27
|
-
/**
|
|
28
|
-
* @fileoverview added by tsickle
|
|
29
|
-
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
|
|
30
|
-
*/
|
|
31
17
|
class DbWrapperService {
|
|
32
|
-
/**
|
|
33
|
-
* @param {?} dbConfig
|
|
34
|
-
* @param {?=} dbSchema
|
|
35
|
-
*/
|
|
36
18
|
constructor(dbConfig, dbSchema) {
|
|
37
19
|
this._dbName = dbConfig.dbName;
|
|
38
20
|
this._location = dbConfig.location || 'default';
|
|
39
21
|
this._iosDatabaseLocation = dbConfig.iosDatabaseLocation || 'Library';
|
|
40
|
-
/** @type {?} */
|
|
41
22
|
const window = (new WindowService()).nativeWindow;
|
|
42
23
|
if (dbSchema) {
|
|
43
24
|
this.schema = dbSchema;
|
|
44
25
|
}
|
|
45
|
-
this._readyPromise = new Promise((
|
|
46
|
-
* @param {?} resolve
|
|
47
|
-
* @param {?} reject
|
|
48
|
-
* @return {?}
|
|
49
|
-
*/
|
|
50
|
-
(resolve, reject) => {
|
|
26
|
+
this._readyPromise = new Promise((resolve, reject) => {
|
|
51
27
|
if (typeof window.cordova === 'undefined') {
|
|
52
|
-
setTimeout((
|
|
53
|
-
|
|
54
|
-
*/
|
|
55
|
-
() => {
|
|
56
|
-
this.init(dbSchema).then((/**
|
|
57
|
-
* @return {?}
|
|
58
|
-
*/
|
|
59
|
-
() => {
|
|
28
|
+
setTimeout(() => {
|
|
29
|
+
this.init(dbSchema).then(() => {
|
|
60
30
|
resolve();
|
|
61
|
-
})
|
|
62
|
-
.catch((
|
|
63
|
-
* @param {?} err
|
|
64
|
-
* @return {?}
|
|
65
|
-
*/
|
|
66
|
-
(err) => {
|
|
31
|
+
})
|
|
32
|
+
.catch((err) => {
|
|
67
33
|
reject(err);
|
|
68
|
-
})
|
|
69
|
-
}
|
|
34
|
+
});
|
|
35
|
+
}, 500);
|
|
70
36
|
}
|
|
71
37
|
else {
|
|
72
|
-
document.addEventListener('deviceready', (
|
|
73
|
-
* @return {?}
|
|
74
|
-
*/
|
|
75
|
-
() => {
|
|
38
|
+
document.addEventListener('deviceready', () => {
|
|
76
39
|
console.log('dbWrapper: device ready.');
|
|
77
|
-
this.initDevice(dbSchema).then((
|
|
78
|
-
|
|
79
|
-
*/
|
|
80
|
-
() => resolve())).catch((/**
|
|
81
|
-
* @param {?} err
|
|
82
|
-
* @return {?}
|
|
83
|
-
*/
|
|
84
|
-
(err) => reject(err)));
|
|
85
|
-
}));
|
|
40
|
+
this.initDevice(dbSchema).then(() => resolve()).catch((err) => reject(err));
|
|
41
|
+
});
|
|
86
42
|
}
|
|
87
|
-
})
|
|
43
|
+
});
|
|
88
44
|
}
|
|
89
|
-
/**
|
|
90
|
-
* @return {?}
|
|
91
|
-
*/
|
|
92
45
|
ready() {
|
|
93
46
|
return this._readyPromise;
|
|
94
47
|
}
|
|
95
|
-
/**
|
|
96
|
-
* @param {?} dbSchema
|
|
97
|
-
* @return {?}
|
|
98
|
-
*/
|
|
99
48
|
initDevice(dbSchema) {
|
|
100
|
-
/** @type {?} */
|
|
101
49
|
const window = (new WindowService()).nativeWindow;
|
|
102
|
-
return new Promise((
|
|
103
|
-
* @param {?} resolve
|
|
104
|
-
* @param {?} reject
|
|
105
|
-
* @return {?}
|
|
106
|
-
*/
|
|
107
|
-
(resolve, reject) => {
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
108
51
|
this.db = window.sqlitePlugin.openDatabase({
|
|
109
52
|
name: this._dbName,
|
|
110
53
|
location: this._location,
|
|
111
54
|
androidLockWorkaround: 1
|
|
112
55
|
});
|
|
113
56
|
if (dbSchema) {
|
|
114
|
-
this.generate(dbSchema).catch(
|
|
115
|
-
* @param {?} err
|
|
116
|
-
* @return {?}
|
|
117
|
-
*/
|
|
118
|
-
err => {
|
|
57
|
+
this.generate(dbSchema).catch(err => {
|
|
119
58
|
reject(err);
|
|
120
|
-
})
|
|
121
|
-
* @return {?}
|
|
122
|
-
*/
|
|
123
|
-
() => { resolve(); }));
|
|
59
|
+
}).then(() => { resolve(); });
|
|
124
60
|
}
|
|
125
61
|
else {
|
|
126
62
|
resolve();
|
|
127
63
|
}
|
|
128
|
-
})
|
|
64
|
+
});
|
|
129
65
|
}
|
|
130
|
-
/**
|
|
131
|
-
* @param {?} dbSchema
|
|
132
|
-
* @return {?}
|
|
133
|
-
*/
|
|
134
66
|
init(dbSchema) {
|
|
135
|
-
/** @type {?} */
|
|
136
67
|
const window = (new WindowService()).nativeWindow;
|
|
137
|
-
return new Promise((
|
|
138
|
-
* @param {?} resolve
|
|
139
|
-
* @param {?} reject
|
|
140
|
-
* @return {?}
|
|
141
|
-
*/
|
|
142
|
-
(resolve, reject) => {
|
|
68
|
+
return new Promise((resolve, reject) => {
|
|
143
69
|
this.db = window.openDatabase(this._dbName, '1.0', 'database', -1);
|
|
144
70
|
if (dbSchema) {
|
|
145
|
-
this.generate(dbSchema).catch(
|
|
146
|
-
* @param {?} err
|
|
147
|
-
* @return {?}
|
|
148
|
-
*/
|
|
149
|
-
err => {
|
|
71
|
+
this.generate(dbSchema).catch(err => {
|
|
150
72
|
reject(err);
|
|
151
|
-
})
|
|
152
|
-
* @return {?}
|
|
153
|
-
*/
|
|
154
|
-
() => {
|
|
73
|
+
}).then(() => {
|
|
155
74
|
resolve();
|
|
156
|
-
})
|
|
75
|
+
});
|
|
157
76
|
}
|
|
158
77
|
else {
|
|
159
78
|
resolve();
|
|
160
79
|
}
|
|
161
|
-
})
|
|
80
|
+
});
|
|
162
81
|
}
|
|
163
|
-
/**
|
|
164
|
-
* @param {?} schema
|
|
165
|
-
* @return {?}
|
|
166
|
-
*/
|
|
167
82
|
generate(schema) {
|
|
168
|
-
/** @type {?} */
|
|
169
83
|
const sentences = [];
|
|
170
84
|
this.schema = schema;
|
|
171
85
|
if (schema.tables) {
|
|
172
|
-
schema.tables.forEach(
|
|
173
|
-
* @param {?} table
|
|
174
|
-
* @return {?}
|
|
175
|
-
*/
|
|
176
|
-
table => {
|
|
86
|
+
schema.tables.forEach(table => {
|
|
177
87
|
sentences.push(this.createTable(table));
|
|
178
|
-
})
|
|
88
|
+
});
|
|
179
89
|
}
|
|
180
90
|
if (schema.views) {
|
|
181
|
-
schema.views.forEach(
|
|
182
|
-
* @param {?} view
|
|
183
|
-
* @return {?}
|
|
184
|
-
*/
|
|
185
|
-
view => {
|
|
91
|
+
schema.views.forEach(view => {
|
|
186
92
|
sentences.push(this.createView(view));
|
|
187
|
-
})
|
|
93
|
+
});
|
|
188
94
|
}
|
|
189
95
|
if (sentences.length === 0) {
|
|
190
96
|
throw new Error('Esquema de la base de datos vacia.');
|
|
191
97
|
}
|
|
192
98
|
return Promise.all(sentences);
|
|
193
99
|
}
|
|
194
|
-
/**
|
|
195
|
-
* @param {?} table
|
|
196
|
-
* @return {?}
|
|
197
|
-
*/
|
|
198
100
|
createTable(table) {
|
|
199
101
|
return __awaiter(this, void 0, void 0, function* () {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
let onConflictPrimary;
|
|
204
|
-
/** @type {?} */
|
|
205
|
-
const sentences = [];
|
|
206
|
-
/** @type {?} */
|
|
207
|
-
const newColumns = [];
|
|
208
|
-
/** @type {?} */
|
|
209
|
-
const tableDDL = yield this.tableExists(table.name);
|
|
102
|
+
let sql = '', onConflictPrimary;
|
|
103
|
+
const sentences = [], newColumns = [];
|
|
104
|
+
const tableDDL = yield this._tableExists(table.name);
|
|
210
105
|
if (tableDDL) {
|
|
211
|
-
table.columns.forEach(
|
|
212
|
-
|
|
213
|
-
* @return {?}
|
|
214
|
-
*/
|
|
215
|
-
col => {
|
|
216
|
-
if (!this.columnExists((/** @type {?} */ (tableDDL)), col.name)) {
|
|
106
|
+
table.columns.forEach(col => {
|
|
107
|
+
if (!this._columnExists(tableDDL, col.name)) {
|
|
217
108
|
newColumns.push(this.addColumn(table.name, col));
|
|
218
109
|
}
|
|
219
|
-
})
|
|
110
|
+
});
|
|
220
111
|
}
|
|
221
112
|
else {
|
|
222
|
-
table.columns.forEach(
|
|
223
|
-
* @param {?} col
|
|
224
|
-
* @return {?}
|
|
225
|
-
*/
|
|
226
|
-
col => {
|
|
113
|
+
table.columns.forEach(col => {
|
|
227
114
|
sentences.push(this.defColumn(col));
|
|
228
|
-
})
|
|
115
|
+
});
|
|
229
116
|
}
|
|
230
117
|
onConflictPrimary = 'on conflict fail';
|
|
231
118
|
if (Array.isArray(table.primary)) {
|
|
@@ -235,203 +122,88 @@ class DbWrapperService {
|
|
|
235
122
|
sentences.push(' CONSTRAINT identificador PRIMARY KEY (' + table.primary + ') ' + onConflictPrimary);
|
|
236
123
|
}
|
|
237
124
|
if (table.uniques) {
|
|
238
|
-
table.uniques.forEach(
|
|
239
|
-
* @param {?} uniq
|
|
240
|
-
* @return {?}
|
|
241
|
-
*/
|
|
242
|
-
uniq => {
|
|
125
|
+
table.uniques.forEach(uniq => {
|
|
243
126
|
sentences.push(this.defUnique(uniq));
|
|
244
|
-
})
|
|
127
|
+
});
|
|
245
128
|
}
|
|
246
129
|
if (table.foreigns) {
|
|
247
|
-
table.foreigns.forEach(
|
|
248
|
-
* @param {?} fore
|
|
249
|
-
* @return {?}
|
|
250
|
-
*/
|
|
251
|
-
fore => {
|
|
130
|
+
table.foreigns.forEach(fore => {
|
|
252
131
|
sentences.push(this.defForeign(fore));
|
|
253
|
-
})
|
|
132
|
+
});
|
|
254
133
|
}
|
|
255
134
|
if (tableDDL && newColumns.length !== 0) {
|
|
256
135
|
return Promise.all(newColumns)
|
|
257
|
-
.then((
|
|
258
|
-
|
|
259
|
-
*/
|
|
260
|
-
() => { console.log(`Table ${table.name} altered.`); }))
|
|
261
|
-
.catch((/**
|
|
262
|
-
* @param {?} err
|
|
263
|
-
* @return {?}
|
|
264
|
-
*/
|
|
265
|
-
(err) => {
|
|
136
|
+
.then(() => { console.log(`Table ${table.name} altered.`); })
|
|
137
|
+
.catch((err) => {
|
|
266
138
|
console.error(`Table ${table.name}: ${err.message}`);
|
|
267
139
|
throw new Error(`Table ${table.name}: ${err.message}`);
|
|
268
|
-
})
|
|
140
|
+
});
|
|
269
141
|
}
|
|
270
142
|
else if (!tableDDL) {
|
|
271
143
|
sql = 'CREATE TABLE IF NOT EXISTS ' + table.name + ' (' + sentences.join(',') + ')';
|
|
272
144
|
return this.query(sql)
|
|
273
|
-
.then((
|
|
274
|
-
|
|
275
|
-
*/
|
|
276
|
-
() => { console.log(`Table ${table.name} created.`); }))
|
|
277
|
-
.catch((/**
|
|
278
|
-
* @param {?} err
|
|
279
|
-
* @return {?}
|
|
280
|
-
*/
|
|
281
|
-
(err) => {
|
|
145
|
+
.then(() => { console.log(`Table ${table.name} created.`); })
|
|
146
|
+
.catch((err) => {
|
|
282
147
|
console.error(`Table ${table.name}: ${err.message}`);
|
|
283
148
|
throw new Error(`Table ${table.name}: ${err.message}`);
|
|
284
|
-
})
|
|
149
|
+
});
|
|
285
150
|
}
|
|
286
151
|
else {
|
|
287
|
-
return Promise.resolve().then((
|
|
288
|
-
* @return {?}
|
|
289
|
-
*/
|
|
290
|
-
() => { console.log(`Table ${table.name} initialized.`); }));
|
|
152
|
+
return Promise.resolve().then(() => { console.log(`Table ${table.name} initialized.`); });
|
|
291
153
|
}
|
|
292
154
|
});
|
|
293
155
|
}
|
|
294
|
-
/**
|
|
295
|
-
* @param {?} view
|
|
296
|
-
* @return {?}
|
|
297
|
-
*/
|
|
298
156
|
createView(view) {
|
|
299
|
-
/** @type {?} */
|
|
300
157
|
let sql;
|
|
301
158
|
sql = 'CREATE VIEW IF NOT EXISTS ' + view.name
|
|
302
159
|
+ ' AS ' + view.sql;
|
|
303
160
|
return this.query(sql);
|
|
304
161
|
}
|
|
305
|
-
/**
|
|
306
|
-
* @param {?} tableName
|
|
307
|
-
* @return {?}
|
|
308
|
-
*/
|
|
309
162
|
getSchemaTable(tableName) {
|
|
310
|
-
/** @type {?} */
|
|
311
163
|
let res;
|
|
312
|
-
this.schema.tables.forEach(
|
|
313
|
-
* @param {?} table
|
|
314
|
-
* @return {?}
|
|
315
|
-
*/
|
|
316
|
-
table => {
|
|
164
|
+
this.schema.tables.forEach(table => {
|
|
317
165
|
if (table.name === tableName) {
|
|
318
166
|
res = table;
|
|
319
167
|
}
|
|
320
|
-
})
|
|
168
|
+
});
|
|
321
169
|
return res;
|
|
322
170
|
}
|
|
323
|
-
/**
|
|
324
|
-
* @param {?} table
|
|
325
|
-
* @return {?}
|
|
326
|
-
*/
|
|
327
171
|
getUniquesTable(table) {
|
|
328
|
-
/** @type {?} */
|
|
329
172
|
let res;
|
|
330
|
-
table.uniques.forEach(
|
|
331
|
-
* @param {?} uniq
|
|
332
|
-
* @return {?}
|
|
333
|
-
*/
|
|
334
|
-
uniq => {
|
|
173
|
+
table.uniques.forEach(uniq => {
|
|
335
174
|
res[uniq.name] = uniq;
|
|
336
|
-
})
|
|
175
|
+
});
|
|
337
176
|
return res;
|
|
338
177
|
}
|
|
339
|
-
/**
|
|
340
|
-
* @param {?} query
|
|
341
|
-
* @param {?=} bindings
|
|
342
|
-
* @return {?}
|
|
343
|
-
*/
|
|
344
178
|
query(query, bindings = []) {
|
|
345
|
-
return new Promise((
|
|
346
|
-
* @param {?} resolve
|
|
347
|
-
* @param {?} reject
|
|
348
|
-
* @return {?}
|
|
349
|
-
*/
|
|
350
|
-
(resolve, reject) => {
|
|
179
|
+
return new Promise((resolve, reject) => {
|
|
351
180
|
if (query.indexOf('select') === 0) {
|
|
352
|
-
this.db.readTransaction((
|
|
353
|
-
|
|
354
|
-
* @return {?}
|
|
355
|
-
*/
|
|
356
|
-
(transaction) => {
|
|
357
|
-
transaction.executeSql(query, bindings, (/**
|
|
358
|
-
* @param {?} transaction
|
|
359
|
-
* @param {?} result
|
|
360
|
-
* @return {?}
|
|
361
|
-
*/
|
|
362
|
-
(transaction, result) => {
|
|
181
|
+
this.db.readTransaction((transaction) => {
|
|
182
|
+
transaction.executeSql(query, bindings, (transaction, result) => {
|
|
363
183
|
resolve(result);
|
|
364
|
-
}
|
|
365
|
-
* @param {?} transaction
|
|
366
|
-
* @param {?} error
|
|
367
|
-
* @return {?}
|
|
368
|
-
*/
|
|
369
|
-
(transaction, error) => {
|
|
184
|
+
}, (transaction, error) => {
|
|
370
185
|
reject(error);
|
|
371
|
-
})
|
|
372
|
-
}
|
|
373
|
-
* @param {?} err
|
|
374
|
-
* @return {?}
|
|
375
|
-
*/
|
|
376
|
-
(err) => reject(err)));
|
|
186
|
+
});
|
|
187
|
+
}, (err) => reject(err));
|
|
377
188
|
}
|
|
378
189
|
else {
|
|
379
|
-
this.db.transaction((
|
|
380
|
-
|
|
381
|
-
* @return {?}
|
|
382
|
-
*/
|
|
383
|
-
(transaction) => {
|
|
384
|
-
transaction.executeSql(query, bindings, (/**
|
|
385
|
-
* @param {?} transaction
|
|
386
|
-
* @param {?} result
|
|
387
|
-
* @return {?}
|
|
388
|
-
*/
|
|
389
|
-
(transaction, result) => {
|
|
190
|
+
this.db.transaction((transaction) => {
|
|
191
|
+
transaction.executeSql(query, bindings, (transaction, result) => {
|
|
390
192
|
resolve(result);
|
|
391
|
-
}
|
|
392
|
-
* @param {?} transaction
|
|
393
|
-
* @param {?} error
|
|
394
|
-
* @return {?}
|
|
395
|
-
*/
|
|
396
|
-
(transaction, error) => {
|
|
193
|
+
}, (transaction, error) => {
|
|
397
194
|
reject(error);
|
|
398
|
-
})
|
|
399
|
-
}
|
|
400
|
-
* @param {?} err
|
|
401
|
-
* @return {?}
|
|
402
|
-
*/
|
|
403
|
-
(err) => reject(err)));
|
|
195
|
+
});
|
|
196
|
+
}, (err) => reject(err));
|
|
404
197
|
}
|
|
405
|
-
})
|
|
198
|
+
});
|
|
406
199
|
}
|
|
407
|
-
/**
|
|
408
|
-
* @param {?} query
|
|
409
|
-
* @param {?=} bindings
|
|
410
|
-
* @return {?}
|
|
411
|
-
*/
|
|
412
200
|
Count(query, bindings = []) {
|
|
413
|
-
return this.query(query, bindings).then(
|
|
414
|
-
* @param {?} rows
|
|
415
|
-
* @return {?}
|
|
416
|
-
*/
|
|
417
|
-
rows => {
|
|
201
|
+
return this.query(query, bindings).then(rows => {
|
|
418
202
|
return this.rowsCount(rows);
|
|
419
|
-
})
|
|
203
|
+
});
|
|
420
204
|
}
|
|
421
|
-
/**
|
|
422
|
-
* @param {?} table
|
|
423
|
-
* @param {?} bindings
|
|
424
|
-
* @return {?}
|
|
425
|
-
*/
|
|
426
205
|
insert(table, bindings) {
|
|
427
|
-
|
|
428
|
-
let sql = '';
|
|
429
|
-
/** @type {?} */
|
|
430
|
-
let sqlBinds = [];
|
|
431
|
-
/** @type {?} */
|
|
432
|
-
let sqlKeys = [];
|
|
433
|
-
/** @type {?} */
|
|
434
|
-
let sqlValues = [];
|
|
206
|
+
let sql = '', sqlBinds = [], sqlKeys = [], sqlValues = [];
|
|
435
207
|
sql = sql + 'insert into ' + table + ' ';
|
|
436
208
|
for (const index in bindings) {
|
|
437
209
|
if (bindings.hasOwnProperty(index)) {
|
|
@@ -441,54 +213,21 @@ class DbWrapperService {
|
|
|
441
213
|
}
|
|
442
214
|
}
|
|
443
215
|
sql = sql + '(' + sqlKeys.join(',') + ') values (' + sqlBinds.join(',') + ')';
|
|
444
|
-
return new Promise((
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
* @return {?}
|
|
448
|
-
*/
|
|
449
|
-
(resolve, reject) => {
|
|
450
|
-
this.db.transaction((/**
|
|
451
|
-
* @param {?} transaction
|
|
452
|
-
* @return {?}
|
|
453
|
-
*/
|
|
454
|
-
transaction => {
|
|
455
|
-
transaction.executeSql(sql, sqlValues, (/**
|
|
456
|
-
* @param {?} transaction
|
|
457
|
-
* @param {?} result
|
|
458
|
-
* @return {?}
|
|
459
|
-
*/
|
|
460
|
-
(transaction, result) => {
|
|
216
|
+
return new Promise((resolve, reject) => {
|
|
217
|
+
this.db.transaction(transaction => {
|
|
218
|
+
transaction.executeSql(sql, sqlValues, (transaction, result) => {
|
|
461
219
|
resolve(result);
|
|
462
|
-
}
|
|
463
|
-
* @param {?} transaction
|
|
464
|
-
* @param {?} error
|
|
465
|
-
* @return {?}
|
|
466
|
-
*/
|
|
467
|
-
(transaction, error) => {
|
|
220
|
+
}, (transaction, error) => {
|
|
468
221
|
reject(error);
|
|
469
|
-
})
|
|
470
|
-
})
|
|
471
|
-
})
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
});
|
|
472
225
|
}
|
|
473
|
-
/**
|
|
474
|
-
* @param {?} table
|
|
475
|
-
* @param {?} bindings
|
|
476
|
-
* @param {?} where
|
|
477
|
-
* @return {?}
|
|
478
|
-
*/
|
|
479
226
|
update(table, bindings, where) {
|
|
480
|
-
|
|
481
|
-
let sql = '';
|
|
482
|
-
/** @type {?} */
|
|
483
|
-
let sqlBinds = [];
|
|
484
|
-
/** @type {?} */
|
|
485
|
-
let sqlKeys = [];
|
|
486
|
-
/** @type {?} */
|
|
487
|
-
let sqlValues = [];
|
|
227
|
+
let sql = '', sqlBinds = [], sqlKeys = [], sqlValues = [];
|
|
488
228
|
sql = sql + 'update ' + table + ' set ';
|
|
489
229
|
for (let index in bindings) {
|
|
490
230
|
if (bindings.hasOwnProperty(index)) {
|
|
491
|
-
/** @type {?} */
|
|
492
231
|
let newObj = bindings[index];
|
|
493
232
|
if (typeof newObj === 'object' && newObj !== null) {
|
|
494
233
|
sqlValues.push(newObj.value);
|
|
@@ -521,105 +260,52 @@ class DbWrapperService {
|
|
|
521
260
|
}
|
|
522
261
|
}
|
|
523
262
|
sql = sql + sqlBinds.join(',') + ' where ' + sqlKeys.join(' and ');
|
|
524
|
-
return new Promise((
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
* @return {?}
|
|
528
|
-
*/
|
|
529
|
-
(resolve, reject) => {
|
|
530
|
-
this.db.transaction((/**
|
|
531
|
-
* @param {?} transaction
|
|
532
|
-
* @return {?}
|
|
533
|
-
*/
|
|
534
|
-
transaction => {
|
|
535
|
-
transaction.executeSql(sql, sqlValues, (/**
|
|
536
|
-
* @param {?} transaction
|
|
537
|
-
* @param {?} result
|
|
538
|
-
* @return {?}
|
|
539
|
-
*/
|
|
540
|
-
(transaction, result) => {
|
|
263
|
+
return new Promise((resolve, reject) => {
|
|
264
|
+
this.db.transaction(transaction => {
|
|
265
|
+
transaction.executeSql(sql, sqlValues, (transaction, result) => {
|
|
541
266
|
resolve(result);
|
|
542
|
-
}
|
|
543
|
-
* @param {?} transaction
|
|
544
|
-
* @param {?} error
|
|
545
|
-
* @return {?}
|
|
546
|
-
*/
|
|
547
|
-
(transaction, error) => {
|
|
267
|
+
}, (transaction, error) => {
|
|
548
268
|
reject(error);
|
|
549
|
-
})
|
|
550
|
-
})
|
|
551
|
-
})
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
});
|
|
552
272
|
}
|
|
553
|
-
/**
|
|
554
|
-
* @param {?} tables
|
|
555
|
-
* @return {?}
|
|
556
|
-
*/
|
|
557
273
|
dropTable(tables) {
|
|
558
|
-
/** @type {?} */
|
|
559
274
|
let cont = 1;
|
|
560
275
|
cont = tables.length;
|
|
561
|
-
return new Promise((
|
|
562
|
-
|
|
563
|
-
* @param {?} reject
|
|
564
|
-
* @return {?}
|
|
565
|
-
*/
|
|
566
|
-
(resolve, reject) => {
|
|
567
|
-
tables.some((/**
|
|
568
|
-
* @param {?} table
|
|
569
|
-
* @return {?}
|
|
570
|
-
*/
|
|
571
|
-
table => {
|
|
276
|
+
return new Promise((resolve, reject) => {
|
|
277
|
+
tables.some(table => {
|
|
572
278
|
if (cont !== 0) {
|
|
573
279
|
this.query('DROP TABLE ' + table)
|
|
574
|
-
.then((
|
|
575
|
-
* @return {?}
|
|
576
|
-
*/
|
|
577
|
-
() => {
|
|
280
|
+
.then(() => {
|
|
578
281
|
if (cont === 1) {
|
|
579
282
|
resolve();
|
|
580
283
|
}
|
|
581
284
|
else {
|
|
582
285
|
cont--;
|
|
583
286
|
}
|
|
584
|
-
}
|
|
585
|
-
* @param {?} err
|
|
586
|
-
* @return {?}
|
|
587
|
-
*/
|
|
588
|
-
err => {
|
|
287
|
+
}, err => {
|
|
589
288
|
cont = 0;
|
|
590
289
|
reject(err);
|
|
591
290
|
return true;
|
|
592
|
-
})
|
|
291
|
+
});
|
|
593
292
|
return false;
|
|
594
293
|
}
|
|
595
|
-
})
|
|
596
|
-
})
|
|
294
|
+
});
|
|
295
|
+
});
|
|
597
296
|
}
|
|
598
|
-
/**
|
|
599
|
-
* @param {?} result
|
|
600
|
-
* @return {?}
|
|
601
|
-
*/
|
|
602
297
|
fetchAll(result) {
|
|
603
|
-
/** @type {?} */
|
|
604
298
|
let output = [];
|
|
605
299
|
for (let i = 0; i < result.rows.length; i++) {
|
|
606
300
|
output.push(result.rows.item(i));
|
|
607
301
|
}
|
|
608
302
|
return output;
|
|
609
303
|
}
|
|
610
|
-
/**
|
|
611
|
-
* @param {?} result
|
|
612
|
-
* @return {?}
|
|
613
|
-
*/
|
|
614
304
|
fetch(result) {
|
|
615
|
-
|
|
616
|
-
let objeto = undefined;
|
|
617
|
-
/** @type {?} */
|
|
618
|
-
let newObj = undefined;
|
|
305
|
+
let objeto = undefined, newObj = undefined;
|
|
619
306
|
if (result.rows.length !== 0) {
|
|
620
307
|
objeto = result.rows.item(0);
|
|
621
308
|
newObj = {};
|
|
622
|
-
/** @type {?} */
|
|
623
309
|
const keys = Object.keys(objeto);
|
|
624
310
|
for (let key in keys) {
|
|
625
311
|
newObj[keys[key]] = objeto[keys[key]];
|
|
@@ -627,21 +313,27 @@ class DbWrapperService {
|
|
|
627
313
|
}
|
|
628
314
|
return newObj;
|
|
629
315
|
}
|
|
630
|
-
/**
|
|
631
|
-
* @param {?} result
|
|
632
|
-
* @return {?}
|
|
633
|
-
*/
|
|
634
316
|
rowsCount(result) {
|
|
635
317
|
return result.rows.length;
|
|
636
318
|
}
|
|
319
|
+
tableExists(table) {
|
|
320
|
+
return this.query('select sql from sqlite_master ' +
|
|
321
|
+
'where type=\'table\' and name=\'' + table + '\'')
|
|
322
|
+
.then(result => (this.rowsCount(result) === 1));
|
|
323
|
+
}
|
|
324
|
+
columnExists(table, column) {
|
|
325
|
+
return this._tableExists(table)
|
|
326
|
+
.then((tableDDL) => {
|
|
327
|
+
if (tableDDL) {
|
|
328
|
+
return this._columnExists(tableDDL, column);
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
return false;
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
}
|
|
637
335
|
/* Private */
|
|
638
|
-
/**
|
|
639
|
-
* @private
|
|
640
|
-
* @param {?} col
|
|
641
|
-
* @return {?}
|
|
642
|
-
*/
|
|
643
336
|
defColumn(col) {
|
|
644
|
-
/** @type {?} */
|
|
645
337
|
let colSql;
|
|
646
338
|
colSql = col.name + ' ' + col.type;
|
|
647
339
|
if (!col.isNullable) {
|
|
@@ -659,17 +351,10 @@ class DbWrapperService {
|
|
|
659
351
|
}
|
|
660
352
|
return colSql;
|
|
661
353
|
}
|
|
662
|
-
/**
|
|
663
|
-
* @private
|
|
664
|
-
* @param {?} unique
|
|
665
|
-
* @return {?}
|
|
666
|
-
*/
|
|
667
354
|
defUnique(unique) {
|
|
668
|
-
/** @type {?} */
|
|
669
355
|
let uniqSql;
|
|
670
356
|
// depende del entorno, producción o desarrollo
|
|
671
|
-
|
|
672
|
-
const onConflictUnique = 'on conflict fail';
|
|
357
|
+
const onConflictUnique = 'on conflict replace';
|
|
673
358
|
if (Array.isArray(unique.fields)) {
|
|
674
359
|
uniqSql = ` CONSTRAINT ${unique.name} UNIQUE ( ${unique.fields.join(',')} ) ${onConflictUnique} `;
|
|
675
360
|
}
|
|
@@ -678,13 +363,7 @@ class DbWrapperService {
|
|
|
678
363
|
}
|
|
679
364
|
return uniqSql;
|
|
680
365
|
}
|
|
681
|
-
/**
|
|
682
|
-
* @private
|
|
683
|
-
* @param {?} fore
|
|
684
|
-
* @return {?}
|
|
685
|
-
*/
|
|
686
366
|
defForeign(fore) {
|
|
687
|
-
/** @type {?} */
|
|
688
367
|
let foreSql;
|
|
689
368
|
if (Array.isArray(fore.foreignKey)) {
|
|
690
369
|
foreSql = ` FOREIGN KEY ( ${fore.foreignKey.join(',')} ) `;
|
|
@@ -700,34 +379,19 @@ class DbWrapperService {
|
|
|
700
379
|
}
|
|
701
380
|
return foreSql;
|
|
702
381
|
}
|
|
703
|
-
|
|
704
|
-
* @private
|
|
705
|
-
* @param {?} table
|
|
706
|
-
* @return {?}
|
|
707
|
-
*/
|
|
708
|
-
tableExists(table) {
|
|
382
|
+
_tableExists(table) {
|
|
709
383
|
return this.query('select sql from sqlite_master ' +
|
|
710
384
|
'where type=\'table\' and name=\'' + table + '\'')
|
|
711
|
-
.then(
|
|
712
|
-
* @param {?} result
|
|
713
|
-
* @return {?}
|
|
714
|
-
*/
|
|
715
|
-
result => {
|
|
385
|
+
.then(result => {
|
|
716
386
|
if (this.rowsCount(result) === 1) {
|
|
717
|
-
return
|
|
387
|
+
return this.fetch(result).sql;
|
|
718
388
|
}
|
|
719
389
|
else {
|
|
720
390
|
return false;
|
|
721
391
|
}
|
|
722
|
-
})
|
|
392
|
+
});
|
|
723
393
|
}
|
|
724
|
-
|
|
725
|
-
* @private
|
|
726
|
-
* @param {?} tableDDL
|
|
727
|
-
* @param {?} column
|
|
728
|
-
* @return {?}
|
|
729
|
-
*/
|
|
730
|
-
columnExists(tableDDL, column) {
|
|
394
|
+
_columnExists(tableDDL, column) {
|
|
731
395
|
if (tableDDL.indexOf(column) === -1) {
|
|
732
396
|
return false;
|
|
733
397
|
}
|
|
@@ -735,27 +399,14 @@ class DbWrapperService {
|
|
|
735
399
|
return true;
|
|
736
400
|
}
|
|
737
401
|
}
|
|
738
|
-
/**
|
|
739
|
-
* @private
|
|
740
|
-
* @param {?} table
|
|
741
|
-
* @param {?} col
|
|
742
|
-
* @return {?}
|
|
743
|
-
*/
|
|
744
402
|
addColumn(table, col) {
|
|
745
|
-
/** @type {?} */
|
|
746
403
|
const colSql = this.defColumn(col);
|
|
747
|
-
/** @type {?} */
|
|
748
404
|
const query = 'alter TABLE ' + table + ' add COLUMN ' + colSql;
|
|
749
405
|
return this.query(query);
|
|
750
406
|
}
|
|
751
407
|
}
|
|
752
|
-
/** @type {?} */
|
|
753
408
|
const DB_CONFIG = new InjectionToken('dbWrapperConfig');
|
|
754
|
-
/** @type {?} */
|
|
755
409
|
const DB_SCHEMA = new InjectionToken('dbWrapperSchema');
|
|
756
|
-
/**
|
|
757
|
-
* @return {?}
|
|
758
|
-
*/
|
|
759
410
|
function getDefaultDbConfig() {
|
|
760
411
|
return {
|
|
761
412
|
dbName: 'db.sqlite',
|
|
@@ -763,30 +414,16 @@ function getDefaultDbConfig() {
|
|
|
763
414
|
iosDatabaseLocation: 'Library'
|
|
764
415
|
};
|
|
765
416
|
}
|
|
766
|
-
/**
|
|
767
|
-
* @param {?} dbConfig
|
|
768
|
-
* @param {?=} dbSchema
|
|
769
|
-
* @return {?}
|
|
770
|
-
*/
|
|
771
417
|
function provideDbWrapper(dbConfig, dbSchema) {
|
|
772
|
-
/** @type {?} */
|
|
773
418
|
const config = !!dbConfig ? dbConfig : getDefaultDbConfig();
|
|
774
419
|
return new DbWrapperService(config, dbSchema);
|
|
775
420
|
}
|
|
776
421
|
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
|
|
780
|
-
*/
|
|
781
|
-
class DbWrapperModule {
|
|
782
|
-
/**
|
|
783
|
-
* @param {?} dbConfig
|
|
784
|
-
* @param {?=} schema
|
|
785
|
-
* @return {?}
|
|
786
|
-
*/
|
|
422
|
+
var DbWrapperModule_1;
|
|
423
|
+
let DbWrapperModule = DbWrapperModule_1 = class DbWrapperModule {
|
|
787
424
|
static forRoot(dbConfig, schema) {
|
|
788
425
|
return {
|
|
789
|
-
ngModule:
|
|
426
|
+
ngModule: DbWrapperModule_1,
|
|
790
427
|
providers: [
|
|
791
428
|
{ provide: DB_CONFIG, useValue: dbConfig },
|
|
792
429
|
{ provide: DB_SCHEMA, useValue: schema },
|
|
@@ -794,70 +431,63 @@ class DbWrapperModule {
|
|
|
794
431
|
]
|
|
795
432
|
};
|
|
796
433
|
}
|
|
797
|
-
}
|
|
798
|
-
DbWrapperModule.decorators = [
|
|
799
|
-
{ type: NgModule }
|
|
800
|
-
];
|
|
801
|
-
|
|
802
|
-
/**
|
|
803
|
-
* @fileoverview added by tsickle
|
|
804
|
-
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
|
|
805
|
-
*/
|
|
806
|
-
/** @enum {string} */
|
|
807
|
-
const DataType = {
|
|
808
|
-
Integer: 'integer',
|
|
809
|
-
Int: 'integer',
|
|
810
|
-
TinyInt: 'integer',
|
|
811
|
-
SmallInt: 'integer',
|
|
812
|
-
BigInt: 'integer',
|
|
813
|
-
Text: 'text',
|
|
814
|
-
Char: 'text',
|
|
815
|
-
NChar: 'text',
|
|
816
|
-
VarChar: 'text',
|
|
817
|
-
NVarChar: 'text',
|
|
818
|
-
CLOB: 'text',
|
|
819
|
-
BLOB: 'blob',
|
|
820
|
-
Real: 'real',
|
|
821
|
-
Double: 'real',
|
|
822
|
-
Decimal: 'real',
|
|
823
|
-
Float: 'real',
|
|
824
|
-
Boolean: 'integer',
|
|
825
|
-
DateTime: 'text',
|
|
826
|
-
Date: 'text',
|
|
827
434
|
};
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
Int: '0',
|
|
832
|
-
TinyInt: '0',
|
|
833
|
-
SmallInt: '0',
|
|
834
|
-
BigInt: '0',
|
|
835
|
-
Text: '\'\'',
|
|
836
|
-
Char: '\'\'',
|
|
837
|
-
NChar: '\'\'',
|
|
838
|
-
VarChar: '\'\'',
|
|
839
|
-
NVarChar: '\'\'',
|
|
840
|
-
CLOB: '\'\'',
|
|
841
|
-
BLOB: '0',
|
|
842
|
-
Real: '0.0',
|
|
843
|
-
Double: '0.0',
|
|
844
|
-
Decimal: '0.0',
|
|
845
|
-
Float: '0.0',
|
|
846
|
-
Boolean: '0',
|
|
847
|
-
DateTime: '(datetime(\'now\', \'localtime\'))',
|
|
848
|
-
Date: '(date(\'now\', \'localtime\'))',
|
|
849
|
-
};
|
|
435
|
+
DbWrapperModule = DbWrapperModule_1 = __decorate([
|
|
436
|
+
NgModule()
|
|
437
|
+
], DbWrapperModule);
|
|
850
438
|
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
439
|
+
var DataType;
|
|
440
|
+
(function (DataType) {
|
|
441
|
+
DataType["Integer"] = "integer";
|
|
442
|
+
DataType["Int"] = "integer";
|
|
443
|
+
DataType["TinyInt"] = "integer";
|
|
444
|
+
DataType["SmallInt"] = "integer";
|
|
445
|
+
DataType["BigInt"] = "integer";
|
|
446
|
+
DataType["Text"] = "text";
|
|
447
|
+
DataType["Char"] = "text";
|
|
448
|
+
DataType["NChar"] = "text";
|
|
449
|
+
DataType["VarChar"] = "text";
|
|
450
|
+
DataType["NVarChar"] = "text";
|
|
451
|
+
DataType["CLOB"] = "text";
|
|
452
|
+
DataType["BLOB"] = "blob";
|
|
453
|
+
DataType["Real"] = "real";
|
|
454
|
+
DataType["Double"] = "real";
|
|
455
|
+
DataType["Decimal"] = "real";
|
|
456
|
+
DataType["Float"] = "real";
|
|
457
|
+
DataType["Boolean"] = "integer";
|
|
458
|
+
DataType["DateTime"] = "text";
|
|
459
|
+
DataType["Date"] = "text";
|
|
460
|
+
})(DataType || (DataType = {}));
|
|
461
|
+
var DefaultType;
|
|
462
|
+
(function (DefaultType) {
|
|
463
|
+
DefaultType["Integer"] = "0";
|
|
464
|
+
DefaultType["Int"] = "0";
|
|
465
|
+
DefaultType["TinyInt"] = "0";
|
|
466
|
+
DefaultType["SmallInt"] = "0";
|
|
467
|
+
DefaultType["BigInt"] = "0";
|
|
468
|
+
DefaultType["Text"] = "''";
|
|
469
|
+
DefaultType["Char"] = "''";
|
|
470
|
+
DefaultType["NChar"] = "''";
|
|
471
|
+
DefaultType["VarChar"] = "''";
|
|
472
|
+
DefaultType["NVarChar"] = "''";
|
|
473
|
+
DefaultType["CLOB"] = "''";
|
|
474
|
+
DefaultType["BLOB"] = "0";
|
|
475
|
+
DefaultType["Real"] = "0.0";
|
|
476
|
+
DefaultType["Double"] = "0.0";
|
|
477
|
+
DefaultType["Decimal"] = "0.0";
|
|
478
|
+
DefaultType["Float"] = "0.0";
|
|
479
|
+
DefaultType["Boolean"] = "0";
|
|
480
|
+
DefaultType["DateTime"] = "(datetime('now', 'localtime'))";
|
|
481
|
+
DefaultType["Date"] = "(date('now', 'localtime'))";
|
|
482
|
+
})(DefaultType || (DefaultType = {}));
|
|
483
|
+
|
|
484
|
+
/*
|
|
485
|
+
* Public API Surface of db-wrapper
|
|
854
486
|
*/
|
|
855
487
|
|
|
856
488
|
/**
|
|
857
|
-
*
|
|
858
|
-
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
|
|
489
|
+
* Generated bundle index. Do not edit.
|
|
859
490
|
*/
|
|
860
491
|
|
|
861
|
-
export {
|
|
862
|
-
|
|
863
|
-
//# sourceMappingURL=db-wrapper.js.map
|
|
492
|
+
export { DB_CONFIG, DB_SCHEMA, DataType, DbWrapperModule, DbWrapperService, DefaultType, getDefaultDbConfig, provideDbWrapper };
|
|
493
|
+
//# sourceMappingURL=db-wrapper.js.map
|