xdriver 1.1.1 → 2.0.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/com.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ export declare function _instance(example: any, type: any): boolean;
2
+ export declare function _copy(src: any, target: any): any;
3
+ export declare class Logger {
4
+ static trace: boolean;
5
+ private readonly catalog;
6
+ constructor(catalog: string);
7
+ private print;
8
+ private getFormatDate;
9
+ debug(msg: string, ...args: any[]): void;
10
+ warn(msg: string, ...args: any[]): void;
11
+ info(msg: string, ...args: any[]): void;
12
+ error(msg: string, ...args: any[]): void;
13
+ success(msg: string, ...args: any[]): void;
14
+ static getLogger(catalog?: string): Logger;
15
+ }
16
+ export declare const logger: Logger;
package/lib/com.js ADDED
@@ -0,0 +1,61 @@
1
+ export function _instance(example, type) {
2
+ let pro = type.prototype;
3
+ example = example.__proto__;
4
+ while (true) {
5
+ if (example === null)
6
+ return false;
7
+ if (pro === example)
8
+ return true;
9
+ example = example.__proto__;
10
+ }
11
+ }
12
+ export function _copy(src, target) {
13
+ if (!target)
14
+ return src;
15
+ for (let prop in src) {
16
+ target[prop] = src[prop];
17
+ }
18
+ return target;
19
+ }
20
+ var LogLevel;
21
+ (function (LogLevel) {
22
+ LogLevel[LogLevel["debug"] = 0] = "debug";
23
+ LogLevel[LogLevel["warn"] = 1] = "warn";
24
+ LogLevel[LogLevel["info"] = 2] = "info";
25
+ LogLevel[LogLevel["error"] = 3] = "error";
26
+ })(LogLevel || (LogLevel = {}));
27
+ export class Logger {
28
+ constructor(catalog) {
29
+ this.catalog = catalog;
30
+ }
31
+ print(fn, ...args) {
32
+ if (Logger.trace) {
33
+ console.trace(...args);
34
+ return;
35
+ }
36
+ fn(...args);
37
+ }
38
+ getFormatDate() {
39
+ const date = new Date();
40
+ return `[${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}.${date.getMilliseconds()}]`;
41
+ }
42
+ debug(msg, ...args) {
43
+ this.print(console.debug, `%c${this.getFormatDate()} [DEBUG] <${this.catalog}> ${msg}`, ...args, "color: gray;");
44
+ }
45
+ warn(msg, ...args) {
46
+ this.print(console.warn, `%c${this.getFormatDate()} [WARN] <${this.catalog}> ${msg}`, ...args, "color: #FFCC00;");
47
+ }
48
+ info(msg, ...args) {
49
+ this.print(console.info, `${this.getFormatDate()} [INFO] <${this.catalog}> ${msg}`, ...args);
50
+ }
51
+ error(msg, ...args) {
52
+ this.print(console.error, `%c${this.getFormatDate()} [ERROR] <${this.catalog}> ${msg}`, ...args, "color: #EF4444;");
53
+ }
54
+ success(msg, ...args) {
55
+ this.print(console.log, `%c${this.getFormatDate()} [SUCCESS] <${this.catalog}> ${msg}`, ...args, "color: #10B981;");
56
+ }
57
+ static getLogger(catalog = 'default') {
58
+ return new Logger(catalog);
59
+ }
60
+ }
61
+ export const logger = Logger.getLogger();
package/lib/const.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ export declare const innerDB: IDBFactory;
2
+ export declare enum IndexdbStatus {
3
+ CONNECT_ING = 0,
4
+ CONNECT_SUCCESS = 1,
5
+ CONNECT_ERROR = 2,
6
+ CONNECT_CHANGE = 3,
7
+ CONNECT_CLOSE = 4,
8
+ DATABASE_ERROR = 5,
9
+ VERSION_CHANGE = 99
10
+ }
11
+ export declare class KeyRange {
12
+ static leq(x: any): IDBKeyRange;
13
+ static lt(x: any): IDBKeyRange;
14
+ static geq(x: any): IDBKeyRange;
15
+ static gt(x: any): IDBKeyRange;
16
+ static between(x: any, y: any): IDBKeyRange;
17
+ static gt_lt(x: any, y: any): IDBKeyRange;
18
+ static gt_leq(x: any, y: any): IDBKeyRange;
19
+ static geq_lt(x: any, y: any): IDBKeyRange;
20
+ static eq(x: any, y: any): IDBKeyRange;
21
+ }
package/lib/const.js ADDED
@@ -0,0 +1,41 @@
1
+ let win = window;
2
+ export const innerDB = win.indexedDB || win.mozIndexedDB || win.webkitIndexedDB || win.msIndexedDB;
3
+ export var IndexdbStatus;
4
+ (function (IndexdbStatus) {
5
+ IndexdbStatus[IndexdbStatus["CONNECT_ING"] = 0] = "CONNECT_ING";
6
+ IndexdbStatus[IndexdbStatus["CONNECT_SUCCESS"] = 1] = "CONNECT_SUCCESS";
7
+ IndexdbStatus[IndexdbStatus["CONNECT_ERROR"] = 2] = "CONNECT_ERROR";
8
+ IndexdbStatus[IndexdbStatus["CONNECT_CHANGE"] = 3] = "CONNECT_CHANGE";
9
+ IndexdbStatus[IndexdbStatus["CONNECT_CLOSE"] = 4] = "CONNECT_CLOSE";
10
+ IndexdbStatus[IndexdbStatus["DATABASE_ERROR"] = 5] = "DATABASE_ERROR";
11
+ IndexdbStatus[IndexdbStatus["VERSION_CHANGE"] = 99] = "VERSION_CHANGE";
12
+ })(IndexdbStatus || (IndexdbStatus = {}));
13
+ export class KeyRange {
14
+ static leq(x) {
15
+ return IDBKeyRange.upperBound(x);
16
+ }
17
+ static lt(x) {
18
+ return IDBKeyRange.upperBound(x, true);
19
+ }
20
+ static geq(x) {
21
+ return IDBKeyRange.lowerBound(x);
22
+ }
23
+ static gt(x) {
24
+ return IDBKeyRange.lowerBound(x, true);
25
+ }
26
+ static between(x, y) {
27
+ return IDBKeyRange.bound(x, y);
28
+ }
29
+ static gt_lt(x, y) {
30
+ return IDBKeyRange.bound(x, y, true, true);
31
+ }
32
+ static gt_leq(x, y) {
33
+ return IDBKeyRange.bound(x, y, true, false);
34
+ }
35
+ static geq_lt(x, y) {
36
+ return IDBKeyRange.bound(x, y, false, true);
37
+ }
38
+ static eq(x, y) {
39
+ return IDBKeyRange.only(x);
40
+ }
41
+ }
@@ -0,0 +1,31 @@
1
+ import { IndexdbStatus } from "./const";
2
+ import Table, { Metadata } from "./table";
3
+ interface EventMap {
4
+ process: (this: Database, status: IndexdbStatus) => void;
5
+ success: (this: Database, connect: IDBDatabase) => void;
6
+ blocked: (this: Database, event: IDBVersionChangeEvent) => void;
7
+ change: (connect: any) => void;
8
+ error: (evt: Event) => void;
9
+ close: () => void;
10
+ }
11
+ type EventKey = keyof EventMap;
12
+ export default class Database {
13
+ private name;
14
+ private version;
15
+ database?: IDBDatabase;
16
+ readonly tableMap: Map<string, Table>;
17
+ private eventMap;
18
+ constructor(database: string, version?: number);
19
+ on<T extends EventKey>(event: T, handle: EventMap[T]): this;
20
+ get connected(): boolean;
21
+ connect(name?: string, version?: number): Promise<Database>;
22
+ drop(): Promise<unknown>;
23
+ close(): void;
24
+ createTable(tableName: string, meta?: Omit<Metadata, 'name'>): Table;
25
+ createTables(tables: Array<string>, meta?: Omit<Metadata, 'name' | 'rows'>): Table[];
26
+ dropTable(tableName: string): boolean;
27
+ table(tableName: string): Table | undefined;
28
+ exists(table: string): boolean;
29
+ getTables(): MapIterator<Table>;
30
+ }
31
+ export {};
@@ -0,0 +1,146 @@
1
+ import { IndexdbStatus, innerDB } from "./const";
2
+ import Table from "./table";
3
+ import { Logger } from './com';
4
+ const logger = Logger.getLogger('Database');
5
+ export default class Database {
6
+ constructor(database, version = 1) {
7
+ this.eventMap = {};
8
+ this.name = database;
9
+ this.version = version;
10
+ this.tableMap = new Map();
11
+ }
12
+ on(event, handle) {
13
+ this.eventMap[event] = handle;
14
+ return this;
15
+ }
16
+ get connected() {
17
+ return this.database != void 0;
18
+ }
19
+ connect(name, version) {
20
+ var _a;
21
+ if (!name && version == void 0 && this.connected) {
22
+ return Promise.resolve(this);
23
+ }
24
+ this.name = name || this.name;
25
+ this.version = version || this.version;
26
+ const request = innerDB.open(this.name, version);
27
+ (_a = this.eventMap.process) === null || _a === void 0 ? void 0 : _a.call(this, IndexdbStatus.CONNECT_ING);
28
+ request.onerror = (e) => {
29
+ var _a, _b;
30
+ logger.error('onerror', e);
31
+ (_a = this.eventMap.process) === null || _a === void 0 ? void 0 : _a.call(this, IndexdbStatus.CONNECT_ERROR);
32
+ (_b = this.eventMap.error) === null || _b === void 0 ? void 0 : _b.call(this, e);
33
+ };
34
+ request.onblocked = (evt) => {
35
+ var _a, _b;
36
+ logger.warn(`onblocked change from ${evt.oldVersion} to ${evt.newVersion}`, evt);
37
+ (_a = this.eventMap.process) === null || _a === void 0 ? void 0 : _a.call(this, IndexdbStatus.CONNECT_CLOSE);
38
+ (_b = this.eventMap.blocked) === null || _b === void 0 ? void 0 : _b.call(this, evt);
39
+ };
40
+ return new Promise((resolve, reject) => {
41
+ request.onupgradeneeded = (event) => {
42
+ var _a, _b;
43
+ logger.info(`Version change from <${event.oldVersion}> to <${event.newVersion}>`, event);
44
+ (_a = this.eventMap.process) === null || _a === void 0 ? void 0 : _a.call(this, IndexdbStatus.VERSION_CHANGE);
45
+ const target = event.target;
46
+ this.database = target.result;
47
+ (_b = this.eventMap.change) === null || _b === void 0 ? void 0 : _b.call(this, this.database);
48
+ };
49
+ request.onsuccess = (e) => {
50
+ var _a, _b;
51
+ logger.info(`Connect Success`, e);
52
+ const target = e.target;
53
+ let connect = target.result;
54
+ this.database = connect;
55
+ let objectStoreNames = connect.objectStoreNames;
56
+ if (objectStoreNames.length) {
57
+ let transaction = connect.transaction(objectStoreNames);
58
+ for (let i = 0; i < objectStoreNames.length; i++) {
59
+ let table = objectStoreNames.item(i);
60
+ if (!table)
61
+ continue;
62
+ let objectStore = transaction.objectStore(table);
63
+ let { indexNames, keyPath: primaryKey, name, autoIncrement = true } = objectStore;
64
+ let indexes = [];
65
+ for (let j = 0; indexNames && j < indexNames.length; j++) {
66
+ let indexName = indexNames.item(j);
67
+ if (!indexName)
68
+ continue;
69
+ let { keyPath, name, multiEntry, unique } = objectStore.index(indexName);
70
+ indexes.push({ name, column: keyPath, unique, multiEntry });
71
+ }
72
+ this.tableMap.set(name, new Table({ name, primaryKey: primaryKey == null ? void 0 : primaryKey, autoIncrement, indexes }, this.database));
73
+ }
74
+ }
75
+ (_a = this.eventMap.process) === null || _a === void 0 ? void 0 : _a.call(this, IndexdbStatus.CONNECT_SUCCESS);
76
+ (_b = this.eventMap.success) === null || _b === void 0 ? void 0 : _b.call(this, this.database);
77
+ resolve(this);
78
+ };
79
+ });
80
+ }
81
+ drop() {
82
+ return new Promise((resolve, reject) => {
83
+ let request = innerDB.deleteDatabase(this.name);
84
+ request.onerror = function (event) {
85
+ reject(event);
86
+ };
87
+ request.onsuccess = function (event) {
88
+ resolve(event);
89
+ };
90
+ this.close();
91
+ });
92
+ }
93
+ close() {
94
+ var _a, _b, _c;
95
+ if (this.connected) {
96
+ (_a = this.eventMap.process) === null || _a === void 0 ? void 0 : _a.call(this, IndexdbStatus.CONNECT_CLOSE);
97
+ (_b = this.database) === null || _b === void 0 ? void 0 : _b.close();
98
+ (_c = this.eventMap.close) === null || _c === void 0 ? void 0 : _c.call(this);
99
+ this.database = void 0;
100
+ }
101
+ }
102
+ createTable(tableName, meta = {}) {
103
+ var _a, _b;
104
+ if (this.exists(tableName)) {
105
+ throw new Error(`Table ${tableName} is exists`);
106
+ }
107
+ const metadata = Object.assign(Object.assign({}, meta), { name: tableName });
108
+ let { name = '', primaryKey = 'id', autoIncrement = true, indexes = [], rows = [] } = metadata;
109
+ const store = (_a = this.database) === null || _a === void 0 ? void 0 : _a.createObjectStore(name, { keyPath: primaryKey, autoIncrement });
110
+ let idxes = indexes.map(({ name, column, unique, multiEntry }) => {
111
+ store === null || store === void 0 ? void 0 : store.createIndex(name, column, { unique, multiEntry });
112
+ return { name, column, unique, multiEntry };
113
+ });
114
+ (_b = metadata.rows) === null || _b === void 0 ? void 0 : _b.forEach(row => store === null || store === void 0 ? void 0 : store.add(row));
115
+ let tab = new Table({ name, primaryKey, autoIncrement, indexes: idxes }, this.database);
116
+ this.tableMap.set(name, tab);
117
+ return tab;
118
+ }
119
+ createTables(tables, meta = {}) {
120
+ return tables.map(table => {
121
+ return this.createTable(table, meta);
122
+ });
123
+ }
124
+ dropTable(tableName) {
125
+ var _a;
126
+ if (this.exists(tableName)) {
127
+ (_a = this.database) === null || _a === void 0 ? void 0 : _a.deleteObjectStore(tableName);
128
+ this.tableMap.delete(tableName);
129
+ return true;
130
+ }
131
+ return false;
132
+ }
133
+ table(tableName) {
134
+ const table = this.tableMap.get(tableName);
135
+ if (!table) {
136
+ logger.error(`Table ${tableName} is not exists`);
137
+ }
138
+ return table;
139
+ }
140
+ exists(table) {
141
+ return this.tableMap.has(table);
142
+ }
143
+ getTables() {
144
+ return this.tableMap.values();
145
+ }
146
+ }
@@ -0,0 +1,14 @@
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 ADDED
@@ -0,0 +1,59 @@
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
+ }
package/lib/index.d.ts CHANGED
@@ -1,7 +1,6 @@
1
- import { IndexdbStatus, TransactionMode, CursorDirection, KeyRange } from "./Const";
2
- import Driver from "./Driver";
3
- import Database from "./Database";
4
- import Table from "./Table";
5
- import TableIndex from "./TableIndex";
6
- export { IndexdbStatus, TransactionMode, CursorDirection, KeyRange, Database, TableIndex, Table };
7
- export default Driver;
1
+ import { IndexdbStatus, KeyRange } from "./const";
2
+ import Driver from "./driver";
3
+ import Database from "./database";
4
+ import Table from "./table";
5
+ export { IndexdbStatus, KeyRange, Database, Table, Driver };
6
+ export default Driver;
package/lib/index.js CHANGED
@@ -1,7 +1,6 @@
1
- import { IndexdbStatus, TransactionMode, CursorDirection, KeyRange } from "./Const";
2
- import Driver from "./Driver";
3
- import Database from "./Database";
4
- import Table from "./Table";
5
- import TableIndex from "./TableIndex";
6
- export { IndexdbStatus, TransactionMode, CursorDirection, KeyRange, Database, TableIndex, Table };
7
- export default Driver;
1
+ import { IndexdbStatus, KeyRange } from "./const";
2
+ import Driver from "./driver";
3
+ import Database from "./database";
4
+ import Table from "./table";
5
+ export { IndexdbStatus, KeyRange, Database, Table, Driver };
6
+ export default Driver;
package/lib/table.d.ts ADDED
@@ -0,0 +1,40 @@
1
+ export type Index = {
2
+ name: string;
3
+ column: Array<string> | string;
4
+ unique?: boolean;
5
+ multiEntry?: boolean;
6
+ };
7
+ export type ITable = {
8
+ name: string;
9
+ primaryKey?: string | string[];
10
+ autoIncrement?: boolean;
11
+ indexes?: Array<Index>;
12
+ };
13
+ export type Row = Record<string, any>;
14
+ export type Rows = Array<Row>;
15
+ export type Metadata = ITable & {
16
+ rows?: Rows;
17
+ };
18
+ export default class Table implements ITable {
19
+ readonly name: string;
20
+ readonly primaryKey: string | string[];
21
+ readonly autoIncrement: boolean;
22
+ readonly indexes: Array<Index>;
23
+ readonly database?: IDBDatabase;
24
+ constructor(table: ITable | string, database?: IDBDatabase);
25
+ insert(data: Row | Rows): Promise<any[]>;
26
+ update(modify: Row, key?: IDBValidKey | IDBKeyRange | null, count?: number): Promise<IDBValidKey>;
27
+ merge(row: Row): Promise<IDBValidKey> | Promise<any[]>;
28
+ delete(key: IDBValidKey | IDBKeyRange): Promise<any[]>;
29
+ clear(): Promise<undefined>;
30
+ dropIndex(name: string): void;
31
+ keys(key?: IDBValidKey | IDBKeyRange | null, count?: number): Promise<IDBValidKey[]>;
32
+ count(key?: IDBValidKey | IDBKeyRange): Promise<number>;
33
+ query(key?: IDBValidKey | IDBKeyRange, count?: number): Promise<Rows>;
34
+ select(key?: IDBValidKey | IDBKeyRange, count?: number, index?: string): Promise<Rows>;
35
+ queryByKey(key: IDBValidKey | IDBKeyRange): Promise<Row>;
36
+ fetch(key?: IDBValidKey | IDBKeyRange, direction?: IDBCursorDirection): Promise<Rows>;
37
+ queryByIndex(indexName: string, key?: IDBValidKey | IDBKeyRange, count?: number): Promise<Rows>;
38
+ deplete(key?: IDBValidKey | IDBKeyRange, direction?: IDBCursorDirection): Promise<Rows>;
39
+ multiple(indexName: string, key?: IDBValidKey | IDBKeyRange, direction?: IDBCursorDirection): Promise<Rows>;
40
+ }
package/lib/table.js ADDED
@@ -0,0 +1,183 @@
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 { _copy, _instance, Logger } from "./com";
11
+ const logger = Logger.getLogger('Table');
12
+ const _getObjectStore = (table, mode = 'readonly') => {
13
+ var _a;
14
+ const trans = (_a = table.database) === null || _a === void 0 ? void 0 : _a.transaction(table.name, mode);
15
+ if (!trans) {
16
+ throw new Error('database not open');
17
+ }
18
+ trans.oncomplete = (event) => {
19
+ logger.debug(`Table ${table.name} transaction success`, event);
20
+ };
21
+ trans.onerror = (event) => {
22
+ logger.error(`Table ${table.name} transaction error`, event);
23
+ };
24
+ return trans.objectStore(table.name);
25
+ };
26
+ const _request = (request) => __awaiter(void 0, void 0, void 0, function* () {
27
+ return new Promise((resolve, reject) => {
28
+ request.onsuccess = (e) => {
29
+ resolve(e.target.result);
30
+ };
31
+ request.onerror = (e) => {
32
+ reject(e.target.error);
33
+ };
34
+ });
35
+ });
36
+ export default class Table {
37
+ constructor(table, database) {
38
+ this.primaryKey = "id";
39
+ this.autoIncrement = false;
40
+ this.indexes = [];
41
+ let metadata;
42
+ if (typeof table === 'string') {
43
+ metadata = { name: table };
44
+ }
45
+ else {
46
+ metadata = Object.assign({}, table);
47
+ }
48
+ const { name = '', primaryKey = 'id', autoIncrement = true, indexes = [] } = metadata;
49
+ this.name = name;
50
+ this.primaryKey = primaryKey;
51
+ this.autoIncrement = autoIncrement;
52
+ this.database = database;
53
+ this.indexes = (indexes || []).map(idx => {
54
+ return Object.assign({ unique: false, multiEntry: false }, idx);
55
+ });
56
+ }
57
+ insert(data) {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ const store = _getObjectStore(this, 'readwrite');
60
+ let results = [];
61
+ let rows = Array.isArray(data) ? data : [data];
62
+ for (let row of rows) {
63
+ let result = yield _request(store.add(row));
64
+ results.push(result);
65
+ }
66
+ return results;
67
+ });
68
+ }
69
+ update(modify, key, count) {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ 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;
83
+ });
84
+ }
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
+ delete(key) {
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ if (!key) {
100
+ return Promise.reject('key is required');
101
+ }
102
+ let objectStore = _getObjectStore(this, 'readwrite');
103
+ let rows = yield _request(objectStore.getAll(key));
104
+ yield _request(objectStore.delete(key));
105
+ return rows;
106
+ });
107
+ }
108
+ clear() {
109
+ return _request(_getObjectStore(this, 'readwrite').clear());
110
+ }
111
+ dropIndex(name) {
112
+ return _getObjectStore(this, 'readwrite').deleteIndex(name);
113
+ }
114
+ keys(key, count) {
115
+ return _request(_getObjectStore(this).getAllKeys(key, count));
116
+ }
117
+ count(key) {
118
+ return _request(_getObjectStore(this).count(key));
119
+ }
120
+ query(key, count) {
121
+ return _request(_getObjectStore(this).getAll(key, count));
122
+ }
123
+ select(key, count, index) {
124
+ if (!index) {
125
+ return this.query(key, count);
126
+ }
127
+ return this.queryByIndex(index, key, count);
128
+ }
129
+ queryByKey(key) {
130
+ return _request(_getObjectStore(this).get(key));
131
+ }
132
+ fetch(key, direction) {
133
+ return __awaiter(this, void 0, void 0, function* () {
134
+ const store = _getObjectStore(this);
135
+ let cursor = yield _request(store.openCursor(key, direction));
136
+ if (!cursor) {
137
+ return [];
138
+ }
139
+ let rows = [];
140
+ while (cursor.value) {
141
+ rows.push(cursor.value);
142
+ cursor.continue();
143
+ }
144
+ return rows;
145
+ });
146
+ }
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;
165
+ });
166
+ }
167
+ multiple(indexName_1, key_1) {
168
+ return __awaiter(this, arguments, void 0, function* (indexName, key, direction = 'next') {
169
+ const store = _getObjectStore(this);
170
+ const index = store.index(indexName);
171
+ const cursor = yield _request(index.openCursor(key, direction));
172
+ if (!cursor) {
173
+ return [];
174
+ }
175
+ let rows = [];
176
+ while (cursor.value) {
177
+ rows.push(cursor.value);
178
+ cursor.continue();
179
+ }
180
+ return rows;
181
+ });
182
+ }
183
+ }
@@ -0,0 +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"}
package/package.json CHANGED
@@ -1,16 +1,15 @@
1
1
  {
2
2
  "name": "xdriver",
3
- "version": "1.1.1",
3
+ "version": "2.0.1",
4
4
  "description": "A simple driver for IndexDB",
5
5
  "main": "./lib/index",
6
6
  "files": [
7
7
  "lib/"
8
8
  ],
9
9
  "scripts": {
10
- "test": "echo \"Error: no test specified\" && exit 1",
11
10
  "build": "tsc",
12
- "build0": "tsc && babel lib --out-dir dist && cd lib && del *.js*",
13
- "build-test": "tsc && webpack --mode production"
11
+ "build:babel": "tsc && babel lib --out-dir dist && cd lib && del *.js*",
12
+ "build:webpack": "tsc && webpack --mode production"
14
13
  },
15
14
  "repository": {
16
15
  "type": "git",
package/lib/Const.d.ts DELETED
@@ -1,32 +0,0 @@
1
- export declare const innerDB: any;
2
- export declare enum TransactionMode {
3
- READ_ONLY = "readonly",
4
- READ_WRITE = "readwrite",
5
- READ_WRITE_FLUSH = "readwriteflush",
6
- VERSION_CHANGE = "versionchange"
7
- }
8
- export declare enum CursorDirection {
9
- NEXT,
10
- NEXT_NO_DUPLICATE,
11
- PREV,
12
- PREV_NO_DUPLICATE
13
- }
14
- export declare class KeyRange {
15
- static leq(x: any): any;
16
- static lt(x: any): any;
17
- static geq(x: any): any;
18
- static gt(x: any): any;
19
- static between(x: any, y: any): any;
20
- static gt_lt(x: any, y: any): any;
21
- static gt_leq(x: any, y: any): any;
22
- static geq_lt(x: any, y: any): any;
23
- static eq(x: any): any;
24
- }
25
- export declare enum IndexdbStatus {
26
- CONNECT_ING = 0,
27
- CONNECT_SUCCESS = 1,
28
- CONNECT_ERROR = 2,
29
- CONNECT_CHANGE = 3,
30
- CONNECT_CLOSE = 4,
31
- DATABASE_ERROR = 0
32
- }
package/lib/Const.js DELETED
@@ -1,57 +0,0 @@
1
- let win = window;
2
- const DBCursor = win.IDBCursor;
3
- const DBKeyRange = win.IDBKeyRange || win.webkitIDBKeyRange || win.msIDBKeyRange;
4
- export const innerDB = win.indexedDB || win.mozIndexedDB || win.webkitIndexedDB || win.msIndexedDB;
5
- const DBTransaction = win.IDBTransaction || win.webkitIDBTransaction || win.msIDBTransaction;
6
- export var TransactionMode;
7
- (function (TransactionMode) {
8
- TransactionMode["READ_ONLY"] = "readonly";
9
- TransactionMode["READ_WRITE"] = "readwrite";
10
- TransactionMode["READ_WRITE_FLUSH"] = "readwriteflush";
11
- TransactionMode["VERSION_CHANGE"] = "versionchange";
12
- })(TransactionMode || (TransactionMode = {}));
13
- export var CursorDirection;
14
- (function (CursorDirection) {
15
- CursorDirection[CursorDirection["NEXT"] = DBCursor.NEXT] = "NEXT";
16
- CursorDirection[CursorDirection["NEXT_NO_DUPLICATE"] = DBCursor.NEXT_NO_DUPLICATE] = "NEXT_NO_DUPLICATE";
17
- CursorDirection[CursorDirection["PREV"] = DBCursor.PREV] = "PREV";
18
- CursorDirection[CursorDirection["PREV_NO_DUPLICATE"] = DBCursor.PREV_NO_DUPLICATE] = "PREV_NO_DUPLICATE";
19
- })(CursorDirection || (CursorDirection = {}));
20
- export class KeyRange {
21
- static leq(x) {
22
- return DBKeyRange.upperBound(x);
23
- }
24
- static lt(x) {
25
- return DBKeyRange.upperBound(x, true);
26
- }
27
- static geq(x) {
28
- return DBKeyRange.lowerBound(x);
29
- }
30
- static gt(x) {
31
- return DBKeyRange.lowerBound(x, true);
32
- }
33
- static between(x, y) {
34
- return DBKeyRange.bound(x, y);
35
- }
36
- static gt_lt(x, y) {
37
- return DBKeyRange.bound(x, y, true, true);
38
- }
39
- static gt_leq(x, y) {
40
- return DBKeyRange.bound(x, y, true, false);
41
- }
42
- static geq_lt(x, y) {
43
- return DBKeyRange.bound(x, y, false, true);
44
- }
45
- static eq(x) {
46
- return DBKeyRange.only(x);
47
- }
48
- }
49
- export var IndexdbStatus;
50
- (function (IndexdbStatus) {
51
- IndexdbStatus[IndexdbStatus["CONNECT_ING"] = 0] = "CONNECT_ING";
52
- IndexdbStatus[IndexdbStatus["CONNECT_SUCCESS"] = 1] = "CONNECT_SUCCESS";
53
- IndexdbStatus[IndexdbStatus["CONNECT_ERROR"] = 2] = "CONNECT_ERROR";
54
- IndexdbStatus[IndexdbStatus["CONNECT_CHANGE"] = 3] = "CONNECT_CHANGE";
55
- IndexdbStatus[IndexdbStatus["CONNECT_CLOSE"] = 4] = "CONNECT_CLOSE";
56
- IndexdbStatus[IndexdbStatus["DATABASE_ERROR"] = 0] = "DATABASE_ERROR";
57
- })(IndexdbStatus || (IndexdbStatus = {}));
package/lib/Database.d.ts DELETED
@@ -1,22 +0,0 @@
1
- import Table from "./Table";
2
- export default class Database {
3
- private name;
4
- private version;
5
- connect: any;
6
- private initialized;
7
- readonly tables: Map<string, Table>;
8
- readonly events: any;
9
- constructor(name: string, version?: number);
10
- initialize(initialized: Function): this;
11
- on(event: any, fn?: Function): this;
12
- isOpen(): boolean;
13
- open(name?: string | undefined, version?: any): Promise<unknown>;
14
- drop(): Promise<unknown>;
15
- close(): void;
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>;
22
- }
package/lib/Database.js DELETED
@@ -1,161 +0,0 @@
1
- import { _instance } from "./Utils";
2
- import { IndexdbStatus, innerDB } from "./Const";
3
- import Table from "./Table";
4
- import { TableIndex } from "./index";
5
- export default class Database {
6
- constructor(name, version) {
7
- this.version = 1;
8
- this.events = {
9
- process: (status) => {
10
- },
11
- success: () => {
12
- },
13
- close: () => {
14
- },
15
- change: () => {
16
- },
17
- error: () => {
18
- }
19
- };
20
- this.name = name;
21
- this.version = version;
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;
32
- }
33
- on(event, fn) {
34
- if (!_instance(event, Object)) {
35
- Object.keys(event).forEach((e) => this.on(e, event[e]));
36
- }
37
- else {
38
- if (!(event in this.events)) {
39
- throw new Error(`Unsupport event type ${event}!`);
40
- }
41
- this.events[event.toString()] = fn;
42
- }
43
- return this;
44
- }
45
- isOpen() {
46
- return this.connect != null;
47
- }
48
- open(name, version) {
49
- if (!name && !version && this.isOpen()) {
50
- return Promise.resolve(this);
51
- }
52
- this.name = name || this.name;
53
- this.version = version || this.version;
54
- const request = innerDB.open(this.name, version);
55
- this.events.process(IndexdbStatus.CONNECT_ING);
56
- request.onerror = (e) => {
57
- const error = e.target.error;
58
- this.events.process(IndexdbStatus.CONNECT_ERROR);
59
- this.events.error.call(this, error);
60
- };
61
- request.onclose = (e) => {
62
- this.events.process(IndexdbStatus.CONNECT_CLOSE);
63
- this.events.close.call(this, e.target.result);
64
- };
65
- return new Promise((resolve, reject) => {
66
- request.onsuccess = (e) => {
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;
83
- this.events.process(IndexdbStatus.CONNECT_SUCCESS);
84
- this.connect.onerror = this.connect.onabort = (e) => {
85
- const error = e.target.error;
86
- this.events.process(IndexdbStatus.DATABASE_ERROR);
87
- this.events.error.call(this, error);
88
- };
89
- this.events.success.call(this, this.connect);
90
- resolve(this);
91
- };
92
- request.onupgradeneeded = (e) => {
93
- this.events.process(IndexdbStatus.CONNECT_SUCCESS);
94
- this.connect = e.target.result;
95
- this.events.change.call(this, this.connect);
96
- this.initialized();
97
- };
98
- });
99
- }
100
- drop() {
101
- this.close();
102
- return new Promise((resolve, reject) => {
103
- let request = innerDB.deleteDatabase(this.name);
104
- request.onerror = function (event) {
105
- reject(event);
106
- };
107
- request.onsuccess = function (event) {
108
- resolve(event);
109
- };
110
- });
111
- }
112
- close() {
113
- if (this.isOpen()) {
114
- this.events.process(IndexdbStatus.CONNECT_CLOSE);
115
- this.connect.close();
116
- this.events.close.call(this, this.connect);
117
- this.connect = null;
118
- }
119
- }
120
- createTable(tableName, options) {
121
- if (this.exists(tableName)) {
122
- throw new Error(`Table ${tableName} is exists`);
123
- }
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;
134
- }
135
- createTables(tables, options) {
136
- return tables.map(table => {
137
- return this.createTable(table, options);
138
- });
139
- }
140
- dropTable(tableName) {
141
- if (this.exists(tableName)) {
142
- this.tables.delete(tableName);
143
- this.connect.deleteObjectStore(tableName);
144
- return true;
145
- }
146
- return false;
147
- }
148
- table(tableName) {
149
- let table = this.tables.get(tableName);
150
- if (typeof table !== "undefined") {
151
- return table;
152
- }
153
- throw new Error(`table ${tableName} not exists!`);
154
- }
155
- exists(table) {
156
- return this.tables.has(table);
157
- }
158
- getTables() {
159
- return this.tables.values();
160
- }
161
- }
package/lib/Driver.d.ts DELETED
@@ -1,14 +0,0 @@
1
- import { KeyRange } from "./Const";
2
- import Database from "./Database";
3
- declare class Driver extends Database {
4
- insert(table: string, data: Array<any>): Promise<unknown>;
5
- query(table: string, keyRange?: KeyRange, limit?: number): any;
6
- select(table: string, where: any, options?: any): any;
7
- selectByKey(table: string, key: any): any;
8
- count(table: string, keyRange?: KeyRange): any;
9
- update(table: string, modify: any, where: any): Promise<unknown> | undefined;
10
- merge(table: string, row: any): Promise<unknown> | undefined;
11
- delete(table: string, key: any): any;
12
- truncate(table: string): any;
13
- }
14
- export default Driver;
package/lib/Driver.js DELETED
@@ -1,31 +0,0 @@
1
- import Database from "./Database";
2
- class Driver extends Database {
3
- insert(table, data) {
4
- return this.table(table).insert(data);
5
- }
6
- query(table, keyRange, limit) {
7
- return this.table(table).query(keyRange, limit);
8
- }
9
- select(table, where, options) {
10
- return this.table(table).select(where, options);
11
- }
12
- selectByKey(table, key) {
13
- return this.table(table).queryByKey(key);
14
- }
15
- count(table, keyRange) {
16
- return this.table(table).count(keyRange);
17
- }
18
- update(table, modify, where) {
19
- return this.table(table).update(modify, where);
20
- }
21
- merge(table, row) {
22
- return this.table(table).merge(row);
23
- }
24
- delete(table, key) {
25
- return this.table(table).delete(key);
26
- }
27
- truncate(table) {
28
- return this.table(table).clear();
29
- }
30
- }
31
- export default Driver;
package/lib/Table.d.ts DELETED
@@ -1,31 +0,0 @@
1
- import { KeyRange, CursorDirection } from "./Const";
2
- import TableIndex from "./TableIndex";
3
- import Database from "./Database";
4
- export default class Table {
5
- readonly name: string;
6
- readonly primaryKey: string;
7
- readonly autoIncrement: boolean;
8
- readonly indexes: Array<TableIndex>;
9
- readonly database: Database;
10
- constructor({ name, primaryKey, autoIncrement, indexes }: {
11
- name?: string | undefined;
12
- primaryKey?: string | undefined;
13
- autoIncrement?: boolean | undefined;
14
- indexes?: never[] | undefined;
15
- }, database: Database);
16
- insert(data: Array<any>): Promise<unknown>;
17
- update(modify: any, where?: any): Promise<unknown> | undefined;
18
- merge(row: any): Promise<unknown> | undefined;
19
- delete(key: any): any;
20
- clear(): any;
21
- dropIndex(name: string): any;
22
- keys(keyRange?: KeyRange, limit?: number): any;
23
- count(keyRange?: KeyRange): any;
24
- query(keyRange?: KeyRange, limit?: number): any;
25
- select(where: any, options?: any): any;
26
- queryByKey(key: any): any;
27
- fetch(keyRange?: KeyRange, direction?: CursorDirection): Promise<unknown>;
28
- queryByIndex(name: string, key: any, count?: number): any;
29
- deplete(direction?: any): Promise<unknown>;
30
- multiple(indexName: string, each: Function, keyRange?: KeyRange, cursorDirection?: CursorDirection): Promise<unknown>;
31
- }
package/lib/Table.js DELETED
@@ -1,173 +0,0 @@
1
- import { _copy, _instance } from "./Utils";
2
- import { CursorDirection, TransactionMode } from "./Const";
3
- const _getObjectStore = (table, mode = TransactionMode.READ_ONLY) => {
4
- const connect = table.database.connect;
5
- const trans = connect.transaction(table.name, mode);
6
- trans.oncomplete = (e) => console.debug(e);
7
- trans.onerror = (e) => console.debug(e);
8
- return trans.objectStore(table.name);
9
- };
10
- const _request = (request, success) => {
11
- return new Promise((resolve, reject) => {
12
- request.onsuccess = (e) => {
13
- !success ? resolve(e.target.result) : success(e.target.result, resolve);
14
- };
15
- request.onerror = (e) => {
16
- reject(e.target.error);
17
- };
18
- });
19
- };
20
- export default class Table {
21
- constructor({ name = '', primaryKey = 'id', autoIncrement = true, indexes = [] }, database) {
22
- this.primaryKey = "id";
23
- this.autoIncrement = false;
24
- this.indexes = [];
25
- this.name = name;
26
- this.primaryKey = primaryKey;
27
- this.autoIncrement = autoIncrement;
28
- this.database = database;
29
- this.indexes = indexes;
30
- }
31
- insert(data) {
32
- const arr = _instance(data, Array) ? data : [data];
33
- const store = _getObjectStore(this, TransactionMode.READ_WRITE);
34
- return new Promise((resolve) => {
35
- let results = [];
36
- arr.forEach((item, index) => {
37
- _request(store.add(item)).then((rs) => {
38
- results.push(rs);
39
- if (index + 1 === arr.length)
40
- resolve(results);
41
- });
42
- });
43
- });
44
- }
45
- update(modify, where) {
46
- const store = _getObjectStore(this, TransactionMode.READ_WRITE);
47
- if (!where) {
48
- _request(store.put(modify));
49
- }
50
- else {
51
- return new Promise((resolve, reject) => {
52
- _request(store.getAll(where)).then((rs) => {
53
- rs = _instance(rs, Array) ? rs : [rs];
54
- let rows = [];
55
- rs.forEach((item, index) => {
56
- _request(store.put(_copy(modify, item))).then((r) => {
57
- rows.push(r);
58
- if (index + 1 === rs.length) {
59
- resolve(rows);
60
- }
61
- });
62
- });
63
- }).catch((err) => reject(err));
64
- });
65
- }
66
- }
67
- merge(row) {
68
- let { primaryKey } = this;
69
- if (row[primaryKey]) {
70
- let where = {};
71
- where[primaryKey] = primaryKey;
72
- let modify = Object.assign({}, row);
73
- delete modify[primaryKey];
74
- return this.update(modify, where);
75
- }
76
- else {
77
- return this.insert(row);
78
- }
79
- }
80
- delete(key) {
81
- return _request(_getObjectStore(this, TransactionMode.READ_WRITE).delete(key));
82
- }
83
- clear() {
84
- return _request(_getObjectStore(this, TransactionMode.READ_WRITE).clear());
85
- }
86
- dropIndex(name) {
87
- return _getObjectStore(this, TransactionMode.READ_WRITE).deleteIndex(name);
88
- }
89
- keys(keyRange, limit) {
90
- const store = _getObjectStore(this);
91
- const request = store.getAllKeys(keyRange, limit);
92
- return _request(request);
93
- }
94
- count(keyRange) {
95
- const request = _getObjectStore(this).count(keyRange);
96
- return _request(request);
97
- }
98
- query(keyRange, limit) {
99
- const request = _getObjectStore(this).getAll(keyRange, limit);
100
- return _request(request);
101
- }
102
- select(where, options) {
103
- if (!where) {
104
- return this.query();
105
- }
106
- let { index = '', limit = void 0, useIndex = false } = options || {};
107
- if (useIndex && index) {
108
- return this.queryByIndex(index, where, limit);
109
- }
110
- return this.query(where, limit);
111
- }
112
- queryByKey(key) {
113
- const request = _getObjectStore(this).get(key);
114
- return _request(request);
115
- }
116
- fetch(keyRange, direction) {
117
- const store = _getObjectStore(this);
118
- const request = store.openCursor(null, direction);
119
- return new Promise((resolve, reject) => {
120
- let rows = [];
121
- _request(request, (cursor) => {
122
- if (!cursor) {
123
- resolve(rows);
124
- return;
125
- }
126
- rows.push(cursor.value);
127
- cursor.continue();
128
- }).catch((err) => {
129
- reject(err);
130
- });
131
- });
132
- }
133
- queryByIndex(name, key, count) {
134
- const store = _getObjectStore(this);
135
- const index = store.index(name);
136
- return _request(index.getAll(key, count));
137
- }
138
- deplete(direction = CursorDirection.NEXT) {
139
- const store = _getObjectStore(this, TransactionMode.READ_WRITE);
140
- const request = store.openCursor(null, direction);
141
- return new Promise((resolve, reject) => {
142
- let rows = [];
143
- _request(request, (cursor) => {
144
- if (!cursor) {
145
- resolve(rows);
146
- return;
147
- }
148
- rows.push(cursor.value);
149
- cursor.continue();
150
- store.delete(cursor.key);
151
- }).catch((err) => {
152
- reject(err);
153
- });
154
- });
155
- }
156
- multiple(indexName, each, keyRange, cursorDirection) {
157
- const store = _getObjectStore(this);
158
- const index = store.index(indexName);
159
- const request = index.openCursor(keyRange, cursorDirection || CursorDirection.NEXT);
160
- return new Promise((resolve, reject) => {
161
- let rows = [];
162
- _request(request, (cursor) => {
163
- if (!cursor) {
164
- resolve(rows);
165
- }
166
- rows.push(cursor.value);
167
- cursor.continue();
168
- }).catch((err) => {
169
- reject(err);
170
- });
171
- });
172
- }
173
- }
@@ -1,7 +0,0 @@
1
- export default class TableIndex {
2
- readonly name: string;
3
- readonly column: Array<string> | string;
4
- readonly unique: boolean;
5
- readonly multiEntry: boolean;
6
- constructor(name: string, column: Array<string> | string, unique?: boolean, multiEntry?: boolean);
7
- }
package/lib/TableIndex.js DELETED
@@ -1,10 +0,0 @@
1
- export default class TableIndex {
2
- constructor(name, column, unique, multiEntry) {
3
- this.unique = false;
4
- this.multiEntry = false;
5
- this.name = name;
6
- this.column = column;
7
- this.unique = !!unique;
8
- this.multiEntry = !!multiEntry;
9
- }
10
- }
package/lib/Utils.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export declare function _instance(example: any, type: any): boolean;
2
- export declare function _copy(src: any, target: any): any;
package/lib/Utils.js DELETED
@@ -1,19 +0,0 @@
1
- export function _instance(example, type) {
2
- let pro = type.prototype;
3
- example = example.__proto__;
4
- while (true) {
5
- if (example === null)
6
- return false;
7
- if (pro === example)
8
- return true;
9
- example = example.__proto__;
10
- }
11
- }
12
- export function _copy(src, target) {
13
- if (!target)
14
- return src;
15
- for (let prop in src) {
16
- target[prop] = src[prop];
17
- }
18
- return target;
19
- }