@datrix/adapter-json 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -6
- package/dist/index.js +30 -30
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +32 -32
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# datrix
|
|
1
|
+
# @datrix/adapter-json
|
|
2
2
|
|
|
3
3
|
A file-based JSON database adapter for Datrix framework. Perfect for development, testing, static sites, and small-scale applications.
|
|
4
4
|
|
|
@@ -18,20 +18,20 @@ A file-based JSON database adapter for Datrix framework. Perfect for development
|
|
|
18
18
|
|
|
19
19
|
```bash
|
|
20
20
|
# pnpm
|
|
21
|
-
pnpm add datrix
|
|
21
|
+
pnpm add @datrix/adapter-json
|
|
22
22
|
|
|
23
23
|
# yarn
|
|
24
|
-
yarn add datrix
|
|
24
|
+
yarn add @datrix/adapter-json
|
|
25
25
|
|
|
26
26
|
# npm
|
|
27
|
-
npm install datrix
|
|
27
|
+
npm install @datrix/adapter-json
|
|
28
28
|
```
|
|
29
29
|
|
|
30
30
|
## Quick Start with Datrix
|
|
31
31
|
|
|
32
32
|
```typescript
|
|
33
|
-
import { defineConfig } from "datrix
|
|
34
|
-
import { JsonAdapter } from "datrix
|
|
33
|
+
import { defineConfig } from "@datrix/core";
|
|
34
|
+
import { JsonAdapter } from "@datrix/adapter-json";
|
|
35
35
|
|
|
36
36
|
// Create Datrix configuration
|
|
37
37
|
const config = defineConfig(() => ({
|
package/dist/index.js
CHANGED
|
@@ -755,13 +755,13 @@ function validateTableName(tableName) {
|
|
|
755
755
|
}
|
|
756
756
|
}
|
|
757
757
|
async function createMetaTable(adapter) {
|
|
758
|
-
const metaExists = await adapter.tableExists(import_core5.
|
|
758
|
+
const metaExists = await adapter.tableExists(import_core5.DATRIX_META_MODEL);
|
|
759
759
|
if (metaExists) {
|
|
760
760
|
return;
|
|
761
761
|
}
|
|
762
762
|
const metaSchema = {
|
|
763
|
-
name: import_core5.
|
|
764
|
-
tableName: import_core5.
|
|
763
|
+
name: import_core5.DATRIX_META_MODEL,
|
|
764
|
+
tableName: import_core5.DATRIX_META_MODEL,
|
|
765
765
|
fields: {
|
|
766
766
|
id: { type: "number", autoIncrement: true },
|
|
767
767
|
key: { type: "string", required: true, unique: true, maxLength: 255 },
|
|
@@ -1625,8 +1625,8 @@ var JsonAdapter = class {
|
|
|
1625
1625
|
* @param tableName - Physical table name (e.g. "users")
|
|
1626
1626
|
*/
|
|
1627
1627
|
async readTableSchema(tableName) {
|
|
1628
|
-
const metaFile = await this.readTable(import_core10.
|
|
1629
|
-
const metaKey = `${import_core10.
|
|
1628
|
+
const metaFile = await this.readTable(import_core10.DATRIX_META_MODEL);
|
|
1629
|
+
const metaKey = `${import_core10.DATRIX_META_KEY_PREFIX}${tableName}`;
|
|
1630
1630
|
const row = metaFile.data.find(
|
|
1631
1631
|
(r) => r["key"] === metaKey
|
|
1632
1632
|
);
|
|
@@ -1641,9 +1641,9 @@ var JsonAdapter = class {
|
|
|
1641
1641
|
* Upsert schema into _datrix metadata table
|
|
1642
1642
|
*/
|
|
1643
1643
|
async upsertSchemaMeta(schema, skipWrite) {
|
|
1644
|
-
const metaKey = `${import_core10.
|
|
1644
|
+
const metaKey = `${import_core10.DATRIX_META_KEY_PREFIX}${schema.tableName ?? schema.name}`;
|
|
1645
1645
|
const metaValue = JSON.stringify(schema);
|
|
1646
|
-
const metaFile = await this.readTable(import_core10.
|
|
1646
|
+
const metaFile = await this.readTable(import_core10.DATRIX_META_MODEL);
|
|
1647
1647
|
const existingIndex = metaFile.data.findIndex(
|
|
1648
1648
|
(r) => r["key"] === metaKey
|
|
1649
1649
|
);
|
|
@@ -1660,15 +1660,15 @@ var JsonAdapter = class {
|
|
|
1660
1660
|
}
|
|
1661
1661
|
metaFile.meta.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1662
1662
|
if (skipWrite) {
|
|
1663
|
-
this.activeTransactionCache.set(import_core10.
|
|
1663
|
+
this.activeTransactionCache.set(import_core10.DATRIX_META_MODEL, {
|
|
1664
1664
|
data: metaFile,
|
|
1665
1665
|
mtime: Date.now()
|
|
1666
1666
|
});
|
|
1667
|
-
this.activeTransactionModifiedTables.add(import_core10.
|
|
1667
|
+
this.activeTransactionModifiedTables.add(import_core10.DATRIX_META_MODEL);
|
|
1668
1668
|
} else {
|
|
1669
|
-
const filePath = this.getTablePath(import_core10.
|
|
1669
|
+
const filePath = this.getTablePath(import_core10.DATRIX_META_MODEL);
|
|
1670
1670
|
await import_promises2.default.writeFile(filePath, JSON.stringify(metaFile, null, 2), "utf-8");
|
|
1671
|
-
await this.updateCache(import_core10.
|
|
1671
|
+
await this.updateCache(import_core10.DATRIX_META_MODEL, metaFile);
|
|
1672
1672
|
}
|
|
1673
1673
|
}
|
|
1674
1674
|
/**
|
|
@@ -1830,12 +1830,12 @@ var JsonAdapter = class {
|
|
|
1830
1830
|
await this.updateCache(tableName, initialContent);
|
|
1831
1831
|
}
|
|
1832
1832
|
if (!isImport) {
|
|
1833
|
-
if (schema.name !== import_core10.
|
|
1834
|
-
const metaExists = await this.tableExists(import_core10.
|
|
1833
|
+
if (schema.name !== import_core10.DATRIX_META_MODEL) {
|
|
1834
|
+
const metaExists = await this.tableExists(import_core10.DATRIX_META_MODEL);
|
|
1835
1835
|
if (!metaExists) {
|
|
1836
1836
|
(0, import_core9.throwMigrationError)({
|
|
1837
1837
|
adapter: "json",
|
|
1838
|
-
message: `Cannot create table '${schema.name}': '${import_core10.
|
|
1838
|
+
message: `Cannot create table '${schema.name}': '${import_core10.DATRIX_META_MODEL}' table does not exist yet. Create '${import_core10.DATRIX_META_MODEL}' first.`
|
|
1839
1839
|
});
|
|
1840
1840
|
}
|
|
1841
1841
|
}
|
|
@@ -1885,27 +1885,27 @@ var JsonAdapter = class {
|
|
|
1885
1885
|
await import_promises2.default.unlink(filePath);
|
|
1886
1886
|
this.invalidateCache(tableName);
|
|
1887
1887
|
}
|
|
1888
|
-
if (!isImport && tableName !== import_core10.
|
|
1889
|
-
const metaKey = `${import_core10.
|
|
1890
|
-
const metaFile = await this.readTable(import_core10.
|
|
1888
|
+
if (!isImport && tableName !== import_core10.DATRIX_META_MODEL) {
|
|
1889
|
+
const metaKey = `${import_core10.DATRIX_META_KEY_PREFIX}${tableName}`;
|
|
1890
|
+
const metaFile = await this.readTable(import_core10.DATRIX_META_MODEL);
|
|
1891
1891
|
metaFile.data = metaFile.data.filter(
|
|
1892
1892
|
(r) => r["key"] !== metaKey
|
|
1893
1893
|
);
|
|
1894
1894
|
metaFile.meta.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1895
1895
|
if (skipWrite) {
|
|
1896
|
-
this.activeTransactionCache.set(import_core10.
|
|
1896
|
+
this.activeTransactionCache.set(import_core10.DATRIX_META_MODEL, {
|
|
1897
1897
|
data: metaFile,
|
|
1898
1898
|
mtime: Date.now()
|
|
1899
1899
|
});
|
|
1900
|
-
this.activeTransactionModifiedTables.add(import_core10.
|
|
1900
|
+
this.activeTransactionModifiedTables.add(import_core10.DATRIX_META_MODEL);
|
|
1901
1901
|
} else {
|
|
1902
|
-
const filePath = this.getTablePath(import_core10.
|
|
1902
|
+
const filePath = this.getTablePath(import_core10.DATRIX_META_MODEL);
|
|
1903
1903
|
await import_promises2.default.writeFile(
|
|
1904
1904
|
filePath,
|
|
1905
1905
|
JSON.stringify(metaFile, null, 2),
|
|
1906
1906
|
"utf-8"
|
|
1907
1907
|
);
|
|
1908
|
-
await this.updateCache(import_core10.
|
|
1908
|
+
await this.updateCache(import_core10.DATRIX_META_MODEL, metaFile);
|
|
1909
1909
|
}
|
|
1910
1910
|
}
|
|
1911
1911
|
}
|
|
@@ -2189,7 +2189,7 @@ var JsonAdapter = class {
|
|
|
2189
2189
|
await import_promises2.default.writeFile(filePath, JSON.stringify(json, null, 2), "utf-8");
|
|
2190
2190
|
await this.updateCache(tableName, json);
|
|
2191
2191
|
}
|
|
2192
|
-
if (tableName !== import_core10.
|
|
2192
|
+
if (tableName !== import_core10.DATRIX_META_MODEL) {
|
|
2193
2193
|
await this.applyOperationsToMetaSchema(tableName, operations, skipWrite);
|
|
2194
2194
|
}
|
|
2195
2195
|
}
|
|
@@ -2248,10 +2248,10 @@ var JsonAdapter = class {
|
|
|
2248
2248
|
this.invalidateCache(from);
|
|
2249
2249
|
await this.updateCache(to, json);
|
|
2250
2250
|
}
|
|
2251
|
-
if (from !== import_core10.
|
|
2252
|
-
const oldKey = `${import_core10.
|
|
2253
|
-
const newKey = `${import_core10.
|
|
2254
|
-
const metaFile = await this.readTable(import_core10.
|
|
2251
|
+
if (from !== import_core10.DATRIX_META_MODEL && to !== import_core10.DATRIX_META_MODEL) {
|
|
2252
|
+
const oldKey = `${import_core10.DATRIX_META_KEY_PREFIX}${from}`;
|
|
2253
|
+
const newKey = `${import_core10.DATRIX_META_KEY_PREFIX}${to}`;
|
|
2254
|
+
const metaFile = await this.readTable(import_core10.DATRIX_META_MODEL);
|
|
2255
2255
|
const row = metaFile.data.find(
|
|
2256
2256
|
(r) => r["key"] === oldKey
|
|
2257
2257
|
);
|
|
@@ -2259,19 +2259,19 @@ var JsonAdapter = class {
|
|
|
2259
2259
|
row["key"] = newKey;
|
|
2260
2260
|
metaFile.meta.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
2261
2261
|
if (skipWrite) {
|
|
2262
|
-
this.activeTransactionCache.set(import_core10.
|
|
2262
|
+
this.activeTransactionCache.set(import_core10.DATRIX_META_MODEL, {
|
|
2263
2263
|
data: metaFile,
|
|
2264
2264
|
mtime: Date.now()
|
|
2265
2265
|
});
|
|
2266
|
-
this.activeTransactionModifiedTables.add(import_core10.
|
|
2266
|
+
this.activeTransactionModifiedTables.add(import_core10.DATRIX_META_MODEL);
|
|
2267
2267
|
} else {
|
|
2268
|
-
const metaPath = this.getTablePath(import_core10.
|
|
2268
|
+
const metaPath = this.getTablePath(import_core10.DATRIX_META_MODEL);
|
|
2269
2269
|
await import_promises2.default.writeFile(
|
|
2270
2270
|
metaPath,
|
|
2271
2271
|
JSON.stringify(metaFile, null, 2),
|
|
2272
2272
|
"utf-8"
|
|
2273
2273
|
);
|
|
2274
|
-
await this.updateCache(import_core10.
|
|
2274
|
+
await this.updateCache(import_core10.DATRIX_META_MODEL, metaFile);
|
|
2275
2275
|
}
|
|
2276
2276
|
}
|
|
2277
2277
|
}
|