Commit 1a91ddf
committed
Abstract Relationships
We've introduced a very, very low-level linking functionality to have
a microstate draw its value from anywhere in the tree. However, what's
lacking is a way to declaratively state from within the microstate
what those links _are_ and where they go.
So, for example in the BigTest network ORM, we want to be able to say:
```js
import { belongsTo } from '@bigtest/network';
class Blog {
title = String;
author = belongsTo(Person);
}
```
and for it to link to the correct person.
To accomplish this, we introduce the concept of a relationship which
is just an abstraction over a Type and a path. Every reference from
one microstate to another is now reconceived as a relatioship.
It's not that complicated though. A relationship is really just a
function that takes the holder object, it's type, it's path, and the
name of the relationship and returns a `{ Type, path }` pair that is
used for linking.
For example, the default relationship is `Child` and is defined as:
```js
function Child(spec) {
return new Relationship(resolve);
function resolve(origin, originType, path, name) {
let Type = typeof spec === 'function' ? spec : typeOf(spec);
return { Type, path: path.concat(name) };
}
}
```
It's what implements the first line DSL.
`belongsTo`, which resolves to a different place in the store, could
be implemented like so:
```js
function belongsTo(Type) {
return new Relationship(resolve);
function resolve(object, objectType, path, name) {
let tableName = pluralize(Type.name.toLowerCase());
let id = valueOf(this)[`${name}Id`];
return { Type, path: path.slice(-3).concat([tableName, "records", id]) };
}
}
```
this assumes a layout of the DB like:
```json
{
blogs: {
nextId: 2,
records: {
0: { title: 'How to blog', authorId: 'id1' },
1: {
title: 'How to build a fully immutable directed cyclic graph in javascript',
authorId: '0'
}
}
},
people: {
nextId: 1,
records: {
0: { firstName: 'Charles', lastName: 'Lowell' }
}
}
}
```
So you can see that what we're really doing is using a relative path
within the DB. The expression
```js
path.slice(-3).concat([tableName, "records", id])
```
is really equivalent to `["..", "..", "..", tableName, "records", id]`
if we were writing it out using a more classic method for expressing paths.
** Follow on Work / Questions **
- Need a way to specify what happens when you _set_ a
relationship. For example, in the case of `belongsTo`, you need to
update the `authorId` atribute at the appropriate path. We might be
able to use some sort of lens here.
- This will mess with Identity. Specifically, if a linked object that
isn't actually contained within this object changes, then the
identity of the object needs to change (I think). For example, if I
say:
```js
blog.author.firstName.set('Carlos')
```
even though the `valueOf(blog)` will not have changed (it still has
`{authorId: 0}`). It should be represented by a new object
because. This will be possible because of laziness.1 parent 1e680d5 commit 1a91ddf
File tree
7 files changed
+77
-76
lines changed- src
- tests
7 files changed
+77
-76
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
31 | 41 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
75 | | - | |
76 | | - | |
| 75 | + | |
| 76 | + | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | 13 | | |
18 | 14 | | |
19 | 15 | | |
| |||
57 | 53 | | |
58 | 54 | | |
59 | 55 | | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | 56 | | |
66 | 57 | | |
67 | 58 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
| 1 | + | |
4 | 2 | | |
5 | 3 | | |
6 | 4 | | |
| |||
118 | 116 | | |
119 | 117 | | |
120 | 118 | | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | 119 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
0 commit comments