@yaks/edge 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 +325 -2
- package/_dist/comp.d.ts +7 -0
- package/_dist/comp.d.ts.map +1 -0
- package/_dist/eid.d.ts +43 -0
- package/_dist/eid.d.ts.map +1 -0
- package/_dist/graph.d.ts +10 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/guard.d.ts +8 -0
- package/_dist/guard.d.ts.map +1 -0
- package/_dist/keywords.d.ts +13 -0
- package/_dist/keywords.d.ts.map +1 -0
- package/_dist/mod.d.ts +58 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/order.d.ts +7 -0
- package/_dist/order.d.ts.map +1 -0
- package/_dist/plugin.d.ts +23 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/relations.d.ts +44 -0
- package/_dist/relations.d.ts.map +1 -0
- package/_dist/say.d.ts +13 -0
- package/_dist/say.d.ts.map +1 -0
- package/_dist/sql.d.ts +10 -0
- package/_dist/sql.d.ts.map +1 -0
- package/_dist/vocab.d.ts +10 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/walk.d.ts +16 -0
- package/_dist/walk.d.ts.map +1 -0
- package/comp.js +25 -0
- package/comp.js.map +1 -0
- package/comp.ts +26 -0
- package/deno.json +1 -0
- package/eid.js +66 -0
- package/eid.js.map +1 -0
- package/eid.ts +109 -0
- package/graph.js +18 -0
- package/graph.js.map +1 -0
- package/graph.ts +23 -0
- package/guard.js +71 -0
- package/guard.js.map +1 -0
- package/guard.ts +75 -0
- package/keywords.js +32 -0
- package/keywords.js.map +1 -0
- package/keywords.ts +32 -0
- package/meta/edge.vocab.json +20 -0
- package/mod.js +58 -0
- package/mod.js.map +1 -0
- package/mod.ts +59 -0
- package/order.js +59 -0
- package/order.js.map +1 -0
- package/order.ts +44 -0
- package/package.json +28 -4
- package/plugin.js +47 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +42 -0
- package/relations.js +80 -0
- package/relations.js.map +1 -0
- package/relations.ts +92 -0
- package/say.js +39 -0
- package/say.js.map +1 -0
- package/say.ts +41 -0
- package/sql.js +58 -0
- package/sql.js.map +1 -0
- package/sql.ts +68 -0
- package/vocab.js +19 -0
- package/vocab.js.map +1 -0
- package/vocab.json +52 -0
- package/vocab.ts +21 -0
- package/walk.js +88 -0
- package/walk.js.map +1 -0
- package/walk.ts +116 -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,326 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @yaks/edge
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A **link** is a directed connection represented by a graph
|
|
4
|
+
[entity](../graph/README.md#data-model), carrying `edge: { from, to, ord? }` and
|
|
5
|
+
a second component naming its **relation**, the kind of connection, such as
|
|
6
|
+
`cites`. @yaks/edge creates links with content-derived eids, validates writes,
|
|
7
|
+
and follows links through storage reads and SQL queries.
|
|
8
|
+
|
|
9
|
+
The relation's **component name** is the name stored beside `edge`; its **query
|
|
10
|
+
name** is the name used to follow that relation in a
|
|
11
|
+
[query](../query/README.md#query-model). Declaring `edge: true` makes the names
|
|
12
|
+
identical. Declaring `edge: 'linked'` on a `links` component makes `links` the
|
|
13
|
+
component name and `linked` the query name. Applications declare their own
|
|
14
|
+
relations in their [vocabulary](../vocab/README.md#vocabulary).
|
|
15
|
+
|
|
16
|
+
A link is an ordinary [bundle](../graph/README.md#data-model). It can carry
|
|
17
|
+
application metadata and use the graph's write and sync interfaces. Its ends and
|
|
18
|
+
relation make its eid, so a link's bundle need not name its entity:
|
|
19
|
+
`{ edge: { from, to }, cites: {} }` is written under that eid. The optional
|
|
20
|
+
`edge.ord` property records a position; the traversal helpers do not sort by it.
|
|
21
|
+
|
|
22
|
+
## Create and remove links
|
|
23
|
+
|
|
24
|
+
Load the component declaration and keywords, then register `edges(vocab)` on the
|
|
25
|
+
graph. Pass `relationIn(vocab, name)` to `link()` and `unlink()` for a relation
|
|
26
|
+
with a component UUID. A string addresses a relation by its component label.
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { equal } from '@yaks/testing'
|
|
30
|
+
import { loadVocab } from '@yaks/vocab'
|
|
31
|
+
import { graph } from '@yaks/graph'
|
|
32
|
+
import { ram } from '@yaks/ram'
|
|
33
|
+
import { edgeDoc, edgeEid, edgeKeywords, edges, link, unlink } from '@yaks/edge'
|
|
34
|
+
|
|
35
|
+
let blog = {
|
|
36
|
+
$defs: {
|
|
37
|
+
post: { component: true, type: 'object' },
|
|
38
|
+
cites: { component: true, type: 'object', edge: true },
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
let vocab = loadVocab([edgeDoc, blog], [edgeKeywords])
|
|
42
|
+
let g = graph({ storage: ram(vocab), vocab, plugins: [edges(vocab)] })
|
|
43
|
+
await g.apply([
|
|
44
|
+
{ entity: { eid: 'p1' }, post: {} },
|
|
45
|
+
{ entity: { eid: 'p2' }, post: {} },
|
|
46
|
+
link('p1', 'cites', 'p2', 1),
|
|
47
|
+
])
|
|
48
|
+
let eid = edgeEid('p1', 'cites', 'p2')
|
|
49
|
+
equal((await g.read('.cites')).map((b) => b.entity.eid), [eid])
|
|
50
|
+
|
|
51
|
+
// Omitting ord leaves the stored position alone.
|
|
52
|
+
await g.apply([link('p1', 'cites', 'p2')])
|
|
53
|
+
equal((await g.read('.cites&?edge'))[0].edge, { from: 'p1', to: 'p2', ord: 1 })
|
|
54
|
+
await g.apply([unlink('p1', 'cites', 'p2')])
|
|
55
|
+
equal(await g.read('.cites'), [])
|
|
56
|
+
await g.apply([link('p1', 'cites', 'p2')])
|
|
57
|
+
equal((await g.read('.cites')).map((b) => b.entity.eid), [eid])
|
|
58
|
+
|
|
59
|
+
// Deleting either endpoint deletes the link entity.
|
|
60
|
+
await g.apply([{ entity: { eid: 'p2' }, $delete: true }])
|
|
61
|
+
equal(await g.read('.edge'), [])
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Put a link in its place
|
|
65
|
+
|
|
66
|
+
Where order matters, a list of links is drawn by `edge.ord`. `placed()` takes
|
|
67
|
+
the links of a list in the order they are drawn, the link to put and its new
|
|
68
|
+
index among the others, and gives the bundles that put it there: one of them
|
|
69
|
+
moving, or a new link (`link()`) joining. Between neighbours that each have a
|
|
70
|
+
position, the moved link alone is written, halfway between them. A list without
|
|
71
|
+
positions is numbered from 1 in its new order, and only the links whose position
|
|
72
|
+
changes are written.
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { equal } from '@yaks/testing'
|
|
76
|
+
import { link, placed } from '@yaks/edge'
|
|
77
|
+
|
|
78
|
+
let [a, b, c] = ['a', 'b', 'c'].map((to, i) => link('p', 'cites', to, i + 1))
|
|
79
|
+
// c between a and b: c alone, halfway
|
|
80
|
+
equal(placed([a, b, c], c, 1), [{ entity: c.entity, edge: { ord: 1.5 } }])
|
|
81
|
+
// a list with no positions yet is numbered in its new order
|
|
82
|
+
let loose = ['a', 'b', 'c'].map((to) => link('p', 'cites', to))
|
|
83
|
+
equal(placed(loose, loose[2], 0).map((x) => x.edge), [
|
|
84
|
+
{ ord: 1 },
|
|
85
|
+
{ ord: 2 },
|
|
86
|
+
{ ord: 3 },
|
|
87
|
+
])
|
|
88
|
+
// a link joining comes whole, with its place
|
|
89
|
+
equal(placed([a, b], link('p', 'cites', 'd'), 2), [link('p', 'cites', 'd', 3)])
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
`edgeEid(from, relation, to)` hashes the two endpoints with the relation
|
|
93
|
+
component's UUID between them. Direction matters. A graph can also address an
|
|
94
|
+
existing link by the eid derived from its component label. When the graph
|
|
95
|
+
receives the full triple, it reuses that eid if the link is stored there.
|
|
96
|
+
`unlink()` deletes the link entity along with its application metadata.
|
|
97
|
+
|
|
98
|
+
Both endpoints are [references](../vocab/README.md#routing-and-references) with
|
|
99
|
+
`death: cascade`. The graph enforces reference validity and deletion;
|
|
100
|
+
`edges(vocab)` supplies eid derivation and validation.
|
|
101
|
+
|
|
102
|
+
## Exports
|
|
103
|
+
|
|
104
|
+
| Import | Exports and purpose |
|
|
105
|
+
| ------------------ | ------------------------------------------------------------------------------------------------------------------------ |
|
|
106
|
+
| `@yaks/edge` | `edgeDoc`, `EDGE`: component declaration and name; `edgeKeywords`, `EDGE_URI`: keyword registration |
|
|
107
|
+
| `@yaks/edge` | `relations`, `names`, `reversed`: interpret relation declarations |
|
|
108
|
+
| `@yaks/edge` | `link`, `unlink`, `edgeEid`: create and remove links and compute eids |
|
|
109
|
+
| `@yaks/edge` | `placed`: put a link in its place among the others of its list |
|
|
110
|
+
| `@yaks/edge` | `tagOf`, `derive`: find the relation component and supply eid derivation |
|
|
111
|
+
| `@yaks/edge` | `edges`, `stated`: graph plugin and validation hook |
|
|
112
|
+
| `@yaks/edge` | `walk`, `Walk`, `Dir`: storage traversal; `traverse`: SQL compiler extension |
|
|
113
|
+
| `@yaks/edge/vocab` | `docs`, `keywords`, `description`, `edgeDoc`, `edgeKeywords`, `relations`, `names`, `reversed`: vocabulary contributions |
|
|
114
|
+
| `@yaks/edge/graph` | `plugins`, `extend`: graph plugins and SQL compiler extensions for composition |
|
|
115
|
+
|
|
116
|
+
## Relation declarations
|
|
117
|
+
|
|
118
|
+
Register `edgeKeywords` when loading the vocabulary. `relations()` maps query
|
|
119
|
+
names to component names; `names()` maps component names to query names.
|
|
120
|
+
`reversed()` maps query names to the phrases declared by `reversed`, for a
|
|
121
|
+
reader displaying links arriving at an entity. Relations without a `reversed`
|
|
122
|
+
phrase are absent from that map.
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
import { equal } from '@yaks/testing'
|
|
126
|
+
import { loadVocab } from '@yaks/vocab'
|
|
127
|
+
import { edgeDoc, edgeKeywords, names, relations, reversed } from '@yaks/edge'
|
|
128
|
+
|
|
129
|
+
let vocab = loadVocab([edgeDoc, {
|
|
130
|
+
$defs: {
|
|
131
|
+
cites: {
|
|
132
|
+
component: true,
|
|
133
|
+
type: 'object',
|
|
134
|
+
edge: true,
|
|
135
|
+
reversed: 'cited by',
|
|
136
|
+
},
|
|
137
|
+
links: { component: true, type: 'object', edge: 'linked' },
|
|
138
|
+
},
|
|
139
|
+
}], [edgeKeywords])
|
|
140
|
+
equal(relations(vocab), { cites: 'cites', linked: 'links' })
|
|
141
|
+
equal(names(vocab), { cites: 'cites', links: 'linked' })
|
|
142
|
+
equal(reversed(vocab), { cites: 'cited by' })
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Eid derivation and validation
|
|
146
|
+
|
|
147
|
+
`edges(vocab)` derives an eid for a link written using a graph
|
|
148
|
+
[alias](../graph/README.md#ids-and-names). When a complete link supplies either
|
|
149
|
+
derived eid, the graph uses the existing link if it finds one. In the graph's
|
|
150
|
+
`mint` [phase](../graph/README.md#data-model), its `stated(vocab)` hook refuses
|
|
151
|
+
writes that specify an endpoint without the other endpoint or a declared
|
|
152
|
+
relation component. A patch specifying neither endpoint can update `ord` without
|
|
153
|
+
restating the link. Bundles for the same entity in a batch are checked together.
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
import { equal, throws } from '@yaks/testing'
|
|
157
|
+
import { loadVocab } from '@yaks/vocab'
|
|
158
|
+
import { graph, mint } from '@yaks/graph'
|
|
159
|
+
import { ram } from '@yaks/ram'
|
|
160
|
+
import {
|
|
161
|
+
derive,
|
|
162
|
+
edgeDoc,
|
|
163
|
+
edgeEid,
|
|
164
|
+
edgeKeywords,
|
|
165
|
+
edges,
|
|
166
|
+
names,
|
|
167
|
+
tagOf,
|
|
168
|
+
} from '@yaks/edge'
|
|
169
|
+
|
|
170
|
+
let vocab = loadVocab([edgeDoc, {
|
|
171
|
+
$defs: { cites: { component: true, type: 'object', edge: true } },
|
|
172
|
+
}], [edgeKeywords])
|
|
173
|
+
let g = graph({ storage: ram(vocab), vocab, plugins: [edges(vocab)] })
|
|
174
|
+
let p1 = mint()
|
|
175
|
+
let p2 = mint()
|
|
176
|
+
let bundle = {
|
|
177
|
+
entity: { eid: '$link' },
|
|
178
|
+
edge: { from: p1, to: p2 },
|
|
179
|
+
cites: {},
|
|
180
|
+
}
|
|
181
|
+
equal(tagOf(bundle, names(vocab)), 'cites')
|
|
182
|
+
equal(derive(names(vocab))(bundle.edge, bundle), edgeEid(p1, 'cites', p2))
|
|
183
|
+
await g.apply([bundle])
|
|
184
|
+
equal((await g.read('.cites')).map((b) => b.entity.eid), [
|
|
185
|
+
edgeEid(p1, 'cites', p2),
|
|
186
|
+
])
|
|
187
|
+
throws(() =>
|
|
188
|
+
g.apply([{ entity: { eid: 'bad' }, edge: { from: p1 }, cites: {} }])
|
|
189
|
+
)
|
|
190
|
+
throws(() => g.apply([{ entity: { eid: 'bad' }, edge: { from: p1, to: p2 } }]))
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Following links through storage
|
|
194
|
+
|
|
195
|
+
The `walk()` helpers follow links of one relation through
|
|
196
|
+
[storage](../graph/README.md#data-model). `walk(storage, vocab)` returns `out`
|
|
197
|
+
for outgoing links, `in` for incoming links, and `reach` for links within a
|
|
198
|
+
required depth limit. These methods take query names and return endpoint eids.
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
import { equal } from '@yaks/testing'
|
|
202
|
+
import { loadVocab } from '@yaks/vocab'
|
|
203
|
+
import { graph, mint } from '@yaks/graph'
|
|
204
|
+
import { ram } from '@yaks/ram'
|
|
205
|
+
import { edgeDoc, edgeKeywords, edges, link, walk } from '@yaks/edge'
|
|
206
|
+
|
|
207
|
+
let vocab = loadVocab([edgeDoc, {
|
|
208
|
+
$defs: { links: { component: true, type: 'object', edge: 'linked' } },
|
|
209
|
+
}], [edgeKeywords])
|
|
210
|
+
let store = ram(vocab)
|
|
211
|
+
let g = graph({ storage: store, vocab, plugins: [edges(vocab)] })
|
|
212
|
+
let p1 = mint()
|
|
213
|
+
let p2 = mint()
|
|
214
|
+
let p3 = mint()
|
|
215
|
+
await g.apply([
|
|
216
|
+
link(p1, 'links', p2),
|
|
217
|
+
link(p2, 'links', p3),
|
|
218
|
+
])
|
|
219
|
+
let w = walk(store, vocab)
|
|
220
|
+
equal(await w.out(p1, 'linked'), [p2])
|
|
221
|
+
equal(await w.in(p3, 'linked'), [p2])
|
|
222
|
+
equal(await w.reach(p1, 'linked', 2), [p2, p3])
|
|
223
|
+
equal(await w.reach(p3, 'linked', 2, 'in'), [p2, p1])
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
Synchronous storage produces synchronous results; asynchronous storage produces
|
|
227
|
+
promises. `reach()` defaults to outgoing links and deduplicates visited eids. It
|
|
228
|
+
includes the start only if a path of at least one hop returns to it. Unknown
|
|
229
|
+
query names throw. To read link bundles instead of endpoint eids, use storage
|
|
230
|
+
reads such as `.edge.from=p1 .links`.
|
|
231
|
+
|
|
232
|
+
## Following links in SQL queries
|
|
233
|
+
|
|
234
|
+
Register `traverse(vocab)` as an [SQL compiler extension](../sql/README.md), or
|
|
235
|
+
pass it to the SQLite adapter's `extend` option. The extension compiles relation
|
|
236
|
+
walks and accepts the query's [edges clause](../query/README.md).
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
import { equal } from '@yaks/testing'
|
|
240
|
+
import { loadVocab } from '@yaks/vocab'
|
|
241
|
+
import { graph } from '@yaks/graph'
|
|
242
|
+
import { open } from '@yaks/sqlite/db'
|
|
243
|
+
import { storage } from '@yaks/sqlite'
|
|
244
|
+
import { edgeDoc, edgeKeywords, edges, link, traverse } from '@yaks/edge'
|
|
245
|
+
|
|
246
|
+
let vocab = loadVocab([edgeDoc, {
|
|
247
|
+
$defs: {
|
|
248
|
+
post: { component: true, type: 'object' },
|
|
249
|
+
cites: { component: true, type: 'object', edge: true },
|
|
250
|
+
},
|
|
251
|
+
}], [edgeKeywords])
|
|
252
|
+
let db = open(':memory:')
|
|
253
|
+
try {
|
|
254
|
+
let store = storage(db, vocab, { extend: [traverse(vocab)] })
|
|
255
|
+
store.install()
|
|
256
|
+
let g = graph({ storage: store, vocab, plugins: [edges(vocab)] })
|
|
257
|
+
await g.apply([
|
|
258
|
+
{ entity: { eid: 'p1' }, post: {} },
|
|
259
|
+
{ entity: { eid: 'p2' }, post: {} },
|
|
260
|
+
{ entity: { eid: 'p3' }, post: {} },
|
|
261
|
+
link('p1', 'cites', 'p2'),
|
|
262
|
+
link('p2', 'cites', 'p3'),
|
|
263
|
+
])
|
|
264
|
+
let found = async (q: string) =>
|
|
265
|
+
(await store.read(q)).map((b) => b.entity.eid).sort()
|
|
266
|
+
equal(await found('.cites[<=1]->p3'), ['p2'])
|
|
267
|
+
equal(await found('.cites[<=2]->p3'), ['p1', 'p2'])
|
|
268
|
+
equal(await found('.cites<-p1'), ['p2', 'p3'])
|
|
269
|
+
equal((await found('.cites&.edge.to=p3')).length, 1)
|
|
270
|
+
equal(await found('.post&.edges_to.edge.from=p1'), ['p2'])
|
|
271
|
+
} finally {
|
|
272
|
+
db.close()
|
|
273
|
+
}
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
Without a bracketed depth, SQL walks exclude the start, deduplicate eids, and
|
|
277
|
+
limit results to 10,000 eids (`WALK_LIMIT` in @yaks/sql). An explicit depth
|
|
278
|
+
bounds recursion by hops and can include the start if a cycle returns to it.
|
|
279
|
+
Reference-property walks remain the SQL compiler's responsibility. Undeclared
|
|
280
|
+
relations are refused.
|
|
281
|
+
|
|
282
|
+
A link is an entity, so its rows are read like any other: `.cites&.edge.to=p3`
|
|
283
|
+
selects the links citing p3, and a reverse association reaches them from a row
|
|
284
|
+
(`.post&.edges_to.edge.from=p1`). The retired `.edges` rider reads as no clause
|
|
285
|
+
at all (@yaks/query `retired`).
|
|
286
|
+
|
|
287
|
+
## Composition
|
|
288
|
+
|
|
289
|
+
`@yaks/edge/vocab` provides vocabulary documents and keywords without importing
|
|
290
|
+
storage or SQL. `@yaks/edge/graph` accepts an object with the loaded vocabulary
|
|
291
|
+
and supplies both the graph plugin and SQL compiler extension.
|
|
292
|
+
|
|
293
|
+
```ts
|
|
294
|
+
import { equal } from '@yaks/testing'
|
|
295
|
+
import { loadVocab } from '@yaks/vocab'
|
|
296
|
+
import { docs, keywords } from '@yaks/edge/vocab'
|
|
297
|
+
import { extend, plugins } from '@yaks/edge/graph'
|
|
298
|
+
import { graph, mint } from '@yaks/graph'
|
|
299
|
+
import { open } from '@yaks/sqlite/db'
|
|
300
|
+
import { storage } from '@yaks/sqlite'
|
|
301
|
+
|
|
302
|
+
let vocab = loadVocab([...docs, {
|
|
303
|
+
$defs: { cites: { component: true, type: 'object', edge: true } },
|
|
304
|
+
}], keywords)
|
|
305
|
+
let host = { vocab }
|
|
306
|
+
let db = open(':memory:')
|
|
307
|
+
try {
|
|
308
|
+
let store = storage(db, vocab, { extend: extend(host) })
|
|
309
|
+
store.install()
|
|
310
|
+
let g = graph({ storage: store, vocab, plugins: plugins(host) })
|
|
311
|
+
let p1 = mint()
|
|
312
|
+
let p2 = mint()
|
|
313
|
+
await g.apply([{
|
|
314
|
+
entity: { eid: '$link' },
|
|
315
|
+
edge: { from: p1, to: p2 },
|
|
316
|
+
cites: {},
|
|
317
|
+
}])
|
|
318
|
+
equal((await g.read(`.cites[<=2]->${p2}`)).map((b) => b.entity.eid), [p1])
|
|
319
|
+
} finally {
|
|
320
|
+
db.close()
|
|
321
|
+
}
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
The package supplies no application relations or storage adapter. SQL traversal
|
|
325
|
+
assumes the SQLite layout used by [@yaks/sqlite](../sqlite/README.md). The core
|
|
326
|
+
uses no platform-specific APIs and can run in Deno, Node and browsers.
|
package/_dist/comp.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { VocabDoc } from "@yaks/vocab";
|
|
2
|
+
/**
|
|
3
|
+
* The `edge` component as a vocabulary document, to load beside your own:
|
|
4
|
+
* `loadVocab([edgeDoc, ...mine], [edgeKeywords])`. The relation components are
|
|
5
|
+
* yours to declare — this document is only the link itself.
|
|
6
|
+
*/ export declare let edgeDoc: VocabDoc;
|
|
7
|
+
//# sourceMappingURL=comp.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"comp.d.ts","sources":["../comp.ts"],"names":[],"mappings":"AAiBA,cAAc,QAAQ,2BAAgC;AAGtD;;;;CAIC,GACD,OAAO,YAAI,SAAS,SAAc"}
|
package/_dist/eid.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Bundle, Eid } from "@yaks/graph";
|
|
2
|
+
import type { Derive } from "@yaks/graph";
|
|
3
|
+
import { type Vocab, type VocabDoc } from "@yaks/vocab/runtime";
|
|
4
|
+
/**
|
|
5
|
+
* A relation as a link names it: its label, the component the link carries,
|
|
6
|
+
* and its identity, what the link's eid hashes. A label alone is a relation
|
|
7
|
+
* with no `$id` yet, whose label is its identity.
|
|
8
|
+
*/ export type Relation = string | {
|
|
9
|
+
label: string;
|
|
10
|
+
id: string;
|
|
11
|
+
};
|
|
12
|
+
/** One relation a vocabulary document declares, for {@link edgeEid},
|
|
13
|
+
* `link` and `unlink`: `relation(kernelDoc, 'cites')`. */ export declare let relation: (doc: VocabDoc, label: string) => Relation;
|
|
14
|
+
/** One relation as a loaded vocabulary holds it, for a relation named by
|
|
15
|
+
* data: `relationIn(vocab, tag)`. */ export declare let relationIn: (vocab: Pick<Vocab, 'comp'>, label: string) => Relation;
|
|
16
|
+
/**
|
|
17
|
+
* The eid a link derives: `sha256("<from>|<relation>|<to>")` formatted as a
|
|
18
|
+
* UUID (@yaks/graph's `derivedEid` — the one derivation shared by everything
|
|
19
|
+
* content-addressed, so an id computed here and an id computed elsewhere are
|
|
20
|
+
* the same id).
|
|
21
|
+
*
|
|
22
|
+
* `relation` is the component stored beside `edge` ({@link relation}), or
|
|
23
|
+
* its identity (`identityIn` over a loaded vocabulary), or its label while it
|
|
24
|
+
* has no `$id`; never the name a query uses for it.
|
|
25
|
+
*/ export declare let edgeEid: (from: Eid, relation: Relation, to: Eid) => Eid;
|
|
26
|
+
/**
|
|
27
|
+
* The name of the relation component in a bundle, or `undefined` when it has
|
|
28
|
+
* none. `tags` is the vocabulary's component → relation-name map
|
|
29
|
+
* ({@link names}); only a declared relation counts, so an ordinary component
|
|
30
|
+
* stored beside the edge is not mistaken for one.
|
|
31
|
+
*/ export declare let tagOf: (bundle: Bundle, tags: Record<string, string>) => string | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* How the `edge` component derives its own entity id — the {@link Derive} the
|
|
34
|
+
* graph calls when an edge bundle arrives under a `$alias`, so that the write
|
|
35
|
+
* creating a link also learns the id it landed on. The relation is hashed by
|
|
36
|
+
* the identity `vocab` gives it.
|
|
37
|
+
*
|
|
38
|
+
* An incomplete link derives nothing (it returns `''`) and the entity is given
|
|
39
|
+
* an ordinary generated id, at which point the {@link stated} hook rejects the
|
|
40
|
+
* write and names the missing part — a far better error than a link quietly
|
|
41
|
+
* identified by half of itself.
|
|
42
|
+
*/ export declare let derive: (tags: Record<string, string>, vocab?: Pick<Vocab, 'comp'>) => Derive;
|
|
43
|
+
//# sourceMappingURL=eid.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"eid.d.ts","sources":["../eid.ts"],"names":[],"mappings":"AAgBA,cAAc,MAAM,EAAQ,GAAG,2BAAgC;AAG/D,cAAc,MAAM,2BAAgC;AAEpD,SAGE,KAAK,KAAK,EACV,KAAK,QAAQ,mCACwB;AAEvC;;;;CAIC,GACD,YAAY,WAAW,MAAM;EAAK,OAAO,MAAM;EAAE,IAAI,MAAM;;AAE3D;wDACwD,GACxD,OAAO,YAAI,WAAY,KAAK,UAAU,OAAO,MAAM,KAAG,SAGpD;AAEF;mCACmC,GACnC,OAAO,YAAI,aACT,OAAO,KAAK,OAAO,SACnB,OAAO,MAAM,KACZ,SAGD;AAEF;;;;;;;;;CASC,GACD,OAAO,YAAI,UAAW,MAAM,KAAK,UAAU,UAAU,IAAI,QAAM,IAG5D;AAEH;;;;;CAKC,GACD,OAAO,YAAI,QACT,QAAQ,QACR,MAAM,OAAO,MAAM,EAAE,MAAM,MAC1B,MAAM,GAAG,SAAS,CAC0C;AAE/D;;;;;;;;;;CAUC,GACD,OAAO,YAAI,SACT,MAAM,OAAO,MAAM,EAAE,MAAM,GAC3B,QAAQ,KAAK,OAAO,YACnB,OAeF"}
|
package/_dist/graph.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Plugin } from "@yaks/graph";
|
|
2
|
+
import type { Extension } from "@yaks/sql";
|
|
3
|
+
import type { Vocab } from "@yaks/vocab";
|
|
4
|
+
/** Derived edge ids, and the relations this vocabulary declares. */ export declare let plugins: (host: {
|
|
5
|
+
vocab: Vocab;
|
|
6
|
+
}) => Plugin[];
|
|
7
|
+
/** The walk over a relation, compiled (./sql.ts). */ export declare let extend: (host: {
|
|
8
|
+
vocab: Vocab;
|
|
9
|
+
}) => Extension[];
|
|
10
|
+
//# sourceMappingURL=graph.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph.d.ts","sources":["../graph.ts"],"names":[],"mappings":"AAUA,cAAc,MAAM,2BAAgC;AACpD,cAAc,SAAS,yBAA8B;AACrD,cAAc,KAAK,2BAAgC;AAInD,kEAAkE,GAClE,OAAO,YAAI,UAAW;EAAQ,OAAO;MAAU,SAA+B;AAE9E,mDAAmD,GACnD,OAAO,YAAI,SAAU;EAAQ,OAAO;MAAU,YAE7C"}
|
package/_dist/guard.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Bundle, Hook } from "@yaks/graph";
|
|
2
|
+
import type { Vocab } from "@yaks/vocab";
|
|
3
|
+
/**
|
|
4
|
+
* The `mint` hook that rejects an incomplete link, naming what is missing.
|
|
5
|
+
* Registered by the {@link plugin}; exported on its own for a graph that wants
|
|
6
|
+
* the check without the rest.
|
|
7
|
+
*/ export declare let stated: (vocab: Vocab) => Hook & ((bundles: Bundle[]) => Bundle[]);
|
|
8
|
+
//# sourceMappingURL=guard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guard.d.ts","sources":["../guard.ts"],"names":[],"mappings":"AAkBA,cAAc,MAAM,EAAa,IAAI,2BAAgC;AAGrE,cAAc,KAAK,2BAAgC;AAoBnD;;;;CAIC,GACD,OAAO,YAAI,SACT,OAAO,UACN,SAAS,SAAS,aAAa,UA0BjC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Keywords } from "@yaks/vocab";
|
|
2
|
+
/** The URI a vocab file declares under `$vocabulary` to use `edge`. */ export declare let EDGE_URI: string;
|
|
3
|
+
/**
|
|
4
|
+
* The `edge` keyword vocabulary, ready to register:
|
|
5
|
+
* `loadVocab(docs, [edgeKeywords])` copies each component's declarations onto
|
|
6
|
+
* `v.comp(name).keywords.edge` and `.reversed`, which is where
|
|
7
|
+
* {@link relations} and {@link reversed} read them.
|
|
8
|
+
*
|
|
9
|
+
* The keyword is the name of the component it accompanies, so one rule ("this
|
|
10
|
+
* component is stored beside that one") covers both an edge's relations and
|
|
11
|
+
* {@link https://jsr.io/@yaks/key | @yaks/key}'s.
|
|
12
|
+
*/ export declare let edgeKeywords: Keywords;
|
|
13
|
+
//# sourceMappingURL=keywords.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keywords.d.ts","sources":["../keywords.ts"],"names":[],"mappings":"AAWA,cAAc,QAAQ,2BAAgC;AAGtD,qEAAqE,GACrE,OAAO,YAAI,iBAAsC;AAEjD;;;;;;;;;CASC,GACD,OAAO,YAAI,cAAc,SAIxB"}
|