@rljson/io 0.0.10 → 0.0.14

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/io-mem.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { JsonValue } from '@rljson/json';
2
- import { ContentType, Rljson } from '@rljson/rljson';
2
+ import { Rljson } from '@rljson/rljson';
3
3
  import { Io } from './io.ts';
4
4
  /**
5
5
  * In-Memory implementation of the Rljson Io interface.
@@ -10,11 +10,7 @@ export declare class IoMem implements Io {
10
10
  isReady(): Promise<void>;
11
11
  dump(): Promise<Rljson>;
12
12
  dumpTable(request: {
13
- name: string;
14
- }): Promise<Rljson>;
15
- readRow(request: {
16
13
  table: string;
17
- rowHash: string;
18
14
  }): Promise<Rljson>;
19
15
  readRows(request: {
20
16
  table: string;
@@ -26,15 +22,14 @@ export declare class IoMem implements Io {
26
22
  data: Rljson;
27
23
  }): Promise<void>;
28
24
  createTable(request: {
29
- name: string;
30
- type: ContentType;
25
+ tableCfg: string;
31
26
  }): Promise<void>;
32
- tables(): Promise<string[]>;
27
+ tables(): Promise<Rljson>;
33
28
  private _isReady;
34
29
  private _mem;
35
30
  private _init;
31
+ private _initTableCfgs;
36
32
  private _createTable;
37
- private _readRow;
38
33
  private _dump;
39
34
  private _dumpTable;
40
35
  private _write;
package/dist/io.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { JsonValue } from '@rljson/json';
2
- import { ContentType, Rljson } from '@rljson/rljson';
2
+ import { Rljson } from '@rljson/rljson';
3
3
  export interface Io {
4
4
  /** A promise resolving once the Io interface is ready
5
5
  *
@@ -10,24 +10,22 @@ export interface Io {
10
10
  dump(): Promise<Rljson>;
11
11
  /** Returns the dump of a complete table */
12
12
  dumpTable(request: {
13
- name: string;
13
+ table: string;
14
14
  }): Promise<Rljson>;
15
- /** Creates a table with a given type */
15
+ /**
16
+ * Creates a table with a given config
17
+ *
18
+ * The config must be already in the database
19
+ */
16
20
  createTable(request: {
17
- name: string;
18
- type: ContentType;
21
+ tableCfg: string;
19
22
  }): Promise<void>;
20
23
  /** Returns the available table names */
21
- tables(): Promise<string[]>;
24
+ tables(): Promise<Rljson>;
22
25
  /** Writes Rljson data in to the database */
23
26
  write(request: {
24
27
  data: Rljson;
25
28
  }): Promise<void>;
26
- /** Reads a specific row from a database table */
27
- readRow(request: {
28
- table: string;
29
- rowHash: string;
30
- }): Promise<Rljson>;
31
29
  /** Queries a list of rows */
