@rljson/io 0.0.63 → 0.0.65

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.
@@ -4,7 +4,252 @@
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 { TableCfg } from '@rljson/rljson';
8
+
9
+ import { IoMem } from './io-mem.ts';
10
+ import { IoMulti } from './io-multi.ts';
11
+
12
+
7
13
  // Run »pnpm updateGoldens« when you change this file
8
- export const example = async () => {};
9
14
 
15
+ /**
16
+ * Example 1: Basic IoMem usage
17
+ */
18
+ export const basicExample = async () => {
19
+ // Create and initialize in-memory database
20
+ const io = new IoMem();
21
+ await io.init();
22
+
23
+ // Create a table with schema
24
+ await io.createOrExtendTable({
25
+ tableCfg: {
26
+ key: 'users',
27
+ type: 'components',
28
+ columns: [
29
+ { key: '_hash', type: 'string', titleShort: 'Hash', titleLong: 'Hash' },
30
+ {
31
+ key: 'id',
32
+ type: 'number',
33
+ titleShort: 'ID',
34
+ titleLong: 'User ID',
35
+ },
36
+ {
37
+ key: 'name',
38
+ type: 'string',
39
+ titleShort: 'Name',
40
+ titleLong: 'User Name',
41
+ },
42
+ {
43
+ key: 'email',
44
+ type: 'string',
45
+ titleShort: 'Email',
46
+ titleLong: 'Email Address',
47
+ },
48
+ ],
49
+ isHead: false,
50
+ isRoot: false,
51
+ isShared: true,
52
+ },
53
+ });
54
+
55
+ // Write data
56
+ await io.write({
57
+ data: {
58
+ users: {
59
+ _type: 'components',
60
+ _data: [
61
+ { id: 1, name: 'Alice', email: 'alice@example.com' },
62
+ { id: 2, name: 'Bob', email: 'bob@example.com' },
63
+ ],
64
+ },
65
+ },
66
+ });
67
+
68
+ // Query data
69
+ const result = await io.readRows({
70
+ table: 'users',
71
+ where: { id: 1 },
72
+ });
73
+
74
+ console.log('Query result:', result);
75
+
76
+ // Get row count
77
+ const count = await io.rowCount('users');
78
+ console.log('Total users:', count);
79
+
80
+ // Dump entire database
81
+ const allData = await io.dump();
82
+ console.log('Database dump:', allData);
83
+
84
+ await io.close();
85
+ };
86
+
87
+ /**
88
+ * Example 2: Schema management
89
+ */
90
+ export const schemaExample = async () => {
91
+ const io = new IoMem();
92
+ await io.init();
93
+
94
+ // Create a table with schema
95
+ await io.createOrExtendTable({
96
+ tableCfg: {
97
+ key: 'products',
98
+ type: 'components',
99
+ columns: [
100
+ { key: '_hash', type: 'string', titleShort: 'Hash', titleLong: 'Hash' },
101
+ {
102
+ key: 'sku',
103
+ type: 'string',
104
+ titleShort: 'SKU',
105
+ titleLong: 'Stock Keeping Unit',
106
+ },
107
+ {
108
+ key: 'name',
109
+ type: 'string',
110
+ titleShort: 'Name',
111
+ titleLong: 'Product Name',
112
+ },
113
+ {
114
+ key: 'price',
115
+ type: 'number',
116
+ titleShort: 'Price',
117
+ titleLong: 'Price in USD',
118
+ },
119
+ {
120
+ key: 'inStock',
121
+ type: 'boolean',
122
+ titleShort: 'In Stock',
123
+ titleLong: 'Available in Stock',
124
+ },
125
+ ],
126
+ isHead: false,
127
+ isRoot: false,
128
+ isShared: true,
129
+ },
130
+ });
131
+
132
+ // Check if table exists
133
+ const exists = await io.tableExists('products');
134
+ console.log('Table exists:', exists);
135
+
136
+ // Get table configuration
137
+ const configs = await io.rawTableCfgs();
138
+ console.log('Table configs:', configs);
139
+
140
+ await io.close();
141
+ };
142
+
143
+ /**
144
+ * Example 3: IoMulti with caching
145
+ */
146
+ export const multiExample = async () => {
147
+ // Create cache layer
148
+ const cache = new IoMem();
149
+ await cache.init();
150
+
151
+ // Create "remote" layer (simulated with another IoMem)
152
+ const remote = new IoMem();
153
+ await remote.init();
154
+
155
+ // Create table schema in both
156
+ const tableCfg: TableCfg = {
157
+ key: 'items',
158
+ type: 'components',
159
+ columns: [
160
+ { key: '_hash', type: 'string', titleShort: 'Hash', titleLong: 'Hash' },
161
+ { key: 'id', type: 'number', titleShort: 'ID', titleLong: 'Item ID' },
162
+ {
163
+ key: 'value',
164
+ type: 'string',
165
+ titleShort: 'Value',
166
+ titleLong: 'Item Value',
167
+ },
168
+ ],
169
+ isHead: false,
170
+ isRoot: false,
171
+ isShared: true,
172
+ };
173
+
174
+ await cache.createOrExtendTable({ tableCfg });
175
+ await remote.createOrExtendTable({ tableCfg });
176
+
177
+ // Pre-populate remote with data
178
+ await remote.write({
179
+ data: {
180
+ items: {
181
+ _type: 'components',
182
+ _data: [
183
+ { id: 1, value: 'Remote Data' },
184
+ { id: 2, value: 'More Data' },
185
+ ],
186
+ },
187
+ },
188
+ });
189
+
190
+ // Create multi-tier Io
191
+ const io = new IoMulti([
192
+ {
193
+ io: cache,
194
+ priority: 1, // Check cache first
195
+ read: true,
196
+ write: true, // Write to cache
197
+ dump: false,
198
+ },
199
+ {
200
+ io: remote,
201
+ priority: 2, // Fallback to remote
202
+ read: true,
203
+ write: true, // Also persist to remote
204
+ dump: true, // Remote is source of truth
205
+ },
206
+ ]);
207
+
208
+ await io.init();
209
+
210
+ // First read hits remote, caches result
211
+ console.log('First read (hits remote):');
212
+ const data1 = await io.readRows({
213
+ table: 'items',
214
+ where: { id: 1 },
215
+ });
216
+ console.log(data1);
217
+
218
+ // Second read hits cache (faster!)
219
+ console.log('Second read (hits cache):');
220
+ const data2 = await io.readRows({
221
+ table: 'items',
222
+ where: { id: 1 },
223
+ });
224
+ console.log(data2);
225
+
226
+ // Write goes to both cache and remote
227
+ await io.write({
228
+ data: {
229
+ items: {
230
+ _type: 'components',
231
+ _data: [{ id: 3, value: 'New Data' }],
232
+ },
233
+ },
234
+ });
235
+
236
+ /* v8 ignore next -- @preserve */
237
+ await io.close();
238
+ };
239
+
240
+ /**
241
+ * Run all examples
242
+ */
243
+ export const example = async () => {
244
+ console.log('=== Basic Example ===');
245
+ await basicExample();
246
+
247
+ console.log('\n=== Schema Example ===');
248
+ await schemaExample();
249
+
250
+ console.log('\n=== Multi-tier Example ===');
251
+ await multiExample();
252
+ };
253
+
254
+ // Uncomment to run examples
10
255
  // example();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rljson/io",
