xdriver 2.0.1 → 2.0.3

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/lib/table.js CHANGED
@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { _copy, _instance, Logger } from "./com";
10
+ import { Logger } from "./com";
11
11
  const logger = Logger.getLogger('Table');
12
12
  const _getObjectStore = (table, mode = 'readonly') => {
13
13
  var _a;
@@ -33,19 +33,163 @@ const _request = (request) => __awaiter(void 0, void 0, void 0, function* () {
33
33
  };
34
34
  });
35
35
  });
36
+ export class RowPacket extends Array {
37
+ constructor(rows = []) {
38
+ super(...rows);
39
+ }
40
+ distinct(column) {
41
+ if (!column) {
42
+ return this;
43
+ }
44
+ let set = new Set();
45
+ return this.filter(row => {
46
+ let union = null;
47
+ if (column !== void 0) {
48
+ union = typeof column === 'function' ? column(row) : row[column];
49
+ }
50
+ else {
51
+ union = row;
52
+ }
53
+ if (!set.has(union)) {
54
+ set.add(union);
55
+ return true;
56
+ }
57
+ return false;
58
+ });
59
+ }
60
+ mapping(keyFn, valFn) {
61
+ if (!keyFn)
62
+ throw new Error('Undefined group by field');
63
+ let mapping = new Map();
64
+ this.forEach((item, index, array) => {
65
+ let unionKey = keyFn.call(this, item, index, array);
66
+ if (mapping.has(unionKey)) {
67
+ throw new Error(`${unionKey} duplicate error`);
68
+ }
69
+ mapping.set(unionKey, valFn ? valFn(item, index, array) : item);
70
+ });
71
+ return mapping;
72
+ }
73
+ group(key, valFn) {
74
+ if (!key)
75
+ throw new Error('Undefined group by field');
76
+ let group = new Map();
77
+ this.forEach((item, index, array) => {
78
+ var _a;
79
+ let unionKey = typeof key === 'function' ? key.call(this, item, index, array) : item[key];
80
+ let value = valFn ? valFn(item, index, this) : item;
81
+ if (!group.has(unionKey)) {
82
+ group.set(unionKey, [value]);
83
+ }
84
+ else {
85
+ (_a = group.get(unionKey)) === null || _a === void 0 ? void 0 : _a.push(value);
86
+ }
87
+ });
88
+ return group;
89
+ }
90
+ max(keyFn) {
91
+ let max = 0;
92
+ let maxRows = [];
93
+ this.forEach((item, index, array) => {
94
+ let unionKey = typeof keyFn === 'function' ? keyFn.call(this, item, index, array) : item[keyFn];
95
+ if (unionKey > max) {
96
+ max = unionKey;
97
+ maxRows = [item];
98
+ }
99
+ else if (unionKey === max) {
100
+ maxRows.push(item);
101
+ }
102
+ });
103
+ return {
104
+ value: max,
105
+ rows: maxRows
106
+ };
107
+ }
108
+ min(keyFn) {
109
+ let min = 0;
110
+ let minRows = [];
111
+ this.forEach((item, index, array) => {
112
+ let unionKey = typeof keyFn === 'function' ? keyFn.call(this, item, index, array) : item[keyFn];
113
+ if (unionKey < min) {
114
+ min = unionKey;
115
+ minRows = [item];
116
+ }
117
+ else if (unionKey === min) {
118
+ minRows.push(item);
119
+ }
120
+ });
121
+ return {
122
+ value: min,
123
+ rows: minRows
124
+ };
125
+ }
126
+ sum(keyFn) {
127
+ return this.reduce((sum, item, index, array) => {
128
+ return sum + (typeof keyFn === 'function' ? keyFn.call(this, item, index, array) : item[keyFn]);
129
+ }, 0);
130
+ }
131
+ average(keyFn) {
132
+ return this.sum(keyFn) / this.length;
133
+ }
134
+ }
135
+ export class Pagination {
136
+ constructor(data, pageSize = 10, pageIndex = 1) {
137
+ this._data = data;
138
+ this._pageSize = pageSize < 1 ? 10 : pageSize;
139
+ this._pageIndex = pageIndex < 0 ? 1 : pageIndex;
140
+ this._total = data.length;
141
+ }
142
+ get totalPage() {
143
+ return Math.ceil(this._total / this._pageSize);
144
+ }
145
+ get total() {
146
+ return this._total;
147
+ }
148
+ get pageIndex() {
149
+ return this._pageIndex;
150
+ }
151
+ get pageSize() {
152
+ return this._pageSize;
153
+ }
154
+ get hasNext() {
155
+ return this._pageIndex * this._pageSize < this._total;
156
+ }
157
+ get hasPrev() {
158
+ return this._pageIndex > 1;
159
+ }
160
+ get data() {
161
+ let start = (this._pageIndex - 1) * this._pageSize;
162
+ let end = start + this._pageSize;
163
+ return this._data.slice(start, end);
164
+ }
165
+ prev() {
166
+ if (this.hasPrev) {
167
+ this._pageIndex--;
168
+ return true;
169
+ }
170
+ return false;
171
+ }
172
+ next() {
173
+ if (this.hasNext) {
174
+ this._pageIndex++;
175
+ return true;
176
+ }
177
+ return false;
178
+ }
179
+ }
36
180
  export default class Table {
37
181
  constructor(table, database) {
38
182
  this.primaryKey = "id";
39
183
  this.autoIncrement = false;
40
184
  this.indexes = [];
41
- let metadata;
185
+ let tableMeta;
42
186
  if (typeof table === 'string') {
43
- metadata = { name: table };
187
+ tableMeta = { name: table };
44
188
  }
45
189
  else {
46
- metadata = Object.assign({}, table);
190
+ tableMeta = Object.assign({}, table);
47
191
  }
48
- const { name = '', primaryKey = 'id', autoIncrement = true, indexes = [] } = metadata;
192
+ const { name = '', primaryKey = 'id', autoIncrement = true, indexes = [] } = tableMeta;
49
193
  this.name = name;
50
194
  this.primaryKey = primaryKey;
51
195
  this.autoIncrement = autoIncrement;
@@ -54,6 +198,14 @@ export default class Table {
54
198
  return Object.assign({ unique: false, multiEntry: false }, idx);
55
199
  });
56
200
  }
