@rljson/io 0.0.9 → 0.0.13
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 +7 -4
- package/dist/io.d.ts +12 -6
- package/dist/io.js +48 -19
- package/dist/src/example.ts +19 -3
- package/package.json +3 -2
package/dist/io-mem.d.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { JsonValue } from '@rljson/json';
|
|
2
|
-
import {
|
|
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.
|
|
6
6
|
*/
|
|
7
7
|
export declare class IoMem implements Io {
|
|
8
|
+
constructor();
|
|
8
9
|
static example: () => IoMem;
|
|
9
10
|
isReady(): Promise<void>;
|
|
10
11
|
dump(): Promise<Rljson>;
|
|
11
12
|
dumpTable(request: {
|
|
12
|
-
|
|
13
|
+
table: string;
|
|
13
14
|
}): Promise<Rljson>;
|
|
14
15
|
readRow(request: {
|
|
15
16
|
table: string;
|
|
@@ -25,11 +26,13 @@ export declare class IoMem implements Io {
|
|
|
25
26
|
data: Rljson;
|
|
26
27
|
}): Promise<void>;
|
|
27
28
|
createTable(request: {
|
|
28
|
-
|
|
29
|
-
type: ContentType;
|
|
29
|
+
tableCfg: string;
|
|
30
30
|
}): Promise<void>;
|
|
31
31
|
tables(): Promise<string[]>;
|
|
32
|
+
private _isReady;
|
|
32
33
|
private _mem;
|
|
34
|
+
private _init;
|
|
35
|
+
private _initTableCfgs;
|
|
33
36
|
private _createTable;
|
|
34
37
|
private _readRow;
|
|
35
38
|
private _dump;
|
package/dist/io.d.ts
CHANGED
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
import { JsonValue } from '@rljson/json';
|
|
2
|
-
import {
|
|
2
|
+
import { Rljson } from '@rljson/rljson';
|
|
3
3
|
export interface Io {
|
|
4
|
-
/** A promise resolving once the Io interface is ready
|
|
4
|
+
/** A promise resolving once the Io interface is ready
|
|
5
|
+
*
|
|
6
|
+
* 💡 Use @rljson/is-ready
|
|
7
|
+
*/
|
|
5
8
|
isReady(): Promise<void>;
|
|
6
9
|
/** Returns the complete db content as Rljson */
|
|
7
10
|
dump(): Promise<Rljson>;
|
|
8
11
|
/** Returns the dump of a complete table */
|
|
9
12
|
dumpTable(request: {
|
|
10
|
-
|
|
13
|
+
table: string;
|
|
11
14
|
}): Promise<Rljson>;
|
|
12
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* Creates a table with a given config
|
|
17
|
+
*
|
|
18
|
+
* The config must be already in the database
|
|
19
|
+
*/
|
|
13
20
|
createTable(request: {
|
|
14
|
-
|
|
15
|
-
type: ContentType;
|
|
21
|
+
tableCfg: string;
|
|
16
22
|
}): Promise<void>;
|
|
17
23
|
/** Returns the available table names */
|
|
18
24
|
tables(): Promise<string[]>;
|
package/dist/io.js
CHANGED
|
@@ -2,19 +2,44 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
3
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
4
|
import { hip, hsh } from "@rljson/hash";
|
|
5
|
+
import { IsReady } from "@rljson/is-ready";
|
|
5
6
|
import { copy, equals } from "@rljson/json";
|
|
6
7
|
// @license
|
|
7
8
|
const _IoMem = class _IoMem {
|
|
9
|
+
// ...........................................................................
|
|
10
|
+
// Constructor & example
|
|
8
11
|
constructor() {
|
|
9
12
|
// ######################
|
|
10
13
|
// Private
|
|
11
14
|
// ######################
|
|
15
|
+
__publicField(this, "_isReady", new IsReady());
|
|
12
16
|
__publicField(this, "_mem", hip({}));
|
|
17
|
+
// ...........................................................................
|
|
18
|
+
__publicField(this, "_initTableCfgs", () => {
|
|
19
|
+
const tableCfg = {
|
|
20
|
+
key: "tableCfgs",
|
|
21
|
+
type: "properties",
|
|
22
|
+
columns: {
|
|
23
|
+
key: { key: "key", type: "string", previous: "string" },
|
|
24
|
+
type: { key: "type", type: "string", previous: "string" }
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
hip(tableCfg);
|
|
28
|
+
this._mem.tableCfgs = hip({
|
|
29
|
+
_data: [tableCfg],
|
|
30
|
+
_type: "properties",
|
|
31
|
+
_tableCfg: tableCfg._hash
|
|
32
|
+
});
|
|
33
|
+
const updateExistingHashes = true;
|
|
34
|
+
const throwOnOnWrongHashes = false;
|
|
35
|
+
hip(this._mem, updateExistingHashes, throwOnOnWrongHashes);
|
|
36
|
+
});
|
|
37
|
+
this._init();
|
|
13
38
|
}
|
|
14
39
|
// ...........................................................................
|
|
15
40
|
// General
|
|
16
41
|
isReady() {
|
|
17
|
-
return
|
|
42
|
+
return this._isReady.promise;
|
|
18
43
|
}
|
|
19
44
|
// ...........................................................................
|
|
20
45
|
// Dump
|
|
@@ -48,23 +73,29 @@ const _IoMem = class _IoMem {
|
|
|
48
73
|
return tables;
|
|
49
74
|
}
|
|
50
75
|
// ...........................................................................
|
|
76
|
+
async _init() {
|
|
77
|
+
this._initTableCfgs();
|
|
78
|
+
this._isReady.resolve();
|
|
79
|
+
}
|
|
80
|
+
// ...........................................................................
|
|
51
81
|
async _createTable(request) {
|
|
52
82
|
var _a;
|
|
53
|
-
const
|
|
54
|
-
|
|
83
|
+
const config = this._mem.tableCfgs._data.find(
|
|
84
|
+
(cfg) => cfg._hash === request.tableCfg
|
|
85
|
+
);
|
|
86
|
+
if (!config) {
|
|
87
|
+
throw new Error(`Table config ${request.tableCfg} not found`);
|
|
88
|
+
}
|
|
89
|
+
const { key, type } = config;
|
|
90
|
+
const existing = this._mem[key];
|
|
55
91
|
if (existing) {
|
|
56
|
-
|
|
57
|
-
throw new Error(
|
|
58
|
-
`Table ${name} already exists with different type: "${existing._type}" vs "${request.type}"`
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
} else {
|
|
62
|
-
const table = hip({
|
|
63
|
-
_data: [],
|
|
64
|
-
_type: type
|
|
65
|
-
});
|
|
66
|
-
(_a = this._mem)[name] ?? (_a[name] = table);
|
|
92
|
+
throw new Error(`Table ${key} already exists`);
|
|
67
93
|
}
|
|
94
|
+
const table = hip({
|
|
95
|
+
_data: [],
|
|
96
|
+
_type: type
|
|
97
|
+
});
|
|
98
|
+
(_a = this._mem)[key] ?? (_a[key] = table);
|
|
68
99
|
}
|
|
69
100
|
// ...........................................................................
|
|
70
101
|
async _readRow(request) {
|
|
@@ -91,12 +122,12 @@ const _IoMem = class _IoMem {
|
|
|
91
122
|
}
|
|
92
123
|
// ...........................................................................
|
|
93
124
|
async _dumpTable(request) {
|
|
94
|
-
const table = this._mem[request.
|
|
125
|
+
const table = this._mem[request.table];
|
|
95
126
|
if (!table) {
|
|
96
|
-
throw new Error(`Table ${request.
|
|
127
|
+
throw new Error(`Table ${request.table} not found`);
|
|
97
128
|
}
|
|
98
129
|
return {
|
|
99
|
-
[request.
|
|
130
|
+
[request.table]: copy(table)
|
|
100
131
|
};
|
|
101
132
|
}
|
|
102
133
|
// ...........................................................................
|
|
@@ -154,8 +185,6 @@ const _IoMem = class _IoMem {
|
|
|
154
185
|
return result;
|
|
155
186
|
}
|
|
156
187
|
};
|
|
157
|
-
// ...........................................................................
|
|
158
|
-
// Constructor & example
|
|
159
188
|
__publicField(_IoMem, "example", () => {
|
|
160
189
|
return new _IoMem();
|
|
161
190
|
});
|
package/dist/src/example.ts
CHANGED
|
@@ -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({
|
|
36
|
+
await ioMem.createTable({ tableCfg: tableCfg._hash });
|
|
21
37
|
|
|
22
38
|
// Write data into the table
|
|
23
39
|
await ioMem.write({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rljson/io",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.13",
|
|
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",
|
|
@@ -57,8 +57,9 @@
|
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
59
|
"@rljson/hash": "^0.0.12",
|
|
60
|
+
"@rljson/is-ready": "^0.0.17",
|
|
60
61
|
"@rljson/json": "^0.0.18",
|
|
61
|
-
"@rljson/rljson": "^0.0.
|
|
62
|
+
"@rljson/rljson": "^0.0.28",
|
|
62
63
|
"@rljson/validate": "^0.0.8"
|
|
63
64
|
}
|
|
64
65
|
}
|