@orkestrel/database 0.0.14 → 0.0.15
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/src/browser/index.js +2 -1
- package/dist/src/browser/index.js.map +1 -1
- package/dist/src/core/index.cjs +61 -75
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.js +62 -76
- package/dist/src/core/index.js.map +1 -1
- package/dist/src/server/index.cjs +24 -24
- package/dist/src/server/index.cjs.map +1 -1
- package/dist/src/server/index.js +25 -25
- package/dist/src/server/index.js.map +1 -1
- package/package.json +12 -12
package/dist/src/server/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DatabaseError, DriverIterator, MemoryDriver, applyQuery, bindRowKey, checkAbort, cloneDriverMetadata, cloneMigrationInput, computeAggregate, equalsValue, extractKey, filterRows, findColumn, isDatabaseError, isKey, matchesQuery, migrateRows, normalizeDriverSchema, planMigration, projectMigrationSchema, validatePage } from "../core/index.js";
|
|
2
|
-
import { cloneJSONValue, isBoolean, isFiniteNumber, isRecord, isString } from "@orkestrel/contract";
|
|
2
|
+
import { cloneJSONValue, isArray, isBigInt, isBoolean, isError, isFiniteNumber, isInteger, isNumber, isObject, isRecord, isString, isUint8Array } from "@orkestrel/contract";
|
|
3
3
|
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
4
4
|
import { dirname } from "node:path";
|
|
5
5
|
import { createSQLiteDatabase, isSQLiteError } from "@orkestrel/sqlite";
|
|
@@ -82,7 +82,7 @@ var METADATA_TABLE = "_metadata";
|
|
|
82
82
|
* ```
|
|
83
83
|
*/
|
|
84
84
|
function matchesAbsentPath(error) {
|
|
85
|
-
if (!(error
|
|
85
|
+
if (!isError(error) || !("code" in error)) return false;
|
|
86
86
|
return error.code === "ENOENT" || error.code === "ENOTDIR";
|
|
87
87
|
}
|
|
88
88
|
/**
|
|
@@ -292,16 +292,16 @@ function encodeValue(value, column) {
|
|
|
292
292
|
return column.storage === "text" || column.storage === "json" ? /* @__PURE__ */ new Uint8Array() : String(null);
|
|
293
293
|
}
|
|
294
294
|
switch (column.storage) {
|
|
295
|
-
case "boolean": return
|
|
295
|
+
case "boolean": return isBoolean(value) ? value ? 1 : 0 : null;
|
|
296
296
|
case "json": try {
|
|
297
297
|
return JSON.stringify(cloneJSONValue(value));
|
|
298
298
|
} catch {
|
|
299
299
|
return null;
|
|
300
300
|
}
|
|
301
|
-
case "integer": return
|
|
302
|
-
case "real": return
|
|
303
|
-
case "text": return
|
|
304
|
-
case "blob": return value
|
|
301
|
+
case "integer": return isBigInt(value) || isFiniteNumber(value) && isInteger(value) ? value : null;
|
|
302
|
+
case "real": return isBigInt(value) || isFiniteNumber(value) ? value : null;
|
|
303
|
+
case "text": return isString(value) ? value : null;
|
|
304
|
+
case "blob": return isUint8Array(value) ? value : null;
|
|
305
305
|
}
|
|
306
306
|
}
|
|
307
307
|
/**
|
|
@@ -326,23 +326,23 @@ function encodeValue(value, column) {
|
|
|
326
326
|
*/
|
|
327
327
|
function decodeValue(value, column) {
|
|
328
328
|
if (value === null) return column.nullable && !column.optional ? null : void 0;
|
|
329
|
-
if (column.optional && column.nullable && (column.storage === "text" || column.storage === "json" ? value
|
|
329
|
+
if (column.optional && column.nullable && (column.storage === "text" || column.storage === "json" ? isUint8Array(value) && value.byteLength === 0 : value === String(null))) return null;
|
|
330
330
|
switch (column.storage) {
|
|
331
331
|
case "boolean":
|
|
332
332
|
if (value === 0 || value === 0n) return false;
|
|
333
333
|
if (value === 1 || value === 1n) return true;
|
|
334
334
|
return;
|
|
335
335
|
case "json":
|
|
336
|
-
if (
|
|
336
|
+
if (!isString(value)) return void 0;
|
|
337
337
|
try {
|
|
338
338
|
return structuredClone(cloneJSONValue(JSON.parse(value)));
|
|
339
339
|
} catch {
|
|
340
340
|
return;
|
|
341
341
|
}
|
|
342
|
-
case "integer": return
|
|
343
|
-
case "real": return
|
|
344
|
-
case "text": return
|
|
345
|
-
case "blob": return value
|
|
342
|
+
case "integer": return isBigInt(value) || isFiniteNumber(value) && isInteger(value) ? value : void 0;
|
|
343
|
+
case "real": return isBigInt(value) || isFiniteNumber(value) ? value : void 0;
|
|
344
|
+
case "text": return isString(value) ? value : void 0;
|
|
345
|
+
case "blob": return isUint8Array(value) ? value : void 0;
|
|
346
346
|
}
|
|
347
347
|
}
|
|
348
348
|
/**
|
|
@@ -477,10 +477,10 @@ function deriveSQLiteIndexName(table, columns) {
|
|
|
477
477
|
* ```
|
|
478
478
|
*/
|
|
479
479
|
function inferValueStorage(value) {
|
|
480
|
-
if (
|
|
481
|
-
if (
|
|
482
|
-
if (
|
|
483
|
-
if (
|
|
480
|
+
if (isBoolean(value)) return "boolean";
|
|
481
|
+
if (isNumber(value)) return isInteger(value) ? "integer" : "real";
|
|
482
|
+
if (isBigInt(value)) return "integer";
|
|
483
|
+
if (isObject(value)) return "json";
|
|
484
484
|
return "text";
|
|
485
485
|
}
|
|
486
486
|
//#endregion
|
|
@@ -1395,7 +1395,7 @@ var JSONDriver = class {
|
|
|
1395
1395
|
async #hydrate(memory, schema, tables) {
|
|
1396
1396
|
for (const table of schema) {
|
|
1397
1397
|
const rows = tables[table.name];
|
|
1398
|
-
if (!
|
|
1398
|
+
if (!isArray(rows)) throw new DatabaseError("DRIVER", "Stored JSON table is invalid", {
|
|
1399
1399
|
path: this.#path,
|
|
1400
1400
|
table: table.name,
|
|
1401
1401
|
aspect: "container"
|
|
@@ -1973,7 +1973,7 @@ var SQLiteDriver = class {
|
|
|
1973
1973
|
const keys = [];
|
|
1974
1974
|
for (const row of rows) {
|
|
1975
1975
|
const value = row[schema.primary];
|
|
1976
|
-
if (
|
|
1976
|
+
if (isString(value) || isNumber(value)) keys.push(value);
|
|
1977
1977
|
}
|
|
1978
1978
|
return keys;
|
|
1979
1979
|
});
|
|
@@ -2106,7 +2106,7 @@ var SQLiteDriver = class {
|
|
|
2106
2106
|
});
|
|
2107
2107
|
for (const [position, column] of group.entries()) {
|
|
2108
2108
|
const entry = entries.find((candidate) => candidate.seqno === position || candidate.seqno === BigInt(position));
|
|
2109
|
-
const stored = entry !== void 0 && (
|
|
2109
|
+
const stored = entry !== void 0 && (isInteger(entry.cid) && entry.cid >= 0 || isBigInt(entry.cid) && entry.cid >= 0n);
|
|
2110
2110
|
if (entry === void 0 || entry.name !== column || !stored || entry.desc !== 0 && entry.desc !== 0n || entry.coll !== "BINARY") throw new DatabaseError("DRIVER", "SQLite index column does not match its declaration", {
|
|
2111
2111
|
table: schema.name,
|
|
2112
2112
|
aspect: "index",
|
|
@@ -2123,11 +2123,11 @@ var SQLiteDriver = class {
|
|
|
2123
2123
|
if (row === void 0) return void 0;
|
|
2124
2124
|
const version = row.version;
|
|
2125
2125
|
const text = row.schema;
|
|
2126
|
-
if (
|
|
2126
|
+
if (!isString(text)) throw new DatabaseError("DRIVER", "Stored SQLite metadata schema is invalid", {
|
|
2127
2127
|
table: METADATA_TABLE,
|
|
2128
2128
|
aspect: "metadata"
|
|
2129
2129
|
});
|
|
2130
|
-
if (
|
|
2130
|
+
if (!isNumber(version) && !isBigInt(version)) throw new DatabaseError("DRIVER", "Stored SQLite metadata version is invalid", {
|
|
2131
2131
|
table: METADATA_TABLE,
|
|
2132
2132
|
aspect: "metadata"
|
|
2133
2133
|
});
|
|
@@ -2141,7 +2141,7 @@ var SQLiteDriver = class {
|
|
|
2141
2141
|
cause: error
|
|
2142
2142
|
});
|
|
2143
2143
|
}
|
|
2144
|
-
if (!
|
|
2144
|
+
if (!isArray(parsed)) throw new DatabaseError("DRIVER", "Stored SQLite metadata schema is invalid", {
|
|
2145
2145
|
table: METADATA_TABLE,
|
|
2146
2146
|
aspect: "metadata"
|
|
2147
2147
|
});
|
|
@@ -2260,7 +2260,7 @@ var SQLiteDriver = class {
|
|
|
2260
2260
|
try {
|
|
2261
2261
|
return operation();
|
|
2262
2262
|
} catch (error) {
|
|
2263
|
-
if (error
|
|
2263
|
+
if (isDatabaseError(error)) throw error;
|
|
2264
2264
|
if (isSQLiteError(error)) {
|
|
2265
2265
|
if (error.code === "CONSTRAINT") throw new DatabaseError("CONFLICT", error.message, {
|
|
2266
2266
|
cause: error,
|
|
@@ -2280,7 +2280,7 @@ var SQLiteDriver = class {
|
|
|
2280
2280
|
code: error.code
|
|
2281
2281
|
});
|
|
2282
2282
|
}
|
|
2283
|
-
throw new DatabaseError("DRIVER", error
|
|
2283
|
+
throw new DatabaseError("DRIVER", isError(error) ? error.message : String(error), { cause: error });
|
|
2284
2284
|
}
|
|
2285
2285
|
}
|
|
2286
2286
|
#require() {
|