gs-idb-pro 0.1.0
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/README.md +342 -0
- package/lib/index.cjs +1 -0
- package/lib/index.d.ts +1118 -0
- package/lib/index.js +1 -0
- package/lib/index.web.js +1 -0
- package/package.json +17 -0
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,1118 @@
|
|
|
1
|
+
import { StrOrNum, IDisposable } from 'gs-base';
|
|
2
|
+
import { DbQuery } from 'gs-idb-basic';
|
|
3
|
+
|
|
4
|
+
interface IDbLtRange {
|
|
5
|
+
/**
|
|
6
|
+
* 小于该值
|
|
7
|
+
*/
|
|
8
|
+
lt: IDBValidKey;
|
|
9
|
+
}
|
|
10
|
+
interface IDbGtRange {
|
|
11
|
+
/**
|
|
12
|
+
* 大于该值
|
|
13
|
+
*/
|
|
14
|
+
gt: IDBValidKey;
|
|
15
|
+
}
|
|
16
|
+
interface IDbLteRange {
|
|
17
|
+
/**
|
|
18
|
+
* 小于或等于该值
|
|
19
|
+
*/
|
|
20
|
+
lte: IDBValidKey;
|
|
21
|
+
}
|
|
22
|
+
interface IDbGteRange {
|
|
23
|
+
/**
|
|
24
|
+
* 大于或等于该值
|
|
25
|
+
*/
|
|
26
|
+
gte: IDBValidKey;
|
|
27
|
+
}
|
|
28
|
+
interface IDbLtGtRange extends IDbLtRange, IDbGtRange {
|
|
29
|
+
}
|
|
30
|
+
interface IDbLtGteRange extends IDbLtRange, IDbGteRange {
|
|
31
|
+
}
|
|
32
|
+
interface IDbLteGtRange extends IDbLteRange, IDbGtRange {
|
|
33
|
+
}
|
|
34
|
+
interface IDbLteGteRange extends IDbLteRange, IDbGteRange {
|
|
35
|
+
}
|
|
36
|
+
type IDbRange = IDbLtRange | IDbGtRange | IDbLteRange | IDbGteRange | IDbLtGtRange | IDbLtGteRange | IDbLteGtRange | IDbLteGteRange;
|
|
37
|
+
type IDbQuery = IDBValidKey | IDbRange;
|
|
38
|
+
|
|
39
|
+
interface ICursorOption {
|
|
40
|
+
query?: IDbQuery;
|
|
41
|
+
/**
|
|
42
|
+
* 游标滚动方向,默认为`prev`
|
|
43
|
+
*/
|
|
44
|
+
direction?: IDBCursorDirection;
|
|
45
|
+
/**
|
|
46
|
+
* 预先跳过条数
|
|
47
|
+
*/
|
|
48
|
+
preSkip?: number;
|
|
49
|
+
/**
|
|
50
|
+
* 从指定的`key`开始, 如果存在`preSkip`会先跳过`preSkip`
|
|
51
|
+
* - 当前`store`必须存在其`key`的索引
|
|
52
|
+
*/
|
|
53
|
+
startKey?: IDBValidKey;
|
|
54
|
+
/**
|
|
55
|
+
* 从指定的'Key'与`PrimaryKey`开始
|
|
56
|
+
* - 使用`startPrimaryKey`时必须同时存在`startKey`
|
|
57
|
+
* - 当前`store`必须存在其`key`的索引
|
|
58
|
+
*/
|
|
59
|
+
startPrimaryKey?: IDBValidKey;
|
|
60
|
+
}
|
|
61
|
+
interface ICursorRow {
|
|
62
|
+
cursor: IDBCursorWithValue;
|
|
63
|
+
end: () => void;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface IIndexSchema {
|
|
67
|
+
name: string;
|
|
68
|
+
keyPath?: string | string[];
|
|
69
|
+
unique?: boolean;
|
|
70
|
+
multiEntry?: boolean;
|
|
71
|
+
}
|
|
72
|
+
type IndexSchema = string | IIndexSchema;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 特殊字段定义
|
|
76
|
+
*/
|
|
77
|
+
interface ISpecialFieldSchema {
|
|
78
|
+
/**
|
|
79
|
+
* 为 false 时代表不支持该字段
|
|
80
|
+
* 为 true 时代表使用默认值
|
|
81
|
+
*/
|
|
82
|
+
name: boolean | string;
|
|
83
|
+
/**
|
|
84
|
+
* 是否在`store`未定义同名索引时,自动创建索引
|
|
85
|
+
* 默认值为`true`
|
|
86
|
+
*/
|
|
87
|
+
isIndexed?: boolean;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 为 false 或 name 为 false 时代表不支持该字段
|
|
91
|
+
* 未定义,或为 true 时代表使用默认值
|
|
92
|
+
*/
|
|
93
|
+
type SpecialFieldSchema = boolean | string | ISpecialFieldSchema;
|
|
94
|
+
|
|
95
|
+
interface ISpecialFields {
|
|
96
|
+
/**
|
|
97
|
+
* 添加时间
|
|
98
|
+
* 默认值为 `added_at`
|
|
99
|
+
* 值为`false` 时代表不创建该字段
|
|
100
|
+
*/
|
|
101
|
+
addedTimeField?: SpecialFieldSchema;
|
|
102
|
+
/**
|
|
103
|
+
* 最后修改数据时间
|
|
104
|
+
* 默认值为 `updated_at`
|
|
105
|
+
* 值为`false` 时代表不创建该字段
|
|
106
|
+
*/
|
|
107
|
+
updatedTimeField?: SpecialFieldSchema;
|
|
108
|
+
/**
|
|
109
|
+
* 累计修改次数
|
|
110
|
+
* 默认值为 `false`
|
|
111
|
+
* 默认字段名为 `updated_count`
|
|
112
|
+
*/
|
|
113
|
+
updatedCountField?: SpecialFieldSchema;
|
|
114
|
+
/**
|
|
115
|
+
* 软删除字段
|
|
116
|
+
* 默认值为 `false`
|
|
117
|
+
* 默认字段名为 `deleted`
|
|
118
|
+
*/
|
|
119
|
+
softDeletedField?: SpecialFieldSchema;
|
|
120
|
+
}
|
|
121
|
+
interface IStoreSchemaTemplate extends ISpecialFields {
|
|
122
|
+
/**
|
|
123
|
+
* 主键包含在数据中的路径
|
|
124
|
+
* 未定义时,数据中将不包含主键
|
|
125
|
+
*/
|
|
126
|
+
keyPath?: string | string[];
|
|
127
|
+
autoIncrement?: boolean;
|
|
128
|
+
/**
|
|
129
|
+
* 索引定义,为字符串时表示索引名称与keyPath
|
|
130
|
+
*/
|
|
131
|
+
indexSchemas?: IndexSchema[];
|
|
132
|
+
}
|
|
133
|
+
declare const defaultStoreSchemaTemplate: Readonly<IStoreSchemaTemplate>;
|
|
134
|
+
/**
|
|
135
|
+
* @template R 在有`mapper`时返回值的类型
|
|
136
|
+
* @template V 原始`store`一行数据的类型
|
|
137
|
+
*/
|
|
138
|
+
type GetRowMapper<R, V> = (item: V, key: IDBValidKey, i: number) => R | Promise<R>;
|
|
139
|
+
/**
|
|
140
|
+
* @template R 在有`mapper`时返回值的类型
|
|
141
|
+
* @template V 原始`store`一行数据的类型
|
|
142
|
+
*/
|
|
143
|
+
type SaveRowMapper<R, V> = (item: DbRecord<V>) => DbRecord<V> | Promise<DbRecord<V>>;
|
|
144
|
+
interface IStoreSchema extends IStoreSchemaTemplate {
|
|
145
|
+
name: string;
|
|
146
|
+
/**
|
|
147
|
+
* 在完成数据库Store定义后执行
|
|
148
|
+
* 用于添加默认数据
|
|
149
|
+
* 或 用于升级了store结构后对旧数据进行升级
|
|
150
|
+
*/
|
|
151
|
+
storeDefined?: (store: IStoreUpgradeable) => void | Promise<void>;
|
|
152
|
+
/**
|
|
153
|
+
* 用于在`store`创建时添加默认数据
|
|
154
|
+
* -如果当前`store` 存在 `keyPath` 可以使用任意合法数据
|
|
155
|
+
* -如果不存在 `keyPath` 则必须包含 `key` 与 `value` 字段
|
|
156
|
+
*/
|
|
157
|
+
defaultData?: DbRecord[];
|
|
158
|
+
/**
|
|
159
|
+
* 用于在数据库升级时,在指定版本范围更新的数据
|
|
160
|
+
*/
|
|
161
|
+
versionData?: IVersionData[];
|
|
162
|
+
/**
|
|
163
|
+
* 在调用所有支持`mapper`方法获取数据时的默认映射
|
|
164
|
+
*/
|
|
165
|
+
defaultGetMapper?: GetRowMapper<any, any>;
|
|
166
|
+
/**
|
|
167
|
+
* 用于在`store`导出数据时的映射
|
|
168
|
+
* - `string[]` 代表要导出的字段
|
|
169
|
+
* - 如果不存在`keyPath`,不支持`string[]`
|
|
170
|
+
*/
|
|
171
|
+
exportMapper?: GetRowMapper<any, any> | string[];
|
|
172
|
+
/**
|
|
173
|
+
* 在调所有保存数据方法时的默认映射
|
|
174
|
+
*/
|
|
175
|
+
defaultSaveMapper?: SaveRowMapper<any, any>;
|
|
176
|
+
/**
|
|
177
|
+
* 用于在调用`store.add()`添加数据时的映射
|
|
178
|
+
*/
|
|
179
|
+
addMapper?: SaveRowMapper<any, any>;
|
|
180
|
+
/**
|
|
181
|
+
* 用于在调用`store.replace()`替换数据时的映射
|
|
182
|
+
*/
|
|
183
|
+
replaceMapper?: SaveRowMapper<any, any>;
|
|
184
|
+
/**
|
|
185
|
+
* 用于在调用`store.change()`修改数据时的映射
|
|
186
|
+
*/
|
|
187
|
+
changeMapper?: SaveRowMapper<any, any>;
|
|
188
|
+
}
|
|
189
|
+
type StoreSchema = string | IStoreSchema;
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 中断循环,并且忽略返回的`value`值
|
|
193
|
+
*/
|
|
194
|
+
declare const Break: unique symbol;
|
|
195
|
+
/**
|
|
196
|
+
* 完成循环:结束循环并将当前`value`包含在返回值中
|
|
197
|
+
*/
|
|
198
|
+
declare const Finished: unique symbol;
|
|
199
|
+
/**
|
|
200
|
+
* 忽略`value`值,该`value`值将不会包含在返回值中
|
|
201
|
+
*/
|
|
202
|
+
declare const Continue: unique symbol;
|
|
203
|
+
/**
|
|
204
|
+
* 跳转到指定的Key,并且忽略返回的`value`值
|
|
205
|
+
*/
|
|
206
|
+
declare const ContinueKey: unique symbol;
|
|
207
|
+
/**
|
|
208
|
+
* 跳转到指定的`Key`
|
|
209
|
+
*/
|
|
210
|
+
declare const NextKey: unique symbol;
|
|
211
|
+
/**
|
|
212
|
+
* 跳转到指定的`PrimaryKey`,并且忽略返回的value`值
|
|
213
|
+
*/
|
|
214
|
+
declare const ContinuePrimaryKey: unique symbol;
|
|
215
|
+
/**
|
|
216
|
+
* 跳转到指定的`PrimaryKey`
|
|
217
|
+
*/
|
|
218
|
+
declare const NextPrimaryKey: unique symbol;
|
|
219
|
+
type VoidControl = typeof Break | typeof ContinueKey | typeof ContinuePrimaryKey | null;
|
|
220
|
+
interface IVoidResult {
|
|
221
|
+
control?: VoidControl;
|
|
222
|
+
key?: IDBValidKey;
|
|
223
|
+
primaryKey?: IDBValidKey;
|
|
224
|
+
}
|
|
225
|
+
type VoidResult = typeof Break | IVoidResult | null | undefined | void;
|
|
226
|
+
type ValueControl = VoidControl | typeof Finished | typeof Continue | typeof NextKey | typeof NextPrimaryKey;
|
|
227
|
+
interface IValueResult<V = any> {
|
|
228
|
+
/**
|
|
229
|
+
* 控制循环流程
|
|
230
|
+
*/
|
|
231
|
+
control?: ValueControl;
|
|
232
|
+
value?: V;
|
|
233
|
+
key?: IDBValidKey;
|
|
234
|
+
primaryKey?: IDBValidKey;
|
|
235
|
+
}
|
|
236
|
+
type VoidCallFn<Row = any> = (value: Row, key: IDBValidKey, i: number) => VoidResult | Promise<VoidResult>;
|
|
237
|
+
type ValueCallFn<Row = any, Rtn = any> = (value: Row, key: IDBValidKey, i: number, results: DbRecordResult<Rtn>[]) => void | IValueResult<Rtn> | Promise<void | IValueResult<Rtn>>;
|
|
238
|
+
interface IForEachArg<Row = any> extends ICursorOption {
|
|
239
|
+
fn: VoidCallFn<Row>;
|
|
240
|
+
}
|
|
241
|
+
interface IForEachResultArg<Row = any, Rtn = any> extends ICursorOption {
|
|
242
|
+
fn?: ValueCallFn<Row, Rtn>;
|
|
243
|
+
/**
|
|
244
|
+
* 值为数组时,代表要返回的字段
|
|
245
|
+
*/
|
|
246
|
+
mapper?: GetRowMapper<Rtn, Row> | string[] | undefined;
|
|
247
|
+
}
|
|
248
|
+
interface IForEachable<Row = any> {
|
|
249
|
+
forEach<Val extends Row = Row>(fn: VoidCallFn<Val> | IForEachArg<Val>): Promise<void>;
|
|
250
|
+
forEach<Rtn = DbRecordResult<Row>, Val extends Row = Row>(fn: ValueCallFn<Val, Rtn> | IForEachResultArg<Val, Rtn>, returns: true): Promise<Rtn[]>;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
interface IInfoTarget {
|
|
254
|
+
store: string;
|
|
255
|
+
index?: string;
|
|
256
|
+
}
|
|
257
|
+
type NativeTarget = IDBObjectStore | IDBIndex;
|
|
258
|
+
type OperationTarget = NativeTarget | IInfoTarget;
|
|
259
|
+
declare function isNativeTarget(target: any): target is IDBObjectStore | IDBIndex;
|
|
260
|
+
|
|
261
|
+
type IDbIteratorParser<T = any> = (cusor: IDBCursorWithValue) => IValueResult<T> | Promise<IValueResult<T>>;
|
|
262
|
+
declare const DbIteratorParsers: Readonly<{
|
|
263
|
+
key: ({ primaryKey: value }: IDBCursorWithValue) => {
|
|
264
|
+
value: IDBValidKey;
|
|
265
|
+
};
|
|
266
|
+
value: ({ value }: IDBCursorWithValue) => {
|
|
267
|
+
value: any;
|
|
268
|
+
};
|
|
269
|
+
keyValue: ({ primaryKey: key, value }: IDBCursorWithValue) => {
|
|
270
|
+
value: any[];
|
|
271
|
+
};
|
|
272
|
+
}>;
|
|
273
|
+
type DbIteratorParsersKey = keyof typeof DbIteratorParsers;
|
|
274
|
+
interface IDbIteratorArg extends ICursorOption {
|
|
275
|
+
endsWithNull?: boolean;
|
|
276
|
+
}
|
|
277
|
+
interface IDbIteratorOption<T> extends Readonly<IDbIteratorArg> {
|
|
278
|
+
readonly writable?: boolean;
|
|
279
|
+
readonly parser?: DbIteratorParsersKey | IDbIteratorParser<T>;
|
|
280
|
+
}
|
|
281
|
+
interface IDbIterator<Row = any, Tar extends OperationTarget = OperationTarget> extends IDbIteratorOption<Row>, AsyncIterable<Row> {
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
interface ICursorReader<T> {
|
|
285
|
+
iterator(arg?: IDbQuery | IDbIteratorArg): AsyncIterable<T>;
|
|
286
|
+
iterator(query: IDbQuery, direction?: IDBCursorDirection): AsyncIterable<T>;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* @template R 在有`mapper`时返回值的类型
|
|
291
|
+
* @template V 原始`store`一行数据的类型
|
|
292
|
+
*/
|
|
293
|
+
interface IPageArg<R = any, V = any> extends Omit<IFindMapArg<R, V>, 'preSkip' | 'startKey' | 'startPrimaryKey'> {
|
|
294
|
+
/**
|
|
295
|
+
* 获取第几页
|
|
296
|
+
*/
|
|
297
|
+
page?: number;
|
|
298
|
+
/**
|
|
299
|
+
* 分页大小限制
|
|
300
|
+
* - 默认为100
|
|
301
|
+
*/
|
|
302
|
+
size?: number;
|
|
303
|
+
}
|
|
304
|
+
interface IPageInfo<R = any, V = any> extends Readonly<Required<IPageArg<R, V>>> {
|
|
305
|
+
readonly total: number;
|
|
306
|
+
readonly pages: number;
|
|
307
|
+
/**
|
|
308
|
+
* 下一页需要跳过的行
|
|
309
|
+
*/
|
|
310
|
+
readonly nextSkip: number;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* @template R 在有`mapper`时返回值的类型
|
|
314
|
+
* @template V 原始`store`一行数据的类型
|
|
315
|
+
*/
|
|
316
|
+
interface IPage<R = any, V = any> {
|
|
317
|
+
info: IPageInfo<R, V>;
|
|
318
|
+
rows: R[];
|
|
319
|
+
}
|
|
320
|
+
interface IPageableQuery<T> {
|
|
321
|
+
page<R = ArrayDbRecord<T>>(): Promise<IPage<R, T>>;
|
|
322
|
+
/**
|
|
323
|
+
* 获取一页数据
|
|
324
|
+
* - 仅当在获取第一页与指定页时调用此方法
|
|
325
|
+
* - 如果是获取下一页则应该调用`nextPage(info:IPageInfo)`方法
|
|
326
|
+
* @param arg 获取后续页时,请使用上一次的`info`
|
|
327
|
+
* @param page 获取第几页,如果存在,将覆盖`arg.page`
|
|
328
|
+
*/
|
|
329
|
+
page<R = ArrayDbRecord<T>>(arg: IPageArg<R, T> | IPageInfo<R, T>, page?: number): Promise<IPage<R, T>>;
|
|
330
|
+
/**
|
|
331
|
+
* 获取下一页
|
|
332
|
+
* @param info 上一页的`info`字段
|
|
333
|
+
*/
|
|
334
|
+
nextPage<R = ArrayDbRecord<T>>(info: IPageInfo<R, T>): Promise<IPage<R, T>>;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
interface IValidDataOperationSchema<T extends OperationTarget> {
|
|
338
|
+
storeSchema?: IStoreSchema;
|
|
339
|
+
target: T;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
interface IDbMap<K extends StrOrNum = StrOrNum, V = any, Tar extends OperationTarget = OperationTarget> extends IValidDataOperationSchema<Tar>, IForEachable<V> {
|
|
343
|
+
readonly size: Promise<number>;
|
|
344
|
+
has(key: K): Promise<boolean>;
|
|
345
|
+
get<T extends V = V>(key: K, defaultValue?: T): Promise<T | undefined>;
|
|
346
|
+
getMany<T extends V = V>(keys: K[]): Promise<(T | undefined)[]>;
|
|
347
|
+
set<T extends V = V>(key: K, value: T): Promise<void>;
|
|
348
|
+
setMany<T extends V = V>(values: [K, V][]): Promise<void>;
|
|
349
|
+
delete(key: K): Promise<void>;
|
|
350
|
+
keys(): AsyncIterable<K>;
|
|
351
|
+
values<T extends V = V>(): AsyncIterable<T>;
|
|
352
|
+
entries<T extends V = V>(): AsyncIterable<[K, T]>;
|
|
353
|
+
/**
|
|
354
|
+
* 转换工作目标
|
|
355
|
+
* - 如果当前`target`为`store`,则返回`this`
|
|
356
|
+
* - 如果当前`target`为`index`,则创建基于其`store`的新实例
|
|
357
|
+
*/
|
|
358
|
+
asStore(): IDataReader<V, Tar>;
|
|
359
|
+
/**
|
|
360
|
+
* 转换工作目标
|
|
361
|
+
* - 如果当前`target`为`store`,则返回`this`
|
|
362
|
+
* - 如果当前`target`为`index`,则创建其`store`的副本
|
|
363
|
+
*/
|
|
364
|
+
asStore(writable: true): IDataWriter<V, Tar>;
|
|
365
|
+
batch<R = any, T extends V = V>(fn: (map: IDbMap<K, T, NativeTarget>) => R | Promise<R>): Promise<R>;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
interface IGetAllArgs<T> {
|
|
369
|
+
query?: IDbQuery | null;
|
|
370
|
+
/**
|
|
371
|
+
* 最多返回条数,默认值为1000
|
|
372
|
+
*/
|
|
373
|
+
limit?: number;
|
|
374
|
+
}
|
|
375
|
+
interface IDataReader<Row = any, Tar extends OperationTarget = OperationTarget> extends IValidDataOperationSchema<Tar>, ICursorReader<Row>, IForEachable<Row>, ICursorQueryable<Row>, IPageableQuery<Row> {
|
|
376
|
+
readonly storeName: string;
|
|
377
|
+
readonly storeSchema: IStoreSchema;
|
|
378
|
+
/**
|
|
379
|
+
* @param query
|
|
380
|
+
* @param limit 最多返回条数,默认值为1000
|
|
381
|
+
*/
|
|
382
|
+
all(query?: IDbQuery, limit?: number): Promise<Row[]>;
|
|
383
|
+
all(args?: IGetAllArgs<Row>): Promise<Row[]>;
|
|
384
|
+
/**
|
|
385
|
+
* 返回满足条件的第一条数据
|
|
386
|
+
* @param key
|
|
387
|
+
*/
|
|
388
|
+
get(key: IDbQuery): Promise<Row | undefined>;
|
|
389
|
+
/**
|
|
390
|
+
* 获取满足每一个条件的第一条数据
|
|
391
|
+
* @param keys
|
|
392
|
+
* @param excludeEmpty 是否排除空数据
|
|
393
|
+
*/
|
|
394
|
+
getMany(keys: IDbQuery[], excludeEmpty?: boolean): Promise<Row[]>;
|
|
395
|
+
/**
|
|
396
|
+
* 创建基于`name`索引的新实例
|
|
397
|
+
*/
|
|
398
|
+
index(name: string): IDataReader<Row, Tar>;
|
|
399
|
+
/**
|
|
400
|
+
* 创建基于`name`索引的新实例
|
|
401
|
+
*/
|
|
402
|
+
index(name: string, writable: true): IDataWriter<Row, Tar>;
|
|
403
|
+
/**
|
|
404
|
+
* 转换工作目标
|
|
405
|
+
* - 如果当前`target`为`store`,则返回`this`
|
|
406
|
+
* - 如果当前`target`为`index`,则创建基于其`store`的新实例
|
|
407
|
+
*/
|
|
408
|
+
asStore(): IDataReader<Row, Tar>;
|
|
409
|
+
/**
|
|
410
|
+
* 转换工作目标
|
|
411
|
+
* - 如果当前`target`为`store`,则返回`this`
|
|
412
|
+
* - 如果当前`target`为`index`,则创建其`store`的副本
|
|
413
|
+
*/
|
|
414
|
+
asStore(writable: true): IDataWriter<Row, Tar>;
|
|
415
|
+
/**
|
|
416
|
+
* 创建基于当前`target`的`IDbMap`实例
|
|
417
|
+
*/
|
|
418
|
+
asMap<Key extends StrOrNum = StrOrNum>(): IDbMap<Key, Row, Tar>;
|
|
419
|
+
batchRead<R = any>(fn: (reader: IDataReader<Row, NativeTarget>) => Promise<R | void>): Promise<R>;
|
|
420
|
+
}
|
|
421
|
+
type NativeReader<T = any> = IDataReader<T, NativeTarget>;
|
|
422
|
+
|
|
423
|
+
declare const Save: unique symbol;
|
|
424
|
+
declare const Delete: unique symbol;
|
|
425
|
+
interface IModifyResult<Row, Ctrl extends VoidControl | ValueControl> {
|
|
426
|
+
/**
|
|
427
|
+
* 控制循环流程
|
|
428
|
+
*/
|
|
429
|
+
control?: Ctrl;
|
|
430
|
+
value?: Row;
|
|
431
|
+
key?: IDBValidKey;
|
|
432
|
+
primaryKey?: IDBValidKey;
|
|
433
|
+
/**
|
|
434
|
+
* 保持为空时,不对修改结果进行保存
|
|
435
|
+
*/
|
|
436
|
+
modify?: typeof Save | typeof Delete | null | undefined;
|
|
437
|
+
}
|
|
438
|
+
type VoidModifyFn<Row = any> = (value: Row, key: IDBValidKey, i: number) => void | IModifyResult<Row, VoidControl> | Promise<void | IModifyResult<Row, VoidControl>>;
|
|
439
|
+
type ValueModifyFn<Row = any, Rtn = any> = (value: Row, key: IDBValidKey, i: number, results: DbRecordResult<Rtn>[]) => void | IModifyResult<Row, ValueControl> | Promise<void | IModifyResult<Row, ValueControl>>;
|
|
440
|
+
interface ICursorArg<Row = any> extends ICursorOption {
|
|
441
|
+
fn: VoidModifyFn<Row>;
|
|
442
|
+
}
|
|
443
|
+
interface ICursorResultArg<Row = any, Rtn = any> extends ICursorOption {
|
|
444
|
+
fn: ValueModifyFn<Row, Rtn>;
|
|
445
|
+
/**
|
|
446
|
+
* 值为数组时,代表要返回的字段
|
|
447
|
+
*/
|
|
448
|
+
mapper?: GetRowMapper<Rtn, Row> | string[] | undefined;
|
|
449
|
+
}
|
|
450
|
+
interface ICursorWriter<Row> {
|
|
451
|
+
/**
|
|
452
|
+
* 可写游标迭代器
|
|
453
|
+
* - 如果需要只读游标,请使用`iterator()`
|
|
454
|
+
* @param option
|
|
455
|
+
*/
|
|
456
|
+
cursor(option?: ICursorOption): AsyncIterable<ICursorRow>;
|
|
457
|
+
/**
|
|
458
|
+
* 可写游标操作
|
|
459
|
+
* - 如果需要只读游标,请使用`forEach()`
|
|
460
|
+
* @param fn
|
|
461
|
+
*/
|
|
462
|
+
cursor<Val extends Row = Row>(fn: VoidModifyFn<Val> | ICursorArg<Val>): Promise<void>;
|
|
463
|
+
/**
|
|
464
|
+
* 可写游标操作
|
|
465
|
+
* - 如果需要只读游标,请使用`forEach()`
|
|
466
|
+
* @param fn
|
|
467
|
+
* @param returns
|
|
468
|
+
*/
|
|
469
|
+
cursor<Rtn = DbRecordResult<Row>, Val extends Row = Row>(fn: ValueModifyFn<Val, Rtn> | ICursorResultArg<Val, Rtn>, returns: true): Promise<Rtn[]>;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
interface IChangeRangeArg<Row> {
|
|
473
|
+
newValue: Partial<Row>;
|
|
474
|
+
/**
|
|
475
|
+
* 用于确实影响范围
|
|
476
|
+
* - 如果`record`中不含`key`值,则必须包含`query`
|
|
477
|
+
* - 如果`query`与`record`中的`key`值同时,则优先使用`query`
|
|
478
|
+
*/
|
|
479
|
+
query?: IDbQuery;
|
|
480
|
+
/**
|
|
481
|
+
* 游标滚动方向
|
|
482
|
+
*/
|
|
483
|
+
direction?: IDBCursorDirection;
|
|
484
|
+
}
|
|
485
|
+
interface IRangeWriter<Row> {
|
|
486
|
+
deleteRange(key: IDbQuery, returns?: false): Promise<void>;
|
|
487
|
+
deleteRange(key: IDbQuery, returns: true): Promise<DbRecordResult<Row>[]>;
|
|
488
|
+
deleteRange(key: IDbQuery, option: IDeleteOption<false | undefined>): Promise<void>;
|
|
489
|
+
deleteRange(key: IDbQuery, option: IDeleteOption<true>): Promise<DbRecordResult<Row>[]>;
|
|
490
|
+
deleteRangeMany(keys: IDbQuery[], returns?: false): Promise<void>;
|
|
491
|
+
deleteRangeMany(keys: IDbQuery[], returns: true): Promise<DbRecordResult<Row>[][]>;
|
|
492
|
+
deleteRangeMany(keys: IDbQuery[], option: IDeleteOption<false | undefined>): Promise<void>;
|
|
493
|
+
deleteRangeMany(keys: IDbQuery[], option: IDeleteOption<true>): Promise<DbRecordResult<Row>[][]>;
|
|
494
|
+
changeRange(arg: DbRecord<Partial<Row>> | IChangeRangeArg<Row>, returns?: false): Promise<void>;
|
|
495
|
+
changeRange(arg: DbRecord<Partial<Row>> | IChangeRangeArg<Row>, returns: true): Promise<DbRecordResult<Row>[]>;
|
|
496
|
+
changeRangeMany(args: DbRecord<Partial<Row>> | IChangeRangeArg<Row>[], returns?: false): Promise<void>;
|
|
497
|
+
changeRangeMany(args: DbRecord<Partial<Row>> | IChangeRangeArg<Row>[], returns: true): Promise<DbRecordResult<Row>[][]>;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
interface IChangeManyOption<Rtn extends boolean | undefined> {
|
|
501
|
+
throwIfMissing?: boolean;
|
|
502
|
+
returns?: Rtn;
|
|
503
|
+
}
|
|
504
|
+
interface IDeleteOption<Rtn extends boolean | undefined> {
|
|
505
|
+
/**
|
|
506
|
+
* 游标滚动方向,默认为`prev`
|
|
507
|
+
*/
|
|
508
|
+
direction?: IDBCursorDirection;
|
|
509
|
+
/**
|
|
510
|
+
* 在设置了`softDeletedField`时,是否进行物理删除
|
|
511
|
+
* - 如果值为`false`则仅使用`softDeletedField.name`进行标记
|
|
512
|
+
*/
|
|
513
|
+
physical?: boolean;
|
|
514
|
+
returns?: Rtn;
|
|
515
|
+
}
|
|
516
|
+
interface IDataWriter<Row = any, Tar extends OperationTarget = OperationTarget> extends IDataReader<Row, Tar>, ICursorWriter<Row>, IRangeWriter<Row> {
|
|
517
|
+
/**
|
|
518
|
+
* 原始的`IDBObjectStore`
|
|
519
|
+
* - 仅当`target`为`NativeTarget`时有效
|
|
520
|
+
*/
|
|
521
|
+
readonly nativeStore?: IDBObjectStore;
|
|
522
|
+
add(record: DbRecord<Row>): Promise<DbRecordResult<Row>>;
|
|
523
|
+
addMany(records: DbRecord<Row>[], returns?: false): Promise<void>;
|
|
524
|
+
addMany(records: DbRecord<Row>[], returns: true): Promise<DbRecordResult<Row>[]>;
|
|
525
|
+
/**
|
|
526
|
+
* 添加或跳过数据
|
|
527
|
+
* - 将先检查`primaryKey`是否存在,再检查当前索引是否存在,如果皆不存在则添加,存在将返回存在的值
|
|
528
|
+
* @param record
|
|
529
|
+
*/
|
|
530
|
+
addOrSkip(record: DbRecord<Row>): Promise<DbRecordResult<Row>>;
|
|
531
|
+
addOrSkipMany(records: DbRecord<Row>[], returns?: false): Promise<void>;
|
|
532
|
+
addOrSkipMany(records: DbRecord<Row>[], returns: true): Promise<DbRecordResult<Row>[]>;
|
|
533
|
+
/**
|
|
534
|
+
* 替换数据
|
|
535
|
+
* @warning 仅针对`primaryKey`检查
|
|
536
|
+
* - 如果未指定`primaryKey`或数据不存在时则为添加
|
|
537
|
+
* @param record
|
|
538
|
+
*/
|
|
539
|
+
replace(record: DbRecord<Row>): Promise<DbRecordResult<Row>>;
|
|
540
|
+
replaceMany(records: DbRecord<Row>[], returns?: false): Promise<void>;
|
|
541
|
+
replaceMany(records: DbRecord<Row>[], returns: true): Promise<DbRecordResult<Row>[]>;
|
|
542
|
+
/**
|
|
543
|
+
* 修改数据
|
|
544
|
+
* @warning 仅针对`primaryKey`检查
|
|
545
|
+
* - 如果未指定`primaryKey`或数据不存在时则为跳过或抛出错误
|
|
546
|
+
* @param record
|
|
547
|
+
* @param throwIfMissing 未有`primaryKey`或数据不存在时, 值为`false`将抛出错误,值为`false`时返回`undefined`且不抛出错误
|
|
548
|
+
*/
|
|
549
|
+
change(record: DbRecord<Partial<Row>>, throwIfMissing?: boolean): Promise<DbRecordResult<Row> | undefined>;
|
|
550
|
+
changeMany(records: DbRecord<Partial<Row>>[], returns?: false): Promise<void>;
|
|
551
|
+
changeMany(records: DbRecord<Partial<Row>>[], returns: true): Promise<(DbRecordResult<Row> | undefined)[]>;
|
|
552
|
+
changeMany(records: DbRecord<Partial<Row>>[], option: IChangeManyOption<false | undefined>): Promise<void>;
|
|
553
|
+
changeMany(records: DbRecord<Partial<Row>>[], option: IChangeManyOption<true>): Promise<(DbRecordResult<Row> | undefined)[]>;
|
|
554
|
+
/**
|
|
555
|
+
* 添加或修改数据
|
|
556
|
+
* @warning 仅针对`primaryKey`检查
|
|
557
|
+
* - 如果未指定`primaryKey`或数据不存在时则为添加
|
|
558
|
+
* @param record
|
|
559
|
+
*/
|
|
560
|
+
addOrChange(record: DbRecord<Partial<Row>>): Promise<DbRecordResult<Row> | undefined>;
|
|
561
|
+
addOrChangeMany(records: DbRecord<Partial<Row>>[], returns?: false): Promise<void>;
|
|
562
|
+
addOrChangeMany(records: DbRecord<Partial<Row>>[], returns: true): Promise<DbRecordResult<Row>[]>;
|
|
563
|
+
/**
|
|
564
|
+
* 删除数据
|
|
565
|
+
* @warning 仅针对`primaryKey`检查
|
|
566
|
+
* - 如果未指定`primaryKey`或数据不存在时则为添加
|
|
567
|
+
* @param key
|
|
568
|
+
* @param returns
|
|
569
|
+
*/
|
|
570
|
+
delete(key: IDbQuery, returns?: false): Promise<void>;
|
|
571
|
+
delete(key: IDbQuery, returns: true): Promise<DbRecordResult<Row>>;
|
|
572
|
+
deleteMany(keys: IDbQuery[], returns?: false): Promise<void>;
|
|
573
|
+
deleteMany(keys: IDbQuery[], returns: true): Promise<DbRecordResult<Row>[]>;
|
|
574
|
+
/**
|
|
575
|
+
* 批量写入
|
|
576
|
+
* @param fn
|
|
577
|
+
* @param rollbackOnError 当存在错误时是否回滚
|
|
578
|
+
*/
|
|
579
|
+
batchWrite<R = any>(fn: (writer: IDataWriter<Row, NativeTarget>) => Promise<R | void>, rollbackOnError?: boolean): Promise<R>;
|
|
580
|
+
}
|
|
581
|
+
type NativeWriter<T = any> = IDataWriter<T, NativeTarget>;
|
|
582
|
+
|
|
583
|
+
interface IStoreUpgradeable<T = any> {
|
|
584
|
+
readonly nativeStore: IDBObjectStore;
|
|
585
|
+
readonly upgradeContext: IUpgradeContext;
|
|
586
|
+
readonly storeSchema: IStoreSchema;
|
|
587
|
+
add(versionBounds: IDbVersionBounds, values: DbRecord<T>[], returns?: false): Promise<void>;
|
|
588
|
+
add(versionBounds: IDbVersionBounds, values: DbRecord<T>[], returns: true): Promise<DbRecord<T>>;
|
|
589
|
+
addOrChange(versionBounds: IDbVersionBounds, values: DbRecord<T>[], returns?: false): Promise<void>;
|
|
590
|
+
addOrChange(versionBounds: IDbVersionBounds, values: DbRecord<T>[], returns: true): Promise<DbRecord<T>[]>;
|
|
591
|
+
replace(versionBounds: IDbVersionBounds, values: DbRecord<T>[], returns?: false): Promise<void>;
|
|
592
|
+
replace(versionBounds: IDbVersionBounds, values: DbRecord<T>[], returns: true): Promise<DbRecord<T>[]>;
|
|
593
|
+
call<R = any>(versionBounds: IDbVersionBounds, fn: (store: IDataWriter, upgradeContext?: IUpgradeContext) => R | Promise<R>): Promise<R>;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
interface IVersionValidateContext {
|
|
597
|
+
readonly schema: Readonly<IDbSchema>;
|
|
598
|
+
readonly db: IDBDatabase;
|
|
599
|
+
readonly stores: IDBObjectStore[];
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
interface IDataOperationSchema {
|
|
603
|
+
store: StoreSchema;
|
|
604
|
+
index?: IndexSchema;
|
|
605
|
+
}
|
|
606
|
+
type DataOperationSchema = string | IDataOperationSchema;
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* 默认值为`addOrChangeMany`
|
|
610
|
+
*/
|
|
611
|
+
type ImportUse = keyof Pick<IDataWriter, 'addMany' | 'addOrChangeMany' | 'replaceMany' | 'addOrSkipMany'>;
|
|
612
|
+
interface IMultiBackup {
|
|
613
|
+
export(): Promise<Record<string, any[]>>;
|
|
614
|
+
/**
|
|
615
|
+
* @param data
|
|
616
|
+
* @param returns
|
|
617
|
+
* @param use 默认值为`addOrChangeMany`
|
|
618
|
+
*/
|
|
619
|
+
import(data: Record<string, any[]>, returns?: false, use?: ImportUse): Promise<void>;
|
|
620
|
+
/**
|
|
621
|
+
* @param data
|
|
622
|
+
* @param returns
|
|
623
|
+
* @param use 默认值为`addOrChangeMany`
|
|
624
|
+
*/
|
|
625
|
+
import(data: Record<string, any[]>, returns?: true, use?: ImportUse): Promise<Record<string, any[]>>;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
interface IDataOperators<S1 = any, S2 = any, S3 = any, S4 = any, S5 = any> extends IMultiBackup {
|
|
629
|
+
read<Rtn = any>(fn: (s1: NativeReader<S1>, s2: NativeReader<S2>, s3: NativeReader<S3>, s4: NativeReader<S4>, s5: NativeReader<S5>, ...stores: NativeReader[]) => Rtn | Promise<Rtn>): Promise<Rtn>;
|
|
630
|
+
write<Rtn = any>(fn: (s1: NativeWriter<S1>, s2: NativeWriter<S2>, s3: NativeWriter<S3>, s4: NativeWriter<S4>, s5: NativeWriter<S5>, ...stores: NativeWriter[]) => Rtn | Promise<Rtn>, rollbackOnError?: boolean): Promise<Rtn>;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
interface IIDbPro extends IMultiBackup {
|
|
634
|
+
readonly initialized: boolean;
|
|
635
|
+
readonly schema: IDbSchema;
|
|
636
|
+
readonly factory: IDataOperatorFactory;
|
|
637
|
+
getStoreSchema(name: string): IStoreSchema;
|
|
638
|
+
/**
|
|
639
|
+
* 立即初始化所有定义
|
|
640
|
+
* 一般不需要调用,仅用于开发时验证配置
|
|
641
|
+
*/
|
|
642
|
+
initSchema(): IDbSchema;
|
|
643
|
+
/**
|
|
644
|
+
* 打开低层数据库
|
|
645
|
+
* 仅在当前类库不能满足需求时使用
|
|
646
|
+
*/
|
|
647
|
+
openNativeDb(): Promise<IDBDatabase>;
|
|
648
|
+
/**
|
|
649
|
+
* 定义`store`
|
|
650
|
+
*/
|
|
651
|
+
store<T = any, RT extends IDataWriter<T, IInfoTarget> = IDataWriter<T, IInfoTarget>>(store: StoreSchema): RT;
|
|
652
|
+
store<T = any, RT extends IDataWriter<T, IInfoTarget> = IDataWriter<T, IInfoTarget>>(operation: IDataOperationSchema): RT;
|
|
653
|
+
store<T = any, RT extends IDataWriter<T, IInfoTarget> = IDataWriter<T, IInfoTarget>>(store: StoreSchema, index: IndexSchema): RT;
|
|
654
|
+
/**
|
|
655
|
+
* 定义多个`store`的联合
|
|
656
|
+
*/
|
|
657
|
+
stores<S1 = any, S2 = any, S3 = any, S4 = any, S5 = any, Rtn extends IDataOperators = IDataOperators<S1, S2, S3, S4, S5>>(schemas: DataOperationSchema[]): Rtn;
|
|
658
|
+
/**
|
|
659
|
+
* 定义一个数据会持久化到数据库的`map`,使用键值对存储,主要用于保存配置类信息
|
|
660
|
+
* @param storeName 默认值为`idp-pro-map`
|
|
661
|
+
* @param defaultData
|
|
662
|
+
*/
|
|
663
|
+
map<K extends StrOrNum = StrOrNum, V = any>(storeName?: string, defaultData?: RequiredDbNoneKeyPathRecord<V, K>[]): IDbMap<K, V, IInfoTarget>;
|
|
664
|
+
/**
|
|
665
|
+
* 定义一个数据会持久化到数据库的`map`,使用键值对存储,主要用于保存配置类信息
|
|
666
|
+
* @param storeName 默认值为`idp-pro-map`
|
|
667
|
+
* @param defaultData
|
|
668
|
+
*/
|
|
669
|
+
map<K extends StrOrNum = StrOrNum, V = any>(defaultData?: RequiredDbNoneKeyPathRecord<V, K>[], storeName?: string): IDbMap<K, V, IInfoTarget>;
|
|
670
|
+
/**
|
|
671
|
+
* 向命令行输出当前配置
|
|
672
|
+
* @param showFn 是否输出函数
|
|
673
|
+
*/
|
|
674
|
+
traceSchema(showFn?: boolean): void | Promise<void>;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
interface IDataOperatorFactory {
|
|
678
|
+
newDbMap?<T extends IDbMap>(schema: IValidDataOperationSchema<any>, db?: IIDbPro): T;
|
|
679
|
+
newReader?<T extends IDataReader>(schema: IValidDataOperationSchema<any>, db?: IIDbPro): T;
|
|
680
|
+
newWriter?<T extends IDataWriter>(schema: IValidDataOperationSchema<any>, db?: IIDbPro): T;
|
|
681
|
+
newDataOperators?<T extends IDataOperators>(schemas: IValidDataOperationSchema<IInfoTarget>[], db: IIDbPro): T;
|
|
682
|
+
newStoreUpgradeable?<T extends IStoreUpgradeable>(nativeStore: IDBObjectStore, storeSchema: IStoreSchema, upgradeContext: IUpgradeContext): T;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
type VersionValidateFn = (context: IVersionValidateContext) => true | string | Promise<true | string>;
|
|
686
|
+
interface IDbSchema {
|
|
687
|
+
name: string;
|
|
688
|
+
/**
|
|
689
|
+
* 默认值为1
|
|
690
|
+
*/
|
|
691
|
+
version?: number;
|
|
692
|
+
/**
|
|
693
|
+
* 用于定义`storeSchemas` 默认值的模板
|
|
694
|
+
*/
|
|
695
|
+
storeTemplate?: IStoreSchemaTemplate;
|
|
696
|
+
storeSchemas?: StoreSchema[];
|
|
697
|
+
/**
|
|
698
|
+
* 当 `schema` 与 `db` 版本不同时,的检测器
|
|
699
|
+
* - 默认值为 `true`
|
|
700
|
+
* - 如果为 `false`,则不会检测
|
|
701
|
+
* - 如果为函数,将使用自定义检测器
|
|
702
|
+
*/
|
|
703
|
+
versionDiffValidate?: boolean | VersionValidateFn;
|
|
704
|
+
/**
|
|
705
|
+
* 当 `schema` 与 `db` 版本相同时的检测器
|
|
706
|
+
* - 默认值为 `true`
|
|
707
|
+
* - 如果为 `false`,则不会检测
|
|
708
|
+
* - 如果为函数,将使用自定义检测器
|
|
709
|
+
*/
|
|
710
|
+
versionSameValidate?: boolean | VersionValidateFn;
|
|
711
|
+
/**
|
|
712
|
+
* 创建或升级前执行
|
|
713
|
+
* 通常用于重新定义store前删除同名store
|
|
714
|
+
*/
|
|
715
|
+
beforeUpgrade?: (context: IUpgradeContext) => Promise<void>;
|
|
716
|
+
/**
|
|
717
|
+
* 创建或升级后执行
|
|
718
|
+
* 通常用于升级了store结构后对旧数据进行升级
|
|
719
|
+
*/
|
|
720
|
+
afterUpgrade?: (context: IUpgradeContext) => Promise<void>;
|
|
721
|
+
/**
|
|
722
|
+
* 用于对 schema 检测并赋予默认值
|
|
723
|
+
* 对于小部分自定义默认值时,可以先调用`validateSchemaWithDefaults()`再进行自定义部分处理
|
|
724
|
+
* @param schema
|
|
725
|
+
*/
|
|
726
|
+
validateSchemaWithDefaults?: (schema: IDbSchema) => IDbSchema;
|
|
727
|
+
/**
|
|
728
|
+
* 数据操作器工厂
|
|
729
|
+
*/
|
|
730
|
+
factory?: IDataOperatorFactory;
|
|
731
|
+
}
|
|
732
|
+
type DBSchema = string | IDbSchema;
|
|
733
|
+
|
|
734
|
+
interface IDbVersionBounds {
|
|
735
|
+
oldMin?: number;
|
|
736
|
+
oldMax?: number;
|
|
737
|
+
newMin?: number;
|
|
738
|
+
newMax?: number;
|
|
739
|
+
}
|
|
740
|
+
interface IUpgradeContextArgs {
|
|
741
|
+
readonly newVersion: number;
|
|
742
|
+
readonly oldVersion: number;
|
|
743
|
+
readonly dbSchema: Readonly<IDbSchema>;
|
|
744
|
+
readonly database: IDBDatabase;
|
|
745
|
+
readonly transaction: IDBTransaction;
|
|
746
|
+
}
|
|
747
|
+
interface IUpgradeContext extends IUpgradeContextArgs, IDisposable {
|
|
748
|
+
store<T = any>(storeName: string): IStoreUpgradeable<T>;
|
|
749
|
+
deleteStoreIfExists(storeName: string): void;
|
|
750
|
+
versionIn(bounds: IDbVersionBounds): boolean;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
interface IDbNoneKeyPathRecord<T = any, K = IDBValidKey> {
|
|
754
|
+
key?: K;
|
|
755
|
+
value: T;
|
|
756
|
+
}
|
|
757
|
+
/**
|
|
758
|
+
* 当前`store`不存在`KeyPath`时,用于一行数据记录的表示
|
|
759
|
+
* - 当使用数组时,`key`为数组第二项,`value`为数组第一项
|
|
760
|
+
* - `value`必须存在值,`key`值可选
|
|
761
|
+
*/
|
|
762
|
+
type DbNoneKeyPathRecord<V = any, K extends IDBValidKey = IDBValidKey> = IDbNoneKeyPathRecord<V, K> | [V, K?];
|
|
763
|
+
type RequiredDbNoneKeyPathRecord<V = any, K extends IDBValidKey = IDBValidKey> = Required<IDbNoneKeyPathRecord<V, K>> | [V, K];
|
|
764
|
+
type ArrayDbRecord<V = any, K extends IDBValidKey = IDBValidKey> = [V, K?] | V;
|
|
765
|
+
type DbRecord<V = any, K extends IDBValidKey = IDBValidKey> = DbNoneKeyPathRecord<V, K> | V;
|
|
766
|
+
type DbRecordResult<V = any, K = IDBValidKey> = Required<IDbNoneKeyPathRecord<V, K>> | V;
|
|
767
|
+
interface IVersionData {
|
|
768
|
+
/**
|
|
769
|
+
* 满足更新数据的版本范围
|
|
770
|
+
*/
|
|
771
|
+
version: IDbVersionBounds;
|
|
772
|
+
/**
|
|
773
|
+
* 需要更新的数据记录
|
|
774
|
+
*/
|
|
775
|
+
data: DbRecord[];
|
|
776
|
+
/**
|
|
777
|
+
* 用于添加数据的方法,默认为`addOrChange`
|
|
778
|
+
*/
|
|
779
|
+
use?: keyof Pick<IStoreUpgradeable, 'add' | 'addOrChange' | 'replace'>;
|
|
780
|
+
}
|
|
781
|
+
declare function parseDbNoneKeyPathRecord<T = any, K extends IDBValidKey = IDBValidKey>(record: any): Required<IDbNoneKeyPathRecord<T, K>>;
|
|
782
|
+
|
|
783
|
+
type FindFn<T> = (value: T, primaryKey: IDBValidKey, index: number) => boolean | void | Promise<boolean | void>;
|
|
784
|
+
/**
|
|
785
|
+
* @template V 原始`store`一行数据的类型
|
|
786
|
+
*/
|
|
787
|
+
interface IFindArg<V = any> extends ICursorOption {
|
|
788
|
+
fn?: FindFn<V> | undefined;
|
|
789
|
+
/**
|
|
790
|
+
* 最多查找次数,默认值为20000
|
|
791
|
+
*/
|
|
792
|
+
maxEmptyChecks?: number;
|
|
793
|
+
}
|
|
794
|
+
/**
|
|
795
|
+
* @template R 在有`mapper`时返回值的类型
|
|
796
|
+
* @template V 原始`store`一行数据的类型
|
|
797
|
+
*/
|
|
798
|
+
interface IFindMapArg<R = any, V = any> extends IFindArg<V> {
|
|
799
|
+
/**
|
|
800
|
+
* 值为数组时,代表要返回的字段
|
|
801
|
+
*/
|
|
802
|
+
mapper?: GetRowMapper<R, V> | string[] | undefined;
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* @template R 在有`mapper`时返回值的类型
|
|
806
|
+
* @template V 原始`store`一行数据的类型
|
|
807
|
+
*/
|
|
808
|
+
interface IFilterArg<R = any, V = any> extends IFindMapArg<R, V> {
|
|
809
|
+
/**
|
|
810
|
+
* 最多返回记录数,默认值为1000
|
|
811
|
+
*/
|
|
812
|
+
limit?: number;
|
|
813
|
+
}
|
|
814
|
+
interface ICursorQueryable<Row> {
|
|
815
|
+
count(): Promise<number>;
|
|
816
|
+
count(query: IDbQuery | FindFn<Row>, direction?: IDBCursorDirection): Promise<number>;
|
|
817
|
+
count(query: IDbQuery, fn: FindFn<Row>, direction?: IDBCursorDirection): Promise<number>;
|
|
818
|
+
count(arg: IFindArg<Row>): Promise<number>;
|
|
819
|
+
getRange(key: IDbQuery, direction?: IDBCursorDirection): Promise<DbRecordResult<Row>[]>;
|
|
820
|
+
getRangeMany(keys: IDbQuery[], direction?: IDBCursorDirection): Promise<DbRecordResult<Row>[]>;
|
|
821
|
+
find(): Promise<Row | undefined>;
|
|
822
|
+
find(query: IDbQuery | FindFn<Row>, direction?: IDBCursorDirection): Promise<ArrayDbRecord<Row> | undefined>;
|
|
823
|
+
find(query: IDbQuery, fn: FindFn<Row>, direction?: IDBCursorDirection): Promise<ArrayDbRecord<Row> | undefined>;
|
|
824
|
+
find<R = ArrayDbRecord<Row>>(args: IFindMapArg<R, Row>): Promise<Row | undefined>;
|
|
825
|
+
filter(): Promise<Row[]>;
|
|
826
|
+
filter<R = ArrayDbRecord<Row>>(args: IFilterArg<R, Row>): Promise<Row[]>;
|
|
827
|
+
filter(query: IDbQuery | FindFn<Row>, direction?: IDBCursorDirection): Promise<ArrayDbRecord<Row>[]>;
|
|
828
|
+
filter(query: IDbQuery, fn: FindFn<Row>, direction?: IDBCursorDirection): Promise<ArrayDbRecord<Row>[]>;
|
|
829
|
+
export<R = ArrayDbRecord<Row>>(): Promise<ArrayDbRecord<R>[]>;
|
|
830
|
+
export<R = ArrayDbRecord<Row>>(query: IDbQuery | FindFn<Row>): Promise<ArrayDbRecord<R>[]>;
|
|
831
|
+
export<R = ArrayDbRecord<Row>>(query: IDbQuery, fn: FindFn<Row>): Promise<ArrayDbRecord<R>[]>;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
type ReadCallFn<T> = (target: NativeTarget) => T | undefined | Promise<T | undefined>;
|
|
835
|
+
type WriteCallFn<T> = (target: NativeTarget, store: IDBObjectStore) => T | undefined | Promise<T | undefined>;
|
|
836
|
+
interface INativeOperationResult<T extends NativeTarget> {
|
|
837
|
+
readonly db?: IDBDatabase;
|
|
838
|
+
readonly tx?: IDBTransaction;
|
|
839
|
+
readonly nativeStore?: IDBObjectStore;
|
|
840
|
+
readonly target: T;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
type ExportDataTo = keyof Pick<IStoreSchema, 'defaultData' | 'versionData'>;
|
|
844
|
+
declare const defaultSpecialFields: Readonly<ISpecialFields>;
|
|
845
|
+
interface IGenerateOption<Ts extends boolean | number | undefined> {
|
|
846
|
+
/**
|
|
847
|
+
* 用于确定特殊索引的字段,未定义则使用`defaultSpecialFields`
|
|
848
|
+
*/
|
|
849
|
+
specialFields?: ISpecialFields;
|
|
850
|
+
/**
|
|
851
|
+
* 用于定义导出数据的位置,未定义则不导出数据
|
|
852
|
+
*/
|
|
853
|
+
dataExportTarget?: ExportDataTo;
|
|
854
|
+
/**
|
|
855
|
+
* 是否输出为字符串
|
|
856
|
+
* - 为`true`时输出为代码最小换行为`160`字符串,否则为`IDbSchema`对象
|
|
857
|
+
* - 为数字时为输出代码最小换行长度,如果小于1,则会使用`1`
|
|
858
|
+
*/
|
|
859
|
+
asString?: Ts;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
interface IOpenCursorArg extends ICursorOption {
|
|
863
|
+
fn: (cursor: IDBCursorWithValue) => void | false | Promise<void | false>;
|
|
864
|
+
}
|
|
865
|
+
declare class DataOperationBase implements IValidDataOperationSchema<any>, IForEachable {
|
|
866
|
+
#private;
|
|
867
|
+
readonly idbPro?: IIDbPro;
|
|
868
|
+
readonly target: any;
|
|
869
|
+
constructor(schema: IValidDataOperationSchema<any>, db?: IIDbPro);
|
|
870
|
+
get storeName(): string;
|
|
871
|
+
get storeSchema(): IStoreSchema;
|
|
872
|
+
get factory(): IDataOperatorFactory;
|
|
873
|
+
get nativeStore(): IDBObjectStore | undefined;
|
|
874
|
+
get keyPath(): string | string[] | null | undefined;
|
|
875
|
+
forEach<T = any>(fn: VoidCallFn<T> | IForEachArg<T>): Promise<void>;
|
|
876
|
+
forEach<R = DbRecordResult, T = any>(fn: ValueCallFn<T, R> | IForEachResultArg<T, R>, returns: true): Promise<R[]>;
|
|
877
|
+
protected tx(writable?: false): Promise<INativeOperationResult<NativeTarget>>;
|
|
878
|
+
protected tx(writable: true): Promise<INativeOperationResult<IDBObjectStore>>;
|
|
879
|
+
protected tx<T>(fn: ReadCallFn<T>): Promise<T>;
|
|
880
|
+
protected tx<T>(writable: false, fn?: ReadCallFn<T>): Promise<T>;
|
|
881
|
+
protected tx<T>(writable: true, fn?: WriteCallFn<T>, rollbackOnError?: boolean): Promise<T>;
|
|
882
|
+
protected openCursor(arg: IOpenCursorArg, writable?: boolean): Promise<void>;
|
|
883
|
+
protected cursorVoid({ query, direction, preSkip, startKey, startPrimaryKey, fn }: ICursorArg, writable: boolean): Promise<void>;
|
|
884
|
+
protected cursorResult({ query, direction, preSkip, startKey, startPrimaryKey, fn, mapper: mpr }: ICursorResultArg, writable: boolean): Promise<any>;
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
declare class DataOperators<S1 = any, S2 = any, S3 = any, S4 = any, S5 = any> implements IDataOperators<S1, S2, S3, S4, S5> {
|
|
888
|
+
#private;
|
|
889
|
+
readonly idbPro: IIDbPro;
|
|
890
|
+
readonly schemas: IValidDataOperationSchema<IInfoTarget>[];
|
|
891
|
+
constructor(idbPro: IIDbPro, schemas: IValidDataOperationSchema<IInfoTarget>[]);
|
|
892
|
+
get storeNames(): string[];
|
|
893
|
+
read<Rtn = any>(fn: (s1: NativeReader<S1>, s2: NativeReader<S2>, s3: NativeReader<S3>, s4: NativeReader<S4>, s5: NativeReader<S5>, ...stores: NativeReader[]) => (Promise<Rtn> | Rtn)): Promise<Rtn>;
|
|
894
|
+
write<Rtn = any>(fn: (s1: NativeWriter<S1>, s2: NativeWriter<S2>, s3: NativeWriter<S3>, s4: NativeWriter<S4>, s5: NativeWriter<S5>, ...stores: NativeWriter[]) => (Promise<Rtn> | Rtn), rollbackOnError?: boolean): Promise<Rtn>;
|
|
895
|
+
export(): Promise<Record<string, any[]>>;
|
|
896
|
+
import(data: Record<string, any[]>, returns?: false, use?: ImportUse): Promise<void>;
|
|
897
|
+
import(data: Record<string, any[]>, returns?: true, use?: ImportUse): Promise<Record<string, any[]>>;
|
|
898
|
+
protected tx(method: keyof Pick<IDataOperatorFactory, 'newReader' | 'newWriter'>, fn: Function, writable?: boolean, rollbackOnError?: boolean): Promise<any>;
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
declare class DataReader<Row, Tar extends OperationTarget = OperationTarget> extends DataOperationBase implements IDataReader<Row, Tar> {
|
|
902
|
+
all(query?: IDbQuery, limit?: number): Promise<Row[]>;
|
|
903
|
+
all(args?: IGetAllArgs<Row>): Promise<Row[]>;
|
|
904
|
+
count(): Promise<number>;
|
|
905
|
+
count(query: IDbQuery | FindFn<Row>, direction?: IDBCursorDirection): Promise<number>;
|
|
906
|
+
count(query: IDbQuery, fn: FindFn<Row>, direction?: IDBCursorDirection): Promise<number>;
|
|
907
|
+
count(arg: IFindArg<Row>): Promise<number>;
|
|
908
|
+
get(key: IDbQuery): Promise<Row | undefined>;
|
|
909
|
+
getMany(keys: IDbQuery[], excludeEmpty?: boolean): Promise<Row[]>;
|
|
910
|
+
getRange(query: IDbQuery, direction?: IDBCursorDirection): Promise<DbRecordResult<Row>[]>;
|
|
911
|
+
getRangeMany(keys: IDbQuery[], direction?: IDBCursorDirection): Promise<DbRecordResult<Row>[]>;
|
|
912
|
+
index(name: string): IDataReader<Row, Tar>;
|
|
913
|
+
index(name: string, writable: true): IDataWriter<Row, Tar>;
|
|
914
|
+
asStore(): IDataReader<Row, Tar>;
|
|
915
|
+
asStore(writable: true): IDataWriter<Row, Tar>;
|
|
916
|
+
batchRead<R = any>(fn: (reader: IDataReader<Row, NativeTarget>) => Promise<void | R>): Promise<R>;
|
|
917
|
+
iterator(query: IDbQuery, direction?: IDBCursorDirection): AsyncIterable<Row>;
|
|
918
|
+
iterator(arg: IDbIteratorArg): AsyncIterable<Row>;
|
|
919
|
+
filter(): Promise<Row[]>;
|
|
920
|
+
filter<R = ArrayDbRecord<Row>>(args: IFilterArg<R, Row>): Promise<R[]>;
|
|
921
|
+
filter(query: IDbQuery | FindFn<Row>, direction?: IDBCursorDirection): Promise<ArrayDbRecord<Row>[]>;
|
|
922
|
+
filter(query: IDbQuery, fn: FindFn<Row>, direction?: IDBCursorDirection): Promise<ArrayDbRecord<Row>[]>;
|
|
923
|
+
filter(arg1?: IDbQuery | FindFn<Row> | IFilterArg<Row>, arg2?: IDBCursorDirection | FindFn<Row>, arg3?: IDBCursorDirection, limit?: number): Promise<ArrayDbRecord<Row>[]>;
|
|
924
|
+
find(): Promise<Row | undefined>;
|
|
925
|
+
find(query: IDbQuery | FindFn<Row>, direction?: IDBCursorDirection): Promise<ArrayDbRecord<Row> | undefined>;
|
|
926
|
+
find(query: IDbQuery, fn: FindFn<Row>, direction?: IDBCursorDirection): Promise<ArrayDbRecord<Row> | undefined>;
|
|
927
|
+
find<R = ArrayDbRecord<Row>>(args: IFindMapArg<R, Row>): Promise<Row | undefined>;
|
|
928
|
+
page<R = ArrayDbRecord<Row>>(): Promise<IPage<R, Row>>;
|
|
929
|
+
page<R = ArrayDbRecord<Row>>(arg: IPageArg<R, Row> | IPageInfo<R, Row>, page?: number): Promise<IPage<R, Row>>;
|
|
930
|
+
nextPage<R = Row>(info: IPageInfo<R, Row>): Promise<IPage<R, Row>>;
|
|
931
|
+
export<R = ArrayDbRecord<Row>>(): Promise<ArrayDbRecord<R>[]>;
|
|
932
|
+
export<R = ArrayDbRecord<Row>>(query: IDbQuery | FindFn<Row>): Promise<ArrayDbRecord<R>[]>;
|
|
933
|
+
export<R = ArrayDbRecord<Row>>(query: IDbQuery, fn: FindFn<Row>): Promise<ArrayDbRecord<R>[]>;
|
|
934
|
+
asMap<Key extends StrOrNum = StrOrNum>(): IDbMap<Key, Row, Tar>;
|
|
935
|
+
protected createOperator(target: OperationTarget, writable?: boolean): any;
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
declare class DataWriter<Row, Tar extends OperationTarget = OperationTarget> extends DataReader<Row, Tar> implements IDataWriter<Row, Tar> {
|
|
939
|
+
add(record: DbRecord<Row>): Promise<DbRecordResult<Row>>;
|
|
940
|
+
addMany(records: DbRecord<Row>[], returns?: false): Promise<void>;
|
|
941
|
+
addMany(records: DbRecord<Row>[], returns: true): Promise<DbRecordResult<Row>[]>;
|
|
942
|
+
addOrSkip(record: DbRecord<Row>): Promise<DbRecordResult<Row>>;
|
|
943
|
+
addOrSkipMany(records: DbRecord<Row>[], returns?: false): Promise<void>;
|
|
944
|
+
addOrSkipMany(records: DbRecord<Row>[], returns: true): Promise<DbRecordResult<Row>[]>;
|
|
945
|
+
replace(record: DbRecord<Row>): Promise<DbRecordResult<Row>>;
|
|
946
|
+
replaceMany(records: DbRecord<Row>[], returns?: false): Promise<void>;
|
|
947
|
+
replaceMany(records: DbRecord<Row>[], returns: true): Promise<DbRecordResult<Row>[]>;
|
|
948
|
+
change(record: DbRecord<Partial<Row>>, throwIfMissing?: boolean): Promise<DbRecordResult<Row> | undefined>;
|
|
949
|
+
changeMany(records: DbRecord<Partial<Row>>[], returns?: false): Promise<void>;
|
|
950
|
+
changeMany(records: DbRecord<Partial<Row>>[], returns: true): Promise<(DbRecordResult<Row> | undefined)[]>;
|
|
951
|
+
changeMany(records: DbRecord<Partial<Row>>[], option: IChangeManyOption<false | undefined>): Promise<void>;
|
|
952
|
+
changeMany(records: DbRecord<Partial<Row>>[], option: IChangeManyOption<true>): Promise<(DbRecordResult<Row> | undefined)[]>;
|
|
953
|
+
addOrChange(record: DbRecord<Partial<Row>>): Promise<DbRecordResult<Row> | undefined>;
|
|
954
|
+
addOrChangeMany(records: DbRecord<Partial<Row>>[], returns?: false): Promise<void>;
|
|
955
|
+
addOrChangeMany(records: DbRecord<Partial<Row>>[], returns: true): Promise<DbRecordResult<Row>[]>;
|
|
956
|
+
delete(key: IDbQuery, returns?: false): Promise<void>;
|
|
957
|
+
delete(key: IDbQuery, returns: true): Promise<DbRecordResult<Row>>;
|
|
958
|
+
deleteMany(keys: IDbQuery[], returns?: false): Promise<void>;
|
|
959
|
+
deleteMany(keys: IDbQuery[], returns: true): Promise<DbRecordResult<Row>[]>;
|
|
960
|
+
deleteRange(key: IDbQuery, returns?: false): Promise<void>;
|
|
961
|
+
deleteRange(key: IDbQuery, returns: true): Promise<DbRecordResult<Row>[]>;
|
|
962
|
+
deleteRange(key: IDbQuery, option: IDeleteOption<false | undefined>): Promise<void>;
|
|
963
|
+
deleteRange(key: IDbQuery, option: IDeleteOption<true>): Promise<DbRecordResult<Row>[]>;
|
|
964
|
+
deleteRangeMany(keys: IDbQuery[], returns?: false): Promise<void>;
|
|
965
|
+
deleteRangeMany(keys: IDbQuery[], returns: true): Promise<DbRecordResult<Row>[][]>;
|
|
966
|
+
deleteRangeMany(keys: IDbQuery[], option: IDeleteOption<false | undefined>): Promise<void>;
|
|
967
|
+
deleteRangeMany(keys: IDbQuery[], option: IDeleteOption<true>): Promise<DbRecordResult<Row>[][]>;
|
|
968
|
+
changeRange(arg: DbRecord<Partial<Row>> | IChangeRangeArg<Row>, returns?: false): Promise<void>;
|
|
969
|
+
changeRange(arg: DbRecord<Partial<Row>> | IChangeRangeArg<Row>, returns: true): Promise<DbRecordResult<Row>[]>;
|
|
970
|
+
changeRangeMany(args: DbRecord<Partial<Row>> | IChangeRangeArg<Row>[], returns?: false): Promise<void>;
|
|
971
|
+
changeRangeMany(args: DbRecord<Partial<Row>> | IChangeRangeArg<Row>[], returns: true): Promise<DbRecordResult<Row>[][]>;
|
|
972
|
+
cursor(option?: ICursorOption): AsyncIterable<ICursorRow>;
|
|
973
|
+
cursor<Val extends Row = Row>(fn: VoidModifyFn<Val> | ICursorArg<Val>): Promise<void>;
|
|
974
|
+
cursor<Rtn = DbRecordResult<Row>, Val extends Row = Row>(fn: ValueModifyFn<Val, Rtn> | ICursorResultArg<Val, Rtn>, returns: true): Promise<Rtn[]>;
|
|
975
|
+
batchWrite<R = any>(fn: (writer: IDataWriter<Row, NativeTarget>) => Promise<void | R>, rollbackOnError?: boolean): Promise<R>;
|
|
976
|
+
protected changeByPk({ pk, record, fn, requiredPk, getOld, requiredOld, saveMapper, getMapper }: {
|
|
977
|
+
pk?: IDbQuery;
|
|
978
|
+
record?: DbRecord<Partial<Row>>;
|
|
979
|
+
fn: (store: IDBObjectStore, pk: DbQuery, newValue: any, oldValue: any, keyPath?: string | string[]) => void | [Row, DbQuery?] | Promise<void | [Row, DbQuery?]>;
|
|
980
|
+
requiredPk?: boolean;
|
|
981
|
+
getOld?: boolean;
|
|
982
|
+
requiredOld?: boolean;
|
|
983
|
+
saveMapper?: SaveRowMapper<any, any>;
|
|
984
|
+
getMapper?: GetRowMapper<any, any>;
|
|
985
|
+
}): any;
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
declare class DbIterator<T> extends DataOperationBase implements IDbIterator<T> {
|
|
989
|
+
direction: IDBCursorDirection;
|
|
990
|
+
query: IDbQuery;
|
|
991
|
+
writable: boolean;
|
|
992
|
+
parser: IDbIteratorParser;
|
|
993
|
+
endsWithNull: boolean;
|
|
994
|
+
preSkip?: number;
|
|
995
|
+
startKey?: IDBValidKey;
|
|
996
|
+
startPrimaryKey?: IDBValidKey;
|
|
997
|
+
constructor(schema: IValidDataOperationSchema<any>, db?: IIDbPro, option?: IDbIteratorOption<T>);
|
|
998
|
+
[Symbol.asyncIterator](): AsyncIterator<T>;
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
declare class DbMap<K extends StrOrNum = StrOrNum, V = any, Tar extends OperationTarget = OperationTarget> extends DataOperationBase implements IDbMap<K, V, Tar> {
|
|
1002
|
+
get size(): Promise<number>;
|
|
1003
|
+
delete(key: K): Promise<void>;
|
|
1004
|
+
batch<R = any, T extends V = V>(fn: (map: IDbMap<K, T, NativeTarget>) => (Promise<R> | R)): Promise<R>;
|
|
1005
|
+
asStore(): IDataReader<V, Tar>;
|
|
1006
|
+
asStore(writable: true): IDataWriter<V, Tar>;
|
|
1007
|
+
entries<T extends V = V>(): AsyncIterable<[K, T]>;
|
|
1008
|
+
get<T extends V = V>(key: K, defaultValue?: T): Promise<T | undefined>;
|
|
1009
|
+
getMany<T extends V = V>(keys: K[]): Promise<(T | undefined)[]>;
|
|
1010
|
+
has(key: K): Promise<boolean>;
|
|
1011
|
+
keys(): AsyncIterable<K>;
|
|
1012
|
+
set<T extends V = V>(key: K, value: T): Promise<void>;
|
|
1013
|
+
setMany<T extends V = V>(values: [K, V][]): Promise<void>;
|
|
1014
|
+
values<T extends V = V>(): AsyncIterable<T>;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
declare function dbStore<T = any, RT extends IDataWriter<T, IInfoTarget> = IDataWriter<T, IInfoTarget>>(schema: StoreSchema): RT;
|
|
1018
|
+
declare function dbStore<T = any, RT extends IDataWriter<T, IInfoTarget> = IDataWriter<T, IInfoTarget>>(schema: IDataOperationSchema): RT;
|
|
1019
|
+
declare function dbStore<T = any, RT extends IDataWriter<T, IInfoTarget> = IDataWriter<T, IInfoTarget>>(store: StoreSchema, index: IndexSchema): RT;
|
|
1020
|
+
declare function dbMap<K extends StrOrNum = StrOrNum, V = any>(storeName?: string, defaultData?: RequiredDbNoneKeyPathRecord<V, K>[]): IDbMap<K, V, IInfoTarget>;
|
|
1021
|
+
declare function dbMap<K extends StrOrNum = StrOrNum, V = any>(defaultData?: RequiredDbNoneKeyPathRecord<V, K>[], storeName?: string): IDbMap<K, V, IInfoTarget>;
|
|
1022
|
+
declare function dbStores<S1 = any, S2 = any, S3 = any, S4 = any, S5 = any, Rtn extends IDataOperators = IDataOperators<S1, S2, S3, S4, S5>>(schemas: DataOperationSchema[]): Rtn;
|
|
1023
|
+
/**
|
|
1024
|
+
* 释放默认数据库实例(如果存在)
|
|
1025
|
+
*/
|
|
1026
|
+
declare function releaseDefaultDB(): void;
|
|
1027
|
+
|
|
1028
|
+
declare class IDbPro implements IIDbPro {
|
|
1029
|
+
#private;
|
|
1030
|
+
constructor(schema: DBSchema, skipPreOpenValidation?: true);
|
|
1031
|
+
/**
|
|
1032
|
+
* 默认实例
|
|
1033
|
+
*/
|
|
1034
|
+
static get defaultDb(): IIDbPro;
|
|
1035
|
+
get initialized(): boolean;
|
|
1036
|
+
get schema(): IDbSchema;
|
|
1037
|
+
get storeNames(): string[];
|
|
1038
|
+
get factory(): IDataOperatorFactory;
|
|
1039
|
+
/**
|
|
1040
|
+
* 释放默认数据库实例(如果存在)
|
|
1041
|
+
*/
|
|
1042
|
+
static releaseDefaultDB(): void;
|
|
1043
|
+
/**
|
|
1044
|
+
* 从已经存在的`name`数据库中打开
|
|
1045
|
+
* @warning 该方法仅用于不需要修改原有数据库结构的场景
|
|
1046
|
+
* - 如果需要修改,请使用 `generateDbSchema()` 从现有数据库生成配置,且适当修改后重新打开
|
|
1047
|
+
* @param name
|
|
1048
|
+
*/
|
|
1049
|
+
static openExistDb(name: string): Promise<IIDbPro>;
|
|
1050
|
+
static store<T = any, RT extends IDataWriter<T> = IDataWriter<T>>(store: StoreSchema): RT;
|
|
1051
|
+
static store<T = any, RT extends IDataWriter<T> = IDataWriter<T>>(operation: IDataOperationSchema): RT;
|
|
1052
|
+
static store<T = any, RT extends IDataWriter<T> = IDataWriter<T>>(store: StoreSchema, index: IndexSchema): RT;
|
|
1053
|
+
static stores<S1 = any, S2 = any, S3 = any, S4 = any, S5 = any, Rtn extends IDataOperators = IDataOperators<S1, S2, S3, S4, S5>>(schemas: DataOperationSchema[]): IDataOperators<Rtn>;
|
|
1054
|
+
static map<K extends StrOrNum = StrOrNum, V = any>(storeName?: string, defaultData?: (IDbNoneKeyPathRecord<V, K> | [V, K])[]): IDbMap<K, V, IInfoTarget>;
|
|
1055
|
+
static map<K extends StrOrNum = StrOrNum, V = any>(defaultData?: (IDbNoneKeyPathRecord<V, K> | [V, K])[], storeName?: string): IDbMap<K, V, IInfoTarget>;
|
|
1056
|
+
openNativeDb(): Promise<IDBDatabase>;
|
|
1057
|
+
store<T = any, RT extends IDataWriter<T, IInfoTarget> = IDataWriter<T, IInfoTarget>>(store: StoreSchema): RT;
|
|
1058
|
+
store<T = any, RT extends IDataWriter<T, IInfoTarget> = IDataWriter<T, IInfoTarget>>(operation: IDataOperationSchema): RT;
|
|
1059
|
+
store<T = any, RT extends IDataWriter<T, IInfoTarget> = IDataWriter<T, IInfoTarget>>(store: StoreSchema, index: IndexSchema): RT;
|
|
1060
|
+
stores<S1 = any, S2 = any, S3 = any, S4 = any, S5 = any, Rtn extends IDataOperators = IDataOperators<S1, S2, S3, S4, S5>>(schemas: DataOperationSchema[]): Rtn;
|
|
1061
|
+
initSchema(): IDbSchema;
|
|
1062
|
+
traceSchema(showFn?: boolean): Promise<void>;
|
|
1063
|
+
map<K extends StrOrNum = StrOrNum, V = any>(storeName?: string, defaultData?: RequiredDbNoneKeyPathRecord<V, K>[]): IDbMap<K, V, IInfoTarget>;
|
|
1064
|
+
map<K extends StrOrNum = StrOrNum, V = any>(defaultData?: RequiredDbNoneKeyPathRecord<V, K>[], storeName?: string): IDbMap<K, V, IInfoTarget>;
|
|
1065
|
+
export(): Promise<Record<string, any[]>>;
|
|
1066
|
+
import(data: Record<string, any[]>, returns?: false, use?: ImportUse): Promise<void>;
|
|
1067
|
+
import(data: Record<string, any[]>, returns?: true, use?: ImportUse): Promise<Record<string, any[]>>;
|
|
1068
|
+
getStoreSchema(name: string): IStoreSchema;
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
declare function parseIDbQuery(query: IDbQuery | IDBKeyRange | undefined | null): IDBValidKey | IDBKeyRange | null | undefined;
|
|
1072
|
+
declare function isIDbQuery(query: any): boolean;
|
|
1073
|
+
|
|
1074
|
+
declare class StoreUpgradeable<T = any> implements IStoreUpgradeable<T> {
|
|
1075
|
+
#private;
|
|
1076
|
+
readonly upgradeContext: IUpgradeContext;
|
|
1077
|
+
readonly storeSchema: IStoreSchema;
|
|
1078
|
+
readonly nativeStore: IDBObjectStore;
|
|
1079
|
+
constructor(upgradeContext: IUpgradeContext, storeSchema: IStoreSchema, nativeStore: IDBObjectStore);
|
|
1080
|
+
get writer(): IDataWriter<T>;
|
|
1081
|
+
add(versionBounds: IDbVersionBounds, values: DbRecord<T>[], returns?: false): Promise<void>;
|
|
1082
|
+
add(versionBounds: IDbVersionBounds, values: DbRecord<T>[], returns: true): Promise<DbRecord<T>>;
|
|
1083
|
+
addOrChange(versionBounds: IDbVersionBounds, values: DbRecord<T>[], returns?: false): Promise<void>;
|
|
1084
|
+
addOrChange(versionBounds: IDbVersionBounds, values: DbRecord<T>[], returns: true): Promise<DbRecord<T>[]>;
|
|
1085
|
+
call<R = any>(versionBounds: IDbVersionBounds, fn: (store: IDataWriter, upgradeContext?: IUpgradeContext) => (Promise<R> | R)): Promise<R>;
|
|
1086
|
+
replace(versionBounds: IDbVersionBounds, values: DbRecord<T>[], returns?: false): Promise<void>;
|
|
1087
|
+
replace(versionBounds: IDbVersionBounds, values: DbRecord<T>[], returns: true): Promise<DbRecord<T>[]>;
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
declare class UpgradeContext implements IUpgradeContext {
|
|
1091
|
+
#private;
|
|
1092
|
+
readonly database: IDBDatabase;
|
|
1093
|
+
readonly newVersion: number;
|
|
1094
|
+
readonly oldVersion: number;
|
|
1095
|
+
readonly dbSchema: Readonly<IDbSchema>;
|
|
1096
|
+
readonly transaction: IDBTransaction;
|
|
1097
|
+
constructor(args: IUpgradeContextArgs);
|
|
1098
|
+
deleteStoreIfExists(storeName: string): void;
|
|
1099
|
+
destroy(): void;
|
|
1100
|
+
store<T = any>(storeName: string): IStoreUpgradeable<T>;
|
|
1101
|
+
versionIn({ oldMin, oldMax, newMax, newMin }: IDbVersionBounds): boolean;
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
declare const validateSchemaWithDefaults: IDbSchema['validateSchemaWithDefaults'];
|
|
1105
|
+
|
|
1106
|
+
/**
|
|
1107
|
+
* 版本差异验证器
|
|
1108
|
+
* @param context
|
|
1109
|
+
*/
|
|
1110
|
+
declare const versionDiffValidate: VersionValidateFn;
|
|
1111
|
+
|
|
1112
|
+
declare const versionSameValidate: VersionValidateFn;
|
|
1113
|
+
|
|
1114
|
+
declare function generateDbSchema(db: string, option?: IGenerateOption<undefined | false>): Promise<IDbSchema>;
|
|
1115
|
+
declare function generateDbSchema(db: string, option: IGenerateOption<true | number>): Promise<string>;
|
|
1116
|
+
|
|
1117
|
+
export { Break, Continue, ContinueKey, ContinuePrimaryKey, DataOperationBase, DataOperators, DataReader, DataWriter, DbIterator, DbIteratorParsers, DbMap, Delete, Finished, IDbPro, NextKey, NextPrimaryKey, Save, StoreUpgradeable, UpgradeContext, dbMap, dbStore, dbStores, defaultSpecialFields, defaultStoreSchemaTemplate, generateDbSchema, isIDbQuery, isNativeTarget, parseDbNoneKeyPathRecord, parseIDbQuery, releaseDefaultDB, validateSchemaWithDefaults, versionDiffValidate, versionSameValidate };
|
|
1118
|
+
export type { ArrayDbRecord, DBSchema, DataOperationSchema, DbIteratorParsersKey, DbNoneKeyPathRecord, DbRecord, DbRecordResult, ExportDataTo, FindFn, GetRowMapper, IChangeManyOption, IChangeRangeArg, ICursorArg, ICursorOption, ICursorQueryable, ICursorReader, ICursorResultArg, ICursorRow, ICursorWriter, IDataOperationSchema, IDataOperatorFactory, IDataOperators, IDataReader, IDataWriter, IDbGtRange, IDbGteRange, IDbIterator, IDbIteratorArg, IDbIteratorOption, IDbIteratorParser, IDbLtGtRange, IDbLtGteRange, IDbLtRange, IDbLteGtRange, IDbLteGteRange, IDbLteRange, IDbMap, IDbNoneKeyPathRecord, IDbQuery, IDbRange, IDbSchema, IDbVersionBounds, IDeleteOption, IFilterArg, IFindArg, IFindMapArg, IForEachArg, IForEachResultArg, IForEachable, IGenerateOption, IGetAllArgs, IIDbPro, IIndexSchema, IInfoTarget, IModifyResult, IMultiBackup, INativeOperationResult, IOpenCursorArg, IPage, IPageArg, IPageInfo, IPageableQuery, IRangeWriter, ISpecialFieldSchema, ISpecialFields, IStoreSchema, IStoreSchemaTemplate, IStoreUpgradeable, IUpgradeContext, IUpgradeContextArgs, IValidDataOperationSchema, IValueResult, IVersionData, IVersionValidateContext, IVoidResult, ImportUse, IndexSchema, NativeReader, NativeTarget, NativeWriter, OperationTarget, ReadCallFn, RequiredDbNoneKeyPathRecord, SaveRowMapper, SpecialFieldSchema, StoreSchema, ValueCallFn, ValueControl, ValueModifyFn, VersionValidateFn, VoidCallFn, VoidControl, VoidModifyFn, VoidResult, WriteCallFn };
|