xdriver 2.0.12 → 2.0.15

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/database.d.ts CHANGED
@@ -10,7 +10,7 @@ export default class Database extends Connection {
10
10
  count(table: string, key: IDBValidKey | IDBKeyRange): Promise<number>;
11
11
  insert(table: string, data: Array<Record<string, any>>): Promise<IDBValidKey[]>;
12
12
  update(table: string, modify: Row, key?: IDBValidKey | IDBKeyRange | null): Promise<IDBValidKey>;
13
- delete(table: string, key: IDBValidKey | IDBKeyRange): Promise<Row[]>;
13
+ delete(table: string, key: IDBValidKey | IDBKeyRange): Promise<void>;
14
14
  truncate(table: string): Promise<void>;
15
15
  getPagination<R extends Row>(table: string, pageSize?: number): Promise<Pagination<R>>;
16
16
  scan<R extends Row>(table: string, key?: IDBValidKey | IDBKeyRange, direction?: IDBCursorDirection, indexName?: string): Promise<RowPacket<R>>;
package/lib/mapper.d.ts CHANGED
@@ -1,16 +1,19 @@
1
1
  import Table, { Pagination, Row, RowPacket } from "./table";
2
2
  export default class Mapper<T extends Row> {
3
- private table;
3
+ private _table;
4
4
  constructor(table: Table);
5
+ get table(): Table;
5
6
  truncate(): Promise<void>;
6
7
  count(key?: IDBValidKey | IDBKeyRange): Promise<number>;
7
8
  insert(data: T | Array<T>): Promise<IDBValidKey[]>;
8
9
  update(modify: Row, key?: IDBValidKey | IDBKeyRange | null): Promise<IDBValidKey>;
9
10
  query(key?: IDBValidKey | IDBKeyRange, count?: number): Promise<RowPacket<T>>;
10
11
  queryById(id: any | Array<any>, indexName?: string): Promise<T | Array<T>>;
11
- delete(key: IDBValidKey | IDBKeyRange): Promise<Array<T>>;
12
+ deleteByKeys(...keys: IDBValidKey[]): Promise<void>;
13
+ delete(key: IDBValidKey | IDBKeyRange): Promise<void>;
12
14
  queryByIndex(indexName: string, key?: IDBValidKey | IDBKeyRange, count?: number): Promise<RowPacket<T>>;
13
15
  queryByKey(key: IDBValidKey | IDBKeyRange): Promise<RowPacket<T>>;
16
+ queryByKeys<R extends Row>(keys: IDBValidKey[]): Promise<RowPacket<R>>;
14
17
  scan(key?: IDBValidKey | IDBKeyRange, direction?: IDBCursorDirection, indexName?: string): Promise<RowPacket<T>>;
15
18
  select(key?: IDBValidKey | IDBKeyRange, count?: number, indexName?: string): Promise<RowPacket<T>>;
16
19
  paginate<R extends Row>(pageNo?: number, pageSize?: number, key?: IDBValidKey | IDBKeyRange, indexName?: string): Promise<RowPacket<R>>;
package/lib/mapper.js CHANGED
@@ -1,47 +1,67 @@
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
+ };
1
10
  export default class Mapper {
2
11
  constructor(table) {
3
- this.table = table;
12
+ this._table = table;
13
+ }
14
+ get table() {
15
+ return this._table;
4
16
  }
5
17
  truncate() {
6
- return this.table.truncate();
18
+ return this._table.truncate();
7
19
  }
8
20
  count(key) {
9
- return this.table.count(key);
21
+ return this._table.count(key);
10
22
  }
11
23
  insert(data) {
12
- return this.table.insert(data);
24
+ return this._table.insert(data);
13
25
  }
14
26
  update(modify, key) {
15
- return this.table.update(modify, key);
27
+ return this._table.update(modify, key);
16
28
  }
17
29
  query(key, count) {
18
- return this.table.query(key, count);
30
+ return this._table.query(key, count);
19
31
  }
20
32
  queryById(id, indexName) {
21
- return this.table.queryById(id, indexName);
33
+ return this._table.queryById(id, indexName);
34
+ }
35
+ deleteByKeys(...keys) {
36
+ return this._table.deleteByKeys(...keys);
22
37
  }
23
38
  delete(key) {
24
- return this.table.delete(key);
39
+ return this._table.delete(key);
25
40
  }
26
41
  queryByIndex(indexName, key, count) {
27
- return this.table.queryByIndex(indexName, key, count);
42
+ return this._table.queryByIndex(indexName, key, count);
28
43
  }
29
44
  queryByKey(key) {
30
- return this.table.queryByKey(key);
45
+ return this._table.queryByKey(key);
46
+ }
47
+ queryByKeys(keys) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ return this._table.queryByKeys(keys);
50
+ });
31
51
  }
