@bbn/bbn 1.0.378 → 1.0.379

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/dist/db.d.ts CHANGED
@@ -11,6 +11,7 @@ declare const db: {
11
11
  _connections: {};
12
12
  _stores: {};
13
13
  ok: boolean;
14
+ updateStructure(storeName: any, structure: any, req: any): void;
14
15
  open(name: any): Promise<unknown>;
15
16
  add(database: string, name: string, structure: Structure): void;
16
17
  };
package/dist/db.js CHANGED
@@ -219,6 +219,38 @@ var dbObject = function (dbName) {
219
219
  var store = tx.objectStore(table);
220
220
  });
221
221
  };
222
+ this.copyTable = function (target, table, fields, where, order, start, limit) {
223
+ if (fields === void 0) { fields = []; }
224
+ if (where === void 0) { where = null; }
225
+ if (order === void 0) { order = null; }
226
+ if (start === void 0) { start = 0; }
227
+ if (limit === void 0) { limit = null; }
228
+ return new Promise(function (resolve) {
229
+ if (!conn.objectStoreNames.contains(target)) {
230
+ db.add(dbName, target, structure[table]);
231
+ }
232
+ if (!conn.objectStoreNames.contains(target)) {
233
+ resolve(0);
234
+ throw new Error(_("The target table %s does not exist", target));
235
+ }
236
+ _this.selectAll(table, fields, where, order, start, limit).then(function (d) {
237
+ if (d.length) {
238
+ _this.insert(target, d).then(function (res) {
239
+ resolve(res);
240
+ });
241
+ }
242
+ else {
243
+ resolve(0);
244
+ }
245
+ });
246
+ });
247
+ };
248
+ this.deleteTable = function (table) {
249
+ return new Promise(function (resolve) {
250
+ conn.deleteObjectStore(table);
251
+ resolve(true);
252
+ });
253
+ };
222
254
  };
223
255
  var db = {
224
256
  _structures: {},
@@ -227,7 +259,24 @@ var db = {
227
259
  /* Address of the CDN (where this file should be hosted) */
228
260
  _stores: {},
229
261
  ok: idb !== undefined,
262
+ updateStructure: function (storeName, structure, req) {
263
+ var primary = structure.keys.PRIMARY.columns.length > 1 ? structure.keys.PRIMARY.columns : structure.keys.PRIMARY.columns[0];
264
+ var stores = req.objectStoreNames;
265
+ if (!stores.contains(storeName)) {
266
+ var store_1 = req.createObjectStore(storeName, { keyPath: primary });
267
+ iterate(structure.keys, function (a, n) {
268
+ if (n !== 'PRIMARY') {
269
+ store_1.createIndex(n, a.columns.length > 1 ? a.columns : a.columns[0], {
270
+ unique: !!a.unique
271
+ });
272
+ }
273
+ });
274
+ }
275
+ else {
276
+ }
277
+ },
230
278
  open: function (name) {
279
+ var _this = this;
231
280
  return new Promise(function (resolve) {
232
281
  if (!db._connections[name]) {
233
282
  if (!db._structures[name]) {
@@ -237,17 +286,9 @@ var db = {
237
286
  var conn_1 = idb.open(name, num);
238
287
  conn_1.onupgradeneeded = function () {
239
288
  log("UPGRADE NEEDED");
240
- var res = conn_1.result;
289
+ var req = conn_1.result;
241
290
  iterate(db._structures[name], function (structure, storeName) {
242
- var primary = structure.keys.PRIMARY.columns.length > 1 ? structure.keys.PRIMARY.columns : structure.keys.PRIMARY.columns[0];
243
- var store = res.createObjectStore(storeName, { keyPath: primary });
244
- iterate(structure.keys, function (a, n) {
245
- if (n !== 'PRIMARY') {
246
- store.createIndex(n, a.columns.length > 1 ? a.columns : a.columns[0], {
247
- unique: !!a.unique
248
- });
249
- }
250
- });
291
+ _this.updateStructure(storeName, structure, req);
251
292
  });
252
293
  };
253
294
  conn_1.onsuccess = function () {
@@ -268,6 +309,9 @@ var db = {
268
309
  db._structures[database] = {};
269
310
  }
270
311
  db._structures[database][name] = structure;
312
+ if (db._connections[database]) {
313
+ this.updateStructure(name, structure, db._connections[database]);
314
+ }
271
315
  }
272
316
  else {
273
317
  throw new Error(_("The database structure for %s is not valid (are there keys and field? Is there a primary?", name));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.378",
3
+ "version": "1.0.379",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",