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