xdriver 2.0.8 → 2.0.10
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 +4 -2
- package/lib/database.js +4 -0
- package/lib/index.d.ts +17 -9
- package/lib/index.js +6 -12
- package/lib/mapper.d.ts +19 -0
- package/lib/mapper.js +47 -0
- package/lib/table.d.ts +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/database.ts +6 -1
- package/src/index.ts +21 -16
- package/src/mapper.ts +65 -0
- package/src/table.ts +1 -1
package/lib/database.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Connection from "./connection";
|
|
2
|
-
import {
|
|
2
|
+
import { Pagination, Row, RowPacket } from "./table";
|
|
3
|
+
import Mapper from "./mapper";
|
|
3
4
|
export default class Database extends Connection {
|
|
4
5
|
query<R extends Row>(table: string, query?: IDBValidKey | IDBKeyRange, count?: number): Promise<RowPacket<R>>;
|
|
5
6
|
select<R extends Row>(table: string, key?: IDBValidKey | IDBKeyRange, index?: string, count?: number): Promise<RowPacket<R>>;
|
|
@@ -9,10 +10,11 @@ export default class Database extends Connection {
|
|
|
9
10
|
count(table: string, key: IDBValidKey | IDBKeyRange): Promise<number>;
|
|
10
11
|
insert(table: string, data: Array<Record<string, any>>): Promise<IDBValidKey[]>;
|
|
11
12
|
update(table: string, modify: Row, key?: IDBValidKey | IDBKeyRange | null): Promise<IDBValidKey>;
|
|
12
|
-
delete(table: string, key: IDBValidKey | IDBKeyRange): Promise<
|
|
13
|
+
delete(table: string, key: IDBValidKey | IDBKeyRange): Promise<Row[]>;
|
|
13
14
|
truncate(table: string): Promise<void>;
|
|
14
15
|
getPagination<R extends Row>(table: string, pageSize?: number): Promise<Pagination<R>>;
|
|
15
16
|
scan<R extends Row>(table: string, key?: IDBValidKey | IDBKeyRange, direction?: IDBCursorDirection, indexName?: string): Promise<RowPacket<R>>;
|
|
16
17
|
getAllData<R extends Row>(table: string): Promise<Array<R>>;
|
|
17
18
|
paginate<R extends Row>(table: string, pageNo?: number, pageSize?: number, key?: IDBValidKey | IDBKeyRange, indexName?: string): Promise<RowPacket<R>>;
|
|
19
|
+
getMapper<R extends Row>(tableName: string): Mapper<R>;
|
|
18
20
|
}
|
package/lib/database.js
CHANGED
|
@@ -8,6 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import Connection from "./connection";
|
|
11
|
+
import Mapper from "./mapper";
|
|
11
12
|
export default class Database extends Connection {
|
|
12
13
|
query(table, query, count = 1000) {
|
|
13
14
|
return this.table(table).query(query, count);
|
|
@@ -59,4 +60,7 @@ export default class Database extends Connection {
|
|
|
59
60
|
return this.table(table).paginate(pageNo, pageSize, key, indexName);
|
|
60
61
|
});
|
|
61
62
|
}
|
|
63
|
+
getMapper(tableName) {
|
|
64
|
+
return new Mapper(this.table(tableName));
|
|
65
|
+
}
|
|
62
66
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { IndexdbStatus, KeyRange } from "./const";
|
|
2
2
|
import Database from "./database";
|
|
3
3
|
import Connection from "./connection";
|
|
4
|
-
import Table, { TableMeta } from "./table";
|
|
4
|
+
import Table, { Row, TableMeta } from "./table";
|
|
5
5
|
import { Logger } from "./com";
|
|
6
|
-
|
|
6
|
+
import Mapper from "./mapper";
|
|
7
|
+
export default class Driver {
|
|
7
8
|
private readonly name;
|
|
8
9
|
private version;
|
|
9
10
|
private readonly tables;
|
|
@@ -12,11 +13,18 @@ declare class Driver {
|
|
|
12
13
|
private hasChange;
|
|
13
14
|
connect(version?: number): Promise<Database>;
|
|
14
15
|
}
|
|
15
|
-
export { IndexdbStatus, KeyRange, Connection, Table, Logger,
|
|
16
|
+
export { IndexdbStatus, KeyRange, Connection, Table, Logger, Mapper, Row };
|
|
17
|
+
export interface TableDeclare<T extends Row> {
|
|
18
|
+
name: string;
|
|
19
|
+
primaryKey?: keyof T | Array<keyof T>;
|
|
20
|
+
autoIncrement?: boolean;
|
|
21
|
+
rows?: Array<T>;
|
|
22
|
+
indexes?: Array<{
|
|
23
|
+
name: string;
|
|
24
|
+
column: Array<keyof T> | keyof T;
|
|
25
|
+
unique?: boolean;
|
|
26
|
+
multiEntry?: boolean;
|
|
27
|
+
}>;
|
|
28
|
+
}
|
|
16
29
|
export declare function getConnect(name: string, tables: Array<TableMeta>, version?: number): Promise<Database>;
|
|
17
|
-
export declare function
|
|
18
|
-
define: (tables: Array<TableMeta>) => {
|
|
19
|
-
connect: (version?: number) => Promise<Database>;
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
export default Driver;
|
|
30
|
+
export declare function openConnect<T extends Row>(name: string, tables: Array<TableDeclare<T>>, version?: number): Promise<Database>;
|
package/lib/index.js
CHANGED
|
@@ -12,8 +12,9 @@ import Database from "./database";
|
|
|
12
12
|
import Connection from "./connection";
|
|
13
13
|
import Table from "./table";
|
|
14
14
|
import { Logger } from "./com";
|
|
15
|
+
import Mapper from "./mapper";
|
|
15
16
|
const logger = Logger.getLogger("Driver");
|
|
16
|
-
class Driver {
|
|
17
|
+
export default class Driver {
|
|
17
18
|
constructor(name, version = 0) {
|
|
18
19
|
this.tables = [];
|
|
19
20
|
this.name = name;
|
|
@@ -136,21 +137,14 @@ class Driver {
|
|
|
136
137
|
});
|
|
137
138
|
}
|
|
138
139
|
}
|
|
139
|
-
export { IndexdbStatus, KeyRange, Connection, Table, Logger,
|
|
140
|
+
export { IndexdbStatus, KeyRange, Connection, Table, Logger, Mapper };
|
|
140
141
|
export function getConnect(name, tables, version = 0) {
|
|
141
142
|
let driver = new Driver(name, version);
|
|
142
143
|
driver.defineTables(...tables);
|
|
143
144
|
return driver.connect();
|
|
144
145
|
}
|
|
145
|
-
export function
|
|
146
|
+
export function openConnect(name, tables, version = 0) {
|
|
146
147
|
let driver = new Driver(name, version);
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
driver.defineTables(...tables);
|
|
150
|
-
return {
|
|
151
|
-
connect: (version) => driver.connect(version),
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
};
|
|
148
|
+
driver.defineTables(...tables.map(t => t));
|
|
149
|
+
return driver.connect();
|
|
155
150
|
}
|
|
156
|
-
export default Driver;
|
package/lib/mapper.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import Table, { Pagination, Row, RowPacket } from "./table";
|
|
2
|
+
export default class Mapper<T extends Row> {
|
|
3
|
+
private table;
|
|
4
|
+
constructor(table: Table);
|
|
5
|
+
truncate(): Promise<void>;
|
|
6
|
+
count(key?: IDBValidKey | IDBKeyRange): Promise<number>;
|
|
7
|
+
insert(data: T | Array<T>): Promise<IDBValidKey[]>;
|
|
8
|
+
update(modify: Row, key?: IDBValidKey | IDBKeyRange | null): Promise<IDBValidKey>;
|
|
9
|
+
query(key?: IDBValidKey | IDBKeyRange, count?: number): Promise<RowPacket<T>>;
|
|
10
|
+
queryById(id: any | Array<any>, indexName?: string): Promise<T | Array<T>>;
|
|
11
|
+
delete(key: IDBValidKey | IDBKeyRange): Promise<Array<T>>;
|
|
12
|
+
queryByIndex(indexName: string, key?: IDBValidKey | IDBKeyRange, count?: number): Promise<RowPacket<T>>;
|
|
13
|
+
queryByKey(key: IDBValidKey | IDBKeyRange): Promise<RowPacket<T>>;
|
|
14
|
+
scan(key?: IDBValidKey | IDBKeyRange, direction?: IDBCursorDirection, indexName?: string): Promise<RowPacket<T>>;
|
|
15
|
+
select(key?: IDBValidKey | IDBKeyRange, count?: number, indexName?: string): Promise<RowPacket<T>>;
|
|
16
|
+
paginate<R extends Row>(pageNo?: number, pageSize?: number, key?: IDBValidKey | IDBKeyRange, indexName?: string): Promise<RowPacket<R>>;
|
|
17
|
+
getPagination(pageSize?: number): Promise<Pagination<T>>;
|
|
18
|
+
getAll(): Promise<Array<T>>;
|
|
19
|
+
}
|
package/lib/mapper.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export default class Mapper {
|
|
2
|
+
constructor(table) {
|
|
3
|
+
this.table = table;
|
|
4
|
+
}
|
|
5
|
+
truncate() {
|
|
6
|
+
return this.table.truncate();
|
|
7
|
+
}
|
|
8
|
+
count(key) {
|
|
9
|
+
return this.table.count(key);
|
|
10
|
+
}
|
|
11
|
+
insert(data) {
|
|
12
|
+
return this.table.insert(data);
|
|
13
|
+
}
|
|
14
|
+
update(modify, key) {
|
|
15
|
+
return this.table.update(modify, key);
|
|
16
|
+
}
|
|
17
|
+
query(key, count) {
|
|
18
|
+
return this.table.query(key, count);
|
|
19
|
+
}
|
|
20
|
+
queryById(id, indexName) {
|
|
21
|
+
return this.table.queryById(id, indexName);
|
|
22
|
+
}
|
|
23
|
+
delete(key) {
|
|
24
|
+
return this.table.delete(key);
|
|
25
|
+
}
|
|
26
|
+
queryByIndex(indexName, key, count) {
|
|
27
|
+
return this.table.queryByIndex(indexName, key, count);
|
|
28
|
+
}
|
|
29
|
+
queryByKey(key) {
|
|
30
|
+
return this.table.queryByKey(key);
|
|
31
|
+
}
|
|
32
|
+
scan(key, direction = 'next', indexName) {
|
|
33
|
+
return this.table.scan(key, direction, indexName);
|
|
34
|
+
}
|
|
35
|
+
select(key, count, indexName) {
|
|
36
|
+
return this.table.select(key, count, indexName);
|
|
37
|
+
}
|
|
38
|
+
paginate(pageNo = 1, pageSize = 10, key, indexName) {
|
|
39
|
+
return this.table.paginate(pageNo, pageSize, key, indexName);
|
|
40
|
+
}
|
|
41
|
+
getPagination(pageSize = 10) {
|
|
42
|
+
return this.table.getPagination(pageSize);
|
|
43
|
+
}
|
|
44
|
+
getAll() {
|
|
45
|
+
return this.table.getAllData();
|
|
46
|
+
}
|
|
47
|
+
}
|
package/lib/table.d.ts
CHANGED
|
@@ -57,7 +57,7 @@ export default class Table implements ITable {
|
|
|
57
57
|
get meta(): TableMeta;
|
|
58
58
|
insert(data: Row | Rows): Promise<Array<IDBValidKey>>;
|
|
59
59
|
update(modify: Row, key?: IDBValidKey | IDBKeyRange | null): Promise<IDBValidKey>;
|
|
60
|
-
delete(key: IDBValidKey | IDBKeyRange): Promise<
|
|
60
|
+
delete<R extends Row>(key: IDBValidKey | IDBKeyRange): Promise<Array<R>>;
|
|
61
61
|
truncate(): Promise<void>;
|
|
62
62
|
dropIndex(name: string): void;
|
|
63
63
|
keys(key?: IDBValidKey | IDBKeyRange | null, count?: number): Promise<IDBValidKey[]>;
|
package/lib/tsconfig.tsbuildinfo
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["../src/com.ts","../src/connection.ts","../src/const.ts","../src/database.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/mapper.ts","../src/table.ts"],"version":"5.9.3"}
|
package/package.json
CHANGED
package/src/database.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Connection from "./connection";
|
|
2
|
-
import {
|
|
2
|
+
import {Pagination, Row, RowPacket} from "./table";
|
|
3
|
+
import Mapper from "./mapper";
|
|
3
4
|
|
|
4
5
|
export default class Database extends Connection {
|
|
5
6
|
|
|
@@ -138,4 +139,8 @@ export default class Database extends Connection {
|
|
|
138
139
|
async paginate<R extends Row>(table: string, pageNo: number = 1, pageSize: number = 10, key?: IDBValidKey | IDBKeyRange, indexName?: string): Promise<RowPacket<R>> {
|
|
139
140
|
return this.table(table).paginate(pageNo, pageSize, key, indexName)
|
|
140
141
|
}
|
|
142
|
+
|
|
143
|
+
getMapper<R extends Row>(tableName: string):Mapper<R> {
|
|
144
|
+
return new Mapper<R>(this.table(tableName))
|
|
145
|
+
}
|
|
141
146
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {IndexdbStatus, innerDB, KeyRange} from "./const";
|
|
2
2
|
import Database from "./database";
|
|
3
3
|
import Connection from "./connection";
|
|
4
|
-
import Table, {TableMeta} from "./table";
|
|
4
|
+
import Table, {Row, TableMeta} from "./table";
|
|
5
5
|
import {Logger} from "./com";
|
|
6
|
-
|
|
6
|
+
import Mapper from "./mapper";
|
|
7
7
|
|
|
8
8
|
const logger = Logger.getLogger("Driver")
|
|
9
9
|
|
|
10
|
-
class Driver {
|
|
10
|
+
export default class Driver {
|
|
11
11
|
private readonly name: string;
|
|
12
12
|
private version: number;
|
|
13
13
|
private readonly tables: Array<TableMeta> = [];
|
|
@@ -144,7 +144,20 @@ class Driver {
|
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
-
export {IndexdbStatus, KeyRange, Connection, Table, Logger,
|
|
147
|
+
export {IndexdbStatus, KeyRange, Connection, Table, Logger, Mapper, Row};
|
|
148
|
+
|
|
149
|
+
export interface TableDeclare<T extends Row> {
|
|
150
|
+
name: string;
|
|
151
|
+
primaryKey?: keyof T | Array<keyof T>;
|
|
152
|
+
autoIncrement?: boolean;
|
|
153
|
+
rows?: Array<T>;
|
|
154
|
+
indexes?: Array<{
|
|
155
|
+
name: string;
|
|
156
|
+
column: Array<keyof T> | keyof T;
|
|
157
|
+
unique?: boolean;
|
|
158
|
+
multiEntry?: boolean;
|
|
159
|
+
}>;
|
|
160
|
+
}
|
|
148
161
|
|
|
149
162
|
export function getConnect(name: string, tables: Array<TableMeta>, version: number = 0) {
|
|
150
163
|
let driver = new Driver(name, version);
|
|
@@ -152,16 +165,8 @@ export function getConnect(name: string, tables: Array<TableMeta>, version: numb
|
|
|
152
165
|
return driver.connect();
|
|
153
166
|
}
|
|
154
167
|
|
|
155
|
-
export function
|
|
168
|
+
export function openConnect<T extends Row>(name: string, tables: Array<TableDeclare<T>>, version: number = 0) {
|
|
156
169
|
let driver = new Driver(name, version);
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
return {
|
|
161
|
-
connect: (version?: number) => driver.connect(version),
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
export default Driver;
|
|
170
|
+
driver.defineTables(...tables.map(t => t as TableMeta));
|
|
171
|
+
return driver.connect();
|
|
172
|
+
}
|
package/src/mapper.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import Table, {Pagination, Row, RowPacket} from "./table";
|
|
2
|
+
|
|
3
|
+
export default class Mapper<T extends Row> {
|
|
4
|
+
private table: Table;
|
|
5
|
+
|
|
6
|
+
constructor(table: Table) {
|
|
7
|
+
this.table = table;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
truncate() {
|
|
11
|
+
return this.table.truncate();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
count(key?: IDBValidKey | IDBKeyRange) {
|
|
15
|
+
return this.table.count(key);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
insert(data: T | Array<T>) {
|
|
19
|
+
return this.table.insert(data);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
update(modify: Row, key?: IDBValidKey | IDBKeyRange | null): Promise<IDBValidKey> {
|
|
23
|
+
return this.table.update(modify, key);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
query(key?: IDBValidKey | IDBKeyRange, count?: number): Promise<RowPacket<T>> {
|
|
27
|
+
return this.table.query(key, count);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
queryById(id: any | Array<any>, indexName?: string): Promise<T | Array<T>> {
|
|
31
|
+
return this.table.queryById(id, indexName);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
delete(key: IDBValidKey | IDBKeyRange): Promise<Array<T>> {
|
|
35
|
+
return this.table.delete(key);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
queryByIndex(indexName: string, key?: IDBValidKey | IDBKeyRange, count?: number): Promise<RowPacket<T>> {
|
|
39
|
+
return this.table.queryByIndex(indexName, key, count);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
queryByKey(key: IDBValidKey | IDBKeyRange): Promise<RowPacket<T>> {
|
|
43
|
+
return this.table.queryByKey(key);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
scan(key?: IDBValidKey | IDBKeyRange, direction: IDBCursorDirection = 'next', indexName?: string): Promise<RowPacket<T>> {
|
|
47
|
+
return this.table.scan(key, direction, indexName);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
select(key?: IDBValidKey | IDBKeyRange, count?: number, indexName?: string): Promise<RowPacket<T>> {
|
|
51
|
+
return this.table.select(key, count, indexName);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
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);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
getPagination(pageSize: number = 10): Promise<Pagination<T>> {
|
|
59
|
+
return this.table.getPagination(pageSize);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getAll(): Promise<Array<T>> {
|
|
63
|
+
return this.table.getAllData();
|
|
64
|
+
}
|
|
65
|
+
}
|
package/src/table.ts
CHANGED
|
@@ -350,7 +350,7 @@ export default class Table implements ITable{
|
|
|
350
350
|
* 根据主键值删除
|
|
351
351
|
* @param key
|
|
352
352
|
*/
|
|
353
|
-
async delete(key: IDBValidKey | IDBKeyRange) {
|
|
353
|
+
async delete<R extends Row>(key: IDBValidKey | IDBKeyRange): Promise<Array<R>> {
|
|
354
354
|
if (!key) {
|
|
355
355
|
return Promise.reject('key is required');
|
|
356
356
|
}
|