3
- "version": "0.0.63",
3
+ "version": "0.0.65",
4
4
  "description": "Low level interface for reading and writing RLJSON data",
5
5
  "homepage": "https://github.com/rljson/io",
6
6
  "bugs": "https://github.com/rljson/io/issues",
@@ -20,31 +20,31 @@
20
20
  ],
21
21
  "type": "module",
22
22
  "devDependencies": {
23
- "@types/node": "^25.0.3",
24
- "@typescript-eslint/eslint-plugin": "^8.50.1",
25
- "@typescript-eslint/parser": "^8.50.1",
26
- "@vitest/coverage-v8": "^4.0.16",
23
+ "@types/node": "^25.1.0",
24
+ "@typescript-eslint/eslint-plugin": "^8.54.0",
25
+ "@typescript-eslint/parser": "^8.54.0",
26
+ "@vitest/coverage-v8": "^4.0.18",
27
27
  "cross-env": "^10.1.0",
28
28
  "eslint": "^9.39.2",
29
- "eslint-plugin-jsdoc": "^61.5.0",
29
+ "eslint-plugin-jsdoc": "^62.5.0",
30
30
  "eslint-plugin-tsdoc": "^0.5.0",
31
- "globals": "^16.5.0",
31
+ "globals": "^17.2.0",
32
32
  "jsdoc": "^4.0.5",
33
33
  "read-pkg": "^10.0.0",
34
34
  "typescript": "~5.9.3",
35
- "typescript-eslint": "^8.50.1",
36
- "vite": "^7.3.0",
37
- "vite-node": "^5.2.0",
35
+ "typescript-eslint": "^8.54.0",
36
+ "vite": "^7.3.1",
37
+ "vite-node": "^5.3.0",
38
38
  "vite-plugin-dts": "^4.5.4",
39
- "vite-tsconfig-paths": "^6.0.3",
40
- "vitest": "^4.0.16",
39
+ "vite-tsconfig-paths": "^6.0.5",
40
+ "vitest": "^4.0.18",
41
41
  "vitest-dom": "^0.1.1"
42
42
  },
43
43
  "dependencies": {
44
- "@rljson/hash": "^0.0.17",
44
+ "@rljson/hash": "^0.0.18",
45
45
  "@rljson/is-ready": "^0.0.17",
46
46
  "@rljson/json": "^0.0.23",
47
- "@rljson/rljson": "^0.0.73",
47
+ "@rljson/rljson": "^0.0.75",
48
48
  "@rljson/validate": "^0.0.11"
49
49
  },
50
50
  "scripts": {