32
30
  readRows(request: {
33
31
  table: string;
package/dist/io.js CHANGED
@@ -14,6 +14,27 @@ const _IoMem = class _IoMem {
14
14
  // ######################
15
15
  __publicField(this, "_isReady", new IsReady());
16
16
  __publicField(this, "_mem", hip({}));
17
+ // ...........................................................................
18
+ __publicField(this, "_initTableCfgs", () => {
19
+ const tableCfg = {
20
+ version: 1,
21
+ key: "tableCfgs",
22
+ type: "properties",
23
+ columns: {
24
+ key: { key: "key", type: "string", previous: "string" },
25
+ type: { key: "type", type: "string", previous: "string" }
26
+ }
27
+ };
28
+ hip(tableCfg);
29
+ this._mem.tableCfgs = hip({
30
+ _data: [tableCfg],
31
+ _type: "properties",
32
+ _tableCfg: tableCfg._hash
33
+ });
34
+ const updateExistingHashes = true;
35
+ const throwOnOnWrongHashes = false;
36
+ hip(this._mem, updateExistingHashes, throwOnOnWrongHashes);
37
+ });
17
38
  this._init();
18
39
  }
19
40
  // ...........................................................................
@@ -31,9 +52,6 @@ const _IoMem = class _IoMem {
31
52
  }
32
53
  // ...........................................................................
33
54
  // Rows
34
- readRow(request) {
35
- return this._readRow(request);
36
- }
37
55
  readRows(request) {
38
56
  return this._readRows(request);
39
57
  }
@@ -48,51 +66,51 @@ const _IoMem = class _IoMem {
48
66
  return this._createTable(request);
49
67
  }
50
68
  async tables() {
51
- const keys = Object.keys(this._mem);
52
- const tables = keys.filter((key) => !key.startsWith("_"));
53
- return tables;
69
+ const tables = [];
70
+ for (const key of Object.keys(this._mem)) {
71
+ const table = this._mem[key];
72
+ const tableCfgRef = table._tableCfg;
73
+ if (tableCfgRef) {
74
+ for (const tableCfg of this._mem.tableCfgs._data) {
75
+ if (tableCfg._hash === tableCfgRef) {
76
+ tables.push(tableCfg);
77
+ }
78
+ }
79
+ }
80
+ }
81
+ const result = {
82
+ tableCfgs: {
83
+ _type: "properties",
84
+ _data: tables
85
+ }
86
+ };
87
+ return hip(result);
54
88
  }
55
89
  // ...........................................................................
56
- _init() {
90
+ async _init() {
91
+ this._initTableCfgs();
57
92
  this._isReady.resolve();
58
93
  }
59
94
  // ...........................................................................
60
95
  async _createTable(request) {
61
96
  var _a;
62
- const { name, type } = request;
63
- const existing = this._mem[name];
64
- if (existing) {
65
- if (existing._type !== type) {
66
- throw new Error(
67
- `Table ${name} already exists with different type: "${existing._type}" vs "${request.type}"`
68
- );
69
- }
70
- } else {
71
- const table = hip({
72
- _data: [],
73
- _type: type
74
- });
75
- (_a = this._mem)[name] ?? (_a[name] = table);
76
- }
77
- }
78
- // ...........................................................................
79
- async _readRow(request) {
80
- const table = this._mem[request.table];
81
- if (!table) {
82
- throw new Error(`Table ${request.table} not found`);
97
+ const config = this._mem.tableCfgs._data.find(
98
+ (cfg) => cfg._hash === request.tableCfg
99
+ );
100
+ if (!config) {
101
+ throw new Error(`Table config ${request.tableCfg} not found`);
83
102
  }
84
- const row = table._data.find((row2) => row2._hash === request.rowHash);
85
- if (!row) {
86
- throw new Error(
87
- `Row "${request.rowHash}" not found in table ${request.table}`
88
- );
103
+ const { key, type } = config;
104
+ const existing = this._mem[key];
105
+ if (existing) {
106
+ throw new Error(`Table ${key} already exists`);
89
107
  }
90
- const result = {
91
- [request.table]: {
92
- _data: [row]
93
- }
108
+ const table = {
109
+ _data: [],
110
+ _type: type,
111
+ _tableCfg: config._hash
94
112
  };
95
- return result;
113
+ (_a = this._mem)[key] ?? (_a[key] = hip(table));
96
114
  }
97
115
  // ...........................................................................
98
116
  async _dump() {
@@ -100,12 +118,12 @@ const _IoMem = class _IoMem {
100
118
  }
101
119
  // ...........................................................................
102
120
  async _dumpTable(request) {
103
- const table = this._mem[request.name];
121
+ const table = this._mem[request.table];
104
122
  if (!table) {
105
- throw new Error(`Table ${request.name} not found`);
123
+ throw new Error(`Table ${request.table} not found`);
106
124
  }
107
125
  return {
108
- [request.name]: copy(table)
126
+ [request.table]: copy(table)
109
127
  };
110
128
  }
111
129
  // ...........................................................................
@@ -4,8 +4,8 @@
4
4
  // Use of this source code is governed by terms that can be
5
5
  // found in the LICENSE file in the root of this package.
6
6
 
7
- import { hsh } from '@rljson/hash';
8
- import { Rljson } from '@rljson/rljson';
7
+ import { hip, hsh } from '@rljson/hash';
8
+ import { Rljson, TableCfg } from '@rljson/rljson';
9
9
 
10
10
  import { IoMem } from './io-mem.ts';
11
11
 
@@ -16,8 +16,24 @@ export const example = async () => {
16
16
  const row = { keyA2: 'a2' };
17
17
  const rowWithHash = hsh(row);
18
18
 
19
+ // Create a table config first
20
+ const tableCfg = hip({
21
+ key: 'tableA',
22
+ type: 'properties',
23
+ columns: {},
24
+ } as TableCfg);
25
+
26
+ await ioMem.write({
27
+ data: {
28
+ tableCfgs: {
29
+ _type: 'properties',
30
+ _data: [tableCfg],
31
+ },
32
+ },
33
+ });
34
+
19
35
  // Create a table first
20
- await ioMem.createTable({ name: 'tableA', type: 'properties' });
36
+ await ioMem.createTable({ tableCfg: tableCfg._hash });
21
37
 
22
38
  // Write data into the table
23
39
  await ioMem.write({
@@ -30,9 +46,9 @@ export const example = async () => {
30
46
  });
31
47
 
32
48
  // Read data from the table
33
- const data: Rljson = await ioMem.readRow({
49
+ const data: Rljson = await ioMem.readRows({
34
50
  table: 'tableA',
35
- rowHash: rowWithHash._hash,
51
+ where: { _hash: rowWithHash._hash },
36
52
  });
37
53
 
38
54
  // Print the return rljson data
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rljson/io",
3
- "version": "0.0.10",
3
+ "version": "0.0.14",
4
4
  "packageManager": "pnpm@10.6.2",
5
5
  "description": "Low level interface for reading and writing RLJSON data",
6
6
  "homepage": "https://github.com/rljson/io",
@@ -29,20 +29,20 @@
29
29
  "updateGoldens": "cross-env UPDATE_GOLDENS=true npm test"
30
30
  },
31
31
  "devDependencies": {
32
- "@types/node": "^22.13.11",
32
+ "@types/node": "^22.13.13",
33
33
  "@typescript-eslint/eslint-plugin": "^8.27.0",
34
34
  "@typescript-eslint/parser": "^8.27.0",
35
35
  "@vitest/coverage-v8": "^3.0.9",
36
36
  "cross-env": "^7.0.3",
37
37
  "eslint": "^9.23.0",
38
- "eslint-plugin-jsdoc": "^50.6.8",
38
+ "eslint-plugin-jsdoc": "^50.6.9",
39
39
  "eslint-plugin-tsdoc": "^0.4.0",
40
40
  "globals": "^16.0.0",
41
41
  "jsdoc": "^4.0.4",
42
42
  "read-pkg": "^9.0.1",
43
43
  "typescript": "~5.8.2",
44
44
  "typescript-eslint": "^8.27.0",
45
- "vite": "^6.2.2",
45
+ "vite": "^6.2.3",
46
46
  "vite-node": "^3.0.9",
47
47
  "vite-plugin-dts": "^4.5.3",
48
48
  "vite-tsconfig-paths": "^5.1.4",
@@ -57,9 +57,9 @@
57
57
  },
58
58
  "dependencies": {
59
59
  "@rljson/hash": "^0.0.12",
60
- "@rljson/is-ready": "^0.0.16",
60
+ "@rljson/is-ready": "^0.0.17",
61
61
  "@rljson/json": "^0.0.18",
62
- "@rljson/rljson": "^0.0.21",
62
+ "@rljson/rljson": "^0.0.29",
63
63
  "@rljson/validate": "^0.0.8"
64
64
  }
65
65
  }