gs-idb-pro 0.1.4 → 1.0.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/lib/{index.web.js → impl.cjs} +216 -574
- package/lib/impl.d.ts +265 -0
- package/lib/{index.js → impl.mjs} +15 -47
- package/lib/index.cjs +11 -1266
- package/lib/index.d.ts +2 -1129
- package/lib/index.mjs +2 -0
- package/lib/type.cjs +31 -0
- package/lib/type.d.ts +870 -0
- package/lib/type.mjs +45 -0
- package/package.json +28 -20
|
@@ -1,335 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
keyValue: ({ primaryKey: key, value }) => ({ value: [key, value] })
|
|
5
|
-
});
|
|
6
|
-
function parseDbNoneKeyPathRecord(record) {
|
|
7
|
-
if (Array.isArray(record))
|
|
8
|
-
return { key: record[1], value: record[0] };
|
|
9
|
-
if ("value" in record) {
|
|
10
|
-
const { key, value } = record;
|
|
11
|
-
return { key, value };
|
|
12
|
-
}
|
|
13
|
-
throw new Error(`not include value in invalid DBRecord:${JSON.stringify(record)}`);
|
|
14
|
-
}
|
|
15
|
-
const Break = Symbol("break"), Finished = Symbol("finished"), Continue = Symbol("continue"), ContinueKey = Symbol("continue key"), NextKey = Symbol("next key"), ContinuePrimaryKey = Symbol("continue primary key"), NextPrimaryKey = Symbol("next primary key");
|
|
16
|
-
function isNativeTarget(target) {
|
|
17
|
-
return target instanceof IDBObjectStore || target instanceof IDBIndex;
|
|
18
|
-
}
|
|
19
|
-
const defaultStoreSchemaTemplate = Object.freeze({
|
|
20
|
-
keyPath: "id",
|
|
21
|
-
autoIncrement: !0,
|
|
22
|
-
addedTimeField: !0,
|
|
23
|
-
updatedTimeField: !0
|
|
24
|
-
}), defaultSpecialFields = Object.freeze({
|
|
25
|
-
addedTimeField: "added_at",
|
|
26
|
-
softDeletedField: "deleted",
|
|
27
|
-
updatedCountField: "updated_count",
|
|
28
|
-
updatedTimeField: "updated_at"
|
|
29
|
-
});
|
|
30
|
-
function isFunction(value) {
|
|
31
|
-
return value instanceof Function || typeof value == "function";
|
|
32
|
-
}
|
|
33
|
-
function isArray(value) {
|
|
34
|
-
return Array.isArray(value) || value instanceof Array || value?.constructor === Array;
|
|
35
|
-
}
|
|
36
|
-
function isBoolean(value) {
|
|
37
|
-
return value === !0 || value === !1 || value instanceof Boolean || typeof value == "boolean";
|
|
38
|
-
}
|
|
39
|
-
function isNumber(value) {
|
|
40
|
-
return typeof value == "number" || value instanceof Number;
|
|
41
|
-
}
|
|
42
|
-
function isString(value) {
|
|
43
|
-
return typeof value == "string" || value instanceof String;
|
|
44
|
-
}
|
|
45
|
-
function isStrOrNum(value) {
|
|
46
|
-
return typeof value == "string" || typeof value == "number" || value instanceof String || value instanceof Number;
|
|
47
|
-
}
|
|
48
|
-
function isObject(value) {
|
|
49
|
-
return value && !isArray(value) && !isFunction(value) && (value instanceof Object || typeof value == "object");
|
|
50
|
-
}
|
|
51
|
-
function isIterable(obj) {
|
|
52
|
-
return obj != null && isFunction(obj[Symbol.iterator]);
|
|
53
|
-
}
|
|
54
|
-
function isAsyncIterable(obj) {
|
|
55
|
-
return obj != null && isFunction(obj[Symbol.asyncIterator]);
|
|
56
|
-
}
|
|
57
|
-
function isIterator(obj) {
|
|
58
|
-
return obj != null && typeof obj.next == "function";
|
|
59
|
-
}
|
|
60
|
-
async function asyncForEach(values, fn) {
|
|
61
|
-
for (let i = 0; i < values.length; i++)
|
|
62
|
-
await fn(values[i]);
|
|
63
|
-
}
|
|
64
|
-
async function asyncMap(data, fn) {
|
|
65
|
-
const arr = await data, result = [];
|
|
66
|
-
for (let i = 0; i < arr.length; i++)
|
|
67
|
-
result.push(await fn(arr[i], i, arr));
|
|
68
|
-
return result;
|
|
69
|
-
}
|
|
70
|
-
function destroy(obj) {
|
|
71
|
-
try {
|
|
72
|
-
obj?.destroy?.();
|
|
73
|
-
} catch {
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
function destroyRecords(...disposables) {
|
|
77
|
-
for (const disposable of disposables)
|
|
78
|
-
if (disposable)
|
|
79
|
-
for (const key of Object.keys(disposable))
|
|
80
|
-
try {
|
|
81
|
-
disposable[key]?.destroy?.();
|
|
82
|
-
} catch {
|
|
83
|
-
} finally {
|
|
84
|
-
delete disposable[key];
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
function copyObject(value) {
|
|
88
|
-
if (typeof value != "object" || value === null || value instanceof Date || value instanceof Function || value instanceof RegExp || typeof Element < "u" && value instanceof Element)
|
|
89
|
-
return value;
|
|
90
|
-
if (value instanceof Set) {
|
|
91
|
-
const set = /* @__PURE__ */ new Set();
|
|
92
|
-
for (const v of value)
|
|
93
|
-
set.add(copyObject(v));
|
|
94
|
-
return set;
|
|
95
|
-
}
|
|
96
|
-
if (value instanceof Map) {
|
|
97
|
-
const map = /* @__PURE__ */ new Map();
|
|
98
|
-
for (const [k2, v] of value)
|
|
99
|
-
map.set(copyObject(k2), copyObject(v));
|
|
100
|
-
return map;
|
|
101
|
-
}
|
|
102
|
-
if (Array.isArray(value)) {
|
|
103
|
-
const arr = [];
|
|
104
|
-
for (const v of value)
|
|
105
|
-
arr.push(copyObject(v));
|
|
106
|
-
return arr;
|
|
107
|
-
}
|
|
108
|
-
const obj = {};
|
|
109
|
-
for (const [k2, v] of Object.entries(value))
|
|
110
|
-
obj[k2] = copyObject(v);
|
|
111
|
-
return obj;
|
|
112
|
-
}
|
|
113
|
-
function copyFields(dist, src, fields) {
|
|
114
|
-
if (fields?.length)
|
|
115
|
-
for (const field of fields)
|
|
116
|
-
src.hasOwnProperty(field) && (dist[field] = src[field]);
|
|
117
|
-
else
|
|
118
|
-
Object.assign(dist, src);
|
|
119
|
-
return dist;
|
|
120
|
-
}
|
|
121
|
-
function deepFreeze(o) {
|
|
122
|
-
return Object.freeze(o), Object.getOwnPropertyNames(o).forEach((prop) => {
|
|
123
|
-
const value = o[prop];
|
|
124
|
-
value !== null && (typeof value == "object" || typeof value == "function") && !Object.isFrozen(value) && deepFreeze(value);
|
|
125
|
-
}), o;
|
|
126
|
-
}
|
|
127
|
-
class ToJsonContext {
|
|
128
|
-
rootData$;
|
|
129
|
-
space;
|
|
130
|
-
showFn;
|
|
131
|
-
spaceEffectiveLength;
|
|
132
|
-
out;
|
|
133
|
-
type;
|
|
134
|
-
ignoreCircularRef;
|
|
135
|
-
constructor(args) {
|
|
136
|
-
let i = 0;
|
|
137
|
-
isObject(args[i]) && args[i].rootData$ && Object.assign(this, args[i++]), i < 1 && (this.rootData$ = args[i++]);
|
|
138
|
-
const lastArgs = args.slice(i), numbers = lastArgs.filter((n) => isNumber(n));
|
|
139
|
-
this.out === void 0 && (this.out = lastArgs.find((f) => isFunction(f)) || null), this.showFn === void 0 && (this.showFn = !!lastArgs.find((s) => isBoolean(s))), this.spaceEffectiveLength === void 0 && (this.spaceEffectiveLength = numbers[0] || 100), this.space === void 0 && (this.space = numbers[1] || 2), this.type === void 0 && (this.type = lastArgs.find((s) => isString(s)) || "js"), this.ignoreCircularRef === void 0 && (this.ignoreCircularRef = !0);
|
|
140
|
-
}
|
|
141
|
-
destroy() {
|
|
142
|
-
this.out = null, this.rootData$ = null;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
async function createJson(value, cfg, refSet) {
|
|
146
|
-
if (value = await value, typeof value == "string" || value instanceof Date)
|
|
147
|
-
return JSON.stringify(value);
|
|
148
|
-
if (value instanceof Function)
|
|
149
|
-
return cfg.showFn ? value.toString() : "undefined";
|
|
150
|
-
if (typeof value != "object" || value === null)
|
|
151
|
-
return value + "";
|
|
152
|
-
if (value instanceof RegExp)
|
|
153
|
-
return value.toString();
|
|
154
|
-
if (typeof Element < "u" && value instanceof Element)
|
|
155
|
-
return "undefined";
|
|
156
|
-
if (refSet.has(value)) {
|
|
157
|
-
if (cfg.ignoreCircularRef) return;
|
|
158
|
-
throw new Error("circular reference");
|
|
159
|
-
}
|
|
160
|
-
refSet.add(value);
|
|
161
|
-
try {
|
|
162
|
-
return cfg.type === "json" ? await toJsonTail(value, cfg, refSet) : await toJsJsonTail(value, cfg, refSet);
|
|
163
|
-
} finally {
|
|
164
|
-
refSet.delete(value);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
async function toJsJsonTail(value, cfg, refSet) {
|
|
168
|
-
const arr = [];
|
|
169
|
-
if (value instanceof Set) {
|
|
170
|
-
for (const v of value)
|
|
171
|
-
(!isFunction(v) || cfg.showFn) && arr.push(await createJson(v, cfg, refSet));
|
|
172
|
-
const { spaceStart: spaceStart2, spaceEnd: spaceEnd2, arrJoin: arrJoin2 } = toJsJsonStringArgs(arr, cfg);
|
|
173
|
-
return arr.length ? `new Set([${spaceStart2}${arrJoin2()}${spaceEnd2}])` : "[]";
|
|
174
|
-
}
|
|
175
|
-
if (value instanceof Map) {
|
|
176
|
-
for (const [k2, v] of value)
|
|
177
|
-
(!isFunction(v) || cfg.showFn) && arr.push(`${objectKey(await createJson(k2, cfg, refSet))}: ${await createJson(v, cfg, refSet)}`);
|
|
178
|
-
const { spaceStart: spaceStart2, spaceEnd: spaceEnd2, arrJoin: arrJoin2 } = toJsJsonStringArgs(arr, cfg);
|
|
179
|
-
return arr.length ? `new Map(Object.entries({${spaceStart2}${arrJoin2()}${spaceEnd2}})` : "new Map()";
|
|
180
|
-
}
|
|
181
|
-
if (Array.isArray(value) || isIterable(value)) {
|
|
182
|
-
for (const v of value)
|
|
183
|
-
(!isFunction(v) || cfg.showFn) && arr.push(await createJson(v, cfg, refSet));
|
|
184
|
-
const { spaceStart: spaceStart2, spaceEnd: spaceEnd2, arrJoin: arrJoin2 } = toJsJsonStringArgs(arr, cfg);
|
|
185
|
-
return arr.length ? `[${spaceStart2}${arrJoin2()}${spaceEnd2}]` : "[]";
|
|
186
|
-
}
|
|
187
|
-
if (isIterator(value) || isAsyncIterable(value)) {
|
|
188
|
-
for await (const v of value)
|
|
189
|
-
(!isFunction(v) || cfg.showFn) && arr.push(await createJson(v, cfg, refSet));
|
|
190
|
-
const { spaceStart: spaceStart2, spaceEnd: spaceEnd2, arrJoin: arrJoin2 } = toJsJsonStringArgs(arr, cfg);
|
|
191
|
-
return arr.length ? `[${spaceStart2}${arrJoin2()}${spaceEnd2}]` : "[]";
|
|
192
|
-
}
|
|
193
|
-
for (const [k2, v] of Object.entries(value))
|
|
194
|
-
(!isFunction(v) || cfg.showFn) && arr.push(`${objectKey(k2)}: ${await createJson(v, cfg, refSet)}`);
|
|
195
|
-
const { spaceStart, spaceEnd, arrJoin } = toJsJsonStringArgs(arr, cfg);
|
|
196
|
-
return arr.length ? `{${spaceStart}${arrJoin()}${spaceEnd}}` : "{}";
|
|
197
|
-
}
|
|
198
|
-
async function toJsonTail(value, cfg, refSet) {
|
|
199
|
-
const arr = [];
|
|
200
|
-
if (Array.isArray(value) || value instanceof Set || isIterable(value) && !(value instanceof Map)) {
|
|
201
|
-
for (const v of value)
|
|
202
|
-
if (v !== void 0 && (!isFunction(v) || cfg.showFn)) {
|
|
203
|
-
const r = await createJson(v, cfg, refSet);
|
|
204
|
-
r !== void 0 && arr.push(r);
|
|
205
|
-
}
|
|
206
|
-
const { spaceStart: spaceStart2, spaceEnd: spaceEnd2, arrJoin: arrJoin2 } = toJsJsonStringArgs(arr, cfg);
|
|
207
|
-
return arr.length ? `[${spaceStart2}${arrJoin2()}${spaceEnd2}]` : "[]";
|
|
208
|
-
}
|
|
209
|
-
if (isAsyncIterable(value) || isIterator(value)) {
|
|
210
|
-
for await (const v of value)
|
|
211
|
-
if (v !== void 0 && (!isFunction(v) || cfg.showFn)) {
|
|
212
|
-
const r = await createJson(v, cfg, refSet);
|
|
213
|
-
r !== void 0 && arr.push(r);
|
|
214
|
-
}
|
|
215
|
-
const { spaceStart: spaceStart2, spaceEnd: spaceEnd2, arrJoin: arrJoin2 } = toJsJsonStringArgs(arr, cfg);
|
|
216
|
-
return arr.length ? `[${spaceStart2}${arrJoin2()}${spaceEnd2}]` : "[]";
|
|
217
|
-
}
|
|
218
|
-
let entries = value instanceof Map ? value.entries() : Object.entries(value);
|
|
219
|
-
for (const [k2, v] of entries)
|
|
220
|
-
if (v !== void 0 && (!isFunction(v) || cfg.showFn)) {
|
|
221
|
-
const r = await createJson(v, cfg, refSet);
|
|
222
|
-
r !== void 0 && arr.push(`"${k2}": ${r}`);
|
|
223
|
-
}
|
|
224
|
-
const { spaceStart, spaceEnd, arrJoin } = toJsJsonStringArgs(arr, cfg);
|
|
225
|
-
return arr.length ? `[${spaceStart}${arrJoin()}${spaceEnd}]` : "[]";
|
|
226
|
-
}
|
|
227
|
-
function toJsJsonStringArgs(arr, cfg) {
|
|
228
|
-
let space = cfg.space;
|
|
229
|
-
arr.reduce((acc, v) => acc + v.length, 0) < cfg.spaceEffectiveLength && (space = 0);
|
|
230
|
-
const spaceStart = space ? `
|
|
231
|
-
` + "".padStart(space) : "", spaceEnd = space ? `
|
|
232
|
-
` : "", separator = space ? `
|
|
233
|
-
`.padEnd(space + 1) : ",";
|
|
234
|
-
return { spaceStart, spaceEnd, arrJoin: space ? () => arr.join(`,
|
|
235
|
-
`).replace(/\n/g, separator) : () => arr.join(",") };
|
|
236
|
-
}
|
|
237
|
-
function objectKey(key) {
|
|
238
|
-
return /^[a-z_]+\w*$/i.test(key) ? key : `"${key}"`;
|
|
239
|
-
}
|
|
240
|
-
async function toJson(...args) {
|
|
241
|
-
const arg = new ToJsonContext(args), refSet = /* @__PURE__ */ new Set();
|
|
242
|
-
try {
|
|
243
|
-
const result = await createJson(arg.rootData$, arg, refSet);
|
|
244
|
-
if (!arg.out) return result;
|
|
245
|
-
arg.out(result);
|
|
246
|
-
} finally {
|
|
247
|
-
refSet.clear(), arg.destroy();
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
async function logJson(...args) {
|
|
251
|
-
const out = globalThis.top?.console?.log || console.log;
|
|
252
|
-
args.push(out), await toJson(...args);
|
|
253
|
-
}
|
|
254
|
-
var Bool = /* @__PURE__ */ ((Bool2) => (Bool2[Bool2.True = 1] = "True", Bool2[Bool2.False = 0] = "False", Bool2))(Bool || {});
|
|
255
|
-
function isDbKey(key) {
|
|
256
|
-
return isStrOrNum(key) || key instanceof Date || key instanceof ArrayBuffer || Array.isArray(key) && key.every(isDbKey);
|
|
257
|
-
}
|
|
258
|
-
function isDbQuery(value) {
|
|
259
|
-
return value instanceof IDBKeyRange || isDbKey(value);
|
|
260
|
-
}
|
|
261
|
-
function isDbQueryOrNull(value) {
|
|
262
|
-
return value == null || isDbQuery(value);
|
|
263
|
-
}
|
|
264
|
-
async function findExistDb(dbName) {
|
|
265
|
-
return (await indexedDB.databases()).find((info) => info.name === dbName);
|
|
266
|
-
}
|
|
267
|
-
function requestDb(request, onsuccess, onupgradeneeded2) {
|
|
268
|
-
return new Promise(async (resolve, reject) => {
|
|
269
|
-
let hasError = !1;
|
|
270
|
-
request.onsuccess = async () => {
|
|
271
|
-
if (!hasError)
|
|
272
|
-
if (onsuccess)
|
|
273
|
-
try {
|
|
274
|
-
resolve(await onsuccess(request.result));
|
|
275
|
-
} catch (e) {
|
|
276
|
-
reject(e);
|
|
277
|
-
} finally {
|
|
278
|
-
request.result?.close?.();
|
|
279
|
-
}
|
|
280
|
-
else resolve(request.result);
|
|
281
|
-
}, request.onblocked = request.onerror = (e) => reject(e.target.error), onupgradeneeded2 && (request.onupgradeneeded = async (ev) => {
|
|
282
|
-
try {
|
|
283
|
-
await onupgradeneeded2(request.result, ev, request);
|
|
284
|
-
} catch (e) {
|
|
285
|
-
hasError = !0, reject(e);
|
|
286
|
-
}
|
|
287
|
-
});
|
|
288
|
-
});
|
|
289
|
-
}
|
|
290
|
-
async function openDb(nameOrArg, fn) {
|
|
291
|
-
const arg = isString(nameOrArg) ? { name: nameOrArg } : nameOrArg;
|
|
292
|
-
isFunction(fn) && (arg.onsuccess = fn);
|
|
293
|
-
const { name, onsuccess, onupgradeneeded: onupgradeneeded2, version } = arg;
|
|
294
|
-
return requestDb(indexedDB.open(name, version), onsuccess, onupgradeneeded2);
|
|
295
|
-
}
|
|
296
|
-
function openTx(db, storeName2, ...args) {
|
|
297
|
-
return new Promise(async (resolve, reject) => {
|
|
298
|
-
let i = 0;
|
|
299
|
-
const fn = isFunction(args[i]) ? args[i++] : void 0, writeable = args[i] ? "readwrite" : "readonly", nativeDb = db instanceof IDBDatabase ? db : await openDb(db), storeNames = Array.isArray(storeName2) ? storeName2 : [storeName2], tx = nativeDb.transaction(storeNames.map((s) => isString(s) ? s : s.store), writeable), storeOrIndexes = storeNames.map((n) => {
|
|
300
|
-
if (isString(n)) return tx.objectStore(n);
|
|
301
|
-
const { store, index } = n;
|
|
302
|
-
return isString(index) ? tx.objectStore(store).index(index) : tx.objectStore(store);
|
|
303
|
-
});
|
|
304
|
-
if (fn)
|
|
305
|
-
try {
|
|
306
|
-
resolve(await fn(...storeOrIndexes)), writeable ? tx.commit() : tx.abort();
|
|
307
|
-
} catch (e) {
|
|
308
|
-
tx.abort(), reject(e);
|
|
309
|
-
} finally {
|
|
310
|
-
nativeDb.close();
|
|
311
|
-
}
|
|
312
|
-
else
|
|
313
|
-
resolve(storeOrIndexes);
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
function readTx(db, storeName2, fn) {
|
|
317
|
-
return openTx(db, storeName2, fn, !1);
|
|
318
|
-
}
|
|
319
|
-
function requestDbResult(request) {
|
|
320
|
-
return new Promise(async (resolve, reject) => {
|
|
321
|
-
request.onsuccess = () => {
|
|
322
|
-
try {
|
|
323
|
-
resolve(request.result);
|
|
324
|
-
} catch (e) {
|
|
325
|
-
reject(e);
|
|
326
|
-
}
|
|
327
|
-
}, request.onerror = (e) => reject(e.target.error);
|
|
328
|
-
});
|
|
329
|
-
}
|
|
330
|
-
const DefaultDbName = "gs-idb", DefaultStorageStoreName = "gs-storage-map", toNum = (v) => v instanceof Date ? v.getTime() : v;
|
|
1
|
+
"use strict";
|
|
2
|
+
var type$1 = require('./type.cjs'), types = require('gs-base/types'), store = require('gs-idb-basic/store'), type = require('gs-idb-basic/type'), basic = require('gs-base/basic'), json = require('gs-base/json'), tx = require('gs-idb-basic/tx'), db = require('gs-idb-basic/db'), storage = require('gs-idb-basic/storage');
|
|
3
|
+
const toNum = (v) => v instanceof Date ? v.getTime() : v;
|
|
331
4
|
function parseIDbQuery(query) {
|
|
332
|
-
if (isDbQueryOrNull(query) || !isObject(query))
|
|
5
|
+
if (type.isDbQueryOrNull(query) || !types.isObject(query))
|
|
333
6
|
return query;
|
|
334
7
|
const range = query;
|
|
335
8
|
if ("lt" in range && "gt" in range) {
|
|
@@ -362,17 +35,17 @@ function isIDbQuery(query) {
|
|
|
362
35
|
}
|
|
363
36
|
async function openCursor(target, arg, fn) {
|
|
364
37
|
const { query, direction = "prev", preSkip, startKey, startPrimaryKey } = arg, request = target.openCursor(parseIDbQuery(query), direction);
|
|
365
|
-
return preSkip && (await requestDbResult(request)).advance(preSkip), startKey && (startPrimaryKey ? (await requestDbResult(request)).continuePrimaryKey(startKey, startPrimaryKey) : (await requestDbResult(request)).continue(startKey)), request;
|
|
38
|
+
return preSkip && (await store.requestDbResult(request)).advance(preSkip), startKey && (startPrimaryKey ? (await store.requestDbResult(request)).continuePrimaryKey(startKey, startPrimaryKey) : (await store.requestDbResult(request)).continue(startKey)), request;
|
|
366
39
|
}
|
|
367
40
|
function getMapperFn(mapper, keyPath, useArrayRecord) {
|
|
368
|
-
return Array.isArray(mapper) ? (v) => copyFields({}, v, mapper) : isFunction(mapper) ? mapper : keyPath ? (v) => v : useArrayRecord ? (v, k) => [v, k] : (key, value) => ({ key, value });
|
|
41
|
+
return Array.isArray(mapper) ? (v) => basic.copyFields({}, v, mapper) : types.isFunction(mapper) ? mapper : keyPath ? (v) => v : useArrayRecord ? (v, k) => [v, k] : (key, value) => ({ key, value });
|
|
369
42
|
}
|
|
370
43
|
class DataOperationBase {
|
|
371
44
|
idbPro;
|
|
372
45
|
target;
|
|
373
46
|
#storeSchema;
|
|
374
|
-
constructor(schema,
|
|
375
|
-
this.idbPro =
|
|
47
|
+
constructor(schema, db2) {
|
|
48
|
+
this.idbPro = db2, this.#storeSchema = schema.storeSchema, this.target = schema.target, type$1.isNativeTarget(schema.target) && (this.tx = this.#nativeOperation);
|
|
376
49
|
}
|
|
377
50
|
get storeName() {
|
|
378
51
|
return this.nativeStore?.name || this.target.store;
|
|
@@ -392,40 +65,40 @@ class DataOperationBase {
|
|
|
392
65
|
}
|
|
393
66
|
get keyPath() {
|
|
394
67
|
const { target } = this;
|
|
395
|
-
if (isNativeTarget(target))
|
|
68
|
+
if (type$1.isNativeTarget(target))
|
|
396
69
|
return target.keyPath;
|
|
397
70
|
const { storeSchema } = this, { index } = target;
|
|
398
71
|
return index ? storeSchema.indexSchemas.find((i) => i.name === index)?.keyPath : storeSchema.keyPath;
|
|
399
72
|
}
|
|
400
73
|
async forEach(arg, returns) {
|
|
401
|
-
return arg = isFunction(arg) ? { fn: arg } : arg, returns ? this.cursorResult(arg, !1) : this.cursorVoid(arg, !1);
|
|
74
|
+
return arg = types.isFunction(arg) ? { fn: arg } : arg, returns ? this.cursorResult(arg, !1) : this.cursorVoid(arg, !1);
|
|
402
75
|
}
|
|
403
76
|
async tx(writable, fn, rollbackOnError) {
|
|
404
77
|
let { target } = this;
|
|
405
|
-
const { store, index } = target,
|
|
406
|
-
let
|
|
78
|
+
const { store: store2, index } = target, db2 = await this.idbPro.openNativeDb();
|
|
79
|
+
let tx2, nativeStore;
|
|
407
80
|
try {
|
|
408
|
-
|
|
81
|
+
tx2 = db2.transaction(store2, writable === !0 ? "readwrite" : "readonly"), target = nativeStore = tx2.objectStore(store2), index && (target = target.index(index));
|
|
409
82
|
} catch (e) {
|
|
410
|
-
throw
|
|
83
|
+
throw db2.close(), e;
|
|
411
84
|
}
|
|
412
85
|
if (!fn)
|
|
413
|
-
return Object.freeze({ db, tx, nativeStore, target });
|
|
86
|
+
return Object.freeze({ db: db2, tx: tx2, nativeStore, target });
|
|
414
87
|
try {
|
|
415
88
|
if (writable === !0) {
|
|
416
89
|
const result = await fn(target, nativeStore);
|
|
417
|
-
return
|
|
90
|
+
return tx2.commit(), result;
|
|
418
91
|
}
|
|
419
92
|
return fn(target);
|
|
420
93
|
} catch (e) {
|
|
421
|
-
throw rollbackOnError !== !1 &&
|
|
94
|
+
throw rollbackOnError !== !1 && tx2.abort(), e;
|
|
422
95
|
} finally {
|
|
423
|
-
|
|
96
|
+
db2.close();
|
|
424
97
|
}
|
|
425
98
|
}
|
|
426
99
|
openCursor(arg, writable) {
|
|
427
|
-
return this.tx(writable, (
|
|
428
|
-
const { fn } = arg, request = await openCursor(
|
|
100
|
+
return this.tx(writable, (store2) => new Promise(async (resolve, reject) => {
|
|
101
|
+
const { fn } = arg, request = await openCursor(store2, arg);
|
|
429
102
|
request.onsuccess = async () => {
|
|
430
103
|
request.result ? await fn(request.result) === !1 && resolve() : resolve();
|
|
431
104
|
}, request.onerror = () => reject(request.error);
|
|
@@ -446,14 +119,14 @@ class DataOperationBase {
|
|
|
446
119
|
primaryKey,
|
|
447
120
|
modify,
|
|
448
121
|
value
|
|
449
|
-
} = isObject(result) ? result : { control: result };
|
|
450
|
-
switch (writable && (modify === Save ? cursor.update(value || ov) : modify === Delete && cursor.delete()), control) {
|
|
451
|
-
case Break:
|
|
122
|
+
} = types.isObject(result) ? result : { control: result };
|
|
123
|
+
switch (writable && (modify === type$1.Save ? cursor.update(value || ov) : modify === type$1.Delete && cursor.delete()), control) {
|
|
124
|
+
case type$1.Break:
|
|
452
125
|
return !1;
|
|
453
|
-
case ContinueKey:
|
|
126
|
+
case type$1.ContinueKey:
|
|
454
127
|
cursor.continue(key);
|
|
455
128
|
break;
|
|
456
|
-
case ContinuePrimaryKey:
|
|
129
|
+
case type$1.ContinuePrimaryKey:
|
|
457
130
|
cursor.continuePrimaryKey(key, primaryKey);
|
|
458
131
|
break;
|
|
459
132
|
default:
|
|
@@ -487,16 +160,16 @@ class DataOperationBase {
|
|
|
487
160
|
primaryKey,
|
|
488
161
|
modify
|
|
489
162
|
} = await fn?.(ov, op, i, results) || {};
|
|
490
|
-
switch (writable && (modify === Save ? cursor.update(value || ov) : modify === Delete && cursor.delete()), (!control || control === Finished || control === NextKey || control === NextPrimaryKey) && results.push(mapper(value || ov, primaryKey || op, i)), control) {
|
|
491
|
-
case Break:
|
|
492
|
-
case Finished:
|
|
163
|
+
switch (writable && (modify === type$1.Save ? cursor.update(value || ov) : modify === type$1.Delete && cursor.delete()), (!control || control === type$1.Finished || control === type$1.NextKey || control === type$1.NextPrimaryKey) && results.push(mapper(value || ov, primaryKey || op, i)), control) {
|
|
164
|
+
case type$1.Break:
|
|
165
|
+
case type$1.Finished:
|
|
493
166
|
return !1;
|
|
494
|
-
case NextKey:
|
|
495
|
-
case ContinueKey:
|
|
167
|
+
case type$1.NextKey:
|
|
168
|
+
case type$1.ContinueKey:
|
|
496
169
|
cursor.continue(key);
|
|
497
170
|
break;
|
|
498
|
-
case NextPrimaryKey:
|
|
499
|
-
case ContinuePrimaryKey:
|
|
171
|
+
case type$1.NextPrimaryKey:
|
|
172
|
+
case type$1.ContinuePrimaryKey:
|
|
500
173
|
cursor.continuePrimaryKey(key, primaryKey);
|
|
501
174
|
break;
|
|
502
175
|
default:
|
|
@@ -531,8 +204,8 @@ class DataOperators {
|
|
|
531
204
|
export() {
|
|
532
205
|
return this.tx("newReader", async (...stores) => {
|
|
533
206
|
const result = {};
|
|
534
|
-
for (const
|
|
535
|
-
result[
|
|
207
|
+
for (const store2 of stores)
|
|
208
|
+
result[store2.storeName] = await store2.export();
|
|
536
209
|
return result;
|
|
537
210
|
});
|
|
538
211
|
}
|
|
@@ -540,28 +213,28 @@ class DataOperators {
|
|
|
540
213
|
return use || (use = "addOrChangeMany"), this.tx("newWriter", async (...stores) => {
|
|
541
214
|
const result = {};
|
|
542
215
|
stores = Array.from(new Map(stores.map((s) => [s.storeName, s.asStore(!0)])).values());
|
|
543
|
-
for (const
|
|
544
|
-
const { storeName } =
|
|
545
|
-
rows && (result[storeName] = await
|
|
216
|
+
for (const store2 of stores) {
|
|
217
|
+
const { storeName } = store2, rows = data[storeName];
|
|
218
|
+
rows && (result[storeName] = await store2[use](rows, returns));
|
|
546
219
|
}
|
|
547
220
|
if (returns) return result;
|
|
548
221
|
}, !0);
|
|
549
222
|
}
|
|
550
223
|
async tx(method, fn, writable, rollbackOnError) {
|
|
551
|
-
const { idbPro, schemas } = this, { factory } = idbPro.schema,
|
|
224
|
+
const { idbPro, schemas } = this, { factory } = idbPro.schema, db2 = await idbPro.openNativeDb();
|
|
552
225
|
try {
|
|
553
|
-
const
|
|
226
|
+
const tx2 = db2.transaction(this.storeNames, writable ? "readwrite" : "readonly");
|
|
554
227
|
try {
|
|
555
228
|
const stores = schemas.map(({ storeSchema, target: info }) => {
|
|
556
|
-
let target =
|
|
229
|
+
let target = tx2.objectStore(info.store);
|
|
557
230
|
return info.index && (target = target.index(info.index)), factory[method]({ storeSchema, target }, idbPro);
|
|
558
231
|
});
|
|
559
232
|
return await fn(...stores);
|
|
560
233
|
} catch (e) {
|
|
561
|
-
throw rollbackOnError !== !1 &&
|
|
234
|
+
throw rollbackOnError !== !1 && tx2.abort(), e;
|
|
562
235
|
}
|
|
563
236
|
} finally {
|
|
564
|
-
|
|
237
|
+
db2.close();
|
|
565
238
|
}
|
|
566
239
|
}
|
|
567
240
|
}
|
|
@@ -574,20 +247,20 @@ class DbIterator extends DataOperationBase {
|
|
|
574
247
|
preSkip;
|
|
575
248
|
startKey;
|
|
576
249
|
startPrimaryKey;
|
|
577
|
-
constructor(schema,
|
|
578
|
-
if (super(schema,
|
|
250
|
+
constructor(schema, db2, option) {
|
|
251
|
+
if (super(schema, db2), !option) return;
|
|
579
252
|
const { parser } = option;
|
|
580
|
-
this.direction = option.direction, this.query = option.query, this.writable = !!option.writable, this.endsWithNull = !!option.endsWithNull, this.preSkip = option.preSkip, this.startKey = option.startKey, this.startPrimaryKey = option.startPrimaryKey, parser && (this.parser = isFunction(parser) ? parser : DbIteratorParsers[parser]);
|
|
253
|
+
this.direction = option.direction, this.query = option.query, this.writable = !!option.writable, this.endsWithNull = !!option.endsWithNull, this.preSkip = option.preSkip, this.startKey = option.startKey, this.startPrimaryKey = option.startPrimaryKey, parser && (this.parser = types.isFunction(parser) ? parser : type$1.DbIteratorParsers[parser]);
|
|
581
254
|
}
|
|
582
255
|
async *[Symbol.asyncIterator]() {
|
|
583
|
-
const { parser, writable, endsWithNull } = this, { db, tx, target } = await this.tx(writable);
|
|
256
|
+
const { parser, writable, endsWithNull } = this, { db: db2, tx: tx2, target } = await this.tx(writable);
|
|
584
257
|
try {
|
|
585
258
|
const request = await openCursor(target, this);
|
|
586
259
|
let cursor;
|
|
587
260
|
if (parser)
|
|
588
|
-
for (; cursor = await requestDbResult(request); ) {
|
|
261
|
+
for (; cursor = await store.requestDbResult(request); ) {
|
|
589
262
|
const { control, value } = await parser(cursor);
|
|
590
|
-
if (control || (yield value), control === Break) break;
|
|
263
|
+
if (control || (yield value), control === type$1.Break) break;
|
|
591
264
|
cursor.continue();
|
|
592
265
|
}
|
|
593
266
|
else {
|
|
@@ -595,23 +268,23 @@ class DbIterator extends DataOperationBase {
|
|
|
595
268
|
const end = () => {
|
|
596
269
|
ended = !0;
|
|
597
270
|
};
|
|
598
|
-
for (; !ended && (cursor = await requestDbResult(request)); )
|
|
271
|
+
for (; !ended && (cursor = await store.requestDbResult(request)); )
|
|
599
272
|
yield { cursor, end };
|
|
600
273
|
}
|
|
601
|
-
writable &&
|
|
274
|
+
writable && tx2?.commit(), endsWithNull && !cursor && (yield null);
|
|
602
275
|
} finally {
|
|
603
|
-
|
|
276
|
+
db2?.close();
|
|
604
277
|
}
|
|
605
278
|
}
|
|
606
279
|
}
|
|
607
280
|
function parseFilterArg(arg1, arg2, arg3, limit) {
|
|
608
|
-
const args = isObject(arg1) ? arg1 : {};
|
|
609
|
-
return isFunction(arg1) ? args.fn = arg1 : isIDbQuery(arg1) && (args.query = arg1), isFunction(arg2) ? args.fn = arg2 : arg2 && (args.direction = arg2), arg3 && (args.direction = arg3), limit ? args.limit = limit : args.limit || (args.limit = 1e3), args.maxEmptyChecks || (args.maxEmptyChecks = 2e4), args;
|
|
281
|
+
const args = types.isObject(arg1) ? arg1 : {};
|
|
282
|
+
return types.isFunction(arg1) ? args.fn = arg1 : isIDbQuery(arg1) && (args.query = arg1), types.isFunction(arg2) ? args.fn = arg2 : arg2 && (args.direction = arg2), arg3 && (args.direction = arg3), limit ? args.limit = limit : args.limit || (args.limit = 1e3), args.maxEmptyChecks || (args.maxEmptyChecks = 2e4), args;
|
|
610
283
|
}
|
|
611
284
|
async function queryPage(anyReader, info, arg) {
|
|
612
285
|
info.size || (info.size = 100), info.nextSkip || (info.nextSkip = 0);
|
|
613
286
|
const { query, direction, total, maxEmptyChecks, fn } = info;
|
|
614
|
-
return await anyReader.batchRead(async (nativeReader) => (total || (info.total = await nativeReader.count({ query, direction, maxEmptyChecks, fn }), info.pages = Math.ceil(info.total / info.size)), info.total < 1 ? { info: deepFreeze(info), rows: [] } : fn ? await queryFnPage(nativeReader, info, arg) : await queryNoneFnPage(nativeReader, info)));
|
|
287
|
+
return await anyReader.batchRead(async (nativeReader) => (total || (info.total = await nativeReader.count({ query, direction, maxEmptyChecks, fn }), info.pages = Math.ceil(info.total / info.size)), info.total < 1 ? { info: basic.deepFreeze(info), rows: [] } : fn ? await queryFnPage(nativeReader, info, arg) : await queryNoneFnPage(nativeReader, info)));
|
|
615
288
|
}
|
|
616
289
|
async function queryNoneFnPage(nativeReader, info) {
|
|
617
290
|
const { keyPath } = nativeReader.storeSchema, { page, query, direction, size, mapper } = info, preSkip = (page - 1) * size, rows = await nativeReader.filter({
|
|
@@ -622,7 +295,7 @@ async function queryNoneFnPage(nativeReader, info) {
|
|
|
622
295
|
mapper: getMapperFn(mapper, keyPath, !0)
|
|
623
296
|
});
|
|
624
297
|
return info.nextSkip = preSkip + rows.length, {
|
|
625
|
-
info: deepFreeze(info),
|
|
298
|
+
info: basic.deepFreeze(info),
|
|
626
299
|
rows
|
|
627
300
|
};
|
|
628
301
|
}
|
|
@@ -637,7 +310,7 @@ async function queryFnPage(reader, info, arg) {
|
|
|
637
310
|
}
|
|
638
311
|
async function queryFnRow({ storeSchema: { defaultGetMapper } }, request, info, keyPath, hasSkip) {
|
|
639
312
|
const { page, size, total, maxEmptyChecks, fn } = info, mapper = getMapperFn(info.mapper || defaultGetMapper, keyPath, !0), rows = [], preSkip = (page - 1) * info.size;
|
|
640
|
-
if (preSkip >= total) return { info: deepFreeze(info), rows: [] };
|
|
313
|
+
if (preSkip >= total) return { info: basic.deepFreeze(info), rows: [] };
|
|
641
314
|
let i = 0;
|
|
642
315
|
return await new Promise(async (resolve, reject) => {
|
|
643
316
|
let ept = 0, skipped = 0;
|
|
@@ -660,19 +333,19 @@ async function queryFnRow({ storeSchema: { defaultGetMapper } }, request, info,
|
|
|
660
333
|
cursor.continue(), i++;
|
|
661
334
|
} : request.onsuccess = rowFn;
|
|
662
335
|
}), i && (info.nextSkip += i + 1), {
|
|
663
|
-
info: deepFreeze(info),
|
|
336
|
+
info: basic.deepFreeze(info),
|
|
664
337
|
rows
|
|
665
338
|
};
|
|
666
339
|
}
|
|
667
340
|
class DataReader extends DataOperationBase {
|
|
668
341
|
all(query, limit) {
|
|
669
342
|
const param = isIDbQuery(query) ? { query } : query || {};
|
|
670
|
-
return isNumber(limit) && (param.count = limit), isNumber(param.count) || (param.count = 1e3), param.query = parseIDbQuery(param.query), this.tx(!1, (store) => requestDbResult(store.getAll(param)));
|
|
343
|
+
return types.isNumber(limit) && (param.count = limit), types.isNumber(param.count) || (param.count = 1e3), param.query = parseIDbQuery(param.query), this.tx(!1, (store$1) => store.requestDbResult(store$1.getAll(param)));
|
|
671
344
|
}
|
|
672
345
|
async count(arg1, arg2, arg3) {
|
|
673
346
|
const args = parseFilterArg(arg1, arg2, arg3), { query, direction, fn } = args;
|
|
674
347
|
if (!fn)
|
|
675
|
-
return await this.tx(!1, (store) => requestDbResult(store.count(parseIDbQuery(query))));
|
|
348
|
+
return await this.tx(!1, (store$1) => store.requestDbResult(store$1.count(parseIDbQuery(query))));
|
|
676
349
|
const { maxEmptyChecks } = args, findFn = fn;
|
|
677
350
|
let count = 0, ept = 0, i = 0;
|
|
678
351
|
return await this.openCursor({
|
|
@@ -688,11 +361,11 @@ class DataReader extends DataOperationBase {
|
|
|
688
361
|
}), count;
|
|
689
362
|
}
|
|
690
363
|
get(key) {
|
|
691
|
-
return this.tx(!1, (store) => requestDbResult(store.get(parseIDbQuery(key))));
|
|
364
|
+
return this.tx(!1, (store$1) => store.requestDbResult(store$1.get(parseIDbQuery(key))));
|
|
692
365
|
}
|
|
693
366
|
getMany(keys, excludeEmpty) {
|
|
694
367
|
return this.batchRead(async (reader) => {
|
|
695
|
-
const rows = await asyncMap(keys, (k) => reader.get(k));
|
|
368
|
+
const rows = await basic.asyncMap(keys, (k) => reader.get(k));
|
|
696
369
|
return excludeEmpty ? rows.filter((v) => v) : rows;
|
|
697
370
|
});
|
|
698
371
|
}
|
|
@@ -700,7 +373,7 @@ class DataReader extends DataOperationBase {
|
|
|
700
373
|
return this.forEach({ query, direction }, !0);
|
|
701
374
|
}
|
|
702
375
|
async getRangeMany(keys, direction, excludeEmpty) {
|
|
703
|
-
return (await this.batchRead((w) => asyncMap(keys, (v) => w.getRange(v, direction)))).flat();
|
|
376
|
+
return (await this.batchRead((w) => basic.asyncMap(keys, (v) => w.getRange(v, direction)))).flat();
|
|
704
377
|
}
|
|
705
378
|
index(name, writable) {
|
|
706
379
|
let { target } = this;
|
|
@@ -720,14 +393,14 @@ class DataReader extends DataOperationBase {
|
|
|
720
393
|
return this.createOperator(target, writable);
|
|
721
394
|
}
|
|
722
395
|
batchRead(fn) {
|
|
723
|
-
return isNativeTarget(this.target) ? fn(this) : this.tx(!1, (
|
|
396
|
+
return type$1.isNativeTarget(this.target) ? fn(this) : this.tx(!1, (store2) => fn(this.idbPro.schema.factory.newReader({
|
|
724
397
|
storeSchema: this.storeSchema,
|
|
725
|
-
target:
|
|
398
|
+
target: store2
|
|
726
399
|
}, this.idbPro)));
|
|
727
400
|
}
|
|
728
401
|
iterator(query, direction) {
|
|
729
402
|
const arg = {};
|
|
730
|
-
return isString(direction) && (arg.direction = direction), isIDbQuery(query) ? arg.query = query : isObject(query) && Object.assign(arg, query), new DbIterator({ storeSchema: this.storeSchema, target: this.target }, this.idbPro, {
|
|
403
|
+
return types.isString(direction) && (arg.direction = direction), isIDbQuery(query) ? arg.query = query : types.isObject(query) && Object.assign(arg, query), new DbIterator({ storeSchema: this.storeSchema, target: this.target }, this.idbPro, {
|
|
731
404
|
...arg,
|
|
732
405
|
parser: "value"
|
|
733
406
|
});
|
|
@@ -740,7 +413,7 @@ class DataReader extends DataOperationBase {
|
|
|
740
413
|
mapper,
|
|
741
414
|
fn: (value, k, i) => {
|
|
742
415
|
if (i >= limit - 1)
|
|
743
|
-
return { control: Finished };
|
|
416
|
+
return { control: type$1.Finished };
|
|
744
417
|
}
|
|
745
418
|
}, !0);
|
|
746
419
|
let ept = 0;
|
|
@@ -751,9 +424,9 @@ class DataReader extends DataOperationBase {
|
|
|
751
424
|
if (fn(value, k, i))
|
|
752
425
|
ept = 0;
|
|
753
426
|
else
|
|
754
|
-
return ++ept >= maxEmptyChecks ? { control: Break } : { control: Continue };
|
|
427
|
+
return ++ept >= maxEmptyChecks ? { control: type$1.Break } : { control: type$1.Continue };
|
|
755
428
|
if (results.length >= limit - 1)
|
|
756
|
-
return { control: Finished };
|
|
429
|
+
return { control: type$1.Finished };
|
|
757
430
|
}
|
|
758
431
|
}, !0);
|
|
759
432
|
}
|
|
@@ -809,7 +482,7 @@ function checkAddValue(storeSchema, value) {
|
|
|
809
482
|
updatedCountField,
|
|
810
483
|
softDeletedField
|
|
811
484
|
} = storeSchema;
|
|
812
|
-
return value = { ...value }, addedTimeField?.name && !isNumber(value[addedTimeField.name]) && (value[addedTimeField.name] = Date.now()), updatedTimeField?.name && !isNumber(value[updatedTimeField.name]) && (value[updatedTimeField.name] = Date.now()), softDeletedField?.name && !isNumber(value[softDeletedField.name]) && (value[softDeletedField.name] = 0), updatedCountField?.name && (value[updatedCountField.name] = 0), value;
|
|
485
|
+
return value = { ...value }, addedTimeField?.name && !types.isNumber(value[addedTimeField.name]) && (value[addedTimeField.name] = Date.now()), updatedTimeField?.name && !types.isNumber(value[updatedTimeField.name]) && (value[updatedTimeField.name] = Date.now()), softDeletedField?.name && !types.isNumber(value[softDeletedField.name]) && (value[softDeletedField.name] = 0), updatedCountField?.name && (value[updatedCountField.name] = 0), value;
|
|
813
486
|
}
|
|
814
487
|
function checkUpdateValue(storeSchema, newValue, oldValue) {
|
|
815
488
|
if (!newValue || !(newValue instanceof Object) || Array.isArray(newValue)) return newValue;
|
|
@@ -823,23 +496,23 @@ class DataWriter extends DataReader {
|
|
|
823
496
|
add(record) {
|
|
824
497
|
return this.changeByPk({
|
|
825
498
|
record,
|
|
826
|
-
fn: async (store, pk, newValue, oldValue, keyPath) => {
|
|
499
|
+
fn: async (store$1, pk, newValue, oldValue, keyPath) => {
|
|
827
500
|
const { storeSchema } = this;
|
|
828
|
-
return newValue = checkAddValue(storeSchema, newValue), keyPath ? [{ ...newValue, ...getKeyValueToObject(keyPath, await requestDbResult(store.add(newValue))) }, pk] : [newValue, await requestDbResult(store.add(newValue, pk))];
|
|
501
|
+
return newValue = checkAddValue(storeSchema, newValue), keyPath ? [{ ...newValue, ...getKeyValueToObject(keyPath, await store.requestDbResult(store$1.add(newValue))) }, pk] : [newValue, await store.requestDbResult(store$1.add(newValue, pk))];
|
|
829
502
|
}
|
|
830
503
|
});
|
|
831
504
|
}
|
|
832
505
|
addMany(records, returns) {
|
|
833
|
-
return this.batchWrite((w) => returns ? asyncMap(records, (v) => w.add(v)) : asyncForEach(records, (v) => w.add(v)), !0);
|
|
506
|
+
return this.batchWrite((w) => returns ? basic.asyncMap(records, (v) => w.add(v)) : basic.asyncForEach(records, (v) => w.add(v)), !0);
|
|
834
507
|
}
|
|
835
508
|
addOrSkip(record) {
|
|
836
509
|
return this.batchWrite(async (w) => {
|
|
837
510
|
const { keyPath: storeKeyPath, defaultGetMapper } = w.storeSchema, { key: pk, value } = storeKeyPath ? {
|
|
838
511
|
key: getValidKeyValue(storeKeyPath, record),
|
|
839
512
|
value: record
|
|
840
|
-
} : parseDbNoneKeyPathRecord(record);
|
|
513
|
+
} : type$1.parseDbNoneKeyPathRecord(record);
|
|
841
514
|
if (pk) {
|
|
842
|
-
const item = await requestDbResult(w.nativeStore.get(pk));
|
|
515
|
+
const item = await store.requestDbResult(w.nativeStore.get(pk));
|
|
843
516
|
if (item)
|
|
844
517
|
return getMapperFn(defaultGetMapper, storeKeyPath)?.(item, pk);
|
|
845
518
|
}
|
|
@@ -853,24 +526,24 @@ class DataWriter extends DataReader {
|
|
|
853
526
|
});
|
|
854
527
|
}
|
|
855
528
|
addOrSkipMany(records, returns) {
|
|
856
|
-
return this.batchWrite((w) => returns ? asyncMap(records, (v) => w.addOrSkip(v)) : asyncForEach(records, (v) => w.addOrSkip(v)), !0);
|
|
529
|
+
return this.batchWrite((w) => returns ? basic.asyncMap(records, (v) => w.addOrSkip(v)) : basic.asyncForEach(records, (v) => w.addOrSkip(v)), !0);
|
|
857
530
|
}
|
|
858
531
|
replace(record) {
|
|
859
532
|
return this.changeByPk({
|
|
860
533
|
record,
|
|
861
534
|
getOld: !0,
|
|
862
|
-
fn: async (store, pk, newValue, oldValue, keyPath) => {
|
|
535
|
+
fn: async (store$1, pk, newValue, oldValue, keyPath) => {
|
|
863
536
|
const { storeSchema } = this, { updatedTimeField, updatedCountField, addedTimeField } = storeSchema;
|
|
864
537
|
return oldValue ? newValue = checkUpdateValue(storeSchema, newValue, {
|
|
865
538
|
[updatedTimeField.name]: oldValue[updatedTimeField.name],
|
|
866
539
|
[updatedCountField.name]: oldValue[updatedCountField.name],
|
|
867
540
|
[addedTimeField.name]: oldValue[addedTimeField.name]
|
|
868
|
-
}) : newValue = checkAddValue(storeSchema, newValue), keyPath ? [{ ...newValue, ...getKeyValueToObject(keyPath, await requestDbResult(store.put(newValue))) }, pk] : [newValue, await requestDbResult(store.put(newValue, pk))];
|
|
541
|
+
}) : newValue = checkAddValue(storeSchema, newValue), keyPath ? [{ ...newValue, ...getKeyValueToObject(keyPath, await store.requestDbResult(store$1.put(newValue))) }, pk] : [newValue, await store.requestDbResult(store$1.put(newValue, pk))];
|
|
869
542
|
}
|
|
870
543
|
});
|
|
871
544
|
}
|
|
872
545
|
replaceMany(records, returns) {
|
|
873
|
-
return this.batchWrite((w) => returns ? asyncMap(records, (v) => w.replace(v)) : asyncForEach(records, (v) => w.replace(v)), !0);
|
|
546
|
+
return this.batchWrite((w) => returns ? basic.asyncMap(records, (v) => w.replace(v)) : basic.asyncForEach(records, (v) => w.replace(v)), !0);
|
|
874
547
|
}
|
|
875
548
|
change(record, throwIfMissing) {
|
|
876
549
|
return this.changeByPk({
|
|
@@ -878,10 +551,10 @@ class DataWriter extends DataReader {
|
|
|
878
551
|
getOld: !0,
|
|
879
552
|
requiredOld: throwIfMissing,
|
|
880
553
|
requiredPk: throwIfMissing,
|
|
881
|
-
fn: async (store, pk, newValue, oldValue, keyPath) => {
|
|
554
|
+
fn: async (store$1, pk, newValue, oldValue, keyPath) => {
|
|
882
555
|
const { storeSchema } = this;
|
|
883
556
|
if (oldValue)
|
|
884
|
-
return newValue = checkUpdateValue(storeSchema, newValue, oldValue), keyPath ? [{ ...newValue, ...getKeyValueToObject(keyPath, await requestDbResult(store.put(newValue))) }, pk] : [newValue, await requestDbResult(store.put(newValue, pk))];
|
|
557
|
+
return newValue = checkUpdateValue(storeSchema, newValue, oldValue), keyPath ? [{ ...newValue, ...getKeyValueToObject(keyPath, await store.requestDbResult(store$1.put(newValue))) }, pk] : [newValue, await store.requestDbResult(store$1.put(newValue, pk))];
|
|
885
558
|
}
|
|
886
559
|
});
|
|
887
560
|
}
|
|
@@ -889,55 +562,55 @@ class DataWriter extends DataReader {
|
|
|
889
562
|
const {
|
|
890
563
|
returns,
|
|
891
564
|
throwIfMissing
|
|
892
|
-
} = isBoolean(option) ? { returns: option } : option || {};
|
|
893
|
-
return this.batchWrite((w) => returns ? asyncMap(records, (v) => w.change(v, throwIfMissing)) : asyncForEach(records, (v) => w.change(v, throwIfMissing)), !0);
|
|
565
|
+
} = types.isBoolean(option) ? { returns: option } : option || {};
|
|
566
|
+
return this.batchWrite((w) => returns ? basic.asyncMap(records, (v) => w.change(v, throwIfMissing)) : basic.asyncForEach(records, (v) => w.change(v, throwIfMissing)), !0);
|
|
894
567
|
}
|
|
895
568
|
addOrChange(record) {
|
|
896
569
|
return this.changeByPk({
|
|
897
570
|
record,
|
|
898
571
|
getOld: !0,
|
|
899
|
-
fn: async (store, pk, newValue, oldValue, keyPath) => {
|
|
572
|
+
fn: async (store$1, pk, newValue, oldValue, keyPath) => {
|
|
900
573
|
const { storeSchema } = this;
|
|
901
|
-
return oldValue ? (newValue = checkUpdateValue(storeSchema, newValue, oldValue), keyPath ? [{ ...newValue, ...getKeyValueToObject(keyPath, await requestDbResult(store.put(newValue))) }, pk] : [newValue, await requestDbResult(store.put(newValue, pk))]) : (newValue = checkAddValue(storeSchema, newValue), keyPath ? [{ ...newValue, ...getKeyValueToObject(keyPath, await requestDbResult(store.add(newValue))) }, pk] : [newValue, await requestDbResult(store.add(newValue, pk))]);
|
|
574
|
+
return oldValue ? (newValue = checkUpdateValue(storeSchema, newValue, oldValue), keyPath ? [{ ...newValue, ...getKeyValueToObject(keyPath, await store.requestDbResult(store$1.put(newValue))) }, pk] : [newValue, await store.requestDbResult(store$1.put(newValue, pk))]) : (newValue = checkAddValue(storeSchema, newValue), keyPath ? [{ ...newValue, ...getKeyValueToObject(keyPath, await store.requestDbResult(store$1.add(newValue))) }, pk] : [newValue, await store.requestDbResult(store$1.add(newValue, pk))]);
|
|
902
575
|
}
|
|
903
576
|
});
|
|
904
577
|
}
|
|
905
578
|
addOrChangeMany(records, returns) {
|
|
906
|
-
return this.batchWrite((w) => returns ? asyncMap(records, (v) => w.addOrChange(v)) : asyncForEach(records, (v) => w.addOrChange(v)), !0);
|
|
579
|
+
return this.batchWrite((w) => returns ? basic.asyncMap(records, (v) => w.addOrChange(v)) : basic.asyncForEach(records, (v) => w.addOrChange(v)), !0);
|
|
907
580
|
}
|
|
908
581
|
delete(pk, returns) {
|
|
909
582
|
const {
|
|
910
583
|
returns: getOld,
|
|
911
584
|
physical
|
|
912
|
-
} = isBoolean(returns) ? { returns } : returns || {}, { storeSchema } = this, softDeletedField = storeSchema.softDeletedField, requiredOld = !!(!physical && softDeletedField?.name);
|
|
585
|
+
} = types.isBoolean(returns) ? { returns } : returns || {}, { storeSchema } = this, softDeletedField = storeSchema.softDeletedField, requiredOld = !!(!physical && softDeletedField?.name);
|
|
913
586
|
return this.changeByPk({
|
|
914
587
|
pk,
|
|
915
588
|
getOld: requiredOld || getOld,
|
|
916
|
-
fn: (
|
|
917
|
-
if (requiredOld ? oldValue && (oldValue = checkUpdateValue(storeSchema, { [softDeletedField.name]: Bool.True }, oldValue),
|
|
589
|
+
fn: (store2, pk2, newValue, oldValue) => {
|
|
590
|
+
if (requiredOld ? oldValue && (oldValue = checkUpdateValue(storeSchema, { [softDeletedField.name]: type.Bool.True }, oldValue), store2.put(oldValue)) : store2.delete(pk2), !!getOld)
|
|
918
591
|
return [oldValue, pk2];
|
|
919
592
|
}
|
|
920
593
|
});
|
|
921
594
|
}
|
|
922
595
|
deleteMany(keys, returns) {
|
|
923
|
-
const { returns: hasRtn } = isBoolean(returns) ? { returns } : returns || {};
|
|
924
|
-
return this.batchWrite((w) => hasRtn ? asyncMap(keys, (v) => w.delete(v, !0)) : asyncForEach(keys, (v) => w.delete(v)), !0);
|
|
596
|
+
const { returns: hasRtn } = types.isBoolean(returns) ? { returns } : returns || {};
|
|
597
|
+
return this.batchWrite((w) => hasRtn ? basic.asyncMap(keys, (v) => w.delete(v, !0)) : basic.asyncForEach(keys, (v) => w.delete(v)), !0);
|
|
925
598
|
}
|
|
926
599
|
deleteRange(query, returns) {
|
|
927
600
|
const {
|
|
928
601
|
returns: hasRtn,
|
|
929
602
|
physical,
|
|
930
603
|
direction
|
|
931
|
-
} = isBoolean(returns) ? { returns } : returns || {}, { name } = this.storeSchema.softDeletedField || {};
|
|
604
|
+
} = types.isBoolean(returns) ? { returns } : returns || {}, { name } = this.storeSchema.softDeletedField || {};
|
|
932
605
|
return this.cursor({
|
|
933
606
|
query,
|
|
934
607
|
direction,
|
|
935
|
-
fn: (value) => physical || !name ? { modify: Delete } : (value[name] = Bool.True, { modify: Save })
|
|
608
|
+
fn: (value) => physical || !name ? { modify: type$1.Delete } : (value[name] = type.Bool.True, { modify: type$1.Save })
|
|
936
609
|
}, hasRtn);
|
|
937
610
|
}
|
|
938
611
|
deleteRangeMany(keys, returns) {
|
|
939
|
-
const option = isBoolean(returns) ? { returns } : returns;
|
|
940
|
-
return this.batchWrite((w) => option?.returns ? asyncMap(keys, (v) => w.deleteRange(v, option)) : asyncForEach(keys, (v) => w.deleteRange(v, option)), !0);
|
|
612
|
+
const option = types.isBoolean(returns) ? { returns } : returns;
|
|
613
|
+
return this.batchWrite((w) => option?.returns ? basic.asyncMap(keys, (v) => w.deleteRange(v, option)) : basic.asyncForEach(keys, (v) => w.deleteRange(v, option)), !0);
|
|
941
614
|
}
|
|
942
615
|
changeRange(arg, returns) {
|
|
943
616
|
let {
|
|
@@ -950,26 +623,26 @@ class DataWriter extends DataReader {
|
|
|
950
623
|
return this.cursor({
|
|
951
624
|
query,
|
|
952
625
|
direction,
|
|
953
|
-
fn: (value) => value instanceof Object ? { modify: Save, value: { ...value, ...newValue } } : { modify: Save, value: newValue }
|
|
626
|
+
fn: (value) => value instanceof Object ? { modify: type$1.Save, value: { ...value, ...newValue } } : { modify: type$1.Save, value: newValue }
|
|
954
627
|
}, returns);
|
|
955
628
|
}
|
|
956
629
|
changeRangeMany(args, returns) {
|
|
957
|
-
return this.batchWrite((w) => returns ? asyncMap(args, (v) => w.changeRange(v, returns)) : asyncForEach(args, (v) => w.changeRange(v, returns)), !0);
|
|
630
|
+
return this.batchWrite((w) => returns ? basic.asyncMap(args, (v) => w.changeRange(v, returns)) : basic.asyncForEach(args, (v) => w.changeRange(v, returns)), !0);
|
|
958
631
|
}
|
|
959
632
|
cursor(arg, returns) {
|
|
960
|
-
return arg = isFunction(arg) ? { fn: arg } : arg || {}, arg.fn ? returns ? this.cursorResult(arg, !0) : this.cursorVoid(arg, !0) : new DbIterator(this, this.idbPro);
|
|
633
|
+
return arg = types.isFunction(arg) ? { fn: arg } : arg || {}, arg.fn ? returns ? this.cursorResult(arg, !0) : this.cursorVoid(arg, !0) : new DbIterator(this, this.idbPro);
|
|
961
634
|
}
|
|
962
635
|
batchWrite(fn, rollbackOnError) {
|
|
963
636
|
const { target } = this;
|
|
964
|
-
if (isNativeTarget(target))
|
|
637
|
+
if (type$1.isNativeTarget(target))
|
|
965
638
|
try {
|
|
966
639
|
return fn(this);
|
|
967
640
|
} catch (e) {
|
|
968
641
|
throw rollbackOnError !== !1 && (target instanceof IDBIndex ? target.objectStore : target).transaction.abort(), e;
|
|
969
642
|
}
|
|
970
|
-
return this.tx(!0, (
|
|
643
|
+
return this.tx(!0, (store2) => fn(this.idbPro.schema.factory.newWriter({
|
|
971
644
|
storeSchema: this.storeSchema,
|
|
972
|
-
target:
|
|
645
|
+
target: store2
|
|
973
646
|
}, this.idbPro)), rollbackOnError);
|
|
974
647
|
}
|
|
975
648
|
changeByPk({ pk, record, fn, requiredPk, getOld, requiredOld, saveMapper, getMapper }) {
|
|
@@ -981,12 +654,12 @@ class DataWriter extends DataReader {
|
|
|
981
654
|
else if (keyPath)
|
|
982
655
|
query = getValidKeyValue(keyPath, record);
|
|
983
656
|
else {
|
|
984
|
-
const { key, value } = parseDbNoneKeyPathRecord(record);
|
|
657
|
+
const { key, value } = type$1.parseDbNoneKeyPathRecord(record);
|
|
985
658
|
query = key, newValue = value;
|
|
986
659
|
}
|
|
987
660
|
if (requiredPk && !query)
|
|
988
661
|
throw new Error(`key is required: ${JSON.stringify(record)}`);
|
|
989
|
-
const oldValue = query && (getOld || requiredOld) ? await requestDbResult(w.nativeStore.get(query)) : void 0;
|
|
662
|
+
const oldValue = query && (getOld || requiredOld) ? await store.requestDbResult(w.nativeStore.get(query)) : void 0;
|
|
990
663
|
if (requiredOld && !oldValue)
|
|
991
664
|
throw new Error(`record not found: ${JSON.stringify(record)}`);
|
|
992
665
|
const result = await fn(w.nativeStore, query, newValue, oldValue, keyPath);
|
|
@@ -997,16 +670,16 @@ class DataWriter extends DataReader {
|
|
|
997
670
|
}
|
|
998
671
|
class DbMap extends DataOperationBase {
|
|
999
672
|
get size() {
|
|
1000
|
-
return this.tx(!1, (store) => requestDbResult(store.count()));
|
|
673
|
+
return this.tx(!1, (store$1) => store.requestDbResult(store$1.count()));
|
|
1001
674
|
}
|
|
1002
675
|
delete(key) {
|
|
1003
|
-
return this.tx(!0, async (t,
|
|
1004
|
-
|
|
676
|
+
return this.tx(!0, async (t, store2) => {
|
|
677
|
+
store2.delete(key);
|
|
1005
678
|
});
|
|
1006
679
|
}
|
|
1007
680
|
batch(fn) {
|
|
1008
681
|
const { idbPro, storeSchema, factory } = this;
|
|
1009
|
-
return this.tx(!0, async (t,
|
|
682
|
+
return this.tx(!0, async (t, store2) => await fn(factory.newDbMap({ storeSchema, target: store2 }, idbPro)));
|
|
1010
683
|
}
|
|
1011
684
|
asStore(writable) {
|
|
1012
685
|
const { factory } = this.idbPro.schema;
|
|
@@ -1018,13 +691,13 @@ class DbMap extends DataOperationBase {
|
|
|
1018
691
|
}, this.idbPro, { parser: "keyValue" });
|
|
1019
692
|
}
|
|
1020
693
|
async get(key, defaultValue) {
|
|
1021
|
-
return await this.tx(!1, (store) => requestDbResult(store.get(key))) || defaultValue;
|
|
694
|
+
return await this.tx(!1, (store$1) => store.requestDbResult(store$1.get(key))) || defaultValue;
|
|
1022
695
|
}
|
|
1023
696
|
getMany(keys) {
|
|
1024
|
-
return this.tx(!1, async (store) => {
|
|
697
|
+
return this.tx(!1, async (store$1) => {
|
|
1025
698
|
const result = [];
|
|
1026
699
|
for (const k of keys)
|
|
1027
|
-
result.push(await requestDbResult(store.get(k)));
|
|
700
|
+
result.push(await store.requestDbResult(store$1.get(k)));
|
|
1028
701
|
return result;
|
|
1029
702
|
});
|
|
1030
703
|
}
|
|
@@ -1035,14 +708,14 @@ class DbMap extends DataOperationBase {
|
|
|
1035
708
|
return new DbIterator({ storeSchema: this.storeSchema, target: this.target }, this.idbPro, { parser: "key" });
|
|
1036
709
|
}
|
|
1037
710
|
set(key, value) {
|
|
1038
|
-
return this.tx(!0, async (t, store) => {
|
|
1039
|
-
await requestDbResult(store.put(value, key));
|
|
711
|
+
return this.tx(!0, async (t, store$1) => {
|
|
712
|
+
await store.requestDbResult(store$1.put(value, key));
|
|
1040
713
|
});
|
|
1041
714
|
}
|
|
1042
715
|
setMany(values) {
|
|
1043
|
-
return this.tx(!0, async (t,
|
|
716
|
+
return this.tx(!0, async (t, store2) => {
|
|
1044
717
|
for (const [k, v] of values)
|
|
1045
|
-
|
|
718
|
+
store2.put(v, k);
|
|
1046
719
|
});
|
|
1047
720
|
}
|
|
1048
721
|
values() {
|
|
@@ -1063,15 +736,15 @@ function equalKeyPath(p1, p2) {
|
|
|
1063
736
|
const versionDiffValidate = ({ stores, schema }) => {
|
|
1064
737
|
const storeSchemas = schema.storeSchemas;
|
|
1065
738
|
let info = "";
|
|
1066
|
-
for (const
|
|
1067
|
-
const ss = storeSchemas.find((s) => s.name ===
|
|
739
|
+
for (const store2 of stores) {
|
|
740
|
+
const ss = storeSchemas.find((s) => s.name === store2.name);
|
|
1068
741
|
if (ss) {
|
|
1069
|
-
if (!equalKeyPath(
|
|
1070
|
-
info = `store [ ${
|
|
742
|
+
if (!equalKeyPath(store2.keyPath, ss.keyPath)) {
|
|
743
|
+
info = `store [ ${store2.name} ] keyPath not equal,schema.keyPath:${ss.keyPath},store.keyPath:${store2.keyPath}[]`;
|
|
1071
744
|
break;
|
|
1072
745
|
}
|
|
1073
|
-
if (!
|
|
1074
|
-
info = `store [ ${
|
|
746
|
+
if (!store2.autoIncrement != !ss.autoIncrement) {
|
|
747
|
+
info = `store [ ${store2.name} ] autoIncrement not equal`;
|
|
1075
748
|
break;
|
|
1076
749
|
}
|
|
1077
750
|
}
|
|
@@ -1079,34 +752,34 @@ const versionDiffValidate = ({ stores, schema }) => {
|
|
|
1079
752
|
return info ? `The existing database is inconsistent with the definition and cannot be corrected: ${info}` : !0;
|
|
1080
753
|
}, versionSameValidate = async (context) => {
|
|
1081
754
|
let result = versionDiffValidate(context);
|
|
1082
|
-
return isString(result) || (result = validateStoreAndIndexes(context)), result;
|
|
755
|
+
return types.isString(result) || (result = validateStoreAndIndexes(context)), result;
|
|
1083
756
|
}, validateStoreAndIndexes = ({ stores, schema }) => {
|
|
1084
757
|
const storeSchemas = schema.storeSchemas, dbStoreNames = stores.map((s) => s.name);
|
|
1085
758
|
let info = "";
|
|
1086
759
|
const missingStoreNames = storeSchemas.map((s) => s.name).filter((s) => !dbStoreNames.includes(s));
|
|
1087
760
|
if (missingStoreNames.length)
|
|
1088
761
|
info = `store [ ${missingStoreNames.join(",")} ] not exist`;
|
|
1089
|
-
else for (const
|
|
1090
|
-
const ss = storeSchemas.find((s) => s.name ===
|
|
1091
|
-
if (ss && (info = validateIndexes$1(
|
|
762
|
+
else for (const store2 of stores) {
|
|
763
|
+
const ss = storeSchemas.find((s) => s.name === store2.name);
|
|
764
|
+
if (ss && (info = validateIndexes$1(store2, Array.from(store2.indexNames), ss.indexSchemas), info))
|
|
1092
765
|
break;
|
|
1093
766
|
}
|
|
1094
767
|
return info ? `The existing database Store index is inconsistent with the definition and requires a database version upgrade to be fixed: ${info}` : !0;
|
|
1095
768
|
};
|
|
1096
|
-
function validateIndexes$1(
|
|
769
|
+
function validateIndexes$1(store2, indexNames, schemas) {
|
|
1097
770
|
if (indexNames.length !== schemas.length)
|
|
1098
|
-
return `store [ ${
|
|
771
|
+
return `store [ ${store2.name} ] index count not equal`;
|
|
1099
772
|
for (const name of indexNames) {
|
|
1100
773
|
const schema = schemas.find((s) => s.name === name);
|
|
1101
774
|
if (!schema)
|
|
1102
|
-
return `store [ ${
|
|
1103
|
-
const index =
|
|
775
|
+
return `store [ ${store2.name} ] index [ ${name} ] not exist`;
|
|
776
|
+
const index = store2.index(name);
|
|
1104
777
|
if (!schema.unique != !index.unique)
|
|
1105
|
-
return `store [ ${
|
|
778
|
+
return `store [ ${store2.name} ] index [ ${name} ] unique not equal`;
|
|
1106
779
|
if (!schema.multiEntry != !index.multiEntry)
|
|
1107
|
-
return `store [ ${
|
|
780
|
+
return `store [ ${store2.name} ] index [ ${name} ] multiEntry not equal`;
|
|
1108
781
|
if (!equalKeyPath(schema.keyPath, index.keyPath))
|
|
1109
|
-
return `store [ ${
|
|
782
|
+
return `store [ ${store2.name} ] index [ ${name} ] keyPath not equal`;
|
|
1110
783
|
}
|
|
1111
784
|
return "";
|
|
1112
785
|
}
|
|
@@ -1139,20 +812,20 @@ class StoreUpgradeable {
|
|
|
1139
812
|
}
|
|
1140
813
|
}
|
|
1141
814
|
const DataOperatorFactory = Object.freeze({
|
|
1142
|
-
newDataOperators(schemas,
|
|
1143
|
-
return new DataOperators(
|
|
815
|
+
newDataOperators(schemas, db2) {
|
|
816
|
+
return new DataOperators(db2, schemas);
|
|
1144
817
|
},
|
|
1145
|
-
newDbMap(schema,
|
|
1146
|
-
return new DbMap(schema,
|
|
818
|
+
newDbMap(schema, db2) {
|
|
819
|
+
return new DbMap(schema, db2);
|
|
1147
820
|
},
|
|
1148
|
-
newReader(schema,
|
|
1149
|
-
return new DataReader(schema,
|
|
821
|
+
newReader(schema, db2) {
|
|
822
|
+
return new DataReader(schema, db2);
|
|
1150
823
|
},
|
|
1151
824
|
newStoreUpgradeable(nativeStore, storeSchema, upgradeContext) {
|
|
1152
825
|
return new StoreUpgradeable(upgradeContext, storeSchema, nativeStore);
|
|
1153
826
|
},
|
|
1154
|
-
newWriter(schema,
|
|
1155
|
-
return new DataWriter(schema,
|
|
827
|
+
newWriter(schema, db2) {
|
|
828
|
+
return new DataWriter(schema, db2);
|
|
1156
829
|
}
|
|
1157
830
|
});
|
|
1158
831
|
function validateSpecialFields(schema) {
|
|
@@ -1173,21 +846,21 @@ function validateSpecialField(schema, field, defaultName, useDefault) {
|
|
|
1173
846
|
function validateSpecialFieldBase(field, defaultName, useDefault) {
|
|
1174
847
|
if (field === !1)
|
|
1175
848
|
return !1;
|
|
1176
|
-
if (isString(field))
|
|
849
|
+
if (types.isString(field))
|
|
1177
850
|
return { name: field };
|
|
1178
|
-
if (isObject(field)) {
|
|
851
|
+
if (types.isObject(field)) {
|
|
1179
852
|
const r = field;
|
|
1180
|
-
return isBoolean(r.name) && (r.name = defaultName), field;
|
|
853
|
+
return types.isBoolean(r.name) && (r.name = defaultName), field;
|
|
1181
854
|
} else if (field === !0 || useDefault)
|
|
1182
855
|
return { name: defaultName };
|
|
1183
856
|
return !1;
|
|
1184
857
|
}
|
|
1185
858
|
function validateStoreSchema(schema, storeTemplate) {
|
|
1186
|
-
let validSchema = isString(schema) ? { name: schema } : schema;
|
|
859
|
+
let validSchema = types.isString(schema) ? { name: schema } : schema;
|
|
1187
860
|
return storeTemplate && (validSchema = { ...storeTemplate, ...validSchema }), validSchema.indexSchemas || (validSchema.indexSchemas = []), validateSpecialFields(validSchema), Object.isFrozen(validSchema) || (validSchema.indexSchemas = validSchema.indexSchemas.map(indexMapper)), validateDefaultData(validSchema), validSchema;
|
|
1188
861
|
}
|
|
1189
862
|
function indexMapper(index) {
|
|
1190
|
-
const schema = isString(index) ? { name: index } : index;
|
|
863
|
+
const schema = types.isString(index) ? { name: index } : index;
|
|
1191
864
|
return schema.keyPath || (schema.keyPath = schema.name), schema;
|
|
1192
865
|
}
|
|
1193
866
|
function validateDefaultData(schema) {
|
|
@@ -1199,66 +872,66 @@ function validateDefaultData(schema) {
|
|
|
1199
872
|
}
|
|
1200
873
|
const validateSchemaWithDefaults = (schema) => {
|
|
1201
874
|
const { versionDiffValidate: vdf, versionSameValidate: vsf, factory: dof } = schema;
|
|
1202
|
-
return schema.storeSchemas || (schema.storeSchemas = []), schema.storeTemplate || (schema.storeTemplate = { ...defaultStoreSchemaTemplate }), !vdf && vdf !== !1 && (schema.versionDiffValidate = versionDiffValidate), !vsf && vsf !== !1 && (schema.versionSameValidate = versionSameValidate), dof ? dof !== DataOperatorFactory && (schema.factory = { ...DataOperatorFactory, ...dof }) : schema.factory = DataOperatorFactory, schema.storeSchemas = schema.storeSchemas.map((s) => validateStoreSchema(s, schema.storeTemplate)), schema;
|
|
875
|
+
return schema.storeSchemas || (schema.storeSchemas = []), schema.storeTemplate || (schema.storeTemplate = { ...type$1.defaultStoreSchemaTemplate }), !vdf && vdf !== !1 && (schema.versionDiffValidate = versionDiffValidate), !vsf && vsf !== !1 && (schema.versionSameValidate = versionSameValidate), dof ? dof !== DataOperatorFactory && (schema.factory = { ...DataOperatorFactory, ...dof }) : schema.factory = DataOperatorFactory, schema.storeSchemas = schema.storeSchemas.map((s) => validateStoreSchema(s, schema.storeTemplate)), schema;
|
|
1203
876
|
};
|
|
1204
877
|
async function validateBeforeOpen(schema) {
|
|
1205
|
-
const { name, version } = schema, existDb = await findExistDb(name);
|
|
878
|
+
const { name, version } = schema, existDb = await db.findExistDb(name);
|
|
1206
879
|
if (!existDb)
|
|
1207
880
|
return !0;
|
|
1208
|
-
const { versionDiffValidate: versionDiffValidate2, versionSameValidate: versionSameValidate2 } = schema, db = await openDb(name);
|
|
881
|
+
const { versionDiffValidate: versionDiffValidate2, versionSameValidate: versionSameValidate2 } = schema, db$1 = await db.openDb(name);
|
|
1209
882
|
try {
|
|
1210
|
-
if (schema.version < db.version)
|
|
883
|
+
if (schema.version < db$1.version)
|
|
1211
884
|
return "The existing database version is greater than the current version";
|
|
1212
|
-
const validate = schema.version === void 0 || db.version === schema.version ? versionSameValidate2 : versionDiffValidate2;
|
|
885
|
+
const validate = schema.version === void 0 || db$1.version === schema.version ? versionSameValidate2 : versionDiffValidate2;
|
|
1213
886
|
if (!validate)
|
|
1214
887
|
return !0;
|
|
1215
|
-
const storeNames = Array.from(db.objectStoreNames);
|
|
888
|
+
const storeNames = Array.from(db$1.objectStoreNames);
|
|
1216
889
|
if (storeNames.length < 1)
|
|
1217
890
|
return `The existing database [ ${name} ] is empty`;
|
|
1218
|
-
const stores = await readTx(db, storeNames), diffResult = await validate({ schema, db, stores });
|
|
1219
|
-
if (isString(diffResult) || version !== existDb.version)
|
|
891
|
+
const stores = await tx.readTx(db$1, storeNames), diffResult = await validate({ schema, db: db$1, stores });
|
|
892
|
+
if (types.isString(diffResult) || version !== existDb.version)
|
|
1220
893
|
return diffResult;
|
|
1221
894
|
} finally {
|
|
1222
|
-
db?.close();
|
|
895
|
+
db$1?.close();
|
|
1223
896
|
}
|
|
1224
897
|
return !0;
|
|
1225
898
|
}
|
|
1226
|
-
function validateIndexes(storeSchema,
|
|
1227
|
-
const { indexSchemas } = storeSchema, existNames =
|
|
899
|
+
function validateIndexes(storeSchema, store2) {
|
|
900
|
+
const { indexSchemas } = storeSchema, existNames = store2.indexNames, schemaNames = indexSchemas.map((option) => option.name);
|
|
1228
901
|
for (const name of Array.from(existNames))
|
|
1229
|
-
schemaNames.includes(name) ||
|
|
902
|
+
schemaNames.includes(name) || store2.deleteIndex(name);
|
|
1230
903
|
for (const indexSchema of indexSchemas)
|
|
1231
|
-
existNames.contains(indexSchema.name) ? validateIndex(
|
|
1232
|
-
return
|
|
904
|
+
existNames.contains(indexSchema.name) ? validateIndex(store2, indexSchema) : createIndex(store2, indexSchema);
|
|
905
|
+
return store2;
|
|
1233
906
|
}
|
|
1234
|
-
function validateIndex(
|
|
1235
|
-
const index =
|
|
1236
|
-
checkIndex(index, indexOption) || (
|
|
907
|
+
function validateIndex(store2, indexOption) {
|
|
908
|
+
const index = store2.index(indexOption.name);
|
|
909
|
+
checkIndex(index, indexOption) || (store2.deleteIndex(indexOption.name), createIndex(store2, indexOption));
|
|
1237
910
|
}
|
|
1238
911
|
function checkIndex(index, indexOption) {
|
|
1239
912
|
return index.unique !== indexOption.unique || index.multiEntry !== indexOption.multiEntry ? !1 : equalKeyPath(indexOption.keyPath, index.keyPath);
|
|
1240
913
|
}
|
|
1241
|
-
function createIndex(
|
|
914
|
+
function createIndex(store2, schema) {
|
|
1242
915
|
try {
|
|
1243
|
-
|
|
916
|
+
store2.createIndex(schema.name, schema.keyPath, {
|
|
1244
917
|
unique: schema.unique,
|
|
1245
918
|
multiEntry: schema.multiEntry
|
|
1246
919
|
});
|
|
1247
920
|
} catch {
|
|
1248
|
-
throw new Error(`store [ ${
|
|
921
|
+
throw new Error(`store [ ${store2.name} ] index [ ${schema.name} ] create error: ${JSON.stringify(schema)}`);
|
|
1249
922
|
}
|
|
1250
923
|
}
|
|
1251
924
|
function validateStoreDefine(context, schema) {
|
|
1252
925
|
return context.database.objectStoreNames.contains(schema.name) ? validateIndexes(schema, context.transaction?.objectStore(schema.name)) : defineStore(schema, context.database);
|
|
1253
926
|
}
|
|
1254
|
-
function defineStore(schema,
|
|
1255
|
-
const
|
|
927
|
+
function defineStore(schema, db2) {
|
|
928
|
+
const store2 = db2.createObjectStore(schema.name, {
|
|
1256
929
|
keyPath: schema.keyPath,
|
|
1257
930
|
autoIncrement: schema.autoIncrement
|
|
1258
931
|
});
|
|
1259
932
|
for (const option of schema.indexSchemas)
|
|
1260
|
-
createIndex(
|
|
1261
|
-
return
|
|
933
|
+
createIndex(store2, option);
|
|
934
|
+
return store2;
|
|
1262
935
|
}
|
|
1263
936
|
class UpgradeContext {
|
|
1264
937
|
database;
|
|
@@ -1271,12 +944,12 @@ class UpgradeContext {
|
|
|
1271
944
|
this.database = args.database, this.newVersion = args.newVersion, this.oldVersion = args.oldVersion, this.dbSchema = args.dbSchema, this.transaction = args.transaction;
|
|
1272
945
|
}
|
|
1273
946
|
deleteStoreIfExists(storeName) {
|
|
1274
|
-
const
|
|
1275
|
-
|
|
947
|
+
const db2 = this.database;
|
|
948
|
+
db2.objectStoreNames.contains(storeName) && db2.deleteObjectStore(storeName);
|
|
1276
949
|
}
|
|
1277
950
|
destroy() {
|
|
1278
951
|
try {
|
|
1279
|
-
destroyRecords(this.#stores);
|
|
952
|
+
basic.destroyRecords(this.#stores);
|
|
1280
953
|
} finally {
|
|
1281
954
|
for (const k of Object.keys(this.#stores))
|
|
1282
955
|
delete this.#stores[k];
|
|
@@ -1303,7 +976,7 @@ async function upgradeDb(dbSchema, database, e, request) {
|
|
|
1303
976
|
const { storeSchemas, beforeUpgrade, afterUpgrade, version } = dbSchema, { newVersion = version, oldVersion } = e, { transaction } = request, context = new UpgradeContext({ database, newVersion, oldVersion, dbSchema, transaction });
|
|
1304
977
|
try {
|
|
1305
978
|
const errors = [];
|
|
1306
|
-
if (isFunction(beforeUpgrade)) try {
|
|
979
|
+
if (types.isFunction(beforeUpgrade)) try {
|
|
1307
980
|
await beforeUpgrade(context);
|
|
1308
981
|
} catch (e2) {
|
|
1309
982
|
errors.push(e2);
|
|
@@ -1333,7 +1006,7 @@ async function upgradeDb(dbSchema, database, e, request) {
|
|
|
1333
1006
|
} catch (e2) {
|
|
1334
1007
|
errors.push(e2);
|
|
1335
1008
|
}
|
|
1336
|
-
if (isFunction(afterUpgrade)) try {
|
|
1009
|
+
if (types.isFunction(afterUpgrade)) try {
|
|
1337
1010
|
await afterUpgrade(context);
|
|
1338
1011
|
} catch (e2) {
|
|
1339
1012
|
errors.push(e2);
|
|
@@ -1341,38 +1014,38 @@ async function upgradeDb(dbSchema, database, e, request) {
|
|
|
1341
1014
|
if (!errors.length) return;
|
|
1342
1015
|
throw errors.length === 1 ? errors[0] : new AggregateError(errors, "Database upgrade error");
|
|
1343
1016
|
} finally {
|
|
1344
|
-
destroy(context);
|
|
1017
|
+
basic.destroy(context);
|
|
1345
1018
|
}
|
|
1346
1019
|
}
|
|
1347
1020
|
function validateDataOperationSchema(schema, dbSchema) {
|
|
1348
1021
|
return Object.isFrozen(dbSchema) ? frozenValidate(schema) : initValidate(schema, dbSchema);
|
|
1349
1022
|
}
|
|
1350
|
-
function frozenValidate(schema
|
|
1351
|
-
let { store, index } = schema;
|
|
1352
|
-
const target = { store: isString(
|
|
1353
|
-
return index && (target.index = isString(index) ? index : index.name), {
|
|
1023
|
+
function frozenValidate(schema) {
|
|
1024
|
+
let { store: store2, index } = schema;
|
|
1025
|
+
const target = { store: types.isString(store2) ? store2 : store2.name };
|
|
1026
|
+
return index && (target.index = types.isString(index) ? index : index.name), {
|
|
1354
1027
|
target
|
|
1355
1028
|
};
|
|
1356
1029
|
}
|
|
1357
1030
|
function initValidate(schema, dbSchema) {
|
|
1358
|
-
let { store, index } = schema;
|
|
1359
|
-
const { storeTemplate } = dbSchema, { storeSchemas = [] } = dbSchema, storeName = isString(
|
|
1031
|
+
let { store: store2, index } = schema;
|
|
1032
|
+
const { storeTemplate } = dbSchema, { storeSchemas = [] } = dbSchema, storeName = types.isString(store2) ? store2 : store2.name, storeIndex = storeSchemas.findIndex((s) => s === storeName || s.name === storeName), existStore = storeIndex > -1 && storeSchemas[storeIndex];
|
|
1360
1033
|
let tmp;
|
|
1361
|
-
isString(
|
|
1034
|
+
types.isString(store2) ? tmp = existStore || storeName : !existStore || types.isString(existStore) || store2 === existStore ? tmp = store2 : tmp = { ...existStore, ...store2 }, (index || existStore?.indexSchemas?.length || store2?.indexSchemas?.length) && (types.isString(tmp) && (tmp = { name: tmp }), tmp.indexSchemas = mergeIndexes(existStore.indexSchemas || [], store2.indexSchemas || [], index));
|
|
1362
1035
|
const validSchema = validateStoreSchema(tmp, storeTemplate);
|
|
1363
1036
|
storeIndex > -1 ? storeSchemas[storeIndex] = validSchema : storeSchemas.push(validSchema), dbSchema.storeSchemas = storeSchemas;
|
|
1364
1037
|
const target = { store: storeName };
|
|
1365
|
-
return index && (target.index = isString(index) ? index : index.name), {
|
|
1038
|
+
return index && (target.index = types.isString(index) ? index : index.name), {
|
|
1366
1039
|
target
|
|
1367
1040
|
};
|
|
1368
1041
|
}
|
|
1369
1042
|
function mergeIndexes(existIndexes, newIndexes, index) {
|
|
1370
1043
|
index && newIndexes.push(index);
|
|
1371
1044
|
for (const index2 of newIndexes) {
|
|
1372
|
-
const indexName = isString(index2) ? index2 : index2.name, existIndex = existIndexes.findIndex((i) => i === indexName || i.name === indexName);
|
|
1045
|
+
const indexName = types.isString(index2) ? index2 : index2.name, existIndex = existIndexes.findIndex((i) => i === indexName || i.name === indexName);
|
|
1373
1046
|
if (existIndex > -1) {
|
|
1374
1047
|
const old = existIndexes[existIndex];
|
|
1375
|
-
isString(old) ? existIndexes[existIndex] = index2 : isString(index2) || (existIndexes[existIndex] = Object.assign(old, index2));
|
|
1048
|
+
types.isString(old) ? existIndexes[existIndex] = index2 : types.isString(index2) || (existIndexes[existIndex] = Object.assign(old, index2));
|
|
1376
1049
|
} else
|
|
1377
1050
|
existIndexes.push(index2);
|
|
1378
1051
|
}
|
|
@@ -1394,13 +1067,13 @@ class IDbPro {
|
|
|
1394
1067
|
#beforeUseValid;
|
|
1395
1068
|
#storeSchemaRecord = {};
|
|
1396
1069
|
constructor(schema, skipPreOpenValidation) {
|
|
1397
|
-
this.#schema = schema = isString(schema) ? { name: schema } : schema, Array.isArray(schema.storeSchemas) || (schema.storeSchemas = []), isObject(schema.storeTemplate) || (schema.storeTemplate = defaultStoreSchemaTemplate), skipPreOpenValidation && (this.#beforeUseValid = !0);
|
|
1070
|
+
this.#schema = schema = types.isString(schema) ? { name: schema } : schema, Array.isArray(schema.storeSchemas) || (schema.storeSchemas = []), types.isObject(schema.storeTemplate) || (schema.storeTemplate = type$1.defaultStoreSchemaTemplate), skipPreOpenValidation && (this.#beforeUseValid = !0);
|
|
1398
1071
|
}
|
|
1399
1072
|
/**
|
|
1400
1073
|
* 默认实例
|
|
1401
1074
|
*/
|
|
1402
1075
|
static get defaultDb() {
|
|
1403
|
-
return this.#_db || (this.#_db = new IDbPro(DefaultDbName));
|
|
1076
|
+
return this.#_db || (this.#_db = new IDbPro(storage.DefaultDbName));
|
|
1404
1077
|
}
|
|
1405
1078
|
get initialized() {
|
|
1406
1079
|
return Object.isFrozen(this.#schema);
|
|
@@ -1409,7 +1082,7 @@ class IDbPro {
|
|
|
1409
1082
|
return this.#schema;
|
|
1410
1083
|
}
|
|
1411
1084
|
get storeNames() {
|
|
1412
|
-
return Array.from(new Set(this.#schema.storeSchemas.map((s) => isString(s) ? s : s.name)));
|
|
1085
|
+
return Array.from(new Set(this.#schema.storeSchemas.map((s) => types.isString(s) ? s : s.name)));
|
|
1413
1086
|
}
|
|
1414
1087
|
get factory() {
|
|
1415
1088
|
return this.#schema.factory || (this.#schema.factory = DataOperatorFactory);
|
|
@@ -1442,13 +1115,13 @@ class IDbPro {
|
|
|
1442
1115
|
return IDbPro.defaultDb.map(storeName, defaultData);
|
|
1443
1116
|
}
|
|
1444
1117
|
async openNativeDb() {
|
|
1445
|
-
const schema = this.#schema = deepFreeze(this.initSchema());
|
|
1118
|
+
const schema = this.#schema = basic.deepFreeze(this.initSchema());
|
|
1446
1119
|
await this.#isBeforeUseValidate();
|
|
1447
1120
|
const { name, version } = schema;
|
|
1448
|
-
return await openDb({
|
|
1121
|
+
return await db.openDb({
|
|
1449
1122
|
name,
|
|
1450
1123
|
version,
|
|
1451
|
-
onupgradeneeded: (
|
|
1124
|
+
onupgradeneeded: (db2, e, request) => upgradeDb(schema, db2, e, request)
|
|
1452
1125
|
});
|
|
1453
1126
|
}
|
|
1454
1127
|
store(arg1, index) {
|
|
@@ -1456,7 +1129,7 @@ class IDbPro {
|
|
|
1456
1129
|
return operation.store || (operation.store = arg1), operation.index || (operation.index = index), this.factory.newWriter(validateDataOperationSchema(operation, this.schema), this);
|
|
1457
1130
|
}
|
|
1458
1131
|
stores(schemas) {
|
|
1459
|
-
const { schema } = this, results = schemas.map((
|
|
1132
|
+
const { schema } = this, results = schemas.map((store2) => validateDataOperationSchema(types.isString(store2) ? { store: store2 } : store2, schema));
|
|
1460
1133
|
return this.factory.newDataOperators(results, this);
|
|
1461
1134
|
}
|
|
1462
1135
|
initSchema() {
|
|
@@ -1465,10 +1138,10 @@ class IDbPro {
|
|
|
1465
1138
|
return this.#schema = validate(this.#schema);
|
|
1466
1139
|
}
|
|
1467
1140
|
async traceSchema(showFn) {
|
|
1468
|
-
await logJson(this.schema, showFn);
|
|
1141
|
+
await json.logJson(this.schema, showFn);
|
|
1469
1142
|
}
|
|
1470
1143
|
map(storeName, defaultData) {
|
|
1471
|
-
const name = isString(storeName) ? storeName : isString(defaultData) ? defaultData : DefaultStorageStoreName;
|
|
1144
|
+
const name = types.isString(storeName) ? storeName : types.isString(defaultData) ? defaultData : storage.DefaultStorageStoreName;
|
|
1472
1145
|
Array.isArray(storeName) && (defaultData = storeName);
|
|
1473
1146
|
const { storeSchemas } = this.schema;
|
|
1474
1147
|
return storeSchemas.find((s) => s.name === name || s === name) || storeSchemas.push({ ...mapStoreSchema, name, defaultData }), this.factory.newDbMap({ target: { store: name } }, this);
|
|
@@ -1484,13 +1157,13 @@ class IDbPro {
|
|
|
1484
1157
|
return this.#storeSchemaRecord[name];
|
|
1485
1158
|
const index = this.schema.storeSchemas.findIndex((s) => s === name || s.name === name);
|
|
1486
1159
|
let storeSchema = this.schema.storeSchemas[index];
|
|
1487
|
-
return this.initialized ? this.#storeSchemaRecord[name] = storeSchema : isString(storeSchema) && (this.schema.storeSchemas[index] = storeSchema = { name: storeSchema }), storeSchema;
|
|
1160
|
+
return this.initialized ? this.#storeSchemaRecord[name] = storeSchema : types.isString(storeSchema) && (this.schema.storeSchemas[index] = storeSchema = { name: storeSchema }), storeSchema;
|
|
1488
1161
|
}
|
|
1489
1162
|
async #isBeforeUseValidate() {
|
|
1490
1163
|
let valid = this.#beforeUseValid;
|
|
1491
1164
|
if (valid === void 0 && (valid = this.#beforeUseValid = await validateBeforeOpen(this.#schema)), valid === !0)
|
|
1492
1165
|
return !0;
|
|
1493
|
-
if (isString(valid))
|
|
1166
|
+
if (types.isString(valid))
|
|
1494
1167
|
throw new Error(valid);
|
|
1495
1168
|
}
|
|
1496
1169
|
}
|
|
@@ -1506,17 +1179,17 @@ function dbStores(schemas) {
|
|
|
1506
1179
|
function releaseDefaultDB() {
|
|
1507
1180
|
IDbPro.releaseDefaultDB();
|
|
1508
1181
|
}
|
|
1509
|
-
function generateStoreSchema(
|
|
1510
|
-
const { name, keyPath, autoIncrement } =
|
|
1182
|
+
function generateStoreSchema(store2, fields) {
|
|
1183
|
+
const { name, keyPath, autoIncrement } = store2;
|
|
1511
1184
|
return {
|
|
1512
1185
|
name,
|
|
1513
1186
|
keyPath,
|
|
1514
1187
|
autoIncrement,
|
|
1515
|
-
...parseIndexes(
|
|
1188
|
+
...parseIndexes(store2, fields)
|
|
1516
1189
|
};
|
|
1517
1190
|
}
|
|
1518
|
-
function parseIndexes(
|
|
1519
|
-
const indexes = Array.from(
|
|
1191
|
+
function parseIndexes(store2, fields) {
|
|
1192
|
+
const indexes = Array.from(store2.indexNames).map((n) => store2.index(n)), indexSchemas = [], paths = [];
|
|
1520
1193
|
for (const { name, keyPath, unique, multiEntry } of indexes)
|
|
1521
1194
|
Array.isArray(keyPath) ? paths.push(...keyPath) : paths.push(keyPath), indexSchemas.push({ name, keyPath, unique, multiEntry });
|
|
1522
1195
|
const keySet = new Set(paths);
|
|
@@ -1531,19 +1204,19 @@ function parseIndexes(store, fields) {
|
|
|
1531
1204
|
function convertSpecialField(field, keySet) {
|
|
1532
1205
|
return keySet.has(field) ? { name: field, isIndexed: !1 } : !1;
|
|
1533
1206
|
}
|
|
1534
|
-
async function generateDbSchema(db, option) {
|
|
1535
|
-
if (!await findExistDb(db))
|
|
1536
|
-
throw new Error(`db [ ${db} ] not exist`);
|
|
1537
|
-
let { asString, specialFields = defaultSpecialFields, dataExportTarget } = option || {};
|
|
1207
|
+
async function generateDbSchema(db$1, option) {
|
|
1208
|
+
if (!await db.findExistDb(db$1))
|
|
1209
|
+
throw new Error(`db [ ${db$1} ] not exist`);
|
|
1210
|
+
let { asString, specialFields = type$1.defaultSpecialFields, dataExportTarget } = option || {};
|
|
1538
1211
|
asString === !0 && (asString = 160), isNaN(asString) || asString < 1 && (asString = 1);
|
|
1539
|
-
let dbSchema = await generateRoot(db, specialFields);
|
|
1540
|
-
return dataExportTarget && await generateData(dbSchema, dataExportTarget), asString ? await toJson({
|
|
1212
|
+
let dbSchema = await generateRoot(db$1, specialFields);
|
|
1213
|
+
return dataExportTarget && await generateData(dbSchema, dataExportTarget), asString ? await json.toJson({
|
|
1541
1214
|
rootData$: dbSchema,
|
|
1542
1215
|
spaceEffectiveLength: asString
|
|
1543
1216
|
}) : dbSchema;
|
|
1544
1217
|
}
|
|
1545
1218
|
async function generateData(dbSchema, dataExportTarget) {
|
|
1546
|
-
const data = await new IDbPro(copyObject(dbSchema), !0).export();
|
|
1219
|
+
const data = await new IDbPro(basic.copyObject(dbSchema), !0).export();
|
|
1547
1220
|
for (const schema of dbSchema.storeSchemas) {
|
|
1548
1221
|
const rows = data[schema.name];
|
|
1549
1222
|
rows?.length && (dataExportTarget === "defaultData" ? schema.defaultData = rows : dataExportTarget === "versionData" && (schema.versionData || (schema.versionData = []), schema.versionData.push({
|
|
@@ -1553,53 +1226,22 @@ async function generateData(dbSchema, dataExportTarget) {
|
|
|
1553
1226
|
}
|
|
1554
1227
|
return dbSchema;
|
|
1555
1228
|
}
|
|
1556
|
-
function generateRoot(db, specialFields) {
|
|
1557
|
-
return openDb(db, async function(existDb) {
|
|
1558
|
-
const names = Array.from(existDb.objectStoreNames),
|
|
1229
|
+
function generateRoot(db$1, specialFields) {
|
|
1230
|
+
return db.openDb(db$1, async function(existDb) {
|
|
1231
|
+
const names = Array.from(existDb.objectStoreNames), tx2 = existDb.transaction(names, "readonly");
|
|
1559
1232
|
try {
|
|
1560
1233
|
return {
|
|
1561
1234
|
name: existDb.name,
|
|
1562
1235
|
version: existDb.version,
|
|
1563
|
-
storeSchemas: names.map((name) => generateStoreSchema(
|
|
1236
|
+
storeSchemas: names.map((name) => generateStoreSchema(tx2.objectStore(name), specialFields))
|
|
1564
1237
|
};
|
|
1565
1238
|
} finally {
|
|
1566
|
-
|
|
1239
|
+
tx2.abort();
|
|
1567
1240
|
}
|
|
1568
1241
|
});
|
|
1569
1242
|
}
|
|
1570
|
-
var generateDbSchema$1 = /* @__PURE__ */ Object.freeze({
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
ContinuePrimaryKey,
|
|
1576
|
-
DataOperationBase,
|
|
1577
|
-
DataOperators,
|
|
1578
|
-
DataReader,
|
|
1579
|
-
DataWriter,
|
|
1580
|
-
DbIterator,
|
|
1581
|
-
DbIteratorParsers,
|
|
1582
|
-
DbMap,
|
|
1583
|
-
Delete,
|
|
1584
|
-
Finished,
|
|
1585
|
-
IDbPro,
|
|
1586
|
-
NextKey,
|
|
1587
|
-
NextPrimaryKey,
|
|
1588
|
-
Save,
|
|
1589
|
-
StoreUpgradeable,
|
|
1590
|
-
UpgradeContext,
|
|
1591
|
-
dbMap,
|
|
1592
|
-
dbStore,
|
|
1593
|
-
dbStores,
|
|
1594
|
-
defaultSpecialFields,
|
|
1595
|
-
defaultStoreSchemaTemplate,
|
|
1596
|
-
generateDbSchema,
|
|
1597
|
-
isIDbQuery,
|
|
1598
|
-
isNativeTarget,
|
|
1599
|
-
parseDbNoneKeyPathRecord,
|
|
1600
|
-
parseIDbQuery,
|
|
1601
|
-
releaseDefaultDB,
|
|
1602
|
-
validateSchemaWithDefaults,
|
|
1603
|
-
versionDiffValidate,
|
|
1604
|
-
versionSameValidate
|
|
1605
|
-
};
|
|
1243
|
+
var generateDbSchema$1 = /* @__PURE__ */ Object.freeze({
|
|
1244
|
+
__proto__: null,
|
|
1245
|
+
generateDbSchema
|
|
1246
|
+
});
|
|
1247
|
+
exports.DataOperationBase = DataOperationBase, exports.DataOperators = DataOperators, exports.DataReader = DataReader, exports.DataWriter = DataWriter, exports.DbIterator = DbIterator, exports.DbMap = DbMap, exports.IDbPro = IDbPro, exports.StoreUpgradeable = StoreUpgradeable, exports.UpgradeContext = UpgradeContext, exports.dbMap = dbMap, exports.dbStore = dbStore, exports.dbStores = dbStores, exports.generateDbSchema = generateDbSchema, exports.isIDbQuery = isIDbQuery, exports.parseIDbQuery = parseIDbQuery, exports.releaseDefaultDB = releaseDefaultDB, exports.validateSchemaWithDefaults = validateSchemaWithDefaults, exports.versionDiffValidate = versionDiffValidate, exports.versionSameValidate = versionSameValidate;
|