@farming-labs/orm-redis 0.0.43
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 +1093 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +38 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +1076 -0
- package/dist/index.js.map +1 -0
- package/package.json +33 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1093 @@
|
|
|
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
|
+
createRedisDriver: () => createRedisDriver
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var import_orm = require("@farming-labs/orm");
|
|
27
|
+
var ormKindKey = "__orm_kind";
|
|
28
|
+
var ormTargetKey = "__orm_target";
|
|
29
|
+
var ormDocIdKey = "__orm_docId";
|
|
30
|
+
var ormRecordKind = "record";
|
|
31
|
+
var ormUniqueKind = "unique";
|
|
32
|
+
var defaultBase = "orm";
|
|
33
|
+
var manifestCache = /* @__PURE__ */ new WeakMap();
|
|
34
|
+
function generateUuid() {
|
|
35
|
+
const randomUuid = globalThis.crypto?.randomUUID;
|
|
36
|
+
if (typeof randomUuid === "function") {
|
|
37
|
+
return randomUuid.call(globalThis.crypto);
|
|
38
|
+
}
|
|
39
|
+
throw new Error("The current runtime does not provide crypto.randomUUID().");
|
|
40
|
+
}
|
|
41
|
+
function getManifest(schema) {
|
|
42
|
+
const cached = manifestCache.get(schema);
|
|
43
|
+
if (cached) return cached;
|
|
44
|
+
const next = (0, import_orm.createManifest)(schema);
|
|
45
|
+
manifestCache.set(schema, next);
|
|
46
|
+
return next;
|
|
47
|
+
}
|
|
48
|
+
function isRecord(value) {
|
|
49
|
+
return !!value && typeof value === "object";
|
|
50
|
+
}
|
|
51
|
+
function hasFunction(value, name) {
|
|
52
|
+
return isRecord(value) && typeof value[name] === "function";
|
|
53
|
+
}
|
|
54
|
+
function joinRedisKey(...parts) {
|
|
55
|
+
return parts.filter((part) => part && part.length > 0).join(":");
|
|
56
|
+
}
|
|
57
|
+
function isRedisRecordItem(value) {
|
|
58
|
+
return isRecord(value) && value[ormKindKey] === ormRecordKind && typeof value[ormDocIdKey] === "string" && isRecord(value.data);
|
|
59
|
+
}
|
|
60
|
+
function isRedisUniqueItem(value) {
|
|
61
|
+
return isRecord(value) && value[ormKindKey] === ormUniqueKind && typeof value[ormTargetKey] === "string";
|
|
62
|
+
}
|
|
63
|
+
function normalizeDecimalString(value) {
|
|
64
|
+
const trimmed = value.trim();
|
|
65
|
+
const match = /^(-?\d+)(?:\.(\d+))?$/.exec(trimmed);
|
|
66
|
+
if (!match) {
|
|
67
|
+
return trimmed;
|
|
68
|
+
}
|
|
69
|
+
const [, integerPart, fractionalPart] = match;
|
|
70
|
+
if (!fractionalPart) {
|
|
71
|
+
return integerPart;
|
|
72
|
+
}
|
|
73
|
+
const normalizedFraction = fractionalPart.replace(/0+$/g, "");
|
|
74
|
+
return normalizedFraction.length ? `${integerPart}.${normalizedFraction}` : integerPart;
|
|
75
|
+
}
|
|
76
|
+
function applyDefault(value, field) {
|
|
77
|
+
if (value !== void 0) return value;
|
|
78
|
+
if (field.generated === "id") return generateUuid();
|
|
79
|
+
if (field.generated === "now") return /* @__PURE__ */ new Date();
|
|
80
|
+
if (typeof field.defaultValue === "function") {
|
|
81
|
+
return field.defaultValue();
|
|
82
|
+
}
|
|
83
|
+
return field.defaultValue;
|
|
84
|
+
}
|
|
85
|
+
function isComparable(value) {
|
|
86
|
+
return value instanceof Date || typeof value === "number" || typeof value === "string" || typeof value === "bigint";
|
|
87
|
+
}
|
|
88
|
+
function evaluateFilter(value, filter) {
|
|
89
|
+
if (!(0, import_orm.isOperatorFilterObject)(filter)) {
|
|
90
|
+
return (0, import_orm.equalValues)(value, filter);
|
|
91
|
+
}
|
|
92
|
+
if ("eq" in filter && !(0, import_orm.equalValues)(value, filter.eq)) return false;
|
|
93
|
+
if ("not" in filter && (0, import_orm.equalValues)(value, filter.not)) return false;
|
|
94
|
+
if ("in" in filter) {
|
|
95
|
+
const values = Array.isArray(filter.in) ? filter.in : [];
|
|
96
|
+
if (!values.some((candidate) => (0, import_orm.equalValues)(candidate, value))) return false;
|
|
97
|
+
}
|
|
98
|
+
if ("contains" in filter) {
|
|
99
|
+
if (typeof value !== "string" || typeof filter.contains !== "string") return false;
|
|
100
|
+
if (!value.includes(filter.contains)) return false;
|
|
101
|
+
}
|
|
102
|
+
if ("gt" in filter) {
|
|
103
|
+
if (!isComparable(value) || value <= filter.gt) return false;
|
|
104
|
+
}
|
|
105
|
+
if ("gte" in filter) {
|
|
106
|
+
if (!isComparable(value) || value < filter.gte) return false;
|
|
107
|
+
}
|
|
108
|
+
if ("lt" in filter) {
|
|
109
|
+
if (!isComparable(value) || value >= filter.lt) return false;
|
|
110
|
+
}
|
|
111
|
+
if ("lte" in filter) {
|
|
112
|
+
if (!isComparable(value) || value > filter.lte) return false;
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
function sortRows(rows, orderBy) {
|
|
117
|
+
if (!orderBy) return rows;
|
|
118
|
+
const entries = Object.entries(orderBy);
|
|
119
|
+
if (!entries.length) return rows;
|
|
120
|
+
return [...rows].sort((left, right) => {
|
|
121
|
+
for (const [field, direction] of entries) {
|
|
122
|
+
const a = left.data[field];
|
|
123
|
+
const b = right.data[field];
|
|
124
|
+
if ((0, import_orm.equalValues)(a, b)) continue;
|
|
125
|
+
if (a === void 0) return direction === "asc" ? -1 : 1;
|
|
126
|
+
if (b === void 0) return direction === "asc" ? 1 : -1;
|
|
127
|
+
if (a == null) return direction === "asc" ? -1 : 1;
|
|
128
|
+
if (b == null) return direction === "asc" ? 1 : -1;
|
|
129
|
+
if (a < b) return direction === "asc" ? -1 : 1;
|
|
130
|
+
if (a > b) return direction === "asc" ? 1 : -1;
|
|
131
|
+
}
|
|
132
|
+
return 0;
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
function pageRows(rows, skip, take) {
|
|
136
|
+
const start = skip ?? 0;
|
|
137
|
+
const end = take === void 0 ? void 0 : start + take;
|
|
138
|
+
return rows.slice(start, end);
|
|
139
|
+
}
|
|
140
|
+
function redisConstraintError(target) {
|
|
141
|
+
const fields = Array.isArray(target) ? target.join(", ") : target ?? "unique fields";
|
|
142
|
+
const error = new Error(`Redis unique constraint violation on ${fields}.`);
|
|
143
|
+
Object.assign(error, {
|
|
144
|
+
name: "RedisUniqueConstraintError",
|
|
145
|
+
code: "REDIS_UNIQUE_CONSTRAINT",
|
|
146
|
+
target
|
|
147
|
+
});
|
|
148
|
+
return error;
|
|
149
|
+
}
|
|
150
|
+
function normalizeNxResult(result) {
|
|
151
|
+
if (typeof result === "boolean") return result;
|
|
152
|
+
if (typeof result === "number") return result === 1;
|
|
153
|
+
if (typeof result === "string") {
|
|
154
|
+
const normalized = result.trim().toUpperCase();
|
|
155
|
+
return normalized === "OK" || normalized === "1" || normalized === "TRUE";
|
|
156
|
+
}
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
async function callRedisCommand(client, names, ...args) {
|
|
160
|
+
const record = client;
|
|
161
|
+
for (const name of names) {
|
|
162
|
+
if (hasFunction(client, name)) {
|
|
163
|
+
return await record[name](...args);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
throw new Error(`The Redis runtime client is missing required command support: ${names[0]}.`);
|
|
167
|
+
}
|
|
168
|
+
function normalizeStoredString(value) {
|
|
169
|
+
if (value === void 0 || value === null) return null;
|
|
170
|
+
if (typeof value === "string") return value;
|
|
171
|
+
if (value instanceof Uint8Array) {
|
|
172
|
+
return new TextDecoder().decode(value);
|
|
173
|
+
}
|
|
174
|
+
return JSON.stringify(value);
|
|
175
|
+
}
|
|
176
|
+
function parseStoredJson(raw) {
|
|
177
|
+
if (raw === void 0 || raw === null) return null;
|
|
178
|
+
if (typeof raw === "string") {
|
|
179
|
+
try {
|
|
180
|
+
return JSON.parse(raw);
|
|
181
|
+
} catch {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (isRecord(raw)) {
|
|
186
|
+
return raw;
|
|
187
|
+
}
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
function normalizeKeyList(value) {
|
|
191
|
+
if (Array.isArray(value)) {
|
|
192
|
+
if (value.length >= 2 && Array.isArray(value[1])) {
|
|
193
|
+
return value[1].map((entry) => String(entry));
|
|
194
|
+
}
|
|
195
|
+
return value.map((entry) => String(entry));
|
|
196
|
+
}
|
|
197
|
+
if (isRecord(value) && Array.isArray(value.keys)) {
|
|
198
|
+
return value.keys.map((entry) => String(entry));
|
|
199
|
+
}
|
|
200
|
+
return [];
|
|
201
|
+
}
|
|
202
|
+
function escapePatternCharacter(character) {
|
|
203
|
+
return /[\\^$+?.()|[\]{}]/.test(character) ? `\\${character}` : character;
|
|
204
|
+
}
|
|
205
|
+
function matchesRedisPattern(key, pattern) {
|
|
206
|
+
const source = Array.from(pattern).map((character) => {
|
|
207
|
+
if (character === "*") return ".*";
|
|
208
|
+
if (character === "?") return ".";
|
|
209
|
+
return escapePatternCharacter(character);
|
|
210
|
+
}).join("");
|
|
211
|
+
return new RegExp(`^${source}$`).test(key);
|
|
212
|
+
}
|
|
213
|
+
function extractScanCursor(value) {
|
|
214
|
+
if (Array.isArray(value) && value.length >= 2) {
|
|
215
|
+
return String(value[0] ?? "0");
|
|
216
|
+
}
|
|
217
|
+
if (isRecord(value) && "cursor" in value) {
|
|
218
|
+
return String(value.cursor ?? "0");
|
|
219
|
+
}
|
|
220
|
+
return "0";
|
|
221
|
+
}
|
|
222
|
+
async function getString(client, key) {
|
|
223
|
+
const value = await callRedisCommand(client, ["get"], key);
|
|
224
|
+
return normalizeStoredString(value);
|
|
225
|
+
}
|
|
226
|
+
async function setString(client, key, value) {
|
|
227
|
+
await callRedisCommand(client, ["set"], key, value);
|
|
228
|
+
}
|
|
229
|
+
async function setStringNx(client, key, value) {
|
|
230
|
+
if (hasFunction(client, "setNX")) {
|
|
231
|
+
return normalizeNxResult(await client.setNX(key, value));
|
|
232
|
+
}
|
|
233
|
+
if (hasFunction(client, "setnx")) {
|
|
234
|
+
return normalizeNxResult(await client.setnx(key, value));
|
|
235
|
+
}
|
|
236
|
+
if (hasFunction(client, "set")) {
|
|
237
|
+
const attempts = [
|
|
238
|
+
[key, value, { NX: true }],
|
|
239
|
+
[key, value, { nx: true }],
|
|
240
|
+
[key, value, "NX"]
|
|
241
|
+
];
|
|
242
|
+
for (const args of attempts) {
|
|
243
|
+
try {
|
|
244
|
+
const result = await client.set(...args);
|
|
245
|
+
if (result === null || result === void 0) {
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
return normalizeNxResult(result);
|
|
249
|
+
} catch {
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (hasFunction(client, "sendCommand")) {
|
|
254
|
+
return normalizeNxResult(await client.sendCommand(["SET", key, value, "NX"]));
|
|
255
|
+
}
|
|
256
|
+
if (await getString(client, key) !== null) {
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
await setString(client, key, value);
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
async function deleteKey(client, key) {
|
|
263
|
+
if (hasFunction(client, "unlink")) {
|
|
264
|
+
await client.unlink(key);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
await callRedisCommand(client, ["del"], key);
|
|
268
|
+
}
|
|
269
|
+
async function listKeys(client, pattern) {
|
|
270
|
+
if (hasFunction(client, "keys")) {
|
|
271
|
+
return normalizeKeyList(await client.keys(pattern));
|
|
272
|
+
}
|
|
273
|
+
if (hasFunction(client, "scanIterator")) {
|
|
274
|
+
const keys = [];
|
|
275
|
+
for await (const item of client.scanIterator({ MATCH: pattern, COUNT: 100 })) {
|
|
276
|
+
keys.push(String(item));
|
|
277
|
+
}
|
|
278
|
+
return keys;
|
|
279
|
+
}
|
|
280
|
+
if (hasFunction(client, "scan")) {
|
|
281
|
+
let cursor = "0";
|
|
282
|
+
const keys = [];
|
|
283
|
+
const scanAttempts = hasFunction(client, "request") ? [
|
|
284
|
+
{ match: pattern, count: 100 },
|
|
285
|
+
{ MATCH: pattern, COUNT: 100 }
|
|
286
|
+
] : [
|
|
287
|
+
{ MATCH: pattern, COUNT: 100 },
|
|
288
|
+
{ match: pattern, count: 100 }
|
|
289
|
+
];
|
|
290
|
+
do {
|
|
291
|
+
let result;
|
|
292
|
+
for (const options of scanAttempts) {
|
|
293
|
+
try {
|
|
294
|
+
result = await client.scan(cursor, options);
|
|
295
|
+
break;
|
|
296
|
+
} catch {
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
if (result === void 0) {
|
|
300
|
+
throw new Error("The Redis runtime client scan() command could not be called.");
|
|
301
|
+
}
|
|
302
|
+
keys.push(...normalizeKeyList(result).filter((key) => matchesRedisPattern(key, pattern)));
|
|
303
|
+
cursor = extractScanCursor(result);
|
|
304
|
+
} while (cursor !== "0");
|
|
305
|
+
return keys;
|
|
306
|
+
}
|
|
307
|
+
throw new Error("The Redis runtime client must provide keys(), scanIterator(), or scan().");
|
|
308
|
+
}
|
|
309
|
+
function createRedisDriverInternal(config) {
|
|
310
|
+
function getSupportedManifest(schema) {
|
|
311
|
+
const manifest = getManifest(schema);
|
|
312
|
+
for (const model of Object.values(manifest.models)) {
|
|
313
|
+
if (model.schema) {
|
|
314
|
+
throw new Error(
|
|
315
|
+
`The Redis runtime does not support schema-qualified tables for model "${model.name}". Use flat table names instead.`
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
const idField = model.fields.id;
|
|
319
|
+
if (idField?.kind === "id" && idField.idType === "integer" && idField.generated === "increment") {
|
|
320
|
+
throw new Error(
|
|
321
|
+
`The Redis runtime does not support generated integer ids for model "${model.name}". Use manual numeric ids or a string id instead.`
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return manifest;
|
|
326
|
+
}
|
|
327
|
+
function getModelBase(schema, modelName) {
|
|
328
|
+
const manifest = getSupportedManifest(schema);
|
|
329
|
+
return config.prefixes?.[modelName] ?? joinRedisKey(config.base ?? defaultBase, manifest.models[modelName].table);
|
|
330
|
+
}
|
|
331
|
+
function recordBase(schema, modelName) {
|
|
332
|
+
return joinRedisKey(getModelBase(schema, modelName), "record");
|
|
333
|
+
}
|
|
334
|
+
function recordKey(schema, modelName, docId) {
|
|
335
|
+
return joinRedisKey(recordBase(schema, modelName), encodeURIComponent(docId));
|
|
336
|
+
}
|
|
337
|
+
function fieldTransform(modelName, fieldName) {
|
|
338
|
+
return config.transforms?.[modelName]?.[fieldName];
|
|
339
|
+
}
|
|
340
|
+
function encodeValue(modelName, field, value) {
|
|
341
|
+
if (value === void 0) return value;
|
|
342
|
+
if (value === null) return null;
|
|
343
|
+
const transform = fieldTransform(modelName, field.name);
|
|
344
|
+
if (transform?.encode) {
|
|
345
|
+
return transform.encode(value);
|
|
346
|
+
}
|
|
347
|
+
if (field.kind === "id" && field.idType === "integer") {
|
|
348
|
+
return Number(value);
|
|
349
|
+
}
|
|
350
|
+
if (field.kind === "enum") {
|
|
351
|
+
return String(value);
|
|
352
|
+
}
|
|
353
|
+
if (field.kind === "boolean") {
|
|
354
|
+
return Boolean(value);
|
|
355
|
+
}
|
|
356
|
+
if (field.kind === "integer") {
|
|
357
|
+
return Number(value);
|
|
358
|
+
}
|
|
359
|
+
if (field.kind === "bigint") {
|
|
360
|
+
return typeof value === "bigint" ? value.toString() : BigInt(value).toString();
|
|
361
|
+
}
|
|
362
|
+
if (field.kind === "decimal") {
|
|
363
|
+
return typeof value === "string" ? normalizeDecimalString(value) : String(value);
|
|
364
|
+
}
|
|
365
|
+
if (field.kind === "datetime") {
|
|
366
|
+
if (value instanceof Date) return value.toISOString();
|
|
367
|
+
return new Date(value).toISOString();
|
|
368
|
+
}
|
|
369
|
+
return value;
|
|
370
|
+
}
|
|
371
|
+
function decodeValue(modelName, field, value, docId) {
|
|
372
|
+
const transform = fieldTransform(modelName, field.name);
|
|
373
|
+
if (transform?.decode) {
|
|
374
|
+
return transform.decode(value);
|
|
375
|
+
}
|
|
376
|
+
if (value === void 0 && field.kind === "id" && docId !== void 0) {
|
|
377
|
+
if (field.idType === "integer") {
|
|
378
|
+
const numeric = Number(docId);
|
|
379
|
+
return Number.isFinite(numeric) ? numeric : void 0;
|
|
380
|
+
}
|
|
381
|
+
return docId;
|
|
382
|
+
}
|
|
383
|
+
if (value === void 0 || value === null) return value ?? null;
|
|
384
|
+
if (field.kind === "id" && field.idType === "integer") {
|
|
385
|
+
return Number(value);
|
|
386
|
+
}
|
|
387
|
+
if (field.kind === "enum") {
|
|
388
|
+
return String(value);
|
|
389
|
+
}
|
|
390
|
+
if (field.kind === "boolean") {
|
|
391
|
+
return Boolean(value);
|
|
392
|
+
}
|
|
393
|
+
if (field.kind === "integer") {
|
|
394
|
+
return Number(value);
|
|
395
|
+
}
|
|
396
|
+
if (field.kind === "bigint") {
|
|
397
|
+
return typeof value === "bigint" ? value : BigInt(value);
|
|
398
|
+
}
|
|
399
|
+
if (field.kind === "decimal") {
|
|
400
|
+
return normalizeDecimalString(String(value));
|
|
401
|
+
}
|
|
402
|
+
if (field.kind === "datetime") {
|
|
403
|
+
return value instanceof Date ? value : new Date(value);
|
|
404
|
+
}
|
|
405
|
+
return value;
|
|
406
|
+
}
|
|
407
|
+
function buildStoredRow(model, data) {
|
|
408
|
+
const stored = {};
|
|
409
|
+
const decoded = {};
|
|
410
|
+
for (const field of Object.values(model.fields)) {
|
|
411
|
+
const value = applyDefault(data[field.name], field);
|
|
412
|
+
if (value === void 0) continue;
|
|
413
|
+
const encoded = encodeValue(model.name, field, value);
|
|
414
|
+
stored[field.column] = encoded;
|
|
415
|
+
decoded[field.name] = decodeValue(model.name, field, encoded);
|
|
416
|
+
}
|
|
417
|
+
const idField = model.fields.id;
|
|
418
|
+
if (!idField) {
|
|
419
|
+
return {
|
|
420
|
+
docId: generateUuid(),
|
|
421
|
+
stored,
|
|
422
|
+
decoded
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
let idValue = decoded[idField.name];
|
|
426
|
+
if (idValue === void 0 || idValue === null) {
|
|
427
|
+
if (idField.idType === "integer") {
|
|
428
|
+
throw new Error(
|
|
429
|
+
`The Redis runtime requires an explicit numeric id for model "${model.name}" when using manual integer ids.`
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
idValue = generateUuid();
|
|
433
|
+
const encodedId = encodeValue(model.name, idField, idValue);
|
|
434
|
+
stored[idField.column] = encodedId;
|
|
435
|
+
decoded[idField.name] = decodeValue(model.name, idField, encodedId);
|
|
436
|
+
idValue = decoded[idField.name];
|
|
437
|
+
}
|
|
438
|
+
return {
|
|
439
|
+
docId: String(idValue),
|
|
440
|
+
stored,
|
|
441
|
+
decoded
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
function buildUpdatedRow(model, current, data) {
|
|
445
|
+
const stored = {
|
|
446
|
+
...current.stored
|
|
447
|
+
};
|
|
448
|
+
const decoded = {
|
|
449
|
+
...current.data
|
|
450
|
+
};
|
|
451
|
+
for (const [fieldName, value] of Object.entries(data)) {
|
|
452
|
+
if (value === void 0) continue;
|
|
453
|
+
const field = model.fields[fieldName];
|
|
454
|
+
if (!field) {
|
|
455
|
+
throw new Error(`Unknown field "${fieldName}" on model "${model.name}".`);
|
|
456
|
+
}
|
|
457
|
+
if (field.name === "id" && !(0, import_orm.equalValues)(current.data.id, value)) {
|
|
458
|
+
throw new Error(
|
|
459
|
+
`The Redis runtime does not support updating the id field for model "${model.name}".`
|
|
460
|
+
);
|
|
461
|
+
}
|
|
462
|
+
const encoded = encodeValue(model.name, field, value);
|
|
463
|
+
stored[field.column] = encoded;
|
|
464
|
+
decoded[field.name] = decodeValue(model.name, field, encoded);
|
|
465
|
+
}
|
|
466
|
+
return {
|
|
467
|
+
docId: current.docId,
|
|
468
|
+
stored,
|
|
469
|
+
decoded
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
async function loadRowByKey(model, key) {
|
|
473
|
+
const item = parseStoredJson(await getString(config.client, key));
|
|
474
|
+
if (!isRedisRecordItem(item)) {
|
|
475
|
+
return null;
|
|
476
|
+
}
|
|
477
|
+
const decoded = {};
|
|
478
|
+
for (const field of Object.values(model.fields)) {
|
|
479
|
+
decoded[field.name] = decodeValue(
|
|
480
|
+
model.name,
|
|
481
|
+
field,
|
|
482
|
+
item.data[field.column],
|
|
483
|
+
item.__orm_docId
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
return {
|
|
487
|
+
docId: item.__orm_docId,
|
|
488
|
+
key,
|
|
489
|
+
data: decoded,
|
|
490
|
+
stored: item.data
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
async function loadRows(schema, modelName) {
|
|
494
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
495
|
+
const keys = await listKeys(config.client, `${recordBase(schema, modelName)}:*`);
|
|
496
|
+
const rows = await Promise.all(keys.map((key) => loadRowByKey(model, key)));
|
|
497
|
+
return rows.filter((row) => !!row);
|
|
498
|
+
}
|
|
499
|
+
function normalizeFilterValue(model, fieldName, value) {
|
|
500
|
+
const field = model.fields[fieldName];
|
|
501
|
+
if (!field || value === void 0 || value === null) {
|
|
502
|
+
return value;
|
|
503
|
+
}
|
|
504
|
+
return decodeValue(model.name, field, encodeValue(model.name, field, value));
|
|
505
|
+
}
|
|
506
|
+
function evaluateModelFilter(model, fieldName, value, filter) {
|
|
507
|
+
if (!(0, import_orm.isOperatorFilterObject)(filter)) {
|
|
508
|
+
return (0, import_orm.equalValues)(value, normalizeFilterValue(model, fieldName, filter));
|
|
509
|
+
}
|
|
510
|
+
const normalized = {
|
|
511
|
+
...filter.eq !== void 0 ? { eq: normalizeFilterValue(model, fieldName, filter.eq) } : {},
|
|
512
|
+
...filter.not !== void 0 ? { not: normalizeFilterValue(model, fieldName, filter.not) } : {},
|
|
513
|
+
...filter.in !== void 0 ? {
|
|
514
|
+
in: (Array.isArray(filter.in) ? filter.in : []).map(
|
|
515
|
+
(entry) => normalizeFilterValue(model, fieldName, entry)
|
|
516
|
+
)
|
|
517
|
+
} : {},
|
|
518
|
+
...filter.contains !== void 0 ? { contains: String(filter.contains) } : {},
|
|
519
|
+
...filter.gt !== void 0 ? { gt: normalizeFilterValue(model, fieldName, filter.gt) } : {},
|
|
520
|
+
...filter.gte !== void 0 ? { gte: normalizeFilterValue(model, fieldName, filter.gte) } : {},
|
|
521
|
+
...filter.lt !== void 0 ? { lt: normalizeFilterValue(model, fieldName, filter.lt) } : {},
|
|
522
|
+
...filter.lte !== void 0 ? { lte: normalizeFilterValue(model, fieldName, filter.lte) } : {}
|
|
523
|
+
};
|
|
524
|
+
return evaluateFilter(value, normalized);
|
|
525
|
+
}
|
|
526
|
+
function matchesModelWhere(model, record, where) {
|
|
527
|
+
if (!where) return true;
|
|
528
|
+
if (where.AND && !where.AND.every((clause) => matchesModelWhere(model, record, clause))) {
|
|
529
|
+
return false;
|
|
530
|
+
}
|
|
531
|
+
if (where.OR && !where.OR.some((clause) => matchesModelWhere(model, record, clause))) {
|
|
532
|
+
return false;
|
|
533
|
+
}
|
|
534
|
+
if (where.NOT && matchesModelWhere(model, record, where.NOT)) {
|
|
535
|
+
return false;
|
|
536
|
+
}
|
|
537
|
+
for (const [key, filter] of Object.entries(where)) {
|
|
538
|
+
if (key === "AND" || key === "OR" || key === "NOT") continue;
|
|
539
|
+
if (!evaluateModelFilter(model, key, record[key], filter)) return false;
|
|
540
|
+
}
|
|
541
|
+
return true;
|
|
542
|
+
}
|
|
543
|
+
function applyModelQuery(model, rows, args = {}) {
|
|
544
|
+
const filtered = rows.filter((row) => matchesModelWhere(model, row.data, args.where));
|
|
545
|
+
const sorted = sortRows(filtered, args.orderBy);
|
|
546
|
+
return pageRows(sorted, args.skip, args.take);
|
|
547
|
+
}
|
|
548
|
+
function serializeUniqueValue(model, fieldName, value) {
|
|
549
|
+
const field = model.fields[fieldName];
|
|
550
|
+
const normalized = decodeValue(model.name, field, encodeValue(model.name, field, value));
|
|
551
|
+
if (normalized instanceof Date) {
|
|
552
|
+
return normalized.toISOString();
|
|
553
|
+
}
|
|
554
|
+
if (typeof normalized === "bigint") {
|
|
555
|
+
return normalized.toString();
|
|
556
|
+
}
|
|
557
|
+
return JSON.stringify(normalized);
|
|
558
|
+
}
|
|
559
|
+
function uniqueLockKey(schema, model, fields, row) {
|
|
560
|
+
const values = fields.map((fieldName) => row[fieldName]);
|
|
561
|
+
if (values.some((value) => value === void 0 || value === null)) {
|
|
562
|
+
return null;
|
|
563
|
+
}
|
|
564
|
+
return joinRedisKey(
|
|
565
|
+
getModelBase(schema, model.name),
|
|
566
|
+
"unique",
|
|
567
|
+
fields.join("+"),
|
|
568
|
+
...fields.map(
|
|
569
|
+
(fieldName) => encodeURIComponent(serializeUniqueValue(model, fieldName, row[fieldName]))
|
|
570
|
+
)
|
|
571
|
+
);
|
|
572
|
+
}
|
|
573
|
+
function uniqueLocksForRow(schema, model, row, docId) {
|
|
574
|
+
const target = recordKey(schema, model.name, docId);
|
|
575
|
+
const locks = [];
|
|
576
|
+
for (const field of Object.values(model.fields)) {
|
|
577
|
+
if (!field.unique) continue;
|
|
578
|
+
const key = uniqueLockKey(schema, model, [field.name], row);
|
|
579
|
+
if (!key) continue;
|
|
580
|
+
locks.push({
|
|
581
|
+
key,
|
|
582
|
+
target,
|
|
583
|
+
fields: [field.name]
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
for (const constraint of model.constraints.unique) {
|
|
587
|
+
const key = uniqueLockKey(schema, model, [...constraint.fields], row);
|
|
588
|
+
if (!key) continue;
|
|
589
|
+
locks.push({
|
|
590
|
+
key,
|
|
591
|
+
target,
|
|
592
|
+
fields: [...constraint.fields]
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
return locks;
|
|
596
|
+
}
|
|
597
|
+
async function loadUniqueRow(schema, modelName, where) {
|
|
598
|
+
const manifest = getSupportedManifest(schema);
|
|
599
|
+
const model = manifest.models[modelName];
|
|
600
|
+
const lookup = (0, import_orm.requireUniqueLookup)(model, where, "FindUnique");
|
|
601
|
+
if (lookup.kind === "id") {
|
|
602
|
+
return loadRowByKey(
|
|
603
|
+
model,
|
|
604
|
+
recordKey(schema, modelName, String(lookup.values[lookup.fields[0].name]))
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
const normalizedLookupRow = Object.fromEntries(
|
|
608
|
+
lookup.fields.map((field) => [
|
|
609
|
+
field.name,
|
|
610
|
+
decodeValue(model.name, field, encodeValue(model.name, field, lookup.values[field.name]))
|
|
611
|
+
])
|
|
612
|
+
);
|
|
613
|
+
const key = uniqueLockKey(
|
|
614
|
+
schema,
|
|
615
|
+
model,
|
|
616
|
+
lookup.fields.map((field) => field.name),
|
|
617
|
+
normalizedLookupRow
|
|
618
|
+
);
|
|
619
|
+
if (!key) {
|
|
620
|
+
return null;
|
|
621
|
+
}
|
|
622
|
+
const lock = parseStoredJson(await getString(config.client, key));
|
|
623
|
+
if (!isRedisUniqueItem(lock)) {
|
|
624
|
+
return null;
|
|
625
|
+
}
|
|
626
|
+
return loadRowByKey(model, lock.__orm_target);
|
|
627
|
+
}
|
|
628
|
+
function findUniqueConflict(model, candidate, existingRows, ignoreDocIds = /* @__PURE__ */ new Set()) {
|
|
629
|
+
const idField = model.fields.id;
|
|
630
|
+
for (const row of existingRows) {
|
|
631
|
+
if (ignoreDocIds.has(row.docId)) continue;
|
|
632
|
+
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])) {
|
|
633
|
+
return [idField.name];
|
|
634
|
+
}
|
|
635
|
+
for (const field of Object.values(model.fields)) {
|
|
636
|
+
if (!field.unique) continue;
|
|
637
|
+
if (candidate[field.name] === void 0 || candidate[field.name] === null) continue;
|
|
638
|
+
if (row.data[field.name] === void 0 || row.data[field.name] === null) continue;
|
|
639
|
+
if ((0, import_orm.equalValues)(candidate[field.name], row.data[field.name])) {
|
|
640
|
+
return [field.name];
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
for (const constraint of model.constraints.unique) {
|
|
644
|
+
if (!constraint.fields.every(
|
|
645
|
+
(fieldName) => candidate[fieldName] !== void 0 && candidate[fieldName] !== null && row.data[fieldName] !== void 0 && row.data[fieldName] !== null
|
|
646
|
+
)) {
|
|
647
|
+
continue;
|
|
648
|
+
}
|
|
649
|
+
if (constraint.fields.every(
|
|
650
|
+
(fieldName) => (0, import_orm.equalValues)(candidate[fieldName], row.data[fieldName])
|
|
651
|
+
)) {
|
|
652
|
+
return [...constraint.fields];
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
return null;
|
|
657
|
+
}
|
|
658
|
+
function serializeItem(value) {
|
|
659
|
+
return JSON.stringify(value);
|
|
660
|
+
}
|
|
661
|
+
function buildRecordItem(row) {
|
|
662
|
+
return {
|
|
663
|
+
__orm_kind: ormRecordKind,
|
|
664
|
+
__orm_docId: row.docId,
|
|
665
|
+
data: row.stored
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
async function putRecordSequential(schema, modelName, row, conditionallyCreate) {
|
|
669
|
+
const key = recordKey(schema, modelName, row.docId);
|
|
670
|
+
const payload = serializeItem(buildRecordItem(row));
|
|
671
|
+
if (conditionallyCreate) {
|
|
672
|
+
if (!await setStringNx(config.client, key, payload)) {
|
|
673
|
+
throw redisConstraintError(["id"]);
|
|
674
|
+
}
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
await setString(config.client, key, payload);
|
|
678
|
+
}
|
|
679
|
+
async function putUniqueSequential(lock) {
|
|
680
|
+
const existing = parseStoredJson(await getString(config.client, lock.key));
|
|
681
|
+
if (isRedisUniqueItem(existing)) {
|
|
682
|
+
if (existing.__orm_target !== lock.target) {
|
|
683
|
+
throw redisConstraintError(lock.fields);
|
|
684
|
+
}
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
const written = await setStringNx(
|
|
688
|
+
config.client,
|
|
689
|
+
lock.key,
|
|
690
|
+
serializeItem({
|
|
691
|
+
__orm_kind: ormUniqueKind,
|
|
692
|
+
__orm_target: lock.target
|
|
693
|
+
})
|
|
694
|
+
);
|
|
695
|
+
if (written) {
|
|
696
|
+
return;
|
|
697
|
+
}
|
|
698
|
+
const current = parseStoredJson(await getString(config.client, lock.key));
|
|
699
|
+
if (isRedisUniqueItem(current) && current.__orm_target === lock.target) {
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
throw redisConstraintError(lock.fields);
|
|
703
|
+
}
|
|
704
|
+
async function deleteSequential(key) {
|
|
705
|
+
await deleteKey(config.client, key);
|
|
706
|
+
}
|
|
707
|
+
async function acquireUniqueLocksSequential(locks) {
|
|
708
|
+
const acquired = [];
|
|
709
|
+
try {
|
|
710
|
+
for (const lock of locks) {
|
|
711
|
+
await putUniqueSequential(lock);
|
|
712
|
+
acquired.push(lock);
|
|
713
|
+
}
|
|
714
|
+
} catch (error) {
|
|
715
|
+
await releaseUniqueLocksSequential(acquired);
|
|
716
|
+
throw error;
|
|
717
|
+
}
|
|
718
|
+
return acquired;
|
|
719
|
+
}
|
|
720
|
+
async function releaseUniqueLocksSequential(locks) {
|
|
721
|
+
for (const lock of [...locks].reverse()) {
|
|
722
|
+
try {
|
|
723
|
+
await deleteSequential(lock.key);
|
|
724
|
+
} catch {
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
async function createRecordWithLocks(schema, modelName, row, model) {
|
|
729
|
+
const locks = uniqueLocksForRow(schema, model, row.decoded, row.docId);
|
|
730
|
+
const acquired = await acquireUniqueLocksSequential(locks);
|
|
731
|
+
try {
|
|
732
|
+
await putRecordSequential(schema, modelName, row, true);
|
|
733
|
+
} catch (error) {
|
|
734
|
+
await releaseUniqueLocksSequential(acquired);
|
|
735
|
+
throw error;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
async function updateRecordWithLocks(schema, modelName, model, current, next) {
|
|
739
|
+
const currentLocks = new Map(
|
|
740
|
+
uniqueLocksForRow(schema, model, current.data, current.docId).map((lock) => [lock.key, lock])
|
|
741
|
+
);
|
|
742
|
+
const nextLocks = new Map(
|
|
743
|
+
uniqueLocksForRow(schema, model, next.decoded, next.docId).map((lock) => [lock.key, lock])
|
|
744
|
+
);
|
|
745
|
+
const addedLocks = [...nextLocks.values()].filter((lock) => !currentLocks.has(lock.key));
|
|
746
|
+
const removedLocks = [...currentLocks.values()].filter((lock) => !nextLocks.has(lock.key));
|
|
747
|
+
const acquired = await acquireUniqueLocksSequential(addedLocks);
|
|
748
|
+
try {
|
|
749
|
+
await putRecordSequential(schema, modelName, next, false);
|
|
750
|
+
} catch (error) {
|
|
751
|
+
await releaseUniqueLocksSequential(acquired);
|
|
752
|
+
throw error;
|
|
753
|
+
}
|
|
754
|
+
for (const lock of removedLocks) {
|
|
755
|
+
await deleteSequential(lock.key);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
async function deleteRecordWithLocks(schema, modelName, model, current) {
|
|
759
|
+
await deleteSequential(recordKey(schema, modelName, current.docId));
|
|
760
|
+
const locks = uniqueLocksForRow(schema, model, current.data, current.docId);
|
|
761
|
+
for (const lock of locks) {
|
|
762
|
+
await deleteSequential(lock.key);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
async function projectRow(schema, modelName, row, select) {
|
|
766
|
+
const modelDefinition = schema.models[modelName];
|
|
767
|
+
const output = {};
|
|
768
|
+
if (!select) {
|
|
769
|
+
for (const fieldName of Object.keys(modelDefinition.fields)) {
|
|
770
|
+
output[fieldName] = row[fieldName];
|
|
771
|
+
}
|
|
772
|
+
return output;
|
|
773
|
+
}
|
|
774
|
+
for (const [key, value] of Object.entries(select)) {
|
|
775
|
+
if (!value) continue;
|
|
776
|
+
if (key in modelDefinition.fields && value === true) {
|
|
777
|
+
output[key] = row[key];
|
|
778
|
+
continue;
|
|
779
|
+
}
|
|
780
|
+
if (key in modelDefinition.relations) {
|
|
781
|
+
output[key] = await resolveRelation(
|
|
782
|
+
schema,
|
|
783
|
+
modelName,
|
|
784
|
+
key,
|
|
785
|
+
row,
|
|
786
|
+
value
|
|
787
|
+
);
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
return output;
|
|
791
|
+
}
|
|
792
|
+
async function resolveRelation(schema, modelName, relationName, row, value) {
|
|
793
|
+
const relation = schema.models[modelName].relations[relationName];
|
|
794
|
+
const relationArgs = value === true ? {} : value;
|
|
795
|
+
if (relation.kind === "belongsTo") {
|
|
796
|
+
const foreignValue = row[relation.foreignKey];
|
|
797
|
+
const targetRows2 = (await loadRows(schema, relation.target)).filter(
|
|
798
|
+
(item) => (0, import_orm.equalValues)(item.data.id, foreignValue)
|
|
799
|
+
);
|
|
800
|
+
const target = applyModelQuery(
|
|
801
|
+
relationTargetManifest(schema, relation.target),
|
|
802
|
+
targetRows2,
|
|
803
|
+
relationArgs
|
|
804
|
+
)[0];
|
|
805
|
+
return target ? projectRow(
|
|
806
|
+
schema,
|
|
807
|
+
relation.target,
|
|
808
|
+
target.data,
|
|
809
|
+
relationArgs.select
|
|
810
|
+
) : null;
|
|
811
|
+
}
|
|
812
|
+
if (relation.kind === "hasOne") {
|
|
813
|
+
const targetRows2 = (await loadRows(schema, relation.target)).filter(
|
|
814
|
+
(item) => (0, import_orm.equalValues)(item.data[relation.foreignKey], row.id)
|
|
815
|
+
);
|
|
816
|
+
const target = applyModelQuery(
|
|
817
|
+
relationTargetManifest(schema, relation.target),
|
|
818
|
+
targetRows2,
|
|
819
|
+
relationArgs
|
|
820
|
+
)[0];
|
|
821
|
+
return target ? projectRow(
|
|
822
|
+
schema,
|
|
823
|
+
relation.target,
|
|
824
|
+
target.data,
|
|
825
|
+
relationArgs.select
|
|
826
|
+
) : null;
|
|
827
|
+
}
|
|
828
|
+
if (relation.kind === "hasMany") {
|
|
829
|
+
const targetRows2 = (await loadRows(schema, relation.target)).filter(
|
|
830
|
+
(item) => (0, import_orm.equalValues)(item.data[relation.foreignKey], row.id)
|
|
831
|
+
);
|
|
832
|
+
const matchedRows2 = applyModelQuery(
|
|
833
|
+
relationTargetManifest(schema, relation.target),
|
|
834
|
+
targetRows2,
|
|
835
|
+
relationArgs
|
|
836
|
+
);
|
|
837
|
+
return Promise.all(
|
|
838
|
+
matchedRows2.map(
|
|
839
|
+
(item) => projectRow(schema, relation.target, item.data, relationArgs.select)
|
|
840
|
+
)
|
|
841
|
+
);
|
|
842
|
+
}
|
|
843
|
+
const throughRows = (await loadRows(schema, relation.through)).filter(
|
|
844
|
+
(item) => (0, import_orm.equalValues)(item.data[relation.from], row.id)
|
|
845
|
+
);
|
|
846
|
+
const targetIds = throughRows.map((item) => item.data[relation.to]);
|
|
847
|
+
const targetRows = (await loadRows(schema, relation.target)).filter(
|
|
848
|
+
(item) => targetIds.some((targetId) => (0, import_orm.equalValues)(targetId, item.data.id))
|
|
849
|
+
);
|
|
850
|
+
const matchedRows = applyModelQuery(
|
|
851
|
+
relationTargetManifest(schema, relation.target),
|
|
852
|
+
targetRows,
|
|
853
|
+
relationArgs
|
|
854
|
+
);
|
|
855
|
+
return Promise.all(
|
|
856
|
+
matchedRows.map(
|
|
857
|
+
(item) => projectRow(schema, relation.target, item.data, relationArgs.select)
|
|
858
|
+
)
|
|
859
|
+
);
|
|
860
|
+
}
|
|
861
|
+
function relationTargetManifest(schema, modelName) {
|
|
862
|
+
return getSupportedManifest(schema).models[modelName];
|
|
863
|
+
}
|
|
864
|
+
let driver;
|
|
865
|
+
driver = {
|
|
866
|
+
handle: (0, import_orm.createDriverHandle)({
|
|
867
|
+
kind: "redis",
|
|
868
|
+
client: {
|
|
869
|
+
client: config.client,
|
|
870
|
+
base: config.base,
|
|
871
|
+
prefixes: config.prefixes
|
|
872
|
+
},
|
|
873
|
+
capabilities: {
|
|
874
|
+
supportsNumericIds: true,
|
|
875
|
+
numericIds: "manual",
|
|
876
|
+
supportsJSON: true,
|
|
877
|
+
supportsDates: true,
|
|
878
|
+
supportsBooleans: true,
|
|
879
|
+
supportsTransactions: false,
|
|
880
|
+
supportsSchemaNamespaces: false,
|
|
881
|
+
supportsTransactionalDDL: false,
|
|
882
|
+
supportsJoin: false,
|
|
883
|
+
nativeRelationLoading: "none",
|
|
884
|
+
textComparison: "case-sensitive",
|
|
885
|
+
textMatching: {
|
|
886
|
+
equality: "case-sensitive",
|
|
887
|
+
contains: "case-sensitive",
|
|
888
|
+
ordering: "case-sensitive"
|
|
889
|
+
},
|
|
890
|
+
upsert: "emulated",
|
|
891
|
+
returning: {
|
|
892
|
+
create: true,
|
|
893
|
+
update: true,
|
|
894
|
+
delete: false
|
|
895
|
+
},
|
|
896
|
+
returningMode: {
|
|
897
|
+
create: "record",
|
|
898
|
+
update: "record",
|
|
899
|
+
delete: "none"
|
|
900
|
+
},
|
|
901
|
+
nativeRelations: {
|
|
902
|
+
singularChains: false,
|
|
903
|
+
hasMany: false,
|
|
904
|
+
manyToMany: false,
|
|
905
|
+
filtered: false,
|
|
906
|
+
ordered: false,
|
|
907
|
+
paginated: false
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
}),
|
|
911
|
+
async findMany(schema, modelName, args) {
|
|
912
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
913
|
+
const rows = applyModelQuery(model, await loadRows(schema, modelName), args);
|
|
914
|
+
return Promise.all(rows.map((row) => projectRow(schema, modelName, row.data, args.select)));
|
|
915
|
+
},
|
|
916
|
+
async findFirst(schema, modelName, args) {
|
|
917
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
918
|
+
const row = applyModelQuery(model, await loadRows(schema, modelName), args)[0];
|
|
919
|
+
if (!row) return null;
|
|
920
|
+
return projectRow(schema, modelName, row.data, args.select);
|
|
921
|
+
},
|
|
922
|
+
async findUnique(schema, modelName, args) {
|
|
923
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
924
|
+
const row = await loadUniqueRow(schema, modelName, args.where);
|
|
925
|
+
if (!row || !matchesModelWhere(model, row.data, args.where)) {
|
|
926
|
+
return null;
|
|
927
|
+
}
|
|
928
|
+
return projectRow(schema, modelName, row.data, args.select);
|
|
929
|
+
},
|
|
930
|
+
async count(schema, modelName, args) {
|
|
931
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
932
|
+
return applyModelQuery(model, await loadRows(schema, modelName), args).length;
|
|
933
|
+
},
|
|
934
|
+
async create(schema, modelName, args) {
|
|
935
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
936
|
+
const existingRows = await loadRows(schema, modelName);
|
|
937
|
+
const built = buildStoredRow(model, args.data);
|
|
938
|
+
const conflict = findUniqueConflict(model, built.decoded, existingRows);
|
|
939
|
+
if (conflict) {
|
|
940
|
+
throw redisConstraintError(conflict);
|
|
941
|
+
}
|
|
942
|
+
await createRecordWithLocks(schema, modelName, built, model);
|
|
943
|
+
return projectRow(schema, modelName, built.decoded, args.select);
|
|
944
|
+
},
|
|
945
|
+
async createMany(schema, modelName, args) {
|
|
946
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
947
|
+
const existingRows = await loadRows(schema, modelName);
|
|
948
|
+
const created = [];
|
|
949
|
+
for (const entry of args.data) {
|
|
950
|
+
const built = buildStoredRow(model, entry);
|
|
951
|
+
const conflict = findUniqueConflict(model, built.decoded, [
|
|
952
|
+
...existingRows,
|
|
953
|
+
...created.map((row) => ({
|
|
954
|
+
docId: row.docId,
|
|
955
|
+
key: recordKey(schema, modelName, row.docId),
|
|
956
|
+
data: row.decoded,
|
|
957
|
+
stored: row.stored
|
|
958
|
+
}))
|
|
959
|
+
]);
|
|
960
|
+
if (conflict) {
|
|
961
|
+
throw redisConstraintError(conflict);
|
|
962
|
+
}
|
|
963
|
+
created.push(built);
|
|
964
|
+
}
|
|
965
|
+
for (const row of created) {
|
|
966
|
+
await createRecordWithLocks(schema, modelName, row, model);
|
|
967
|
+
}
|
|
968
|
+
return Promise.all(
|
|
969
|
+
created.map((row) => projectRow(schema, modelName, row.decoded, args.select))
|
|
970
|
+
);
|
|
971
|
+
},
|
|
972
|
+
async update(schema, modelName, args) {
|
|
973
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
974
|
+
const rows = await loadRows(schema, modelName);
|
|
975
|
+
const current = applyModelQuery(model, rows, {
|
|
976
|
+
where: args.where,
|
|
977
|
+
take: 1
|
|
978
|
+
})[0];
|
|
979
|
+
if (!current) return null;
|
|
980
|
+
const next = buildUpdatedRow(model, current, args.data);
|
|
981
|
+
const conflict = findUniqueConflict(model, next.decoded, rows, /* @__PURE__ */ new Set([current.docId]));
|
|
982
|
+
if (conflict) {
|
|
983
|
+
throw redisConstraintError(conflict);
|
|
984
|
+
}
|
|
985
|
+
await updateRecordWithLocks(schema, modelName, model, current, next);
|
|
986
|
+
return projectRow(schema, modelName, next.decoded, args.select);
|
|
987
|
+
},
|
|
988
|
+
async updateMany(schema, modelName, args) {
|
|
989
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
990
|
+
const rows = await loadRows(schema, modelName);
|
|
991
|
+
const matched = applyModelQuery(model, rows, {
|
|
992
|
+
where: args.where
|
|
993
|
+
});
|
|
994
|
+
if (!matched.length) return 0;
|
|
995
|
+
const nextRows = matched.map(
|
|
996
|
+
(row) => buildUpdatedRow(model, row, args.data)
|
|
997
|
+
);
|
|
998
|
+
const keepIds = new Set(matched.map((row) => row.docId));
|
|
999
|
+
const remaining = rows.filter((row) => !keepIds.has(row.docId));
|
|
1000
|
+
const pending = [];
|
|
1001
|
+
for (const next of nextRows) {
|
|
1002
|
+
const conflict = findUniqueConflict(model, next.decoded, [...remaining, ...pending]);
|
|
1003
|
+
if (conflict) {
|
|
1004
|
+
throw redisConstraintError(conflict);
|
|
1005
|
+
}
|
|
1006
|
+
pending.push({
|
|
1007
|
+
docId: next.docId,
|
|
1008
|
+
key: recordKey(schema, modelName, next.docId),
|
|
1009
|
+
data: next.decoded,
|
|
1010
|
+
stored: next.stored
|
|
1011
|
+
});
|
|
1012
|
+
}
|
|
1013
|
+
for (let index = 0; index < matched.length; index += 1) {
|
|
1014
|
+
await updateRecordWithLocks(schema, modelName, model, matched[index], nextRows[index]);
|
|
1015
|
+
}
|
|
1016
|
+
return nextRows.length;
|
|
1017
|
+
},
|
|
1018
|
+
async upsert(schema, modelName, args) {
|
|
1019
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
1020
|
+
const lookup = (0, import_orm.requireUniqueLookup)(model, args.where, "Upsert");
|
|
1021
|
+
(0, import_orm.validateUniqueLookupUpdateData)(
|
|
1022
|
+
model,
|
|
1023
|
+
args.update,
|
|
1024
|
+
lookup,
|
|
1025
|
+
"Upsert"
|
|
1026
|
+
);
|
|
1027
|
+
const current = await loadUniqueRow(schema, modelName, args.where);
|
|
1028
|
+
if (current && matchesModelWhere(model, current.data, args.where)) {
|
|
1029
|
+
const rows2 = await loadRows(schema, modelName);
|
|
1030
|
+
const next = buildUpdatedRow(
|
|
1031
|
+
model,
|
|
1032
|
+
current,
|
|
1033
|
+
args.update
|
|
1034
|
+
);
|
|
1035
|
+
const conflict2 = findUniqueConflict(model, next.decoded, rows2, /* @__PURE__ */ new Set([current.docId]));
|
|
1036
|
+
if (conflict2) {
|
|
1037
|
+
throw redisConstraintError(conflict2);
|
|
1038
|
+
}
|
|
1039
|
+
await updateRecordWithLocks(schema, modelName, model, current, next);
|
|
1040
|
+
return projectRow(schema, modelName, next.decoded, args.select);
|
|
1041
|
+
}
|
|
1042
|
+
const created = buildStoredRow(
|
|
1043
|
+
model,
|
|
1044
|
+
(0, import_orm.mergeUniqueLookupCreateData)(
|
|
1045
|
+
model,
|
|
1046
|
+
args.create,
|
|
1047
|
+
lookup,
|
|
1048
|
+
"Upsert"
|
|
1049
|
+
)
|
|
1050
|
+
);
|
|
1051
|
+
const rows = await loadRows(schema, modelName);
|
|
1052
|
+
const conflict = findUniqueConflict(model, created.decoded, rows);
|
|
1053
|
+
if (conflict) {
|
|
1054
|
+
throw redisConstraintError(conflict);
|
|
1055
|
+
}
|
|
1056
|
+
await createRecordWithLocks(schema, modelName, created, model);
|
|
1057
|
+
return projectRow(schema, modelName, created.decoded, args.select);
|
|
1058
|
+
},
|
|
1059
|
+
async delete(schema, modelName, args) {
|
|
1060
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
1061
|
+
const row = applyModelQuery(model, await loadRows(schema, modelName), {
|
|
1062
|
+
where: args.where,
|
|
1063
|
+
take: 1
|
|
1064
|
+
})[0];
|
|
1065
|
+
if (!row) return 0;
|
|
1066
|
+
await deleteRecordWithLocks(schema, modelName, model, row);
|
|
1067
|
+
return 1;
|
|
1068
|
+
},
|
|
1069
|
+
async deleteMany(schema, modelName, args) {
|
|
1070
|
+
const model = getSupportedManifest(schema).models[modelName];
|
|
1071
|
+
const rows = applyModelQuery(model, await loadRows(schema, modelName), {
|
|
1072
|
+
where: args.where
|
|
1073
|
+
});
|
|
1074
|
+
for (const row of rows) {
|
|
1075
|
+
await deleteRecordWithLocks(schema, modelName, model, row);
|
|
1076
|
+
}
|
|
1077
|
+
return rows.length;
|
|
1078
|
+
},
|
|
1079
|
+
async transaction(schema, run) {
|
|
1080
|
+
getSupportedManifest(schema);
|
|
1081
|
+
return run(driver);
|
|
1082
|
+
}
|
|
1083
|
+
};
|
|
1084
|
+
return driver;
|
|
1085
|
+
}
|
|
1086
|
+
function createRedisDriver(config) {
|
|
1087
|
+
return createRedisDriverInternal(config);
|
|
1088
|
+
}
|
|
1089
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1090
|
+
0 && (module.exports = {
|
|
1091
|
+
createRedisDriver
|
|
1092
|
+
});
|
|
1093
|
+
//# sourceMappingURL=index.cjs.map
|