xdriver 1.0.8 → 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 +10 -7
- package/lib/Database.js +58 -27
- package/lib/Driver.d.ts +3 -1
- package/lib/Driver.js +7 -1
- package/lib/Table.d.ts +1 -2
- package/lib/Table.js +3 -7
- package/lib/TableIndex.d.ts +2 -2
- package/package.json +1 -1
package/lib/Database.d.ts
CHANGED
|
@@ -3,17 +3,20 @@ export default class Database {
|
|
|
3
3
|
private name;
|
|
4
4
|
private version;
|
|
5
5
|
connect: any;
|
|
6
|
+
private initialized;
|
|
7
|
+
readonly tables: Map<string, Table>;
|
|
6
8
|
readonly events: any;
|
|
7
9
|
constructor(name: string, version?: number);
|
|
10
|
+
initialize(initialized: Function): this;
|
|
8
11
|
on(event: any, fn?: Function): this;
|
|
9
12
|
isOpen(): boolean;
|
|
10
|
-
open(name?: string | undefined, version?:
|
|
13
|
+
open(name?: string | undefined, version?: any): Promise<unknown>;
|
|
11
14
|
drop(): Promise<unknown>;
|
|
12
15
|
close(): void;
|
|
13
|
-
createTable(
|
|
14
|
-
createTables(tables: Array<string
|
|
15
|
-
dropTable(tableName: string):
|
|
16
|
-
table(tableName: string): Table;
|
|
17
|
-
exists(table: string):
|
|
18
|
-
|
|
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>;
|
|
19
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
|
}
|
|
@@ -22,6 +20,15 @@ export default class Database {
|
|
|
22
20
|
this.name = name;
|
|
23
21
|
this.version = version;
|
|
24
22
|
this.connect = null;
|
|
23
|
+
this.initialized = () => { };
|
|
24
|
+
this.tables = new Map();
|
|
25
|
+
}
|
|
26
|
+
initialize(initialized) {
|
|
27
|
+
if (this.isOpen()) {
|
|
28
|
+
throw new Error('You must be call this method before open');
|
|
29
|
+
}
|
|
30
|
+
this.initialized = initialized;
|
|
31
|
+
return this;
|
|
25
32
|
}
|
|
26
33
|
on(event, fn) {
|
|
27
34
|
if (!_instance(event, Object)) {
|
|
@@ -39,6 +46,9 @@ export default class Database {
|
|
|
39
46
|
return this.connect != null;
|
|
40
47
|
}
|
|
41
48
|
open(name, version) {
|
|
49
|
+
if (!name && !version && this.isOpen()) {
|
|
50
|
+
return Promise.resolve(this);
|
|
51
|
+
}
|
|
42
52
|
this.name = name || this.name;
|
|
43
53
|
this.version = version || this.version;
|
|
44
54
|
const request = innerDB.open(this.name, version);
|
|
@@ -52,9 +62,24 @@ export default class Database {
|
|
|
52
62
|
this.events.process(IndexdbStatus.CONNECT_CLOSE);
|
|
53
63
|
this.events.close.call(this, e.target.result);
|
|
54
64
|
};
|
|
55
|
-
return new Promise((resolve) => {
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
56
66
|
request.onsuccess = (e) => {
|
|
57
|
-
|
|
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;
|
|
58
83
|
this.events.process(IndexdbStatus.CONNECT_SUCCESS);
|
|
59
84
|
this.connect.onerror = this.connect.onabort = (e) => {
|
|
60
85
|
const error = e.target.error;
|
|
@@ -68,7 +93,7 @@ export default class Database {
|
|
|
68
93
|
this.events.process(IndexdbStatus.CONNECT_SUCCESS);
|
|
69
94
|
this.connect = e.target.result;
|
|
70
95
|
this.events.change.call(this, this.connect);
|
|
71
|
-
|
|
96
|
+
this.initialized();
|
|
72
97
|
};
|
|
73
98
|
});
|
|
74
99
|
}
|
|
@@ -92,39 +117,45 @@ export default class Database {
|
|
|
92
117
|
this.connect = null;
|
|
93
118
|
}
|
|
94
119
|
}
|
|
95
|
-
createTable(
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
table = Object.assign({ name: tab }, options);
|
|
120
|
+
createTable(tableName, options) {
|
|
121
|
+
if (this.exists(tableName)) {
|
|
122
|
+
throw new Error(`Table ${tableName} is exists`);
|
|
99
123
|
}
|
|
100
|
-
|
|
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;
|
|
101
134
|
}
|
|
102
135
|
createTables(tables, options) {
|
|
103
136
|
return tables.map(table => {
|
|
104
|
-
|
|
105
|
-
if (_instance(table, String)) {
|
|
106
|
-
Object.assign(opts, { name: table });
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
Object.assign(opts, table);
|
|
110
|
-
}
|
|
111
|
-
return new Table(opts, this);
|
|
137
|
+
return this.createTable(table, options);
|
|
112
138
|
});
|
|
113
139
|
}
|
|
114
140
|
dropTable(tableName) {
|
|
115
|
-
|
|
141
|
+
if (this.exists(tableName)) {
|
|
142
|
+
this.tables.delete(tableName);
|
|
143
|
+
this.connect.deleteObjectStore(tableName);
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
return false;
|
|
116
147
|
}
|
|
117
148
|
table(tableName) {
|
|
118
|
-
|
|
119
|
-
|
|
149
|
+
let table = this.tables.get(tableName);
|
|
150
|
+
if (typeof table !== "undefined") {
|
|
151
|
+
return table;
|
|
120
152
|
}
|
|
121
|
-
|
|
153
|
+
throw new Error(`table ${tableName} not exists!`);
|
|
122
154
|
}
|
|
123
155
|
exists(table) {
|
|
124
|
-
|
|
125
|
-
return tables && tables.contains(table);
|
|
156
|
+
return this.tables.has(table);
|
|
126
157
|
}
|
|
127
|
-
|
|
128
|
-
return this.
|
|
158
|
+
getTables() {
|
|
159
|
+
return this.tables.values();
|
|
129
160
|
}
|
|
130
161
|
}
|
package/lib/Driver.d.ts
CHANGED
|
@@ -2,10 +2,12 @@ import { KeyRange } from "./Const";
|
|
|
2
2
|
import Database from "./Database";
|
|
3
3
|
declare class Driver extends Database {
|
|
4
4
|
insert(table: string, data: Array<any>): Promise<unknown>;
|
|
5
|
-
|
|
5
|
+
query(table: string, keyRange?: KeyRange, limit?: number): any;
|
|
6
|
+
select(table: string, where: any, options?: any): any;
|
|
6
7
|
selectByKey(table: string, key: any): any;
|
|
7
8
|
count(table: string, keyRange?: KeyRange): any;
|
|
8
9
|
update(table: string, modify: any, where: any): Promise<unknown> | undefined;
|
|
10
|
+
merge(table: string, row: any): Promise<unknown> | undefined;
|
|
9
11
|
delete(table: string, key: any): any;
|
|
10
12
|
truncate(table: string): any;
|
|
11
13
|
}
|
package/lib/Driver.js
CHANGED
|
@@ -3,9 +3,12 @@ class Driver extends Database {
|
|
|
3
3
|
insert(table, data) {
|
|
4
4
|
return this.table(table).insert(data);
|
|
5
5
|
}
|
|
6
|
-
|
|
6
|
+
query(table, keyRange, limit) {
|
|
7
7
|
return this.table(table).query(keyRange, limit);
|
|
8
8
|
}
|
|
9
|
+
select(table, where, options) {
|
|
10
|
+
return this.table(table).select(where, options);
|
|
11
|
+
}
|
|
9
12
|
selectByKey(table, key) {
|
|
10
13
|
return this.table(table).queryByKey(key);
|
|
11
14
|
}
|
|
@@ -15,6 +18,9 @@ class Driver extends Database {
|
|
|
15
18
|
update(table, modify, where) {
|
|
16
19
|
return this.table(table).update(modify, where);
|
|
17
20
|
}
|
|
21
|
+
merge(table, row) {
|
|
22
|
+
return this.table(table).merge(row);
|
|
23
|
+
}
|
|
18
24
|
delete(table, key) {
|
|
19
25
|
return this.table(table).delete(key);
|
|
20
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
|
|
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 = []
|
|
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
|
-
|
|
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];
|
|
@@ -107,7 +103,7 @@ export default class Table {
|
|
|
107
103
|
if (!where) {
|
|
108
104
|
return this.query();
|
|
109
105
|
}
|
|
110
|
-
let { index = '', limit = void 0, useIndex = false } = options;
|
|
106
|
+
let { index = '', limit = void 0, useIndex = false } = options || {};
|
|
111
107
|
if (useIndex && index) {
|
|
112
108
|
return this.queryByIndex(index, where, limit);
|
|
113
109
|
}
|
package/lib/TableIndex.d.ts
CHANGED
|
@@ -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
|
}
|