defuss-db 1.1.4 → 2.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/README.md +202 -90
- package/dist/client.cjs +241 -222
- package/dist/client.d.ts +16 -77
- package/dist/client.mjs +231 -223
- package/dist/index.cjs +866 -30
- package/dist/index.d.cts +243 -56
- package/dist/index.d.mts +243 -56
- package/dist/index.d.ts +243 -56
- package/dist/index.mjs +856 -31
- package/dist/runtime-C-5MF9ur.mjs +242 -0
- package/dist/runtime-CJK5Yy0J.d.ts +17 -0
- package/dist/runtime-E8GdpV6o.cjs +251 -0
- package/dist/server.cjs +1064 -642
- package/dist/server.d.ts +77 -210
- package/dist/server.mjs +1055 -645
- package/package.json +12 -5
package/dist/client.cjs
CHANGED
|
@@ -1,24 +1,18 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var index = require('./index.cjs');
|
|
4
|
+
var runtime = require('./runtime-E8GdpV6o.cjs');
|
|
4
5
|
|
|
5
6
|
class DexieProvider {
|
|
6
7
|
db;
|
|
7
8
|
tables = /* @__PURE__ */ new Map();
|
|
9
|
+
definitions = /* @__PURE__ */ new Map();
|
|
8
10
|
databaseName;
|
|
9
|
-
schema = /* @__PURE__ */ new Map();
|
|
10
11
|
schemaVersion = 1;
|
|
11
12
|
isOpen = false;
|
|
12
13
|
constructor(databaseName) {
|
|
13
|
-
|
|
14
|
-
databaseName = "DefussDB";
|
|
15
|
-
}
|
|
16
|
-
this.databaseName = databaseName;
|
|
14
|
+
this.databaseName = databaseName || "DefussDB";
|
|
17
15
|
}
|
|
18
|
-
/**
|
|
19
|
-
* Connects to IndexedDB. Dexie.js manages connections internally.
|
|
20
|
-
* @param options - Dexie-specific configuration options.
|
|
21
|
-
*/
|
|
22
16
|
async connect(options = {}) {
|
|
23
17
|
if (this.db && this.isConnected()) {
|
|
24
18
|
return;
|
|
@@ -31,261 +25,286 @@ class DexieProvider {
|
|
|
31
25
|
this.db = new DexieImpl(this.databaseName, options);
|
|
32
26
|
this.isOpen = true;
|
|
33
27
|
}
|
|
34
|
-
/**
|
|
35
|
-
* Disconnects from IndexedDB.
|
|
36
|
-
*/
|
|
37
28
|
async disconnect() {
|
|
38
29
|
if (this.db) {
|
|
39
30
|
this.db.close();
|
|
40
31
|
this.isOpen = false;
|
|
32
|
+
this.tables.clear();
|
|
41
33
|
}
|
|
42
34
|
}
|
|
43
|
-
/**
|
|
44
|
-
* Checks if the provider is currently connected to the database.
|
|
45
|
-
* @returns True if connected, false otherwise.
|
|
46
|
-
*/
|
|
47
35
|
isConnected() {
|
|
48
36
|
return this.isOpen;
|
|
49
37
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
this.schema.set(table, { pk: "++" });
|
|
38
|
+
buildSchemaString(definition) {
|
|
39
|
+
const parts = ["id"];
|
|
40
|
+
for (const index of definition.indexes) {
|
|
41
|
+
if (index.selectorKey === "id") {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
parts.push(index.unique ? `&${index.storageKey}` : index.storageKey);
|
|
58
45
|
}
|
|
59
|
-
|
|
46
|
+
return parts.join(", ");
|
|
47
|
+
}
|
|
48
|
+
async applySchema() {
|
|
60
49
|
if (this.isOpen) {
|
|
61
50
|
this.db.close();
|
|
62
51
|
}
|
|
63
|
-
const
|
|
64
|
-
for (const
|
|
65
|
-
|
|
66
|
-
if (column === "pk") return "++pk";
|
|
67
|
-
return indexType ? `${indexType}${column}` : column;
|
|
68
|
-
}).join(", ");
|
|
69
|
-
tableSchema[tableName] = schemaString;
|
|
52
|
+
const schema = {};
|
|
53
|
+
for (const definition of this.definitions.values()) {
|
|
54
|
+
schema[definition.name] = this.buildSchemaString(definition);
|
|
70
55
|
}
|
|
71
|
-
this.
|
|
56
|
+
this.schemaVersion += 1;
|
|
57
|
+
this.db.version(this.schemaVersion).stores(schema);
|
|
72
58
|
await this.db.open();
|
|
73
59
|
this.isOpen = true;
|
|
74
|
-
|
|
75
|
-
this.
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Adds columns to the schema if they don't exist, with proper indexing.
|
|
79
|
-
* @param table - The name of the table.
|
|
80
|
-
* @param record - The record with properties to consider adding.
|
|
81
|
-
*/
|
|
82
|
-
async ensureColumnsForRecord(table, record) {
|
|
83
|
-
const tableSchema = this.schema.get(table) || { pk: "++" };
|
|
84
|
-
let schemaUpdated = false;
|
|
85
|
-
for (const [prop, value] of Object.entries(record)) {
|
|
86
|
-
if (prop === "pk") continue;
|
|
87
|
-
if (prop in tableSchema) continue;
|
|
88
|
-
if (prop.endsWith("_index")) {
|
|
89
|
-
const baseField = prop.slice(0, -6);
|
|
90
|
-
if (!(baseField in tableSchema)) {
|
|
91
|
-
if (Array.isArray(value)) {
|
|
92
|
-
tableSchema[prop] = "*";
|
|
93
|
-
} else {
|
|
94
|
-
tableSchema[prop] = "";
|
|
95
|
-
}
|
|
96
|
-
schemaUpdated = true;
|
|
97
|
-
}
|
|
98
|
-
} else if (!(prop in tableSchema)) {
|
|
99
|
-
tableSchema[prop] = "";
|
|
100
|
-
schemaUpdated = true;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
if (schemaUpdated) {
|
|
104
|
-
this.schema.set(table, tableSchema);
|
|
105
|
-
await this.createTable(table);
|
|
60
|
+
this.tables.clear();
|
|
61
|
+
for (const definition of this.definitions.values()) {
|
|
62
|
+
this.tables.set(definition.name, this.db.table(definition.name));
|
|
106
63
|
}
|
|
107
64
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
*/
|
|
114
|
-
async insert(table, record) {
|
|
115
|
-
await this.createTable(table);
|
|
116
|
-
const insertData = {};
|
|
117
|
-
for (const [key, value] of Object.entries(record)) {
|
|
118
|
-
if (key === "pk") {
|
|
119
|
-
if (value !== void 0) {
|
|
120
|
-
insertData.pk = value;
|
|
121
|
-
}
|
|
122
|
-
continue;
|
|
123
|
-
}
|
|
124
|
-
insertData[key] = value;
|
|
65
|
+
async ensureTable(definition) {
|
|
66
|
+
const normalized = runtime.normalizeTableDefinition(definition);
|
|
67
|
+
const existing = this.definitions.get(normalized.name);
|
|
68
|
+
if (existing) {
|
|
69
|
+
return;
|
|
125
70
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
return String(id);
|
|
71
|
+
this.definitions.set(normalized.name, normalized);
|
|
72
|
+
await this.applySchema();
|
|
73
|
+
}
|
|
74
|
+
getTable(definition) {
|
|
75
|
+
const normalized = runtime.normalizeTableDefinition(definition);
|
|
76
|
+
const tableDexie = this.tables.get(normalized.name);
|
|
77
|
+
if (!tableDexie) {
|
|
78
|
+
throw new Error(`defuss-db: table '${normalized.name}' is not initialized.`);
|
|
135
79
|
}
|
|
80
|
+
return { normalized, tableDexie };
|
|
136
81
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
async find(table, query) {
|
|
144
|
-
await this.createTable(table);
|
|
145
|
-
const tableDexie = this.tables.get(table);
|
|
146
|
-
await this.ensureColumnsForRecord(table, query);
|
|
147
|
-
if (Object.keys(query).length === 0) {
|
|
82
|
+
async queryRecords(definition, selector) {
|
|
83
|
+
const { normalized, tableDexie } = this.getTable(definition);
|
|
84
|
+
const selectorEntries = Object.entries(selector).filter(
|
|
85
|
+
(entry) => entry[1] !== void 0
|
|
86
|
+
);
|
|
87
|
+
if (selectorEntries.length === 0) {
|
|
148
88
|
const results2 = await tableDexie.toArray();
|
|
149
|
-
return results2.map((result) =>
|
|
89
|
+
return results2.map((result) => fromDexieRecord(result));
|
|
150
90
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
91
|
+
const indexedEntry = selectorEntries.find(([key, value]) => {
|
|
92
|
+
return runtime.resolveIndexedSelectorValue(normalized, key, value) !== null;
|
|
93
|
+
});
|
|
94
|
+
let results;
|
|
95
|
+
if (indexedEntry) {
|
|
96
|
+
const indexedSelector = runtime.resolveIndexedSelectorValue(
|
|
97
|
+
normalized,
|
|
98
|
+
indexedEntry[0],
|
|
99
|
+
normalizeDexieIndexableValue(indexedEntry[1])
|
|
100
|
+
);
|
|
101
|
+
if (indexedSelector.storageKey === "id") {
|
|
102
|
+
const result = await tableDexie.get(indexedSelector.value);
|
|
103
|
+
results = result ? [result] : [];
|
|
104
|
+
} else {
|
|
105
|
+
results = await tableDexie.where(indexedSelector.storageKey).equals(indexedSelector.value).toArray();
|
|
158
106
|
}
|
|
159
107
|
} else {
|
|
160
|
-
|
|
161
|
-
if (key.endsWith("_index")) {
|
|
162
|
-
collection = collection.and((item) => item[key] === value);
|
|
163
|
-
} else {
|
|
164
|
-
collection = collection.and((item) => item[key] === value);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
108
|
+
results = await tableDexie.toArray();
|
|
167
109
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
async findOne(table, query) {
|
|
178
|
-
if (Object.keys(query).length === 1 && "pk" in query) {
|
|
179
|
-
await this.createTable(table);
|
|
180
|
-
const tableDexie = this.tables.get(table);
|
|
181
|
-
try {
|
|
182
|
-
const result = await tableDexie.get(query.pk);
|
|
183
|
-
return result ? this.normalizeRecord(result) : null;
|
|
184
|
-
} catch (err) {
|
|
185
|
-
}
|
|
110
|
+
return results.map((result) => fromDexieRecord(result)).filter((result) => runtime.matchesSelector(result, selector, normalized));
|
|
111
|
+
}
|
|
112
|
+
async insert(definition, value) {
|
|
113
|
+
await this.ensureTable(definition);
|
|
114
|
+
const { normalized, tableDexie } = this.getTable(definition);
|
|
115
|
+
const storedRecord = await toDexieStoredRecord(normalized, prepareDexiePrimaryKey(value));
|
|
116
|
+
const insertedId = await tableDexie.add(storedRecord);
|
|
117
|
+
if (typeof insertedId === "bigint" || typeof insertedId === "number") {
|
|
118
|
+
return insertedId;
|
|
186
119
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* Updates records in the table based on query.
|
|
192
|
-
* @param table - The name of the table.
|
|
193
|
-
* @param query - Query criteria.
|
|
194
|
-
* @param update - Fields to update.
|
|
195
|
-
*/
|
|
196
|
-
async update(table, query, update) {
|
|
197
|
-
await this.createTable(table);
|
|
198
|
-
const tableDexie = this.tables.get(table);
|
|
199
|
-
await this.ensureColumnsForRecord(table, query);
|
|
200
|
-
await this.ensureColumnsForRecord(table, update);
|
|
201
|
-
const updateData = {};
|
|
202
|
-
for (const [key, value] of Object.entries(update)) {
|
|
203
|
-
if (key === "pk") continue;
|
|
204
|
-
updateData[key] = value;
|
|
120
|
+
if (typeof insertedId === "string") {
|
|
121
|
+
return insertedId;
|
|
205
122
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
123
|
+
return String(insertedId);
|
|
124
|
+
}
|
|
125
|
+
async find(definition, selector = {}) {
|
|
126
|
+
await this.ensureTable(definition);
|
|
127
|
+
return this.queryRecords(definition, selector);
|
|
128
|
+
}
|
|
129
|
+
async findOne(definition, selector = {}) {
|
|
130
|
+
const results = await this.find(definition, selector);
|
|
131
|
+
return results[0] ?? null;
|
|
132
|
+
}
|
|
133
|
+
async update(definition, selector, update) {
|
|
134
|
+
await this.ensureTable(definition);
|
|
135
|
+
const { normalized, tableDexie } = this.getTable(definition);
|
|
136
|
+
if (Object.keys(selector).length === 0) {
|
|
137
|
+
throw new Error("defuss-db: update requires a non-empty selector.");
|
|
209
138
|
}
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
|
|
139
|
+
const records = await this.queryRecords(definition, selector);
|
|
140
|
+
for (const record of records) {
|
|
141
|
+
if (update.id !== void 0 && update.id !== record.id) {
|
|
142
|
+
throw new Error("defuss-db: id cannot be updated.");
|
|
213
143
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
144
|
+
const nextRecord = {
|
|
145
|
+
...record,
|
|
146
|
+
...update,
|
|
147
|
+
id: record.id
|
|
148
|
+
};
|
|
149
|
+
const storedRecord = await toDexieStoredRecord(normalized, nextRecord);
|
|
150
|
+
await tableDexie.put(storedRecord);
|
|
219
151
|
}
|
|
220
152
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
async delete(table, query) {
|
|
227
|
-
await this.createTable(table);
|
|
228
|
-
const tableDexie = this.tables.get(table);
|
|
229
|
-
await this.ensureColumnsForRecord(table, query);
|
|
230
|
-
if (Object.keys(query).length === 1 && "pk" in query) {
|
|
231
|
-
await tableDexie.delete(query.pk);
|
|
232
|
-
return;
|
|
153
|
+
async delete(definition, selector) {
|
|
154
|
+
await this.ensureTable(definition);
|
|
155
|
+
const { tableDexie } = this.getTable(definition);
|
|
156
|
+
if (Object.keys(selector).length === 0) {
|
|
157
|
+
throw new Error("defuss-db: delete requires a non-empty selector.");
|
|
233
158
|
}
|
|
234
|
-
const
|
|
235
|
-
|
|
236
|
-
|
|
159
|
+
const records = await this.queryRecords(definition, selector);
|
|
160
|
+
const ids = records.map((record) => record.id).filter((id) => id !== void 0);
|
|
161
|
+
await tableDexie.bulkDelete(ids);
|
|
162
|
+
}
|
|
163
|
+
async upsert(definition, selector, value) {
|
|
164
|
+
await this.ensureTable(definition);
|
|
165
|
+
const normalized = runtime.normalizeTableDefinition(definition);
|
|
166
|
+
runtime.resolveUniqueSelector(normalized, selector);
|
|
167
|
+
const mergedValue = runtime.mergeSelectorIntoValue(normalized, selector, value);
|
|
168
|
+
const existing = await this.findOne(definition, selector);
|
|
169
|
+
if (existing?.id !== void 0) {
|
|
170
|
+
await this.update(definition, { id: existing.id }, mergedValue);
|
|
171
|
+
return existing.id;
|
|
172
|
+
}
|
|
173
|
+
try {
|
|
174
|
+
return await this.insert(definition, mergedValue);
|
|
175
|
+
} catch (error) {
|
|
176
|
+
const constraintName = error && typeof error === "object" && "name" in error ? String(error.name) : "";
|
|
177
|
+
if (constraintName !== "ConstraintError") {
|
|
178
|
+
throw error;
|
|
237
179
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
await tableDexie.bulkDelete(keys);
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Upserts a record into the table based on query.
|
|
245
|
-
* @param table - The name of the table.
|
|
246
|
-
* @param record - The record to upsert.
|
|
247
|
-
* @param query - Query criteria.
|
|
248
|
-
* @returns The primary key of the upserted record.
|
|
249
|
-
*/
|
|
250
|
-
async upsert(table, record, query) {
|
|
251
|
-
await this.createTable(table);
|
|
252
|
-
const existingRecord = await this.findOne(table, query);
|
|
253
|
-
if (existingRecord && existingRecord.pk !== void 0) {
|
|
254
|
-
const mergedRecord = { ...record };
|
|
255
|
-
await this.update(
|
|
256
|
-
table,
|
|
257
|
-
{ pk: existingRecord.pk },
|
|
258
|
-
mergedRecord
|
|
259
|
-
);
|
|
260
|
-
return existingRecord.pk;
|
|
261
|
-
} else {
|
|
262
|
-
const mergedRecord = { ...record };
|
|
263
|
-
for (const [key, value] of Object.entries(query)) {
|
|
264
|
-
if (!(key in mergedRecord)) {
|
|
265
|
-
mergedRecord[key] = value;
|
|
266
|
-
}
|
|
180
|
+
const retried = await this.findOne(definition, selector);
|
|
181
|
+
if (!retried?.id) {
|
|
182
|
+
throw error;
|
|
267
183
|
}
|
|
268
|
-
|
|
184
|
+
await this.update(definition, { id: retried.id }, mergedValue);
|
|
185
|
+
return retried.id;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function bytesToBase64(bytes) {
|
|
190
|
+
if (typeof Buffer !== "undefined") {
|
|
191
|
+
return Buffer.from(bytes).toString("base64");
|
|
192
|
+
}
|
|
193
|
+
let binary = "";
|
|
194
|
+
for (const byte of bytes) {
|
|
195
|
+
binary += String.fromCharCode(byte);
|
|
196
|
+
}
|
|
197
|
+
return btoa(binary);
|
|
198
|
+
}
|
|
199
|
+
function base64ToArrayBuffer(base64) {
|
|
200
|
+
if (typeof Buffer !== "undefined") {
|
|
201
|
+
const bytes = Buffer.from(base64, "base64");
|
|
202
|
+
const result2 = new Uint8Array(new ArrayBuffer(bytes.byteLength));
|
|
203
|
+
result2.set(bytes);
|
|
204
|
+
return result2.buffer;
|
|
205
|
+
}
|
|
206
|
+
const binary = atob(base64);
|
|
207
|
+
const result = new Uint8Array(new ArrayBuffer(binary.length));
|
|
208
|
+
for (let index = 0; index < binary.length; index += 1) {
|
|
209
|
+
result[index] = binary.charCodeAt(index);
|
|
210
|
+
}
|
|
211
|
+
return result.buffer;
|
|
212
|
+
}
|
|
213
|
+
function isBlobLike(value) {
|
|
214
|
+
return typeof value === "object" && value !== null && typeof value.arrayBuffer === "function" && typeof value.slice === "function" && typeof value.type === "string";
|
|
215
|
+
}
|
|
216
|
+
function isEncodedDexieValue(value) {
|
|
217
|
+
return value.__defussType === "blob";
|
|
218
|
+
}
|
|
219
|
+
async function encodeDexieValue(value) {
|
|
220
|
+
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
|
|
221
|
+
return value;
|
|
222
|
+
}
|
|
223
|
+
if (value instanceof Date || value instanceof ArrayBuffer) {
|
|
224
|
+
return value;
|
|
225
|
+
}
|
|
226
|
+
if (isBlobLike(value)) {
|
|
227
|
+
return {
|
|
228
|
+
__defussType: "blob",
|
|
229
|
+
value: bytesToBase64(new Uint8Array(await value.arrayBuffer())),
|
|
230
|
+
mimeType: value.type
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
if (Array.isArray(value)) {
|
|
234
|
+
return Promise.all(value.map((item) => encodeDexieValue(item)));
|
|
235
|
+
}
|
|
236
|
+
if (typeof value === "object") {
|
|
237
|
+
const result = {};
|
|
238
|
+
for (const [key, nestedValue] of Object.entries(value)) {
|
|
239
|
+
result[key] = await encodeDexieValue(nestedValue);
|
|
269
240
|
}
|
|
241
|
+
return result;
|
|
242
|
+
}
|
|
243
|
+
return value;
|
|
244
|
+
}
|
|
245
|
+
function decodeDexieValue(value) {
|
|
246
|
+
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint" || value instanceof Date || value instanceof ArrayBuffer) {
|
|
247
|
+
return value;
|
|
248
|
+
}
|
|
249
|
+
if (Array.isArray(value)) {
|
|
250
|
+
return value.map((item) => decodeDexieValue(item));
|
|
251
|
+
}
|
|
252
|
+
if (typeof value === "object" && value !== null && isEncodedDexieValue(value)) {
|
|
253
|
+
return new Blob([
|
|
254
|
+
base64ToArrayBuffer(String(value.value ?? ""))
|
|
255
|
+
], {
|
|
256
|
+
type: String(value.mimeType ?? "")
|
|
257
|
+
});
|
|
270
258
|
}
|
|
271
|
-
|
|
272
|
-
* Normalizes a database record to match DefussRecord structure.
|
|
273
|
-
* @param record - The record from the database.
|
|
274
|
-
* @returns A normalized DefussRecord.
|
|
275
|
-
*/
|
|
276
|
-
normalizeRecord(record) {
|
|
259
|
+
if (typeof value === "object" && value !== null) {
|
|
277
260
|
const result = {};
|
|
278
|
-
for (const [key,
|
|
279
|
-
|
|
280
|
-
result.pk = value;
|
|
281
|
-
continue;
|
|
282
|
-
}
|
|
283
|
-
if (key.startsWith("_")) continue;
|
|
284
|
-
result[key] = value;
|
|
261
|
+
for (const [key, nestedValue] of Object.entries(value)) {
|
|
262
|
+
result[key] = decodeDexieValue(nestedValue);
|
|
285
263
|
}
|
|
286
264
|
return result;
|
|
287
265
|
}
|
|
266
|
+
return value;
|
|
267
|
+
}
|
|
268
|
+
async function toDexieStoredRecord(definition, value) {
|
|
269
|
+
return encodeDexieValue(runtime.toStoredRecord(definition, value));
|
|
270
|
+
}
|
|
271
|
+
function fromDexieRecord(storedRecord) {
|
|
272
|
+
return runtime.fromStoredRecord(decodeDexieValue(storedRecord));
|
|
273
|
+
}
|
|
274
|
+
function prepareDexiePrimaryKey(value) {
|
|
275
|
+
const nextValue = { ...value };
|
|
276
|
+
if (nextValue.id === void 0) {
|
|
277
|
+
nextValue.id = createDexiePrimaryKey();
|
|
278
|
+
return nextValue;
|
|
279
|
+
}
|
|
280
|
+
if (typeof nextValue.id === "bigint") {
|
|
281
|
+
nextValue.id = String(nextValue.id);
|
|
282
|
+
}
|
|
283
|
+
return nextValue;
|
|
284
|
+
}
|
|
285
|
+
function normalizeDexieIndexableValue(value) {
|
|
286
|
+
if (typeof value === "bigint") {
|
|
287
|
+
return String(value);
|
|
288
|
+
}
|
|
289
|
+
return value;
|
|
290
|
+
}
|
|
291
|
+
function createDexiePrimaryKey() {
|
|
292
|
+
if (typeof globalThis.crypto?.randomUUID === "function") {
|
|
293
|
+
return globalThis.crypto.randomUUID();
|
|
294
|
+
}
|
|
295
|
+
return `id_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
|
|
288
296
|
}
|
|
289
297
|
|
|
298
|
+
exports.AggregationBuilder = index.AggregationBuilder;
|
|
290
299
|
exports.DefussTable = index.DefussTable;
|
|
300
|
+
exports.aggregationReducers = index.aggregationReducers;
|
|
301
|
+
exports.avgBy = index.avgBy;
|
|
302
|
+
exports.countRows = index.countRows;
|
|
303
|
+
exports.createAggregation = index.createAggregation;
|
|
304
|
+
exports.defineTable = index.defineTable;
|
|
305
|
+
exports.firstBy = index.firstBy;
|
|
306
|
+
exports.lastBy = index.lastBy;
|
|
307
|
+
exports.maxBy = index.maxBy;
|
|
308
|
+
exports.minBy = index.minBy;
|
|
309
|
+
exports.sumBy = index.sumBy;
|
|
291
310
|
exports.DexieProvider = DexieProvider;
|
package/dist/client.d.ts
CHANGED
|
@@ -1,93 +1,32 @@
|
|
|
1
|
-
import { DefussProvider, DefussRecord, PrimaryKeyValue,
|
|
2
|
-
export { DefussTable } from './index.js';
|
|
1
|
+
import { DefussProvider, DefussRecord, DefussTableDefinition, PrimaryKeyValue, DefussSelector } from './index.js';
|
|
2
|
+
export { AggregationBuilder, AggregationCreateOptions, AggregationDistinctOptions, AggregationDistinctSelector, AggregationFieldSortSpec, AggregationGroupContext, AggregationGroupKeys, AggregationGroupReducer, AggregationJoinResult, AggregationJoinSpec, AggregationJoinType, AggregationMergeContext, AggregationMergeRowsFn, AggregationMergeRowsResult, AggregationNamespacedRow, AggregationProjectionMap, AggregationRemoveFieldsInput, AggregationResolverSortSpec, AggregationRowContext, AggregationRowMapper, AggregationRowsLoader, AggregationRowsSource, AggregationSortComparator, AggregationSortContext, AggregationSortDirection, AggregationSortSpec, AggregationSource, AggregationTableLike, AggregationTableSource, AggregationValueResolver, DefussIndexDefinition, DefussTable, DefussValue, RecordValue, aggregationReducers, avgBy, countRows, createAggregation, defineTable, firstBy, lastBy, maxBy, minBy, sumBy } from './index.js';
|
|
3
3
|
import { DexieOptions, Dexie, Table, IndexableType } from 'dexie';
|
|
4
|
+
import { N as NormalizedDefussTableDefinition } from './runtime-CJK5Yy0J.js';
|
|
4
5
|
|
|
5
6
|
type DexieProviderOptions = DexieOptions;
|
|
6
|
-
/**
|
|
7
|
-
* DexieProvider implements DefussProvider for IndexedDB using Dexie.js.
|
|
8
|
-
*/
|
|
9
7
|
declare class DexieProvider implements DefussProvider<DexieOptions> {
|
|
10
8
|
db: Dexie;
|
|
11
9
|
tables: Map<string, Table<any, IndexableType>>;
|
|
10
|
+
definitions: Map<string, NormalizedDefussTableDefinition<any>>;
|
|
12
11
|
databaseName: string;
|
|
13
|
-
schema: Map<string, Record<string, string>>;
|
|
14
12
|
schemaVersion: number;
|
|
15
13
|
isOpen: boolean;
|
|
16
14
|
constructor(databaseName: string);
|
|
17
|
-
/**
|
|
18
|
-
* Connects to IndexedDB. Dexie.js manages connections internally.
|
|
19
|
-
* @param options - Dexie-specific configuration options.
|
|
20
|
-
*/
|
|
21
15
|
connect(options?: DexieOptions): Promise<void>;
|
|
22
|
-
/**
|
|
23
|
-
* Disconnects from IndexedDB.
|
|
24
|
-
*/
|
|
25
16
|
disconnect(): Promise<void>;
|
|
26
|
-
/**
|
|
27
|
-
* Checks if the provider is currently connected to the database.
|
|
28
|
-
* @returns True if connected, false otherwise.
|
|
29
|
-
*/
|
|
30
17
|
isConnected(): boolean;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Inserts a record into a table.
|
|
44
|
-
* @param table - The name of the table.
|
|
45
|
-
* @param record - The record to insert.
|
|
46
|
-
* @returns The primary key of the inserted record.
|
|
47
|
-
*/
|
|
48
|
-
insert<T extends DefussRecord>(table: string, record: T): Promise<PrimaryKeyValue>;
|
|
49
|
-
/**
|
|
50
|
-
* Finds records in the table based on query.
|
|
51
|
-
* @param table - The name of the table.
|
|
52
|
-
* @param query - Query criteria.
|
|
53
|
-
* @returns An array of matching records.
|
|
54
|
-
*/
|
|
55
|
-
find<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>): Promise<T[]>;
|
|
56
|
-
/**
|
|
57
|
-
* Finds a single record in the table based on query.
|
|
58
|
-
* @param table - The name of the table.
|
|
59
|
-
* @param query - Query criteria.
|
|
60
|
-
* @returns The first matching record or null.
|
|
61
|
-
*/
|
|
62
|
-
findOne<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>): Promise<T | null>;
|
|
63
|
-
/**
|
|
64
|
-
* Updates records in the table based on query.
|
|
65
|
-
* @param table - The name of the table.
|
|
66
|
-
* @param query - Query criteria.
|
|
67
|
-
* @param update - Fields to update.
|
|
68
|
-
*/
|
|
69
|
-
update<T extends DefussRecord>(table: string, query: Partial<Record<string, RecordValue>>, update: Partial<T>): Promise<void>;
|
|
70
|
-
/**
|
|
71
|
-
* Deletes records from the table based on query.
|
|
72
|
-
* @param table - The name of the table.
|
|
73
|
-
* @param query - Query criteria.
|
|
74
|
-
*/
|
|
75
|
-
delete(table: string, query: Partial<DefussRecord>): Promise<void>;
|
|
76
|
-
/**
|
|
77
|
-
* Upserts a record into the table based on query.
|
|
78
|
-
* @param table - The name of the table.
|
|
79
|
-
* @param record - The record to upsert.
|
|
80
|
-
* @param query - Query criteria.
|
|
81
|
-
* @returns The primary key of the upserted record.
|
|
82
|
-
*/
|
|
83
|
-
upsert<T extends DefussRecord>(table: string, record: T, query: Partial<Record<string, RecordValue>>): Promise<PrimaryKeyValue>;
|
|
84
|
-
/**
|
|
85
|
-
* Normalizes a database record to match DefussRecord structure.
|
|
86
|
-
* @param record - The record from the database.
|
|
87
|
-
* @returns A normalized DefussRecord.
|
|
88
|
-
*/
|
|
89
|
-
private normalizeRecord;
|
|
18
|
+
private buildSchemaString;
|
|
19
|
+
private applySchema;
|
|
20
|
+
ensureTable<T extends DefussRecord>(definition: DefussTableDefinition<T>): Promise<void>;
|
|
21
|
+
private getTable;
|
|
22
|
+
private queryRecords;
|
|
23
|
+
insert<T extends DefussRecord>(definition: DefussTableDefinition<T>, value: T): Promise<PrimaryKeyValue>;
|
|
24
|
+
find<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector?: DefussSelector): Promise<T[]>;
|
|
25
|
+
findOne<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector?: DefussSelector): Promise<T | null>;
|
|
26
|
+
update<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector: DefussSelector, update: Partial<T>): Promise<void>;
|
|
27
|
+
delete<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector: DefussSelector): Promise<void>;
|
|
28
|
+
upsert<T extends DefussRecord>(definition: DefussTableDefinition<T>, selector: DefussSelector, value: T): Promise<PrimaryKeyValue>;
|
|
90
29
|
}
|
|
91
30
|
|
|
92
|
-
export { DefussProvider, DefussRecord, DexieProvider, PrimaryKeyValue
|
|
31
|
+
export { DefussProvider, DefussRecord, DefussSelector, DefussTableDefinition, DexieProvider, PrimaryKeyValue };
|
|
93
32
|
export type { DexieProviderOptions };
|