32
52
  scan(key, direction = 'next', indexName) {
33
- return this.table.scan(key, direction, indexName);
53
+ return this._table.scan(key, direction, indexName);
34
54
  }
35
55
  select(key, count, indexName) {
36
- return this.table.select(key, count, indexName);
56
+ return this._table.select(key, count, indexName);
37
57
  }
38
58
  paginate(pageNo = 1, pageSize = 10, key, indexName) {
39
- return this.table.paginate(pageNo, pageSize, key, indexName);
59
+ return this._table.paginate(pageNo, pageSize, key, indexName);
40
60
  }
41
61
  getPagination(pageSize = 10) {
42
- return this.table.getPagination(pageSize);
62
+ return this._table.getPagination(pageSize);
43
63
  }
44
64
  getAll() {
45
- return this.table.getAllData();
65
+ return this._table.getAllData();
46
66
  }
47
67
  }
package/lib/table.d.ts CHANGED
@@ -15,8 +15,20 @@ export type Rows = Array<Row>;
15
15
  export type TableMeta = ITable & {
16
16
  rows?: Rows;
17
17
  };
18
- export declare class RowPacket<T extends Row> extends Array<T> {
18
+ export declare class RowPacket<T extends Row> {
19
+ private readonly rows;
19
20
  constructor(rows?: Array<T>);
21
+ [Symbol.iterator](): Generator<T, void, unknown>;
22
+ forEach(callback: (value: T, index: number, array: Array<T>) => void): void;
23
+ filter(callback: (value: T, index: number, array: Array<T>) => boolean): Array<T>;
24
+ reduce<U>(callback: (previousValue: U, currentValue: T, currentIndex: number, array: Array<T>) => U, initialValue: U): U;
25
+ map<U>(callback: (value: T, index: number, array: Array<T>) => U): Array<U>;
26
+ some(callback: (value: T, index: number, array: Array<T>) => boolean): boolean;
27
+ find(callback: (value: T, index: number, array: Array<T>) => boolean): T | undefined;
28
+ push(...items: Array<T>): void;
29
+ slice(start?: number, end?: number): Array<T>;
30
+ join(separator?: string): string;
31
+ get length(): number;
20
32
  distinct(column?: string | ((row: T) => any)): Array<T>;
21
33
  mapping<K, T>(keyFn: (row: T, index: number, array: Array<T>) => K): Map<K, T>;
22
34
  mapping<K, V>(keyFn: (row: T, index: number, array: Array<T>) => K, valFn: (row: T, index: number, array: Array<T>) => V): Map<K, V>;
@@ -57,12 +69,14 @@ export default class Table implements ITable {
57
69
  get meta(): TableMeta;
58
70
  insert(data: Row | Rows): Promise<Array<IDBValidKey>>;
59
71
  update(modify: Row, key?: IDBValidKey | IDBKeyRange | null): Promise<IDBValidKey>;
60
- delete<R extends Row>(key: IDBValidKey | IDBKeyRange): Promise<Array<R>>;
72
+ deleteByKeys<R extends Row>(...keys: IDBValidKey[]): Promise<void>;
73
+ delete<R extends Row>(key: IDBValidKey | IDBKeyRange): Promise<void>;
61
74
  truncate(): Promise<void>;
62
75
  dropIndex(name: string): void;
63
76
  keys(key?: IDBValidKey | IDBKeyRange | null, count?: number): Promise<IDBValidKey[]>;
64
77
  count(key?: IDBValidKey | IDBKeyRange): Promise<number>;
65
78
  query<R extends Row>(key?: IDBValidKey | IDBKeyRange, count?: number): Promise<RowPacket<R>>;
79
+ queryByKeys<R extends Row>(keys: IDBValidKey[]): Promise<RowPacket<R>>;
66
80
  queryByIndex<R extends Row>(indexName: string, key?: IDBValidKey | IDBKeyRange, count?: number): Promise<RowPacket<R>>;
67
81
  select<R extends Row>(key?: IDBValidKey | IDBKeyRange, count?: number, indexName?: string): Promise<RowPacket<R>>;
68
82
  queryByKey<R extends Row>(key: IDBValidKey | IDBKeyRange): Promise<RowPacket<R>>;
package/lib/table.js CHANGED
@@ -33,13 +33,48 @@ const _request = (request) => __awaiter(void 0, void 0, void 0, function* () {
33
33
  };
34
34
  });
35
35
  });
