xdriver 1.0.11 → 1.1.1

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
@@ -4,6 +4,7 @@ export default class Database {
4
4
  private version;
5
5
  connect: any;
6
6
  private initialized;
7
+ readonly tables: Map<string, Table>;
7
8
  readonly events: any;
8
9
  constructor(name: string, version?: number);
9
10
  initialize(initialized: Function): this;
@@ -12,10 +13,10 @@ export default class Database {
12
13
  open(name?: string | undefined, version?: any): Promise<unknown>;
13
14
  drop(): Promise<unknown>;
14
15
  close(): void;
15
- createTable(tab: string | object, options?: object): Table;
16
- createTables(tables: Array<string | object>, options?: object): Table[];
17
- dropTable(tableName: string): any;
18
- table(tableName: string): Table;
19
- exists(table: string): any;
20
- tables(): any;
16
+ createTable(tableName: string, options?: object): Table;
17
+ createTables(tables: Array<string>, options?: object): Table[];
18
+ dropTable(tableName: string): boolean;
19
+ protected table(tableName: string): Table;
20
+ exists(table: string): boolean;
21
+ getTables(): IterableIterator<Table>;
21
22
  }
package/lib/Database.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { _instance } from "./Utils";
2
2
  import { IndexdbStatus, innerDB } from "./Const";
3
3
  import Table from "./Table";
4
+ import { TableIndex } from "./index";
4
5
  export default class Database {
5
6
  constructor(name, version) {
6
7
  this.version = 1;
@@ -8,13 +9,10 @@ export default class Database {
8
9
  process: (status) => {
9
10
  },
10
11
  success: () => {
11
- console.debug("success");
12
12
  },
13
13
  close: () => {
14
- console.debug("close");
15
14
  },
16
15
  change: () => {
17
- console.debug("change");
18
16
  },
19
17
  error: () => {
20
18
  }
@@ -23,6 +21,7 @@ export default class Database {
23
21
  this.version = version;
24
22
  this.connect = null;
25
23
  this.initialized = () => { };
24
+ this.tables = new Map();
26
25
  }
27
26
  initialize(initialized) {
28
27
  if (this.isOpen()) {
@@ -65,7 +64,22 @@ export default class Database {
65
64
  };
66
65
  return new Promise((resolve, reject) => {
67
66
  request.onsuccess = (e) => {
68
- this.connect = e.target.result;
67
+ let connect = e.target.result;
68
+ let objectStoreNames = connect.objectStoreNames;
69
+ let transaction = connect.transaction(objectStoreNames);
70
+ for (let i = 0; i < objectStoreNames.length; i++) {
71
+ let table = objectStoreNames.item(i);
72
+ let objectStore = transaction.objectStore(table);
73
+ let { indexNames = [], keyPath: primaryKey, name, autoIncrement = true } = objectStore;
74
+ let indexes = [];
75
+ for (let j = 0; j < indexNames.length; j++) {
76
+ let indexName = indexNames.item(j);
77
+ let { keyPath, name, multiEntry, unique } = objectStore.index(indexName);
78
+ indexes.push(new TableIndex(name, keyPath, unique, multiEntry));
79
+ }
80
+ this.tables.set(name, new Table({ name, primaryKey, autoIncrement, indexes }, this));
81
+ }
82
+ this.connect = connect;
69
83
  this.events.process(IndexdbStatus.CONNECT_SUCCESS);
70
84
  this.connect.onerror = this.connect.onabort = (e) => {
71
85
  const error = e.target.error;
@@ -103,39 +117,45 @@ export default class Database {
103
117
  this.connect = null;
104
118
  }
105
119
  }
106
- createTable(tab, options) {
107
- let table = tab;
108
- if (_instance(tab, String)) {
109
- table = Object.assign({ name: tab }, options);
120
+ createTable(tableName, options) {
121
+ if (this.exists(tableName)) {
122
+ throw new Error(`Table ${tableName} is exists`);
110
123
  }
111
- return new Table(table, this);
124
+ let { name = '', primaryKey = 'id', autoIncrement = true, indexes = [], data = [] } = Object.assign({ name: tableName }, options);
125
+ const store = this.connect.createObjectStore(name, { keyPath: primaryKey, autoIncrement });
126
+ let idxes = indexes.map(({ name, column, unique, multiEntry }) => {
127
+ store.createIndex(name, column, { unique, multiEntry });
128
+ return new TableIndex(name, column, unique, multiEntry);
129
+ });
130
+ data.forEach(item => store.add(item));
131
+ let tab = new Table({ name, primaryKey, autoIncrement, indexes: idxes }, this);
132
+ this.tables.set(name, tab);
133
+ return tab;
112
134
  }
113
135
  createTables(tables, options) {
114
136
  return tables.map(table => {
115
- let opts = Object.assign({}, options);
116
- if (_instance(table, String)) {
117
- Object.assign(opts, { name: table });
118
- }
119
- else {
120
- Object.assign(opts, table);
121
- }
122
- return new Table(opts, this);
137
+ return this.createTable(table, options);
123
138
  });
124
139
  }
125
140
  dropTable(tableName) {
126
- return this.connect.deleteObjectStore(tableName);
141
+ if (this.exists(tableName)) {
142
+ this.tables.delete(tableName);
143
+ this.connect.deleteObjectStore(tableName);
144
+ return true;
145
+ }
146
+ return false;
127
147
  }
128
148
  table(tableName) {
129
- if (!this.exists(tableName)) {
130
- throw new Error(`table ${tableName} not exists!`);
149
+ let table = this.tables.get(tableName);
150
+ if (typeof table !== "undefined") {
151
+ return table;
131
152
  }
132
- return new Table({ name: tableName }, this);
153
+ throw new Error(`table ${tableName} not exists!`);
133
154
  }
134
155
  exists(table) {
135
- const tables = this.tables();
136
- return tables && tables.contains(table);
156
+ return this.tables.has(table);
137
157
  }
138
- tables() {
139
- return this.connect['objectStoreNames'];
158
+ getTables() {
159
+ return this.tables.values();
140
160
  }
141
161
  }
package/lib/Driver.d.ts CHANGED
@@ -7,6 +7,7 @@ declare class Driver extends Database {
7
7
  selectByKey(table: string, key: any): any;
8
8
  count(table: string, keyRange?: KeyRange): any;
9
9
  update(table: string, modify: any, where: any): Promise<unknown> | undefined;
10
+ merge(table: string, row: any): Promise<unknown> | undefined;
10
11
  delete(table: string, key: any): any;
11
12
  truncate(table: string): any;
12
13
  }
package/lib/Driver.js CHANGED
@@ -18,6 +18,9 @@ class Driver extends Database {
18
18
  update(table, modify, where) {
19
19
  return this.table(table).update(modify, where);
20
20
  }
21
+ merge(table, row) {
22
+ return this.table(table).merge(row);
23
+ }
21
24
  delete(table, key) {
22
25
  return this.table(table).delete(key);
23
26
  }
package/lib/Table.d.ts CHANGED
@@ -7,12 +7,11 @@ export default class Table {
7
7
  readonly autoIncrement: boolean;
8
8
  readonly indexes: Array<TableIndex>;
9
9
  readonly database: Database;
10
- constructor({ name, primaryKey, autoIncrement, indexes, data }: {
10
+ constructor({ name, primaryKey, autoIncrement, indexes }: {
11
11
  name?: string | undefined;
12
12
  primaryKey?: string | undefined;
13
13
  autoIncrement?: boolean | undefined;
14
14
  indexes?: never[] | undefined;
15
- data?: never[] | undefined;
16
15
  }, database: Database);
17
16
  insert(data: Array<any>): Promise<unknown>;
18
17
  update(modify: any, where?: any): Promise<unknown> | undefined;
package/lib/Table.js CHANGED
@@ -18,7 +18,7 @@ const _request = (request, success) => {
18
18
  });
19
19
  };
20
20
  export default class Table {
21
- constructor({ name = '', primaryKey = 'id', autoIncrement = true, indexes = [], data = [] }, database) {
21
+ constructor({ name = '', primaryKey = 'id', autoIncrement = true, indexes = [] }, database) {
22
22
  this.primaryKey = "id";
23
23
  this.autoIncrement = false;
24
24
  this.indexes = [];
@@ -26,11 +26,7 @@ export default class Table {
26
26
  this.primaryKey = primaryKey;
27
27
  this.autoIncrement = autoIncrement;
28
28
  this.database = database;
29
- if (!database.exists(name)) {
30
- const store = database.connect.createObjectStore(name, { keyPath: this.primaryKey, autoIncrement });
31
- indexes.forEach(({ name, column, unique, multiEntry }) => store.createIndex(name, column, { unique, multiEntry }));
32
- data.forEach(item => store.add(item));
33
- }
29
+ this.indexes = indexes;
34
30
  }
35
31
  insert(data) {
36
32
  const arr = _instance(data, Array) ? data : [data];
@@ -1,7 +1,7 @@
1
1
  export default class TableIndex {
2
2
  readonly name: string;
3
- readonly column: string;
3
+ readonly column: Array<string> | string;
4
4
  readonly unique: boolean;
5
5
  readonly multiEntry: boolean;
6
- constructor(name: string, column: string, unique?: boolean, multiEntry?: boolean);
6
+ constructor(name: string, column: Array<string> | string, unique?: boolean, multiEntry?: boolean);
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xdriver",
3
- "version": "1.0.11",
3
+ "version": "1.1.1",
4
4
  "description": "A simple driver for IndexDB",
5
5
  "main": "./lib/index",
6
6
  "files": [