@bbn/bbn 1.0.370 → 1.0.372

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.js CHANGED
@@ -1,55 +1,47 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- var __generator = (this && this.__generator) || function (thisArg, body) {
11
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
12
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
- function verb(n) { return function (v) { return step([n, v]); }; }
14
- function step(op) {
15
- if (f) throw new TypeError("Generator is already executing.");
16
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
- if (y = 0, t) op = [op[0] & 2, t.value];
19
- switch (op[0]) {
20
- case 0: case 1: t = op; break;
21
- case 4: _.label++; return { value: op[1], done: false };
22
- case 5: _.label++; y = op[1]; op = [0]; continue;
23
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
- default:
25
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
- if (t[2]) _.ops.pop();
30
- _.trys.pop(); continue;
31
- }
32
- op = body.call(thisArg, _);
33
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
- }
36
- };
37
1
  import _ from './_.js';
38
2
  import each from './fn/loop/each.js';
39
3
  import iterate from './fn/loop/iterate.js';
40
4
  import log from './fn/browser/log.js';
41
5
  var idb = window['indexedDB'] || window['webkitIndexedDB'] || window['mozIndexedDB'] || window['OIndexedDB'] || window['msIndexedDB'];
6
+ var transformResult = function (obj, fields) {
7
+ if (fields === null || fields === void 0 ? void 0 : fields.length) {
8
+ var res_1 = {};
9
+ iterate(obj, function (v, n) {
10
+ if (fields.indexOf(n) > -1) {
11
+ res_1[n] = v;
12
+ }
13
+ });
14
+ return res_1;
15
+ }
16
+ return obj;
17
+ };
42
18
  var dbObject = function (dbName) {
43
19
  var _this = this;
44
20
  var conn = db._connections[dbName];
45
21
  var structure = db._structures[dbName];
22
+ var lastError = null;
23
+ var onError = function (req, resolve) {
24
+ req.onerror = function () {
25
+ lastError = req.error;
26
+ log(req.error);
27
+ resolve(req.error);
28
+ };
29
+ };
30
+ var getStore = function (table, mode) {
31
+ var tx = conn.transaction([table], mode.toLowerCase().indexOf('r') === 0 ? "readonly" : "readwrite");
32
+ tx.onabort = function () {
33
+ lastError = tx.error;
34
+ throw new Error(tx.error);
35
+ };
36
+ return [tx, tx.objectStore(table)];
37
+ };
38
+ this.lastError = function () { return lastError; };
46
39
  this.insert = function (table, data) {
47
40
  if (!Array.isArray(data)) {
48
41
  data = [data];
49
42
  }
50
43
  return new Promise(function (resolve) {
51
- var tx = conn.transaction(table, "readwrite");
52
- var store = tx.objectStore(table);
44
+ var _a = getStore(table, 'w'), tx = _a[0], store = _a[1];
53
45
  var res = data.length;
54
46
  each(data, function (a) {
55
47
  var request = store.put(a);
@@ -58,9 +50,6 @@ var dbObject = function (dbName) {
58
50
  res--;
59
51
  };
60
52
  });
61
- tx.onabort = function () {
62
- throw new Error(tx.error);
63
- };
64
53
  tx.oncomplete = function () {
65
54
  resolve(res);
66
55
  };
@@ -68,8 +57,7 @@ var dbObject = function (dbName) {
68
57
  };
69
58
  this.update = function (table, data, where) {
70
59
  return new Promise(function (resolve) {
71
- var tx = conn.transaction(table, "readwrite");
72
- var store = tx.objectStore(table);
60
+ var _a = getStore(table, 'w'), tx = _a[0], store = _a[1];
73
61
  var arch = structure[table];
74
62
  var primary = arch.keys.PRIMARY.columns.length > 1 ? arch.keys.PRIMARY.columns : arch.keys.PRIMARY.columns[0];
75
63
  if (!where[primary]) {
@@ -77,14 +65,8 @@ var dbObject = function (dbName) {
77
65
  }
78
66
  var res = 1;
79
67
  data[primary] = where[primary];
80
- var request = store.put(data);
81
- request.onerror = function () {
82
- log(request.error);
83
- res--;
84
- };
85
- tx.onabort = function () {
86
- throw new Error(tx.error);
87
- };
68
+ var req = store.put(data);
69
+ onError(req, resolve);
88
70
  tx.oncomplete = function () {
89
71
  resolve(res);
90
72
  };
@@ -92,22 +74,15 @@ var dbObject = function (dbName) {
92
74
  };
93
75
  this.delete = function (table, where) {
94
76
  return new Promise(function (resolve) {
95
- var tx = conn.transaction(table, "readwrite");
96
- var store = tx.objectStore(table);
77
+ var _a = getStore(table, 'w'), tx = _a[0], store = _a[1];
97
78
  var arch = structure[table];
98
79
  var primary = arch.keys.PRIMARY.columns.length > 1 ? arch.keys.PRIMARY.columns : arch.keys.PRIMARY.columns[0];
99
80
  if (!where[primary]) {
100
81
  throw new Error(_("No "));
101
82
  }
102
83
  var res = 1;
103
- var request = store.delete(where[primary]);
104
- request.onerror = function () {
105
- log(request.error);
106
- res--;
107
- };
108
- tx.onabort = function () {
109
- throw new Error(tx.error);
110
- };
84
+ var req = store.delete(where[primary]);
85
+ onError(req, resolve);
111
86
  tx.oncomplete = function () {
112
87
  resolve(res);
113
88
  };
@@ -115,57 +90,89 @@ var dbObject = function (dbName) {
115
90
  };
116
91
  this.selectOne = function (table, field, where, order, start, limit) {
117
92
  return new Promise(function (resolve) {
118
- _this.select(table, [field], where, order, start, limit).then(function (d) {
93
+ _this.selectAll(table, [field], where, order, start, 1).then(function (d) {
119
94
  var _a;
120
95
  resolve((_a = d[field]) !== null && _a !== void 0 ? _a : undefined);
121
96
  });
122
97
  });
123
98
  };
124
- this.select = function (table, fields, where, order, start, limit) { return __awaiter(_this, void 0, void 0, function () {
125
- var tx, store, arch, primary;
126
- return __generator(this, function (_a) {
127
- tx = conn.transaction(table, "readonly");
128
- store = tx.objectStore(table);
129
- arch = structure[table];
130
- primary = arch.keys.PRIMARY.columns.length > 1 ? arch.keys.PRIMARY.columns : arch.keys.PRIMARY.columns[0];
131
- if (!where[primary]) {
132
- throw new Error(_("No "));
133
- }
134
- return [2 /*return*/, new Promise(function (resolve) {
135
- var req = store.get(where[primary]);
136
- req.onsuccess = function () {
137
- var obj = req.result;
138
- if (fields.length) {
139
- var res_1 = {};
140
- iterate(obj, function (v, n) {
141
- if (fields.indexOf(n) > -1) {
142
- res_1[n] = v;
143
- }
144
- });
145
- return resolve(res_1);
146
- }
147
- else {
148
- resolve(obj);
149
- }
150
- };
151
- })];
99
+ this.select = function (table, fields, where, order, start) {
100
+ return new Promise(function (resolve) {
101
+ _this.selectAll(table, fields, where, order, start, 1).then(function (d) {
102
+ resolve(d.length ? d[0] : null);
103
+ });
152
104
  });
153
- }); };
105
+ };
154
106
  this.selectAll = function (table, fields, where, order, start, limit) {
155
- var tx = conn.transaction(table, "readonly");
156
- var store = tx.objectStore(table);
157
- var arch = structure[table];
158
- var primary = arch.keys.PRIMARY.columns.length > 1 ? arch.keys.PRIMARY.columns : arch.keys.PRIMARY.columns[0];
159
- if (!where[primary]) {
160
- throw new Error(_("No "));
161
- }
107
+ if (fields === void 0) { fields = []; }
108
+ if (where === void 0) { where = null; }
109
+ if (order === void 0) { order = null; }
110
+ if (start === void 0) { start = 0; }
111
+ if (limit === void 0) { limit = null; }
162
112
  return new Promise(function (resolve) {
163
- var req = store.get(structure.keys.PRIMARY);
113
+ var _a = getStore(table, 'r'), tx = _a[0], store = _a[1];
114
+ var arch = structure[table];
115
+ var primary = arch.keys.PRIMARY.columns.length > 1 ? arch.keys.PRIMARY.columns : arch.keys.PRIMARY.columns[0];
116
+ var search = bbn.fn.isObject(where) ? Object.keys(where)[0] : (where ? primary : null);
117
+ var results = [];
118
+ if (search === primary) {
119
+ if (bbn.fn.isArray(where === null || where === void 0 ? void 0 : where[primary])) {
120
+ var req = [];
121
+ var max_1 = Math.min(where[primary].length, limit || 9999999);
122
+ var _loop_1 = function (i) {
123
+ var getter = store.get(where[primary][i]);
124
+ getter.onsuccess = function () {
125
+ var obj = getter.result;
126
+ results.push(transformResult(obj, fields));
127
+ if (results.length === max_1) {
128
+ resolve(results);
129
+ }
130
+ };
131
+ onError(getter, resolve);
132
+ req.push(getter);
133
+ };
134
+ for (var i = start || 0; i < max_1; i++) {
135
+ _loop_1(i);
136
+ }
137
+ }
138
+ else {
139
+ var req_1 = store.get((where === null || where === void 0 ? void 0 : where[primary]) || where);
140
+ req_1.onsuccess = function () {
141
+ var obj = req_1.result;
142
+ results.push(transformResult(obj, fields));
143
+ resolve(results);
144
+ };
145
+ onError(req_1, resolve);
146
+ }
147
+ }
148
+ else {
149
+ var req = store.openCursor();
150
+ var i_1 = 0;
151
+ req.onsuccess = function (e) {
152
+ var cursor = e.target.result;
153
+ if (cursor) {
154
+ if (!where || !bbn.fn.search([cursor.value], where)) {
155
+ if (i_1 >= start) {
156
+ results.push(transformResult(cursor.value, fields));
157
+ if (results.length === limit) {
158
+ resolve(results);
159
+ }
160
+ }
161
+ i_1++;
162
+ }
163
+ cursor.continue();
164
+ }
165
+ else {
166
+ resolve(results);
167
+ }
168
+ };
169
+ onError(req, resolve);
170
+ }
164
171
  });
165
172
  };
166
173
  this.getColumnValues = function (table, field, where, order, start, limit) {
167
174
  return new Promise(function (resolve) {
168
- var tx = conn.transaction(table, "read");
175
+ var tx = conn.transaction([table], "read");
169
176
  var store = tx.objectStore(table);
170
177
  });
171
178
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.370",
3
+ "version": "1.0.372",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",