36
- export class RowPacket extends Array {
36
+ export class RowPacket {
37
37
  constructor(rows = []) {
38
- super(...rows);
38
+ this.rows = rows;
39
+ }
40
+ *[Symbol.iterator]() {
41
+ for (const item of this.rows) {
42
+ yield item;
43
+ }
44
+ }
45
+ forEach(callback) {
46
+ this.rows.forEach(callback);
47
+ }
48
+ filter(callback) {
49
+ return this.rows.filter(callback);
50
+ }
51
+ reduce(callback, initialValue) {
52
+ return this.rows.reduce(callback, initialValue);
53
+ }
54
+ map(callback) {
55
+ return this.rows.map(callback);
56
+ }
57
+ some(callback) {
58
+ return this.rows.some(callback);
59
+ }
60
+ find(callback) {
61
+ return this.rows.find(callback);
62
+ }
63
+ push(...items) {
64
+ this.rows.push(...items);
65
+ }
66
+ slice(start, end) {
67
+ return this.rows.slice(start, end);
68
+ }
69
+ join(separator) {
70
+ return this.rows.join(separator);
71
+ }
72
+ get length() {
73
+ return this.rows.length;
39
74
  }
40
75
  distinct(column) {
41
76
  if (!column) {
42
- return this;
77
+ return this.rows;
43
78
  }
44
79
  let set = new Set();
45
80
  return this.filter(row => {
@@ -77,7 +112,7 @@ export class RowPacket extends Array {
77
112
  this.forEach((item, index, array) => {
78
113
  var _a;
79
114
  let unionKey = typeof key === 'function' ? key.call(this, item, index, array) : item[key];
80
- let value = valFn ? valFn(item, index, this) : item;
115
+ let value = valFn ? valFn(item, index, this.rows) : item;
81
116
  if (!group.has(unionKey)) {
82
117
  group.set(unionKey, [value]);
83
118
  }
@@ -218,15 +253,24 @@ export default class Table {
218
253
  return _request(store.put(modify, key));
219
254
  });
220
255
  }
256
+ deleteByKeys(...keys) {
257
+ return __awaiter(this, void 0, void 0, function* () {
258
+ if (!keys.length) {
259
+ return Promise.reject('keys is required');
260
+ }
261
+ let objectStore = _getObjectStore(this, 'readwrite');
262
+ for (let key of keys) {
263
+ yield _request(objectStore.delete(key));
264
+ }
265
+ });
266
+ }
221
267
  delete(key) {
222
268
  return __awaiter(this, void 0, void 0, function* () {
223
269
  if (!key) {
224
270
  return Promise.reject('key is required');
225
271
  }
226
272
  let objectStore = _getObjectStore(this, 'readwrite');
227
- let rows = yield _request(objectStore.getAll(key));
228
273
  yield _request(objectStore.delete(key));
229
- return rows;
230
274
  });
231
275
  }
232
276
  truncate() {
@@ -247,6 +291,20 @@ export default class Table {
247
291
  return new RowPacket(rows);
248
292
  });
249
293
  }
294
+ queryByKeys(keys) {
295
+ return __awaiter(this, void 0, void 0, function* () {
296
+ if (!keys.length) {
297
+ return Promise.reject('keys is required');
298
+ }
299
+ let objectStore = _getObjectStore(this);
300
+ let rows = [];
301
+ for (let key of keys) {
302
+ let row = yield _request(objectStore.get(IDBKeyRange.only(key)));
303
+ rows.push(row);
304
+ }
305
+ return new RowPacket(rows);
306
+ });
307
+ }
250
308
  queryByIndex(indexName, key, count) {
251
309
  return __awaiter(this, void 0, void 0, function* () {
252
310
  const store = _getObjectStore(this);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xdriver",
3
- "version": "2.0.12",
3
+ "version": "2.0.15",
4
4
  "description": "A simple driver for IndexDB",
5
5
  "main": "./lib/index",
6
6
  "files": [
package/src/mapper.ts CHANGED
@@ -1,65 +1,77 @@
1
1
  import Table, {Pagination, Row, RowPacket} from "./table";
2
2
 
3
3
  export default class Mapper<T extends Row> {
4
- private table: Table;
4
+ private _table: Table;
5
5
 
6
6
  constructor(table: Table) {
7
- this.table = table;
7
+ this._table = table;
8
+ }
9
+
10
+ get table(): Table {
11
+ return this._table;
8
12
  }
9
13
 
10
14
  truncate() {
11
- return this.table.truncate();
15
+ return this._table.truncate();
12
16
  }
13
17
 
14
18
  count(key?: IDBValidKey | IDBKeyRange) {
15
- return this.table.count(key);
19
+ return this._table.count(key);
16
20
  }
17
21
 
18
22
  insert(data: T | Array<T>) {
19
- return this.table.insert(data);
23
+ return this._table.insert(data);
20
24
  }
21
25
 
22
26
  update(modify: Row, key?: IDBValidKey | IDBKeyRange | null): Promise<IDBValidKey> {
23
- return this.table.update(modify, key);
27
+ return this._table.update(modify, key);
24
28
  }
25
29
 
26
30
  query(key?: IDBValidKey | IDBKeyRange, count?: number): Promise<RowPacket<T>> {
27
- return this.table.query(key, count);
31
+ return this._table.query(key, count);
28
32
  }
29
33
 
30
34
  queryById(id: any | Array<any>, indexName?: string): Promise<T | Array<T>> {
31
- return this.table.queryById(id, indexName);
35
+ return this._table.queryById(id, indexName);
36
+ }
37
+
38
+ deleteByKeys(...keys: IDBValidKey[]): Promise<void> {
39
+ return this._table.deleteByKeys(...keys);
32
40
  }
33
41
 
34
- delete(key: IDBValidKey | IDBKeyRange): Promise<Array<T>> {
35
- return this.table.delete(key);
42
+ delete(key: IDBValidKey | IDBKeyRange): Promise<void> {
43
+ return this._table.delete(key);
36
44
  }
37
45
 
38
46
  queryByIndex(indexName: string, key?: IDBValidKey | IDBKeyRange, count?: number): Promise<RowPacket<T>> {
39
- return this.table.queryByIndex(indexName, key, count);
47
+ return this._table.queryByIndex(indexName, key, count);
40
48
  }
41
49
 
42
50
  queryByKey(key: IDBValidKey | IDBKeyRange): Promise<RowPacket<T>> {
43
- return this.table.queryByKey(key);
51
+ return this._table.queryByKey(key);
52
+ }
53
+
54
+ async queryByKeys<R extends Row>(keys: IDBValidKey[]): Promise<RowPacket<R>> {
55
+ return this._table.queryByKeys(keys);
44
56
  }
45
57
 
46
58
  scan(key?: IDBValidKey | IDBKeyRange, direction: IDBCursorDirection = 'next', indexName?: string): Promise<RowPacket<T>> {
47
- return this.table.scan(key, direction, indexName);
59
+ return this._table.scan(key, direction, indexName);
48
60
  }
49
61
 
50
62
  select(key?: IDBValidKey | IDBKeyRange, count?: number, indexName?: string): Promise<RowPacket<T>> {
51
- return this.table.select(key, count, indexName);
63
+ return this._table.select(key, count, indexName);
52
64
  }
53
65
 
54
66
  paginate<R extends Row>(pageNo: number = 1, pageSize: number = 10, key?: IDBValidKey | IDBKeyRange, indexName?: string): Promise<RowPacket<R>> {
55
- return this.table.paginate(pageNo, pageSize, key, indexName);
67
+ return this._table.paginate(pageNo, pageSize, key, indexName);
56
68
  }
57
69
 
58
70
  getPagination(pageSize: number = 10): Promise<Pagination<T>> {
59
- return this.table.getPagination(pageSize);
71
+ return this._table.getPagination(pageSize);
60
72
  }
61
73
 
62
74
  getAll(): Promise<Array<T>> {
63
- return this.table.getAllData();
75
+ return this._table.getAllData();
64
76
  }
65
77
  }
package/src/table.ts CHANGED
@@ -49,10 +49,56 @@ export type Rows = Array<Row>;
49
49
 
50
50
  export type TableMeta = ITable & { rows?: Rows };
51
51
 
52
- export class RowPacket<T extends Row> extends Array<T> {
52
+ export class RowPacket<T extends Row> {
53
+
54
+ private readonly rows: Array<T>;
53
55
 
54
56
  constructor(rows: Array<T> = []) {
55
- super(...rows)
57
+ this.rows = rows;
58
+ }
59
+
60
+ *[Symbol.iterator]() {
61
+ // 使用 generator 函数最简单!
62
+ for (const item of this.rows) {
63
+ yield item;
64
+ }
65
+ }
66
+
67
+ forEach(callback: (value: T, index: number, array: Array<T>) => void) {
68
+ this.rows.forEach(callback);
69
+ }
70
+
71
+ filter(callback: (value: T, index: number, array: Array<T>) => boolean): Array<T> {
72
+ return this.rows.filter(callback);
73
+ }
74
+
75
+ reduce<U>(callback: (previousValue: U, currentValue: T, currentIndex: number, array: Array<T>) => U,
76
+ initialValue: U): U {
77
+ return this.rows.reduce(callback, initialValue);
78
+ }
79
+
80
+ map<U>(callback: (value: T, index: number, array: Array<T>) => U): Array<U> {
81
+ return this.rows.map(callback);
82
+ }
83
+
84
+ some(callback: (value: T, index: number, array: Array<T>) => boolean): boolean {
85
+ return this.rows.some(callback);
86
+ }
87
+ find(callback: (value: T, index: number, array: Array<T>) => boolean): T | undefined {
88
+ return this.rows.find(callback);
89
+ }
90
+ push(...items: Array<T>) {
91
+ this.rows.push(...items);
92
+ }
93
+ slice(start?: number, end?: number): Array<T> {
94
+ return this.rows.slice(start, end);
95
+ }
96
+ join(separator?: string): string {
97
+ return this.rows.join(separator);
98
+ }
99
+
100
+ get length(): number {
101
+ return this.rows.length;
56
102
  }
57
103
 
58
104
  /**
@@ -61,7 +107,7 @@ export class RowPacket<T extends Row> extends Array<T> {
61
107
  */
62
108
  distinct(column?: string | ((row: T) => any)): Array<T> {
63
109
  if (!column) {
64
- return this;
110
+ return this.rows;
65
111
  }
66
112
  let set = new Set<any>();
67
113
  return this.filter(row => {
@@ -111,7 +157,7 @@ export class RowPacket<T extends Row> extends Array<T> {
111
157
  let group = new Map<K, Array<V | T>>();
112
158
  this.forEach((item, index, array) => {
113
159
  let unionKey: K = typeof key === 'function' ? key.call(this, item, index, array) : item[key];
114
- let value = valFn ? valFn(item, index, this) : item;
160
+ let value = valFn ? valFn(item, index, this.rows) : item;
115
161
  if (!group.has(unionKey)) {
116
162
  group.set(unionKey, [value]);
117
163
  } else {
@@ -346,18 +392,25 @@ export default class Table implements ITable{
346
392
  return _request(store.put(modify, key as IDBValidKey));
347
393
  }
348
394
 
395
+ async deleteByKeys<R extends Row>(...keys: IDBValidKey[]): Promise<void> {
396
+ if (!keys.length) {
397
+ return Promise.reject('keys is required');
398
+ }
399
+ let objectStore = _getObjectStore(this, 'readwrite');
400
+ for (let key of keys) {
401
+ await _request(objectStore.delete(key));
402
+ }
403
+ }
349
404
  /**
350
405
  * 根据主键值删除
351
406
  * @param key
352
407
  */
353
- async delete<R extends Row>(key: IDBValidKey | IDBKeyRange): Promise<Array<R>> {
408
+ async delete<R extends Row>(key: IDBValidKey | IDBKeyRange): Promise<void> {
354
409
  if (!key) {
355
410
  return Promise.reject('key is required');
356
411
  }
357
412
  let objectStore = _getObjectStore(this, 'readwrite');
358
- let rows = await _request(objectStore.getAll(key));
359
413
  await _request(objectStore.delete(key));
360
- return rows;
361
414
  }
362
415
 
363
416
  /**
@@ -402,6 +455,19 @@ export default class Table implements ITable{
402
455
  return new RowPacket<R>(rows);
403
456
  }
404
457
 
458
+ async queryByKeys<R extends Row>(keys: IDBValidKey[]): Promise<RowPacket<R>> {
459
+ if (!keys.length) {
460
+ return Promise.reject('keys is required');
461
+ }
462
+ let objectStore = _getObjectStore(this);
463
+ let rows: Array<R> = []
464
+ for (let key of keys) {
465
+ let row = await _request(objectStore.get(IDBKeyRange.only(key)));
466
+ rows.push(row)
467
+ }
468
+ return new RowPacket(rows);
469
+ }
470
+
405
471
  /**
406
472
  *
407
473
  * @param indexName 索引名称
@@ -473,7 +539,7 @@ export default class Table implements ITable{
473
539
  * @param direction
474
540
  * @param indexName
475
541
  */
476
- async scan<R extends Row>(key?: IDBValidKey | IDBKeyRange, direction: IDBCursorDirection = 'next', indexName?: string): Promise<RowPacket<R>> {
542
+ async scan<R extends Row>(key?: IDBValidKey | IDBKeyRange, direction: IDBCursorDirection = 'next', indexName?: string): Promise<RowPacket<R>> {
477
543
  const store = _getObjectStore(this)
478
544
  let request: IDBRequest<IDBCursorWithValue | null>
479
545
  if (!indexName) {