@yaks/ram 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/README.md +446 -2
- package/_dist/mod.d.ts +37 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/store.d.ts +93 -0
- package/_dist/store.d.ts.map +1 -0
- package/deno.json +1 -0
- package/mod.js +37 -0
- package/mod.js.map +1 -0
- package/mod.ts +38 -0
- package/package.json +19 -4
- package/records.js +115 -0
- package/records.js.map +1 -0
- package/records.ts +111 -0
- package/rules.js +200 -0
- package/rules.js.map +1 -0
- package/rules.ts +234 -0
- package/store.js +985 -0
- package/store.js.map +1 -0
- package/store.ts +1139 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Jeff Peterson
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,447 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @yaks/ram
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Synchronous in-memory [Storage](../graph/README.md#data-model) for
|
|
4
|
+
[@yaks/graph](../graph/README.md). It holds
|
|
5
|
+
[bundles](../graph/README.md#data-model) in a JavaScript `Map` and answers
|
|
6
|
+
[queries](../query/README.md#query-model) with
|
|
7
|
+
[@yaks/match](../match/README.md). Use it for tests, browser state, or a local
|
|
8
|
+
copy of server data. Discarding it loses its contents.
|
|
9
|
+
|
|
10
|
+
A **store** is the synchronous Storage returned by `ram(vocab, options?)`, bound
|
|
11
|
+
to one [vocabulary](../vocab/README.md#vocabulary) (`Store`). Its writes run in
|
|
12
|
+
[transactions](../graph/README.md#data-model); reads need no `await`.
|
|
13
|
+
|
|
14
|
+
## Use
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { graph } from '@yaks/graph'
|
|
18
|
+
import { ram } from '@yaks/ram'
|
|
19
|
+
import { loadVocab } from '@yaks/vocab'
|
|
20
|
+
import { equal } from '@yaks/testing'
|
|
21
|
+
|
|
22
|
+
const vocab = loadVocab({
|
|
23
|
+
$defs: {
|
|
24
|
+
book: {
|
|
25
|
+
type: 'object',
|
|
26
|
+
component: true,
|
|
27
|
+
properties: {
|
|
28
|
+
title: { type: 'string' },
|
|
29
|
+
pages: { type: 'number' },
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
})
|
|
34
|
+
const g = graph({ storage: ram(vocab), vocab })
|
|
35
|
+
g.install()
|
|
36
|
+
await g.apply([{ entity: { eid: 'b1' }, book: { title: 'Dune', pages: 412 } }])
|
|
37
|
+
equal((await g.read('.book.pages>300'))[0].book, { title: 'Dune', pages: 412 })
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
The store's reads and writes are synchronous. A graph using it also runs
|
|
41
|
+
synchronously when its plugins do; `await` works with either return form.
|
|
42
|
+
|
|
43
|
+
## Install
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
deno add jsr:@yaks/ram jsr:@yaks/graph jsr:@yaks/vocab
|
|
47
|
+
# Node projects can use: npx jsr add @yaks/ram @yaks/graph @yaks/vocab
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Exports
|
|
51
|
+
|
|
52
|
+
| Export | Purpose |
|
|
53
|
+
| --------- | ----------------------------------------------------------- |
|
|
54
|
+
| `ram` | Construct a store over an empty Map. |
|
|
55
|
+
| `Store` | Synchronous Storage interface. |
|
|
56
|
+
| `Tx` | Store transaction interface. |
|
|
57
|
+
| `RamOpts` | Construction options: `now`, `number`, `adopt`, `computed`. |
|
|
58
|
+
| `Query` | Query input type from @yaks/match. |
|
|
59
|
+
|
|
60
|
+
The root module is the only export path.
|
|
61
|
+
|
|
62
|
+
## Reads
|
|
63
|
+
|
|
64
|
+
| Method | Result |
|
|
65
|
+
| ---------------------------- | -------------------------------------------------------------------------------------- |
|
|
66
|
+
| `install()` | Does nothing; a Map has no schema to install. |
|
|
67
|
+
| `read(query, opts?, comps?)` | Matching bundles, with ordering and pagination; `comps` restricts returned components. |
|
|
68
|
+
| `rows(query, opts?)` | One `{ eid }` per match, or aggregate results for `.count`, `.distinct`, `.tally`. |
|
|
69
|
+
| `get(eids, comps?)` | Stored bundles for those eids; unknown eids omitted, tombstones included. |
|
|
70
|
+
| `worn(comp, prop)` | Whether a property's value implies that its entity carries the component. |
|
|
71
|
+
| `tx(body)` | Callback result; commits on success, rolls back on throw or rejected promise. |
|
|
72
|
+
|
|
73
|
+
`worn` returns false for undeclared properties, computed properties, and
|
|
74
|
+
properties overridden by `computed`. `read` and `get` add computed values before
|
|
75
|
+
restricting returned components. Stored bundles are replaced rather than mutated
|
|
76
|
+
by writes, and a write that changes nothing keeps the bundle it found; callers
|
|
77
|
+
must not mutate returned bundles.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { ram } from '@yaks/ram'
|
|
81
|
+
import { loadVocab } from '@yaks/vocab'
|
|
82
|
+
import { equal } from '@yaks/testing'
|
|
83
|
+
|
|
84
|
+
const vocab = loadVocab({
|
|
85
|
+
$defs: {
|
|
86
|
+
book: {
|
|
87
|
+
type: 'object',
|
|
88
|
+
component: true,
|
|
89
|
+
properties: { title: { type: 'string' }, pages: { type: 'number' } },
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
})
|
|
93
|
+
const store = ram(vocab)
|
|
94
|
+
store.tx((tx) =>
|
|
95
|
+
tx.patch([
|
|
96
|
+
{ entity: { eid: 'b1' }, book: { title: 'Dune', pages: 412 } },
|
|
97
|
+
{ entity: { eid: 'b2' }, book: { title: 'Dune Messiah', pages: 256 } },
|
|
98
|
+
])
|
|
99
|
+
)
|
|
100
|
+
equal(store.read('.book .order=-book.pages .limit=1')[0].entity.eid, 'b1')
|
|
101
|
+
equal(store.rows('.book .count'), [{ value: '', n: 2 }])
|
|
102
|
+
equal(store.rows('.book.title=Dune'), [{ eid: 'b1' }])
|
|
103
|
+
equal(store.rows('.book .distinct=book.pages'), [{ value: 256 }, {
|
|
104
|
+
value: 412,
|
|
105
|
+
}])
|
|
106
|
+
equal(store.rows('.book .tally=book.pages'), [
|
|
107
|
+
{ value: 256, n: 1 },
|
|
108
|
+
{ value: 412, n: 1 },
|
|
109
|
+
])
|
|
110
|
+
equal(store.get(['b1', 'missing'], ['book']).map((b) => b.book), [
|
|
111
|
+
{ title: 'Dune', pages: 412 },
|
|
112
|
+
])
|
|
113
|
+
equal(store.worn('book', 'pages'), true)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
The store selects candidates by component and builds value indexes when a query
|
|
117
|
+
first asks for equality on text, enum, or reference properties. A **band**
|
|
118
|
+
groups numeric values by their floor, such as `10.2` and `10.9` in band `10`.
|
|
119
|
+
Numeric range queries build band indexes on demand. Writes and rollback keep
|
|
120
|
+
these indexes current; the matcher still checks candidates against the full
|
|
121
|
+
query.
|
|
122
|
+
|
|
123
|
+
`now` supplies the reference time for relative time expressions. Per-read
|
|
124
|
+
`opts.now` overrides the construction option:
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
import { ram } from '@yaks/ram'
|
|
128
|
+
import { loadVocab } from '@yaks/vocab'
|
|
129
|
+
import { equal } from '@yaks/testing'
|
|
130
|
+
|
|
131
|
+
const vocab = loadVocab({
|
|
132
|
+
$defs: {
|
|
133
|
+
release: {
|
|
134
|
+
type: 'object',
|
|
135
|
+
component: true,
|
|
136
|
+
properties: { at: { type: 'string', format: 'date-time' } },
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
})
|
|
140
|
+
const store = ram(vocab, { now: Date.parse('2026-01-01T12:00:00Z') })
|
|
141
|
+
store.tx((tx) =>
|
|
142
|
+
tx.patch([
|
|
143
|
+
{ entity: { eid: 'r1' }, release: { at: '2026-01-01T10:00:00Z' } },
|
|
144
|
+
])
|
|
145
|
+
)
|
|
146
|
+
equal(store.read('.release.at=today').length, 1)
|
|
147
|
+
equal(
|
|
148
|
+
store.read('.release.at=today', {
|
|
149
|
+
now: Date.parse('2026-01-02T12:00:00Z'),
|
|
150
|
+
}).length,
|
|
151
|
+
0,
|
|
152
|
+
)
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Patches and transactions
|
|
156
|
+
|
|
157
|
+
Use the graph's `apply` for application writes. Direct `tx.patch` bypasses
|
|
158
|
+
validation, [preconditions](../graph/README.md#writes-and-reads),
|
|
159
|
+
reference-deletion rules, plugins, and provenance stamps. It follows the
|
|
160
|
+
[patch](../graph/README.md#data-model) semantics; undeclared properties are
|
|
161
|
+
never stored. Computed properties are stored only when `adopt` is enabled.
|
|
162
|
+
|
|
163
|
+
A transaction offers `read`, `get`, `patch`, `evict`, `remove`, `revive`, and
|
|
164
|
+
`bindings`. `patch` returns identities it creates, including reference targets;
|
|
165
|
+
it also returns an existing empty identity when it receives a number.
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
import { ram } from '@yaks/ram'
|
|
169
|
+
import { loadVocab } from '@yaks/vocab'
|
|
170
|
+
import { equal, throws } from '@yaks/testing'
|
|
171
|
+
|
|
172
|
+
const vocab = loadVocab({
|
|
173
|
+
$defs: {
|
|
174
|
+
book: {
|
|
175
|
+
type: 'object',
|
|
176
|
+
component: true,
|
|
177
|
+
properties: { title: { type: 'string' }, pages: { type: 'number' } },
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
})
|
|
181
|
+
const store = ram(vocab)
|
|
182
|
+
store.tx((tx) =>
|
|
183
|
+
tx.patch([
|
|
184
|
+
{ entity: { eid: 'b1' }, book: { title: 'Dune', pages: 412 } },
|
|
185
|
+
])
|
|
186
|
+
)
|
|
187
|
+
await throws(() =>
|
|
188
|
+
store.tx((tx) => {
|
|
189
|
+
tx.patch([{ entity: { eid: 'b1' }, book: { pages: 1 } }])
|
|
190
|
+
store.tx((inner) => inner.patch([{ entity: { eid: 'b2' }, book: {} }]))
|
|
191
|
+
throw new Error('cancel')
|
|
192
|
+
})
|
|
193
|
+
)
|
|
194
|
+
equal(store.get(['b1'])[0].book, { title: 'Dune', pages: 412 })
|
|
195
|
+
equal(store.get(['b2']), [])
|
|
196
|
+
store.tx((tx) => tx.patch([{ entity: { eid: 'b1' }, book: { title: null } }]))
|
|
197
|
+
equal(store.get(['b1'])[0].book, { pages: 412 })
|
|
198
|
+
store.tx((tx) => tx.patch([{ entity: { eid: 'b1' }, book: null }]))
|
|
199
|
+
equal(store.get(['b1']), [{ entity: { eid: 'b1' } }])
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Nested transactions roll back only their own writes on failure; an outer
|
|
203
|
+
rollback also undoes successful inner transactions. The undo log keeps previous
|
|
204
|
+
bundles for changed entities and restores numbering and identity reservations,
|
|
205
|
+
without copying the entire Map. Declared
|
|
206
|
+
[unique indexes](../vocab/README.md#identity-and-indexes) are enforced during
|
|
207
|
+
writes and restored by rollback.
|
|
208
|
+
|
|
209
|
+
```ts
|
|
210
|
+
import { ram } from '@yaks/ram'
|
|
211
|
+
import { loadVocab } from '@yaks/vocab'
|
|
212
|
+
import { equal, throws } from '@yaks/testing'
|
|
213
|
+
|
|
214
|
+
const vocab = loadVocab({
|
|
215
|
+
$defs: {
|
|
216
|
+
book: {
|
|
217
|
+
type: 'object',
|
|
218
|
+
component: true,
|
|
219
|
+
properties: { isbn: { type: 'string', unique: true } },
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
})
|
|
223
|
+
const store = ram(vocab)
|
|
224
|
+
store.tx((tx) => tx.patch([{ entity: { eid: 'b1' }, book: { isbn: '123' } }]))
|
|
225
|
+
await throws(() =>
|
|
226
|
+
store.tx((tx) =>
|
|
227
|
+
tx.patch([
|
|
228
|
+
{ entity: { eid: 'b2' }, book: { isbn: '123' } },
|
|
229
|
+
])
|
|
230
|
+
)
|
|
231
|
+
)
|
|
232
|
+
equal(store.read('.book').map((b) => b.entity.eid), ['b1'])
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### Eviction and deletion
|
|
236
|
+
|
|
237
|
+
**eviction** (`tx.evict`) removes a live entity's components from the Map and
|
|
238
|
+
reserves its identity, including its number, for a later patch. Evicted entities
|
|
239
|
+
are absent from `get` and queries. Eviction leaves
|
|
240
|
+
[tombstones](../graph/README.md#writes-and-reads) alone.
|
|
241
|
+
|
|
242
|
+
`tx.remove` replaces an entity's components with a tombstone, keeping its eid
|
|
243
|
+
and number. Patches to a tombstoned entity do nothing. `tx.revive` clears the
|
|
244
|
+
tombstone, leaving only the identity ready for further patches. The graph
|
|
245
|
+
decides when application writes revive entities.
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
import { ram } from '@yaks/ram'
|
|
249
|
+
import { loadVocab } from '@yaks/vocab'
|
|
250
|
+
import { equal } from '@yaks/testing'
|
|
251
|
+
|
|
252
|
+
const vocab = loadVocab({
|
|
253
|
+
$defs: { book: { type: 'object', component: true, properties: {} } },
|
|
254
|
+
})
|
|
255
|
+
const store = ram(vocab, { number: { book: 'B' } })
|
|
256
|
+
const write = () =>
|
|
257
|
+
store.tx((tx) =>
|
|
258
|
+
tx.patch([
|
|
259
|
+
{ entity: { eid: 'b1' }, book: {} },
|
|
260
|
+
])
|
|
261
|
+
)
|
|
262
|
+
write()
|
|
263
|
+
store.tx((tx) => tx.evict(['b1']))
|
|
264
|
+
equal(store.get(['b1']), [])
|
|
265
|
+
write()
|
|
266
|
+
equal(store.get(['b1'])[0].entity.num, 1)
|
|
267
|
+
store.tx((tx) => tx.remove([{ eid: 'b1' }]))
|
|
268
|
+
write()
|
|
269
|
+
equal(store.get(['b1'])[0].tombstone, {})
|
|
270
|
+
store.tx((tx) => tx.revive(['b1']))
|
|
271
|
+
equal(store.get(['b1']), [{ entity: { eid: 'b1', num: 1, prefix: 'B' } }])
|
|
272
|
+
write()
|
|
273
|
+
equal(store.read('.book').length, 1)
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
## Numbering and adoption
|
|
277
|
+
|
|
278
|
+
Numbers are off by default. With `number: true`, entities receiving their own
|
|
279
|
+
components receive sequential numbers starting at 1; reference-only identities
|
|
280
|
+
remain unnumbered until their own components arrive. A table of letters,
|
|
281
|
+
component → letter, numbers by series instead, as @yaks/sqlite does: an entity
|
|
282
|
+
is numbered once, on the patch that first gives it a component in the table, and
|
|
283
|
+
holds `entity{prefix, num}`; each series counts on from its last number,
|
|
284
|
+
stepping over one an entity holds. Rollback restores the counters.
|
|
285
|
+
|
|
286
|
+
Where the vocabulary declares numbers (@yaks/id's `entity{prefix, num}`), a
|
|
287
|
+
patch stating them gives its entity that id, born or not, or none for
|
|
288
|
+
`num: null`. An id another entity holds is refused, unless that entity moves off
|
|
289
|
+
it in the same patch (@yaks/graph `renumbering`). `adopt: true` is for a mirror:
|
|
290
|
+
it never generates numbers, takes every stated one unchecked whatever its
|
|
291
|
+
vocabulary says, and stores incoming computed values.
|
|
292
|
+
|
|
293
|
+
```ts
|
|
294
|
+
import { ram } from '@yaks/ram'
|
|
295
|
+
import { loadVocab } from '@yaks/vocab'
|
|
296
|
+
import { equal } from '@yaks/testing'
|
|
297
|
+
|
|
298
|
+
const vocab = loadVocab({
|
|
299
|
+
$defs: {
|
|
300
|
+
book: { type: 'object', component: true, properties: {} },
|
|
301
|
+
cache: { type: 'object', component: true, properties: {} },
|
|
302
|
+
},
|
|
303
|
+
})
|
|
304
|
+
const store = ram(vocab, { number: { book: 'B' } })
|
|
305
|
+
store.tx((tx) =>
|
|
306
|
+
tx.patch([
|
|
307
|
+
{ entity: { eid: 'b1' }, book: {} },
|
|
308
|
+
{ entity: { eid: 'c1' }, cache: {} },
|
|
309
|
+
])
|
|
310
|
+
)
|
|
311
|
+
equal(store.get(['b1', 'c1']).map((b) => b.entity.num), [1, undefined])
|
|
312
|
+
equal(store.get(['b1'])[0].entity.prefix, 'B')
|
|
313
|
+
store.tx((tx) => tx.patch([{ entity: { eid: 'b1' }, cache: {} }]))
|
|
314
|
+
equal(store.get(['b1'])[0].entity.num, 1)
|
|
315
|
+
const copy = ram(vocab, { number: true, adopt: true })
|
|
316
|
+
copy.tx((tx) =>
|
|
317
|
+
tx.patch([
|
|
318
|
+
{ entity: { eid: 'b1', num: 42 }, book: {} },
|
|
319
|
+
{ entity: { eid: 'b2' }, book: {} },
|
|
320
|
+
])
|
|
321
|
+
)
|
|
322
|
+
equal(copy.get(['b1', 'b2']).map((b) => b.entity.num), [42, undefined])
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
## Computed properties
|
|
326
|
+
|
|
327
|
+
`computed` maps `comp.prop` to a function of a bundle and the matcher
|
|
328
|
+
[Index](../match/README.md). These functions supply
|
|
329
|
+
[computed properties](../vocab/README.md#the-format) for filtering and returned
|
|
330
|
+
bundles. The vocabulary's [status ladders](../vocab/README.md#kinds-and-status)
|
|
331
|
+
need no function. A query on another computed property without a function throws
|
|
332
|
+
`Unsupported`.
|
|
333
|
+
|
|
334
|
+
```ts
|
|
335
|
+
import { ram } from '@yaks/ram'
|
|
336
|
+
import { loadVocab } from '@yaks/vocab'
|
|
337
|
+
import { equal } from '@yaks/testing'
|
|
338
|
+
|
|
339
|
+
const vocab = loadVocab({
|
|
340
|
+
$defs: {
|
|
341
|
+
lamp: {
|
|
342
|
+
type: 'object',
|
|
343
|
+
component: true,
|
|
344
|
+
properties: {
|
|
345
|
+
watts: { type: 'number' },
|
|
346
|
+
glow: { type: 'string', computed: true },
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
},
|
|
350
|
+
})
|
|
351
|
+
const store = ram(vocab, {
|
|
352
|
+
computed: {
|
|
353
|
+
'lamp.glow': (b) => Number(b.lamp?.watts) > 40 ? 'bright' : 'dim',
|
|
354
|
+
},
|
|
355
|
+
})
|
|
356
|
+
store.tx((tx) =>
|
|
357
|
+
tx.patch([
|
|
358
|
+
{ entity: { eid: 'l1' }, lamp: { watts: 60, glow: 'dim' } },
|
|
359
|
+
])
|
|
360
|
+
)
|
|
361
|
+
equal(store.read('.lamp.glow=bright')[0].lamp, { watts: 60, glow: 'bright' })
|
|
362
|
+
equal(store.worn('lamp', 'glow'), false)
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
The store materializes status both in query results and in `get`:
|
|
366
|
+
|
|
367
|
+
```ts
|
|
368
|
+
import { ram } from '@yaks/ram'
|
|
369
|
+
import { loadVocab } from '@yaks/vocab'
|
|
370
|
+
import { equal } from '@yaks/testing'
|
|
371
|
+
|
|
372
|
+
const vocab = loadVocab({
|
|
373
|
+
$defs: {
|
|
374
|
+
task: { component: true, status: { completed: 'done', default: 'open' } },
|
|
375
|
+
completed: { component: true },
|
|
376
|
+
},
|
|
377
|
+
})
|
|
378
|
+
const store = ram(vocab)
|
|
379
|
+
store.tx((tx) =>
|
|
380
|
+
tx.patch([
|
|
381
|
+
{ entity: { eid: 't1' }, task: {} },
|
|
382
|
+
{ entity: { eid: 't2' }, task: {}, completed: {} },
|
|
383
|
+
])
|
|
384
|
+
)
|
|
385
|
+
equal(store.get(['t1'])[0].task, { status: 'open' })
|
|
386
|
+
equal(store.read('.task.status=done')[0].entity.eid, 't2')
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
## Declared rules
|
|
390
|
+
|
|
391
|
+
`Tx.bindings` evaluates
|
|
392
|
+
[declared rules](../graph/README.md#rules-over-more-than-one-entity) with a
|
|
393
|
+
proposed batch temporarily patched into the Map, then rewinds it. Patterns join
|
|
394
|
+
on their variables in memory. At least one pattern must match an entity in a
|
|
395
|
+
nonempty batch; evaluation uses component and value indexes to find the other
|
|
396
|
+
matches.
|
|
397
|
+
|
|
398
|
+
```ts
|
|
399
|
+
import { graph } from '@yaks/graph'
|
|
400
|
+
import { ram } from '@yaks/ram'
|
|
401
|
+
import { loadVocab } from '@yaks/vocab'
|
|
402
|
+
import { equal } from '@yaks/testing'
|
|
403
|
+
|
|
404
|
+
const vocab = loadVocab({
|
|
405
|
+
$defs: {
|
|
406
|
+
book: { type: 'object', component: true, properties: {} },
|
|
407
|
+
shelf: {
|
|
408
|
+
type: 'object',
|
|
409
|
+
component: true,
|
|
410
|
+
properties: { aisle: { type: 'string' } },
|
|
411
|
+
},
|
|
412
|
+
},
|
|
413
|
+
})
|
|
414
|
+
const g = graph({
|
|
415
|
+
storage: ram(vocab),
|
|
416
|
+
vocab,
|
|
417
|
+
plugins: [{
|
|
418
|
+
name: 'shelving',
|
|
419
|
+
declared: [{ name: 'unshelved', match: '.book, +!shelf, +shelf.aisle=Z' }],
|
|
420
|
+
}],
|
|
421
|
+
})
|
|
422
|
+
await g.apply([{ entity: { eid: 'b1' }, book: {} }])
|
|
423
|
+
equal((await g.get(['b1']))[0].shelf, { aisle: 'Z' })
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
## Limits
|
|
427
|
+
|
|
428
|
+
There is no persistence or process boundary; use
|
|
429
|
+
[@yaks/sqlite](../sqlite/README.md) for SQLite storage. RAM returns properties
|
|
430
|
+
that were written, whereas SQL storage can return `null` for unwritten
|
|
431
|
+
properties. Missing and `null` values match the same way. RAM preserves
|
|
432
|
+
JavaScript value types; SQL storage may return an integer for a stored boolean.
|
|
433
|
+
|
|
434
|
+
Unsupported queries throw `Unsupported` from @yaks/match: examples include
|
|
435
|
+
`.near` and computed properties without a function. Aggregates use `rows`,
|
|
436
|
+
rather than `read`. See the [matcher](../match/README.md) for the supported
|
|
437
|
+
subset. Text search matches tokens in stored text without a full-text index or
|
|
438
|
+
relevance ranking; tokenization can differ from a database's full-text engine.
|
|
439
|
+
|
|
440
|
+
The package is pure TypeScript with no Deno, Node, or DOM-specific imports and
|
|
441
|
+
can run in browsers and server JavaScript runtimes. Its runtime dependencies
|
|
442
|
+
include @yaks/graph, @yaks/match, and @yaks/fp; @yaks/vocab supplies schema
|
|
443
|
+
types.
|
|
444
|
+
|
|
445
|
+
## License
|
|
446
|
+
|
|
447
|
+
Apache-2.0
|
package/_dist/mod.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/ram — the in-memory storage adapter: a
|
|
3
|
+
* {@link https://jsr.io/@yaks/graph | @yaks/graph} `Storage` over a plain Map
|
|
4
|
+
* of bundles, fully synchronous, with no database underneath it.
|
|
5
|
+
*
|
|
6
|
+
* ## Why
|
|
7
|
+
* A graph needs somewhere to keep its entities. A server keeps them in SQLite;
|
|
8
|
+
* a page, a worker, or a test has nowhere to put a database and nothing to
|
|
9
|
+
* install — so this package keeps them in a Map, evaluates queries with
|
|
10
|
+
* {@link https://jsr.io/@yaks/match | @yaks/match}, and implements the same
|
|
11
|
+
* five members every other adapter does. The same `apply()`, the same query
|
|
12
|
+
* grammar, the same bundles.
|
|
13
|
+
*
|
|
14
|
+
* ## Use
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { graph } from '@yaks/graph'
|
|
17
|
+
* import { ram } from '@yaks/ram'
|
|
18
|
+
* // let g = graph({ storage: ram(vocab), vocab })
|
|
19
|
+
* // g.apply([{ entity: { eid: 'b1' }, doc: { title: 'Dune' } }])
|
|
20
|
+
* // g.read('.kind=doc') // → the bundles, no await
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* {@link ram} binds a {@link Store} to a vocabulary. `install()` does
|
|
24
|
+
* nothing — a Map has no schema. `tx()` commits when its body
|
|
25
|
+
* returns and rolls back when it throws, so a rejected write leaves the map
|
|
26
|
+
* exactly as it was.
|
|
27
|
+
*
|
|
28
|
+
* ## What it is not
|
|
29
|
+
* It is not a database: there is no persistence and no process boundary. Its
|
|
30
|
+
* indexes (by component, and by value where a query asks for one) decide how
|
|
31
|
+
* much a read looks at, never what it answers. A query it cannot answer
|
|
32
|
+
* exactly — a nearest-neighbour search, an edge listing — throws @yaks/match's
|
|
33
|
+
* `Unsupported`, the same error @yaks/sql throws.
|
|
34
|
+
*
|
|
35
|
+
* @module
|
|
36
|
+
*/ export { type Query, ram, type RamOpts, type Store, type Tx } from "./store.js";
|
|
37
|
+
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sources":["../mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmCC,GAED,SAAS,KAAK,KAAK,EAAE,GAAG,EAAE,KAAK,OAAO,EAAE,KAAK,KAAK,EAAE,KAAK,EAAE,qBAAoB"}
|