@bbn/bbn 1.0.377 → 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
@@ -1,3 +1,12 @@
1
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
2
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
3
+ if (ar || !(i in from)) {
4
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
5
+ ar[i] = from[i];
6
+ }
7
+ }
8
+ return to.concat(ar || Array.prototype.slice.call(from));
9
+ };
1
10
  import _ from './_.js';
2
11
  import each from './fn/loop/each.js';
3
12
  import iterate from './fn/loop/iterate.js';
@@ -210,6 +219,38 @@ var dbObject = function (dbName) {
210
219
  var store = tx.objectStore(table);
211
220
  });
212
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
+ };
213
254
  };
214
255
  var db = {
215
256
  _structures: {},
@@ -218,26 +259,36 @@ var db = {
218
259
  /* Address of the CDN (where this file should be hosted) */
219
260
  _stores: {},
220
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
+ },
221
278
  open: function (name) {
279
+ var _this = this;
222
280
  return new Promise(function (resolve) {
223
281
  if (!db._connections[name]) {
224
282
  if (!db._structures[name]) {
225
283
  throw new Error(_("Impossible to find a structure for the database %s", name));
226
284
  }
227
- var conn_1 = idb.open(name);
285
+ var num = Math.max.apply(Math, __spreadArray([1], Object.keys(db._structures[name]).map(function (a) { return db._structures[name][a].num || 1; }), false));
286
+ var conn_1 = idb.open(name, num);
228
287
  conn_1.onupgradeneeded = function () {
229
288
  log("UPGRADE NEEDED");
230
- var res = conn_1.result;
289
+ var req = conn_1.result;
231
290
  iterate(db._structures[name], function (structure, storeName) {
232
- var primary = structure.keys.PRIMARY.columns.length > 1 ? structure.keys.PRIMARY.columns : structure.keys.PRIMARY.columns[0];
233
- var store = res.createObjectStore(storeName, { keyPath: primary });
234
- iterate(structure.keys, function (a, n) {
235
- if (n !== 'PRIMARY') {
236
- store.createIndex(n, a.columns.length > 1 ? a.columns : a.columns[0], {
237
- unique: !!a.unique
238
- });
239
- }
240
- });
291
+ _this.updateStructure(storeName, structure, req);
241
292
  });
242
293
  };
243
294
  conn_1.onsuccess = function () {
@@ -258,6 +309,9 @@ var db = {
258
309
  db._structures[database] = {};
259
310
  }
260
311
  db._structures[database][name] = structure;
312
+ if (db._connections[database]) {
313
+ this.updateStructure(name, structure, db._connections[database]);
314
+ }
261
315
  }
262
316
  else {
263
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.377",
3
+ "version": "1.0.379",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",