201
+ get meta() {
202
+ return {
203
+ name: this.name,
204
+ primaryKey: this.primaryKey,
205
+ autoIncrement: this.autoIncrement,
206
+ indexes: this.indexes
207
+ };
208
+ }
57
209
  insert(data) {
58
210
  return __awaiter(this, void 0, void 0, function* () {
59
211
  const store = _getObjectStore(this, 'readwrite');
@@ -66,34 +218,12 @@ export default class Table {
66
218
  return results;
67
219
  });
68
220
  }
69
- update(modify, key, count) {
221
+ update(modify, key) {
70
222
  return __awaiter(this, void 0, void 0, function* () {
71
223
  const store = _getObjectStore(this, 'readwrite');
72
- if (!key || !_instance(key, IDBKeyRange)) {
73
- return _request(store.put(modify, key));
74
- }
75
- let rows = yield _request(store.getAll(key, count));
76
- rows = _instance(rows, Array) ? rows : [rows];
77
- let results = [];
78
- for (let row of rows) {
79
- let rs = yield _request(store.put(_copy(modify, row)));
80
- results.push(rs);
81
- }
82
- return results;
224
+ return _request(store.put(modify, key));
83
225
  });
84
226
  }
85
- merge(row) {
86
- let { primaryKey } = this;
87
- let keys = Array.isArray(primaryKey) ? primaryKey : [primaryKey];
88
- let keyArray = [];
89
- for (let key of keys) {
90
- keyArray.push(row[key]);
91
- }
92
- if (keyArray.length) {
93
- return this.update(row, keyArray);
94
- }
95
- return this.insert(row);
96
- }
97
227
  delete(key) {
98
228
  return __awaiter(this, void 0, void 0, function* () {
99
229
  if (!key) {
@@ -105,7 +235,7 @@ export default class Table {
105
235
  return rows;
106
236
  });
107
237
  }
108
- clear() {
238
+ truncate() {
109
239
  return _request(_getObjectStore(this, 'readwrite').clear());
110
240
  }
111
241
  dropIndex(name) {
@@ -118,66 +248,90 @@ export default class Table {
118
248
  return _request(_getObjectStore(this).count(key));
119
249
  }
120
250
  query(key, count) {
121
- return _request(_getObjectStore(this).getAll(key, count));
251
+ return __awaiter(this, void 0, void 0, function* () {
252
+ let rows = yield _request(_getObjectStore(this).getAll(key, count));
253
+ return new RowPacket(rows);
254
+ });
255
+ }
256
+ queryByIndex(indexName, key, count) {
257
+ return __awaiter(this, void 0, void 0, function* () {
258
+ const store = _getObjectStore(this);
259
+ const index = store.index(indexName);
260
+ let rows = yield _request(index.getAll(key || null, count));
261
+ return new RowPacket(rows);
262
+ });
122
263
  }
123
- select(key, count, index) {
124
- if (!index) {
264
+ select(key, count, indexName) {
265
+ if (!indexName) {
125
266
  return this.query(key, count);
126
267
  }
127
- return this.queryByIndex(index, key, count);
268
+ return this.queryByIndex(indexName, key, count);
128
269
  }
129
270
  queryByKey(key) {
130
- return _request(_getObjectStore(this).get(key));
131
- }
132
- fetch(key, direction) {
133
271
  return __awaiter(this, void 0, void 0, function* () {
272
+ let rows = yield _request(_getObjectStore(this).get(key));
273
+ return new RowPacket(rows);
274
+ });
275
+ }
276
+ paginate() {
277
+ return __awaiter(this, arguments, void 0, function* (pageNo = 1, pageSize = 10, key, indexName) {
134
278
  const store = _getObjectStore(this);
135
- let cursor = yield _request(store.openCursor(key, direction));
136
- if (!cursor) {
137
- return [];
279
+ let request;
280
+ let count = pageSize * pageNo;
281
+ if (indexName) {
282
+ const index = store.index(indexName);
283
+ request = index.getAll(key, count);
138
284
  }
139
- let rows = [];
140
- while (cursor.value) {
141
- rows.push(cursor.value);
142
- cursor.continue();
285
+ else {
286
+ request = store.getAll(key, count);
143
287
  }
144
- return rows;
288
+ let rows = yield _request(request);
289
+ return new RowPacket(rows.slice(pageSize * (pageNo - 1), pageSize * pageNo));
145
290
  });
146
291
  }
147
- queryByIndex(indexName, key, count) {
148
- const store = _getObjectStore(this);
149
- const index = store.index(indexName);
150
- return _request(index.getAll(key || null, count));
151
- }
152
- deplete(key_1) {
153
- return __awaiter(this, arguments, void 0, function* (key, direction = 'next') {
154
- const store = _getObjectStore(this, 'readwrite');
155
- const cursor = yield _request(store.openCursor(key, direction));
156
- if (!cursor) {
157
- return [];
158
- }
159
- let rows = [];
160
- while (cursor.value) {
161
- rows.push(cursor.value);
162
- cursor.continue();
163
- }
164
- return rows;
292
+ queryById(id, indexName) {
293
+ return __awaiter(this, void 0, void 0, function* () {
294
+ const [result] = yield this.select(IDBKeyRange.only(id), 1, indexName);
295
+ return result;
165
296
  });
166
297
  }
167
- multiple(indexName_1, key_1) {
168
- return __awaiter(this, arguments, void 0, function* (indexName, key, direction = 'next') {
298
+ scan(key_1) {
299
+ return __awaiter(this, arguments, void 0, function* (key, direction = 'next', indexName) {
169
300
  const store = _getObjectStore(this);
170
- const index = store.index(indexName);
171
- const cursor = yield _request(index.openCursor(key, direction));
172
- if (!cursor) {
173
- return [];
301
+ let request;
302
+ if (!indexName) {
303
+ request = store.openCursor(key, direction);
174
304
  }
175
- let rows = [];
176
- while (cursor.value) {
177
- rows.push(cursor.value);
178
- cursor.continue();
305
+ else {
306
+ request = store.index(indexName).openCursor(key, direction);
179
307
  }
180
- return rows;
308
+ return new Promise((resolve, reject) => {
309
+ let packets = new RowPacket([]);
310
+ request.onsuccess = function (event) {
311
+ const req = event.target;
312
+ const cursor = req.result;
313
+ if (cursor) {
314
+ packets.push(cursor.value);
315
+ cursor.continue();
316
+ }
317
+ else {
318
+ resolve(packets);
319
+ }
320
+ };
321
+ request.onerror = function (event) {
322
+ reject(event);
323
+ };
324
+ });
325
+ });
326
+ }
327
+ getAllData() {
328
+ const store = _getObjectStore(this);
329
+ return _request(store.getAll());
330
+ }
331
+ getPagination() {
332
+ return __awaiter(this, arguments, void 0, function* (pageSize = 10, pageIndex = 1) {
333
+ const rows = yield this.getAllData();
334
+ return new Pagination(rows, pageSize, pageIndex);
181
335
  });
182
336
  }
183
337
  }
@@ -1 +1 @@
1
- {"root":["../src/com.ts","../src/const.ts","../src/database.ts","../src/driver.ts","../src/index.ts","../src/table.ts"],"version":"5.9.3"}
1
+ {"root":["../src/com.ts","../src/connection.ts","../src/const.ts","../src/database.ts","../src/index.ts","../src/table.ts"],"version":"5.9.3"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xdriver",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "A simple driver for IndexDB",
5
5
  "main": "./lib/index",
6
6
  "files": [
package/lib/driver.d.ts DELETED
@@ -1,14 +0,0 @@
1
- import Database from "./database";
2
- import { Row, Rows } from "./table";
3
- export default class Driver extends Database {
4
- insert(table: string, data: Array<Record<string, any>>): Promise<any[]> | undefined;
5
- query(table: string, query?: IDBValidKey | IDBKeyRange, count?: number): Promise<Rows> | undefined;
6
- select(table: string, key?: IDBValidKey | IDBKeyRange, index?: string, count?: number): Promise<Rows> | undefined;
7
- selectByKey(table: string, key: IDBValidKey | IDBKeyRange): Promise<Row> | undefined;
8
- count(table: string, key: IDBValidKey | IDBKeyRange): Promise<number> | undefined;
9
- update(table: string, modify: Row, key?: IDBValidKey | IDBKeyRange | null, count?: number): Promise<IDBValidKey> | undefined;
10
- merge(table: string, row: Row | Rows): Promise<IDBValidKey> | Promise<any[]> | undefined;
11
- delete(table: string, key: IDBValidKey | IDBKeyRange): Promise<any[]> | undefined;
12
- truncate(table: string): Promise<undefined> | undefined;
13
- static connect(database: string, version: number | undefined, initialize: (driver: Driver) => void): Promise<Driver>;
14
- }
package/lib/driver.js DELETED
@@ -1,59 +0,0 @@
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
- import Database from "./database";
11
- export default class Driver extends Database {
12
- insert(table, data) {
13
- var _a;
14
- return (_a = this.table(table)) === null || _a === void 0 ? void 0 : _a.insert(data);
15
- }
16
- query(table, query, count = 1000) {
17
- var _a;
18
- return (_a = this.table(table)) === null || _a === void 0 ? void 0 : _a.query(query, count);
19
- }
20
- select(table, key, index, count = 1000) {
21
- var _a;
22
- return (_a = this.table(table)) === null || _a === void 0 ? void 0 : _a.select(key, count, index);
23
- }
24
- selectByKey(table, key) {
25
- var _a;
26
- return (_a = this.table(table)) === null || _a === void 0 ? void 0 : _a.queryByKey(key);
27
- }
28
- count(table, key) {
29
- var _a;
30
- return (_a = this.table(table)) === null || _a === void 0 ? void 0 : _a.count(key);
31
- }
32
- update(table, modify, key, count) {
33
- var _a;
34
- return (_a = this.table(table)) === null || _a === void 0 ? void 0 : _a.update(modify, key, count);
35
- }
36
- merge(table, row) {
37
- var _a;
38
- return (_a = this.table(table)) === null || _a === void 0 ? void 0 : _a.merge(row);
39
- }
40
- delete(table, key) {
41
- var _a;
42
- return (_a = this.table(table)) === null || _a === void 0 ? void 0 : _a.delete(key);
43
- }
44
- truncate(table) {
45
- var _a;
46
- return (_a = this.table(table)) === null || _a === void 0 ? void 0 : _a.clear();
47
- }
48
- static connect(database_1) {
49
- return __awaiter(this, arguments, void 0, function* (database, version = 1, initialize) {
50
- const driver = new Driver(database, version);
51
- driver.on('change', () => {
52
- initialize(driver);
53
- });
54
- yield driver.connect();
55
- return driver;
56
- });
57
- }
58
- ;
59
- }