@farming-labs/orm-unstorage 0.0.37
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/dist/index.cjs +902 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +24 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +885 -0
- package/dist/index.js.map +1 -0
- package/package.json +33 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,902 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createUnstorageDriver: () => createUnstorageDriver
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var import_node_crypto = require("crypto");
|
|
27
|
+
var import_orm = require("@farming-labs/orm");
|
|
28
|
+
var import_unstorage = require("unstorage");
|
|
29
|
+
var ormKindKey = "__orm_kind";
|
|
30
|
+
var ormTargetKey = "__orm_target";
|
|
31
|
+
var ormRecordKind = "record";
|
|
32
|
+
var ormUniqueKind = "unique";
|
|
33
|
+
var defaultBase = "orm";
|
|
34
|
+
var manifestCache = /* @__PURE__ */ new WeakMap();
|
|
35
|
+
function getManifest(schema) {
|
|
36
|
+
const cached = manifestCache.get(schema);
|
|
37
|
+
if (cached) return cached;
|
|
38
|
+
const next = (0, import_orm.createManifest)(schema);
|
|
39
|
+
manifestCache.set(schema, next);
|
|
40
|
+
return next;
|
|
41
|
+
}
|
|
42
|
+
function isRecord(value) {
|
|
43
|
+
return !!value && typeof value === "object";
|
|
44
|
+
}
|
|
45
|
+
function isUnstorageRecordItem(value) {
|
|
46
|
+
return isRecord(value) && value[ormKindKey] === ormRecordKind && isRecord(value.data);
|
|
47
|
+
}
|
|
48
|
+
function isUnstorageUniqueItem(value) {
|
|
49
|
+
return isRecord(value) && value[ormKindKey] === ormUniqueKind && typeof value[ormTargetKey] === "string";
|
|
50
|
+
}
|
|
51
|
+
function normalizeDecimalString(value) {
|
|
52
|
+
const trimmed = value.trim();
|
|
53
|
+
const match = /^(-?\d+)(?:\.(\d+))?$/.exec(trimmed);
|
|
54
|
+
if (!match) {
|
|
55
|
+
return trimmed;
|
|
56
|
+
}
|
|
57
|
+
const [, integerPart, fractionalPart] = match;
|
|
58
|
+
if (!fractionalPart) {
|
|
59
|
+
return integerPart;
|
|
60
|
+
}
|
|
61
|
+
const normalizedFraction = fractionalPart.replace(/0+$/g, "");
|
|
62
|
+
return normalizedFraction.length ? `${integerPart}.${normalizedFraction}` : integerPart;
|
|
63
|
+
}
|
|
64
|
+
function applyDefault(value, field) {
|
|
65
|
+
if (value !== void 0) return value;
|
|
66
|
+
if (field.generated === "id") return (0, import_node_crypto.randomUUID)();
|
|
67
|
+
if (field.generated === "now") return /* @__PURE__ */ new Date();
|
|
68
|
+
if (typeof field.defaultValue === "function") {
|
|
69
|
+
return field.defaultValue();
|
|
70
|
+
}
|
|
71
|
+
return field.defaultValue;
|
|
72
|
+
}
|
|
73
|
+
function isComparable(value) {
|
|
74
|
+
return value instanceof Date || typeof value === "number" || typeof value === "string" || typeof value === "bigint";
|
|
75
|
+
}
|
|
76
|
+
function evaluateFilter(value, filter) {
|
|
77
|
+
if (!(0, import_orm.isOperatorFilterObject)(filter)) {
|
|
78
|
+
return (0, import_orm.equalValues)(value, filter);
|
|
79
|
+
}
|
|
80
|
+
if ("eq" in filter && !(0, import_orm.equalValues)(value, filter.eq)) return false;
|
|
81
|
+
if ("not" in filter && (0, import_orm.equalValues)(value, filter.not)) return false;
|
|
82
|
+
if ("in" in filter) {
|
|
83
|
+
const values = Array.isArray(filter.in) ? filter.in : [];
|
|
84
|
+
if (!values.some((candidate) => (0, import_orm.equalValues)(candidate, value))) return false;
|
|
85
|
+
}
|
|
86
|
+
if ("contains" in filter) {
|
|
87
|
+
if (typeof value !== "string" || typeof filter.contains !== "string") return false;
|
|
88
|
+
if (!value.includes(filter.contains)) return false;
|
|
89
|
+
}
|
|
90
|
+
if ("gt" in filter) {
|
|
91
|
+
if (!isComparable(value) || value <= filter.gt) return false;
|
|
92
|
+
}
|
|
93
|
+
if ("gte" in filter) {
|
|
94
|
+
if (!isComparable(value) || value < filter.gte) return false;
|
|
95
|
+
}
|
|
96
|
+
if ("lt" in filter) {
|
|
97
|
+
if (!isComparable(value) || value >= filter.lt) return false;
|
|
98
|
+
}
|
|
99
|
+
if ("lte" in filter) {
|
|
100
|
+
if (!isComparable(value) || value > filter.lte) return false;
|
|
101
|
+
}
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
function sortRows(rows, orderBy) {
|
|
105
|
+
if (!orderBy) return rows;
|
|
106
|
+
const entries = Object.entries(orderBy);
|
|
107
|
+
if (!entries.length) return rows;
|
|
108
|
+
return [...rows].sort((left, right) => {
|
|
109
|
+
for (const [field, direction] of entries) {
|
|
110
|
+
const a = left.data[field];
|
|
111
|
+
const b = right.data[field];
|
|
112
|
+
if ((0, import_orm.equalValues)(a, b)) continue;
|
|
113
|
+
if (a === void 0) return direction === "asc" ? -1 : 1;
|
|
114
|
+
if (b === void 0) return direction === "asc" ? 1 : -1;
|
|
115
|
+
if (a == null) return direction === "asc" ? -1 : 1;
|
|
116
|
+
if (b == null) return direction === "asc" ? 1 : -1;
|
|
117
|
+
if (a < b) return direction === "asc" ? -1 : 1;
|
|
118
|
+
if (a > b) return direction === "asc" ? 1 : -1;
|
|
119
|
+
}
|
|
120
|
+
return 0;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
function pageRows(rows, skip, take) {
|
|
124
|
+
const start = skip ?? 0;
|
|
125
|
+
const end = take === void 0 ? void 0 : start + take;
|
|
126
|
+
return rows.slice(start, end);
|
|
127
|
+
}
|
|
128
|
+
function unstorageConstraintError(target) {
|
|
129
|
+
const fields = Array.isArray(target) ? target.join(", ") : target ?? "unique fields";
|
|
130
|
+
const error = new Error(`Unstorage unique constraint violation on ${fields}.`);
|
|
131
|
+
Object.assign(error, {
|
|
132
|
+
name: "UnstorageUniqueConstraintError",
|
|
133
|
+
code: "UNSTORAGE_UNIQUE_CONSTRAINT",
|
|
134
|
+
target
|
|
135
|
+
});
|
|
136
|
+
return error;
|
|
137
|
+
}
|
|
138
|
+
function createUnstorageDriverInternal(config) {
|
|
139
|
+
function getSupportedManifest(schema) {
|
|
140
|
+
const manifest = getManifest(schema);
|
|
141
|
+
for (const model of Object.values(manifest.models)) {
|
|
142
|
+
if (model.schema) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
`The Unstorage runtime does not support schema-qualified tables for model "${model.name}". Use flat table names instead.`
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
const idField = model.fields.id;
|
|
148
|
+
if (idField?.kind === "id" && idField.idType === "integer" && idField.generated === "increment") {
|
|
149
|
+
throw new Error(
|
|
150
|
+
`The Unstorage runtime does not support generated integer ids for model "${model.name}". Use manual numeric ids or a string id instead.`
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return manifest;
|
|
155
|
+
}
|
|
156
|
+
function getModelBase(schema, modelName) {
|
|
157
|
+
const manifest = getSupportedManifest(schema);
|
|
158
|
+
return config.prefixes?.[modelName] ?? (0, import_unstorage.joinKeys)(config.base ?? defaultBase, manifest.models[modelName].table);
|
|
159
|
+
}
|
|
160
|
+
function recordBase(schema, modelName) {
|
|
161
|
+
return (0, import_unstorage.joinKeys)(getModelBase(schema, modelName), "record");
|
|
162
|
+
}
|
|
163
|
+
function recordKey(schema, modelName, docId) {
|
|
164
|
+
return (0, import_unstorage.joinKeys)(recordBase(schema, modelName), encodeURIComponent(docId));
|
|
165
|
+
}
|
|
166
|
+
function fieldTransform(modelName, fieldName) {
|
|
167
|
+
return config.transforms?.[modelName]?.[fieldName];
|
|
168
|
+
}
|
|
169
|
+
function encodeValue(modelName, field, value) {
|
|
170
|
+
if (value === void 0) return value;
|
|
171
|
+
if (value === null) return null;
|
|
172
|
+
const transform = fieldTransform(modelName, field.name);
|
|
173
|
+
if (transform?.encode) {
|
|
174
|
+
return transform.encode(value);
|
|
175
|
+
}
|
|
176
|
+
if (field.kind === "id" && field.idType === "integer") {
|
|
177
|
+
return Number(value);
|
|
178
|
+
}
|
|
179
|
+
if (field.kind === "enum") {
|
|
180
|
+
return String(value);
|
|
181
|
+
}
|
|
182
|
+
if (field.kind === "boolean") {
|
|
183
|
+
return Boolean(value);
|
|
184
|
+
}
|
|
185
|
+
if (field.kind === "integer") {
|
|
186
|
+
return Number(value);
|
|
187
|
+
}
|
|
188
|
+
if (field.kind === "bigint") {
|
|
189
|
+
return typeof value === "bigint" ? value.toString() : BigInt(value).toString();
|
|
190
|
+
}
|
|
191
|
+
if (field.kind === "decimal") {
|
|
192
|
+
return typeof value === "string" ? normalizeDecimalString(value) : String(value);
|
|
193
|
+
}
|
|
194
|
+
if (field.kind === "datetime") {
|
|
195
|
+
if (value instanceof Date) return value.toISOString();
|
|
196
|
+
return new Date(value).toISOString();
|
|
197
|
+
}
|
|
198
|
+
return value;
|
|
199
|
+
}
|
|
200
|
+
function decodeValue(modelName, field, value, docId) {
|
|
201
|
+
const transform = fieldTransform(modelName, field.name);
|
|
202
|
+
if (transform?.decode) {
|
|
203
|
+
return transform.decode(value);
|
|
204
|
+
}
|
|
205
|
+
if (value === void 0 && field.kind === "id" && docId !== void 0) {
|
|
206
|
+
if (field.idType === "integer") {
|
|
207
|
+
const numeric = Number(docId);
|
|
208
|
+
return Number.isFinite(numeric) ? numeric : void 0;
|
|
209
|
+
}
|
|
210
|
+
return docId;
|
|
211
|
+
}
|
|
212
|
+
if (value === void 0 || value === null) return value ?? null;
|
|
213
|
+
if (field.kind === "id" && field.idType === "integer") {
|
|
214
|
+
return Number(value);
|
|
215
|
+
}
|
|
216
|
+
if (field.kind === "enum") {
|
|
217
|
+
return String(value);
|
|
218
|
+
}
|
|
219
|
+
if (field.kind === "boolean") {
|
|
220
|
+
return Boolean(value);
|
|
221
|
+
}
|
|
222
|
+
if (field.kind === "integer") {
|
|
223
|
+
return Number(value);
|
|
224
|
+
}
|
|
225
|
+
if (field.kind === "bigint") {
|
|
226
|
+
return typeof value === "bigint" ? value : BigInt(value);
|
|
227
|
+
}
|
|
228
|
+
if (field.kind === "decimal") {
|
|
229
|
+
return normalizeDecimalString(String(value));
|
|
230
|
+
}
|
|
231
|
+
if (field.kind === "datetime") {
|
|
232
|
+
return value instanceof Date ? value : new Date(value);
|
|
233
|
+
}
|
|
234
|
+
return value;
|
|
235
|
+
}
|
|
236
|
+
function buildStoredRow(model, data) {
|
|
237
|
+
const stored = {};
|
|
238
|
+
const decoded = {};
|
|
239
|
+
for (const field of Object.values(model.fields)) {
|
|
240
|
+
const value = applyDefault(data[field.name], field);
|
|
241
|
+
if (value === void 0) continue;
|
|
242
|
+
const encoded = encodeValue(model.name, field, value);
|
|
243
|
+
stored[field.column] = encoded;
|
|
244
|
+
decoded[field.name] = decodeValue(model.name, field, encoded);
|
|
245
|
+
}
|
|
246
|
+
const idField = model.fields.id;
|
|
247
|
+
if (!idField) {
|
|
248
|
+
return {
|
|
249
|
+
docId: (0, import_node_crypto.randomUUID)(),
|
|
250
|
+
stored,
|
|
251
|
+
decoded
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
let idValue = decoded[idField.name];
|
|
255
|
+
if (idValue === void 0 || idValue === null) {
|
|
256
|
+
if (idField.idType === "integer") {
|
|
257
|
+
throw new Error(
|
|
258
|
+
`The Unstorage runtime requires an explicit numeric id for model "${model.name}" when using manual integer ids.`
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
idValue = (0, import_node_crypto.randomUUID)();
|
|
262
|
+
const encodedId = encodeValue(model.name, idField, idValue);
|
|
263
|
+
stored[idField.column] = encodedId;
|
|
264
|
+
decoded[idField.name] = decodeValue(model.name, idField, encodedId);
|
|
265
|
+
idValue = decoded[idField.name];
|
|
266
|
+
}
|
|
267
|
+
return {
|
|
268
|
+
docId: String(idValue),
|
|
269
|
+
stored,
|
|
270
|
+
decoded
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
function buildUpdatedRow(model, current, data) {
|
|
274
|
+
const stored = {
|
|
275
|
+
...current.stored
|
|
276
|
+
};
|
|
277
|
+
const decoded = {
|
|
278
|
+
...current.data
|
|
279
|
+
};
|
|
280
|
+
for (const [fieldName, value] of Object.entries(data)) {
|
|
281
|
+
if (value === void 0) continue;
|
|
282
|
+
const field = model.fields[fieldName];
|
|
283
|
+
if (!field) {
|
|
284
|
+
throw new Error(`Unknown field "${fieldName}" on model "${model.name}".`);
|
|
285
|
+
}
|
|
286
|
+
if (field.name === "id" && !(0, import_orm.equalValues)(current.data.id, value)) {
|
|
287
|
+
throw new Error(
|
|
288
|
+
`The Unstorage runtime does not support updating the id field for model "${model.name}".`
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
const encoded = encodeValue(model.name, field, value);
|
|
292
|
+
stored[field.column] = encoded;
|
|
293
|
+
decoded[field.name] = decodeValue(model.name, field, encoded);
|
|
294
|
+
}
|
|
295
|
+
return {
|
|
296
|
+
docId: current.docId,
|
|
297
|
+
stored,
|
|
298
|
+
decoded
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
async function loadRowByKey(model, key) {
|
|
302
|
+
const item = await config.storage.getItem(key);
|
|
303
|
+
if (!isUnstorageRecordItem(item)) {
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
const decoded = {};
|
|
307
|
+
for (const field of Object.values(model.fields)) {
|
|
308
|
+
decoded[field.name] = decodeValue(
|
|
309
|
+
model.name,
|
|
310
|
+
field,
|
|
311
|
+
item.data[field.column],
|
|
312
|
+
item.__orm_docId
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
return {
|
|
316
|
+
docId: item.__orm_docId,
|
|
317
|
+
key,
|
|
318
|
+
data: decoded,
|
|
319
|
+
stored: item.data
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
async function loadRows(schema, modelName) {
|
|
323
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
324
|
+
const keys = await config.storage.getKeys(recordBase(schema, modelName));
|
|
325
|
+
const rows = await Promise.all(keys.map((key) => loadRowByKey(model, key)));
|
|
326
|
+
return rows.filter((row) => !!row);
|
|
327
|
+
}
|
|
328
|
+
function normalizeFilterValue(model, fieldName, value) {
|
|
329
|
+
const field = model.fields[fieldName];
|
|
330
|
+
if (!field || value === void 0 || value === null) {
|
|
331
|
+
return value;
|
|
332
|
+
}
|
|
333
|
+
return decodeValue(model.name, field, encodeValue(model.name, field, value));
|
|
334
|
+
}
|
|
335
|
+
function evaluateModelFilter(model, fieldName, value, filter) {
|
|
336
|
+
if (!(0, import_orm.isOperatorFilterObject)(filter)) {
|
|
337
|
+
return (0, import_orm.equalValues)(value, normalizeFilterValue(model, fieldName, filter));
|
|
338
|
+
}
|
|
339
|
+
const normalized = {
|
|
340
|
+
...filter.eq !== void 0 ? { eq: normalizeFilterValue(model, fieldName, filter.eq) } : {},
|
|
341
|
+
...filter.not !== void 0 ? { not: normalizeFilterValue(model, fieldName, filter.not) } : {},
|
|
342
|
+
...filter.in !== void 0 ? {
|
|
343
|
+
in: (Array.isArray(filter.in) ? filter.in : []).map(
|
|
344
|
+
(entry) => normalizeFilterValue(model, fieldName, entry)
|
|
345
|
+
)
|
|
346
|
+
} : {},
|
|
347
|
+
...filter.contains !== void 0 ? { contains: String(filter.contains) } : {},
|
|
348
|
+
...filter.gt !== void 0 ? { gt: normalizeFilterValue(model, fieldName, filter.gt) } : {},
|
|
349
|
+
...filter.gte !== void 0 ? { gte: normalizeFilterValue(model, fieldName, filter.gte) } : {},
|
|
350
|
+
...filter.lt !== void 0 ? { lt: normalizeFilterValue(model, fieldName, filter.lt) } : {},
|
|
351
|
+
...filter.lte !== void 0 ? { lte: normalizeFilterValue(model, fieldName, filter.lte) } : {}
|
|
352
|
+
};
|
|
353
|
+
return evaluateFilter(value, normalized);
|
|
354
|
+
}
|
|
355
|
+
function matchesModelWhere(model, record, where) {
|
|
356
|
+
if (!where) return true;
|
|
357
|
+
if (where.AND && !where.AND.every((clause) => matchesModelWhere(model, record, clause))) {
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
if (where.OR && !where.OR.some((clause) => matchesModelWhere(model, record, clause))) {
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
if (where.NOT && matchesModelWhere(model, record, where.NOT)) {
|
|
364
|
+
return false;
|
|
365
|
+
}
|
|
366
|
+
for (const [key, filter] of Object.entries(where)) {
|
|
367
|
+
if (key === "AND" || key === "OR" || key === "NOT") continue;
|
|
368
|
+
if (!evaluateModelFilter(model, key, record[key], filter)) return false;
|
|
369
|
+
}
|
|
370
|
+
return true;
|
|
371
|
+
}
|
|
372
|
+
function applyModelQuery(model, rows, args = {}) {
|
|
373
|
+
const filtered = rows.filter((row) => matchesModelWhere(model, row.data, args.where));
|
|
374
|
+
const sorted = sortRows(filtered, args.orderBy);
|
|
375
|
+
return pageRows(sorted, args.skip, args.take);
|
|
376
|
+
}
|
|
377
|
+
function serializeUniqueValue(model, fieldName, value) {
|
|
378
|
+
const field = model.fields[fieldName];
|
|
379
|
+
const normalized = decodeValue(model.name, field, encodeValue(model.name, field, value));
|
|
380
|
+
if (normalized instanceof Date) {
|
|
381
|
+
return normalized.toISOString();
|
|
382
|
+
}
|
|
383
|
+
if (typeof normalized === "bigint") {
|
|
384
|
+
return normalized.toString();
|
|
385
|
+
}
|
|
386
|
+
return JSON.stringify(normalized);
|
|
387
|
+
}
|
|
388
|
+
function uniqueLockKey(schema, model, fields, row) {
|
|
389
|
+
const values = fields.map((fieldName) => row[fieldName]);
|
|
390
|
+
if (values.some((value) => value === void 0 || value === null)) {
|
|
391
|
+
return null;
|
|
392
|
+
}
|
|
393
|
+
return (0, import_unstorage.joinKeys)(
|
|
394
|
+
getModelBase(schema, model.name),
|
|
395
|
+
"unique",
|
|
396
|
+
fields.join("+"),
|
|
397
|
+
...fields.map(
|
|
398
|
+
(fieldName) => encodeURIComponent(serializeUniqueValue(model, fieldName, row[fieldName]))
|
|
399
|
+
)
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
function uniqueLocksForRow(schema, model, row, docId) {
|
|
403
|
+
const target = recordKey(schema, model.name, docId);
|
|
404
|
+
const locks = [];
|
|
405
|
+
for (const field of Object.values(model.fields)) {
|
|
406
|
+
if (!field.unique) continue;
|
|
407
|
+
const key = uniqueLockKey(schema, model, [field.name], row);
|
|
408
|
+
if (!key) continue;
|
|
409
|
+
locks.push({
|
|
410
|
+
key,
|
|
411
|
+
target,
|
|
412
|
+
fields: [field.name]
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
for (const constraint of model.constraints.unique) {
|
|
416
|
+
const key = uniqueLockKey(schema, model, [...constraint.fields], row);
|
|
417
|
+
if (!key) continue;
|
|
418
|
+
locks.push({
|
|
419
|
+
key,
|
|
420
|
+
target,
|
|
421
|
+
fields: [...constraint.fields]
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
return locks;
|
|
425
|
+
}
|
|
426
|
+
async function loadUniqueRow(schema, modelName, where) {
|
|
427
|
+
const manifest = getSupportedManifest(schema);
|
|
428
|
+
const model = manifest.models[modelName];
|
|
429
|
+
const lookup = (0, import_orm.requireUniqueLookup)(model, where, "FindUnique");
|
|
430
|
+
if (lookup.kind === "id") {
|
|
431
|
+
return loadRowByKey(
|
|
432
|
+
model,
|
|
433
|
+
recordKey(schema, modelName, String(lookup.values[lookup.fields[0].name]))
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
const normalizedLookupRow = Object.fromEntries(
|
|
437
|
+
lookup.fields.map((field) => [
|
|
438
|
+
field.name,
|
|
439
|
+
decodeValue(model.name, field, encodeValue(model.name, field, lookup.values[field.name]))
|
|
440
|
+
])
|
|
441
|
+
);
|
|
442
|
+
const key = uniqueLockKey(
|
|
443
|
+
schema,
|
|
444
|
+
model,
|
|
445
|
+
lookup.fields.map((field) => field.name),
|
|
446
|
+
normalizedLookupRow
|
|
447
|
+
);
|
|
448
|
+
if (!key) {
|
|
449
|
+
return null;
|
|
450
|
+
}
|
|
451
|
+
const lock = await config.storage.getItem(key);
|
|
452
|
+
if (!isUnstorageUniqueItem(lock)) {
|
|
453
|
+
return null;
|
|
454
|
+
}
|
|
455
|
+
return loadRowByKey(model, lock.__orm_target);
|
|
456
|
+
}
|
|
457
|
+
function findUniqueConflict(model, candidate, existingRows, ignoreDocIds = /* @__PURE__ */ new Set()) {
|
|
458
|
+
const idField = model.fields.id;
|
|
459
|
+
for (const row of existingRows) {
|
|
460
|
+
if (ignoreDocIds.has(row.docId)) continue;
|
|
461
|
+
if (idField && candidate[idField.name] !== void 0 && candidate[idField.name] !== null && row.data[idField.name] !== void 0 && row.data[idField.name] !== null && (0, import_orm.equalValues)(candidate[idField.name], row.data[idField.name])) {
|
|
462
|
+
return [idField.name];
|
|
463
|
+
}
|
|
464
|
+
for (const field of Object.values(model.fields)) {
|
|
465
|
+
if (!field.unique) continue;
|
|
466
|
+
if (candidate[field.name] === void 0 || candidate[field.name] === null) continue;
|
|
467
|
+
if (row.data[field.name] === void 0 || row.data[field.name] === null) continue;
|
|
468
|
+
if ((0, import_orm.equalValues)(candidate[field.name], row.data[field.name])) {
|
|
469
|
+
return [field.name];
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
for (const constraint of model.constraints.unique) {
|
|
473
|
+
if (!constraint.fields.every(
|
|
474
|
+
(fieldName) => candidate[fieldName] !== void 0 && candidate[fieldName] !== null && row.data[fieldName] !== void 0 && row.data[fieldName] !== null
|
|
475
|
+
)) {
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
if (constraint.fields.every(
|
|
479
|
+
(fieldName) => (0, import_orm.equalValues)(candidate[fieldName], row.data[fieldName])
|
|
480
|
+
)) {
|
|
481
|
+
return [...constraint.fields];
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return null;
|
|
486
|
+
}
|
|
487
|
+
function buildRecordItem(row) {
|
|
488
|
+
return {
|
|
489
|
+
__orm_kind: ormRecordKind,
|
|
490
|
+
__orm_docId: row.docId,
|
|
491
|
+
data: row.stored
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
async function putRecordSequential(schema, modelName, row, conditionallyCreate) {
|
|
495
|
+
const key = recordKey(schema, modelName, row.docId);
|
|
496
|
+
if (conditionallyCreate && await config.storage.hasItem(key)) {
|
|
497
|
+
throw unstorageConstraintError(["id"]);
|
|
498
|
+
}
|
|
499
|
+
await config.storage.setItem(key, buildRecordItem(row));
|
|
500
|
+
}
|
|
501
|
+
async function putUniqueSequential(lock) {
|
|
502
|
+
const existing = await config.storage.getItem(lock.key);
|
|
503
|
+
if (isUnstorageUniqueItem(existing) && existing.__orm_target !== lock.target) {
|
|
504
|
+
throw unstorageConstraintError(lock.fields);
|
|
505
|
+
}
|
|
506
|
+
if (!existing) {
|
|
507
|
+
await config.storage.setItem(lock.key, {
|
|
508
|
+
__orm_kind: ormUniqueKind,
|
|
509
|
+
__orm_target: lock.target
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
async function deleteSequential(key) {
|
|
514
|
+
await config.storage.removeItem(key);
|
|
515
|
+
}
|
|
516
|
+
async function acquireUniqueLocksSequential(locks) {
|
|
517
|
+
const acquired = [];
|
|
518
|
+
try {
|
|
519
|
+
for (const lock of locks) {
|
|
520
|
+
await putUniqueSequential(lock);
|
|
521
|
+
acquired.push(lock);
|
|
522
|
+
}
|
|
523
|
+
} catch (error) {
|
|
524
|
+
await releaseUniqueLocksSequential(acquired);
|
|
525
|
+
throw error;
|
|
526
|
+
}
|
|
527
|
+
return acquired;
|
|
528
|
+
}
|
|
529
|
+
async function releaseUniqueLocksSequential(locks) {
|
|
530
|
+
for (const lock of [...locks].reverse()) {
|
|
531
|
+
try {
|
|
532
|
+
await deleteSequential(lock.key);
|
|
533
|
+
} catch {
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
async function createRecordWithLocks(schema, modelName, row, model) {
|
|
538
|
+
const locks = uniqueLocksForRow(schema, model, row.decoded, row.docId);
|
|
539
|
+
const acquired = await acquireUniqueLocksSequential(locks);
|
|
540
|
+
try {
|
|
541
|
+
await putRecordSequential(schema, modelName, row, true);
|
|
542
|
+
} catch (error) {
|
|
543
|
+
await releaseUniqueLocksSequential(acquired);
|
|
544
|
+
throw error;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
async function updateRecordWithLocks(schema, modelName, model, current, next) {
|
|
548
|
+
const currentLocks = new Map(
|
|
549
|
+
uniqueLocksForRow(schema, model, current.data, current.docId).map((lock) => [lock.key, lock])
|
|
550
|
+
);
|
|
551
|
+
const nextLocks = new Map(
|
|
552
|
+
uniqueLocksForRow(schema, model, next.decoded, next.docId).map((lock) => [lock.key, lock])
|
|
553
|
+
);
|
|
554
|
+
const addedLocks = [...nextLocks.values()].filter((lock) => !currentLocks.has(lock.key));
|
|
555
|
+
const removedLocks = [...currentLocks.values()].filter((lock) => !nextLocks.has(lock.key));
|
|
556
|
+
const acquired = await acquireUniqueLocksSequential(addedLocks);
|
|
557
|
+
try {
|
|
558
|
+
await putRecordSequential(schema, modelName, next, false);
|
|
559
|
+
} catch (error) {
|
|
560
|
+
await releaseUniqueLocksSequential(acquired);
|
|
561
|
+
throw error;
|
|
562
|
+
}
|
|
563
|
+
for (const lock of removedLocks) {
|
|
564
|
+
await deleteSequential(lock.key);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
async function deleteRecordWithLocks(schema, modelName, model, current) {
|
|
568
|
+
await deleteSequential(recordKey(schema, modelName, current.docId));
|
|
569
|
+
const locks = uniqueLocksForRow(schema, model, current.data, current.docId);
|
|
570
|
+
for (const lock of locks) {
|
|
571
|
+
await deleteSequential(lock.key);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
async function projectRow(schema, modelName, row, select) {
|
|
575
|
+
const modelDefinition = schema.models[modelName];
|
|
576
|
+
const output = {};
|
|
577
|
+
if (!select) {
|
|
578
|
+
for (const fieldName of Object.keys(modelDefinition.fields)) {
|
|
579
|
+
output[fieldName] = row[fieldName];
|
|
580
|
+
}
|
|
581
|
+
return output;
|
|
582
|
+
}
|
|
583
|
+
for (const [key, value] of Object.entries(select)) {
|
|
584
|
+
if (value !== true && value === void 0) continue;
|
|
585
|
+
if (key in modelDefinition.fields && value === true) {
|
|
586
|
+
output[key] = row[key];
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
if (key in modelDefinition.relations) {
|
|
590
|
+
output[key] = await resolveRelation(
|
|
591
|
+
schema,
|
|
592
|
+
modelName,
|
|
593
|
+
key,
|
|
594
|
+
row,
|
|
595
|
+
value
|
|
596
|
+
);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
return output;
|
|
600
|
+
}
|
|
601
|
+
async function resolveRelation(schema, modelName, relationName, row, value) {
|
|
602
|
+
const relation = schema.models[modelName].relations[relationName];
|
|
603
|
+
const relationArgs = value === true ? {} : value;
|
|
604
|
+
if (relation.kind === "belongsTo") {
|
|
605
|
+
const foreignValue = row[relation.foreignKey];
|
|
606
|
+
const targetRows2 = (await loadRows(schema, relation.target)).filter(
|
|
607
|
+
(item) => (0, import_orm.equalValues)(item.data.id, foreignValue)
|
|
608
|
+
);
|
|
609
|
+
const target = applyModelQuery(
|
|
610
|
+
relationTargetManifest(schema, relation.target),
|
|
611
|
+
targetRows2,
|
|
612
|
+
relationArgs
|
|
613
|
+
)[0];
|
|
614
|
+
return target ? projectRow(
|
|
615
|
+
schema,
|
|
616
|
+
relation.target,
|
|
617
|
+
target.data,
|
|
618
|
+
relationArgs.select
|
|
619
|
+
) : null;
|
|
620
|
+
}
|
|
621
|
+
if (relation.kind === "hasOne") {
|
|
622
|
+
const targetRows2 = (await loadRows(schema, relation.target)).filter(
|
|
623
|
+
(item) => (0, import_orm.equalValues)(item.data[relation.foreignKey], row.id)
|
|
624
|
+
);
|
|
625
|
+
const target = applyModelQuery(
|
|
626
|
+
relationTargetManifest(schema, relation.target),
|
|
627
|
+
targetRows2,
|
|
628
|
+
relationArgs
|
|
629
|
+
)[0];
|
|
630
|
+
return target ? projectRow(
|
|
631
|
+
schema,
|
|
632
|
+
relation.target,
|
|
633
|
+
target.data,
|
|
634
|
+
relationArgs.select
|
|
635
|
+
) : null;
|
|
636
|
+
}
|
|
637
|
+
if (relation.kind === "hasMany") {
|
|
638
|
+
const targetRows2 = (await loadRows(schema, relation.target)).filter(
|
|
639
|
+
(item) => (0, import_orm.equalValues)(item.data[relation.foreignKey], row.id)
|
|
640
|
+
);
|
|
641
|
+
const matchedRows2 = applyModelQuery(
|
|
642
|
+
relationTargetManifest(schema, relation.target),
|
|
643
|
+
targetRows2,
|
|
644
|
+
relationArgs
|
|
645
|
+
);
|
|
646
|
+
return Promise.all(
|
|
647
|
+
matchedRows2.map(
|
|
648
|
+
(item) => projectRow(schema, relation.target, item.data, relationArgs.select)
|
|
649
|
+
)
|
|
650
|
+
);
|
|
651
|
+
}
|
|
652
|
+
const throughRows = (await loadRows(schema, relation.through)).filter(
|
|
653
|
+
(item) => (0, import_orm.equalValues)(item.data[relation.from], row.id)
|
|
654
|
+
);
|
|
655
|
+
const targetIds = throughRows.map((item) => item.data[relation.to]);
|
|
656
|
+
const targetRows = (await loadRows(schema, relation.target)).filter(
|
|
657
|
+
(item) => targetIds.some((targetId) => (0, import_orm.equalValues)(targetId, item.data.id))
|
|
658
|
+
);
|
|
659
|
+
const matchedRows = applyModelQuery(
|
|
660
|
+
relationTargetManifest(schema, relation.target),
|
|
661
|
+
targetRows,
|
|
662
|
+
relationArgs
|
|
663
|
+
);
|
|
664
|
+
return Promise.all(
|
|
665
|
+
matchedRows.map(
|
|
666
|
+
(item) => projectRow(schema, relation.target, item.data, relationArgs.select)
|
|
667
|
+
)
|
|
668
|
+
);
|
|
669
|
+
}
|
|
670
|
+
function relationTargetManifest(schema, modelName) {
|
|
671
|
+
return getSupportedManifest(schema).models[modelName];
|
|
672
|
+
}
|
|
673
|
+
let driver;
|
|
674
|
+
driver = {
|
|
675
|
+
handle: (0, import_orm.createDriverHandle)({
|
|
676
|
+
kind: "unstorage",
|
|
677
|
+
client: {
|
|
678
|
+
storage: config.storage,
|
|
679
|
+
base: config.base,
|
|
680
|
+
prefixes: config.prefixes
|
|
681
|
+
},
|
|
682
|
+
capabilities: {
|
|
683
|
+
supportsNumericIds: true,
|
|
684
|
+
numericIds: "manual",
|
|
685
|
+
supportsJSON: true,
|
|
686
|
+
supportsDates: true,
|
|
687
|
+
supportsBooleans: true,
|
|
688
|
+
supportsTransactions: false,
|
|
689
|
+
supportsSchemaNamespaces: false,
|
|
690
|
+
supportsTransactionalDDL: false,
|
|
691
|
+
supportsJoin: false,
|
|
692
|
+
nativeRelationLoading: "none",
|
|
693
|
+
textComparison: "case-sensitive",
|
|
694
|
+
textMatching: {
|
|
695
|
+
equality: "case-sensitive",
|
|
696
|
+
contains: "case-sensitive",
|
|
697
|
+
ordering: "case-sensitive"
|
|
698
|
+
},
|
|
699
|
+
upsert: "emulated",
|
|
700
|
+
returning: {
|
|
701
|
+
create: true,
|
|
702
|
+
update: true,
|
|
703
|
+
delete: false
|
|
704
|
+
},
|
|
705
|
+
returningMode: {
|
|
706
|
+
create: "record",
|
|
707
|
+
update: "record",
|
|
708
|
+
delete: "none"
|
|
709
|
+
},
|
|
710
|
+
nativeRelations: {
|
|
711
|
+
singularChains: false,
|
|
712
|
+
hasMany: false,
|
|
713
|
+
manyToMany: false,
|
|
714
|
+
filtered: false,
|
|
715
|
+
ordered: false,
|
|
716
|
+
paginated: false
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
}),
|
|
720
|
+
async findMany(schema, modelName, args) {
|
|
721
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
722
|
+
const rows = applyModelQuery(model, await loadRows(schema, modelName), args);
|
|
723
|
+
return Promise.all(rows.map((row) => projectRow(schema, modelName, row.data, args.select)));
|
|
724
|
+
},
|
|
725
|
+
async findFirst(schema, modelName, args) {
|
|
726
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
727
|
+
const row = applyModelQuery(model, await loadRows(schema, modelName), args)[0];
|
|
728
|
+
if (!row) return null;
|
|
729
|
+
return projectRow(schema, modelName, row.data, args.select);
|
|
730
|
+
},
|
|
731
|
+
async findUnique(schema, modelName, args) {
|
|
732
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
733
|
+
const row = await loadUniqueRow(schema, modelName, args.where);
|
|
734
|
+
if (!row || !matchesModelWhere(model, row.data, args.where)) {
|
|
735
|
+
return null;
|
|
736
|
+
}
|
|
737
|
+
return projectRow(schema, modelName, row.data, args.select);
|
|
738
|
+
},
|
|
739
|
+
async count(schema, modelName, args) {
|
|
740
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
741
|
+
return applyModelQuery(model, await loadRows(schema, modelName), args).length;
|
|
742
|
+
},
|
|
743
|
+
async create(schema, modelName, args) {
|
|
744
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
745
|
+
const existingRows = await loadRows(schema, modelName);
|
|
746
|
+
const built = buildStoredRow(model, args.data);
|
|
747
|
+
const conflict = findUniqueConflict(model, built.decoded, existingRows);
|
|
748
|
+
if (conflict) {
|
|
749
|
+
throw unstorageConstraintError(conflict);
|
|
750
|
+
}
|
|
751
|
+
await createRecordWithLocks(schema, modelName, built, model);
|
|
752
|
+
return projectRow(schema, modelName, built.decoded, args.select);
|
|
753
|
+
},
|
|
754
|
+
async createMany(schema, modelName, args) {
|
|
755
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
756
|
+
const existingRows = await loadRows(schema, modelName);
|
|
757
|
+
const created = [];
|
|
758
|
+
for (const entry of args.data) {
|
|
759
|
+
const built = buildStoredRow(model, entry);
|
|
760
|
+
const conflict = findUniqueConflict(model, built.decoded, [
|
|
761
|
+
...existingRows,
|
|
762
|
+
...created.map((row) => ({
|
|
763
|
+
docId: row.docId,
|
|
764
|
+
key: recordKey(schema, modelName, row.docId),
|
|
765
|
+
data: row.decoded,
|
|
766
|
+
stored: row.stored
|
|
767
|
+
}))
|
|
768
|
+
]);
|
|
769
|
+
if (conflict) {
|
|
770
|
+
throw unstorageConstraintError(conflict);
|
|
771
|
+
}
|
|
772
|
+
created.push(built);
|
|
773
|
+
}
|
|
774
|
+
for (const row of created) {
|
|
775
|
+
await createRecordWithLocks(schema, modelName, row, model);
|
|
776
|
+
}
|
|
777
|
+
return Promise.all(
|
|
778
|
+
created.map((row) => projectRow(schema, modelName, row.decoded, args.select))
|
|
779
|
+
);
|
|
780
|
+
},
|
|
781
|
+
async update(schema, modelName, args) {
|
|
782
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
783
|
+
const rows = await loadRows(schema, modelName);
|
|
784
|
+
const current = applyModelQuery(model, rows, {
|
|
785
|
+
where: args.where,
|
|
786
|
+
take: 1
|
|
787
|
+
})[0];
|
|
788
|
+
if (!current) return null;
|
|
789
|
+
const next = buildUpdatedRow(model, current, args.data);
|
|
790
|
+
const conflict = findUniqueConflict(model, next.decoded, rows, /* @__PURE__ */ new Set([current.docId]));
|
|
791
|
+
if (conflict) {
|
|
792
|
+
throw unstorageConstraintError(conflict);
|
|
793
|
+
}
|
|
794
|
+
await updateRecordWithLocks(schema, modelName, model, current, next);
|
|
795
|
+
return projectRow(schema, modelName, next.decoded, args.select);
|
|
796
|
+
},
|
|
797
|
+
async updateMany(schema, modelName, args) {
|
|
798
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
799
|
+
const rows = await loadRows(schema, modelName);
|
|
800
|
+
const matched = applyModelQuery(model, rows, {
|
|
801
|
+
where: args.where
|
|
802
|
+
});
|
|
803
|
+
if (!matched.length) return 0;
|
|
804
|
+
const nextRows = matched.map(
|
|
805
|
+
(row) => buildUpdatedRow(model, row, args.data)
|
|
806
|
+
);
|
|
807
|
+
const keepIds = new Set(matched.map((row) => row.docId));
|
|
808
|
+
const remaining = rows.filter((row) => !keepIds.has(row.docId));
|
|
809
|
+
const pending = [];
|
|
810
|
+
for (const next of nextRows) {
|
|
811
|
+
const conflict = findUniqueConflict(model, next.decoded, [...remaining, ...pending]);
|
|
812
|
+
if (conflict) {
|
|
813
|
+
throw unstorageConstraintError(conflict);
|
|
814
|
+
}
|
|
815
|
+
pending.push({
|
|
816
|
+
docId: next.docId,
|
|
817
|
+
key: recordKey(schema, modelName, next.docId),
|
|
818
|
+
data: next.decoded,
|
|
819
|
+
stored: next.stored
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
for (let index = 0; index < matched.length; index += 1) {
|
|
823
|
+
await updateRecordWithLocks(schema, modelName, model, matched[index], nextRows[index]);
|
|
824
|
+
}
|
|
825
|
+
return nextRows.length;
|
|
826
|
+
},
|
|
827
|
+
async upsert(schema, modelName, args) {
|
|
828
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
829
|
+
const lookup = (0, import_orm.requireUniqueLookup)(model, args.where, "Upsert");
|
|
830
|
+
(0, import_orm.validateUniqueLookupUpdateData)(
|
|
831
|
+
model,
|
|
832
|
+
args.update,
|
|
833
|
+
lookup,
|
|
834
|
+
"Upsert"
|
|
835
|
+
);
|
|
836
|
+
const current = await loadUniqueRow(schema, modelName, args.where);
|
|
837
|
+
if (current && matchesModelWhere(model, current.data, args.where)) {
|
|
838
|
+
const rows2 = await loadRows(schema, modelName);
|
|
839
|
+
const next = buildUpdatedRow(
|
|
840
|
+
model,
|
|
841
|
+
current,
|
|
842
|
+
args.update
|
|
843
|
+
);
|
|
844
|
+
const conflict2 = findUniqueConflict(model, next.decoded, rows2, /* @__PURE__ */ new Set([current.docId]));
|
|
845
|
+
if (conflict2) {
|
|
846
|
+
throw unstorageConstraintError(conflict2);
|
|
847
|
+
}
|
|
848
|
+
await updateRecordWithLocks(schema, modelName, model, current, next);
|
|
849
|
+
return projectRow(schema, modelName, next.decoded, args.select);
|
|
850
|
+
}
|
|
851
|
+
const created = buildStoredRow(
|
|
852
|
+
model,
|
|
853
|
+
(0, import_orm.mergeUniqueLookupCreateData)(
|
|
854
|
+
model,
|
|
855
|
+
args.create,
|
|
856
|
+
lookup,
|
|
857
|
+
"Upsert"
|
|
858
|
+
)
|
|
859
|
+
);
|
|
860
|
+
const rows = await loadRows(schema, modelName);
|
|
861
|
+
const conflict = findUniqueConflict(model, created.decoded, rows);
|
|
862
|
+
if (conflict) {
|
|
863
|
+
throw unstorageConstraintError(conflict);
|
|
864
|
+
}
|
|
865
|
+
await createRecordWithLocks(schema, modelName, created, model);
|
|
866
|
+
return projectRow(schema, modelName, created.decoded, args.select);
|
|
867
|
+
},
|
|
868
|
+
async delete(schema, modelName, args) {
|
|
869
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
870
|
+
const row = applyModelQuery(model, await loadRows(schema, modelName), {
|
|
871
|
+
where: args.where,
|
|
872
|
+
take: 1
|
|
873
|
+
})[0];
|
|
874
|
+
if (!row) return 0;
|
|
875
|
+
await deleteRecordWithLocks(schema, modelName, model, row);
|
|
876
|
+
return 1;
|
|
877
|
+
},
|
|
878
|
+
async deleteMany(schema, modelName, args) {
|
|
879
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
880
|
+
const rows = applyModelQuery(model, await loadRows(schema, modelName), {
|
|
881
|
+
where: args.where
|
|
882
|
+
});
|
|
883
|
+
for (const row of rows) {
|
|
884
|
+
await deleteRecordWithLocks(schema, modelName, model, row);
|
|
885
|
+
}
|
|
886
|
+
return rows.length;
|
|
887
|
+
},
|
|
888
|
+
async transaction(schema, run) {
|
|
889
|
+
getSupportedManifest(schema);
|
|
890
|
+
return run(driver);
|
|
891
|
+
}
|
|
892
|
+
};
|
|
893
|
+
return driver;
|
|
894
|
+
}
|
|
895
|
+
function createUnstorageDriver(config) {
|
|
896
|
+
return createUnstorageDriverInternal(config);
|
|
897
|
+
}
|
|
898
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
899
|
+
0 && (module.exports = {
|
|
900
|
+
createUnstorageDriver
|
|
901
|
+
});
|
|
902
|
+
//# sourceMappingURL=index.cjs.map
|