• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

microstates / lab / 16
96%
master: 96%

Build:
Build:
LAST BUILD BRANCH: greenkeeper/monorepo.babel7-20200530201154
DEFAULT BRANCH: master
Ran 07 Dec 2018 11:52PM UTC
Jobs 1
Files 12
Run time 557min
Badge
Embed ▾
README BADGES
x

If you need to use a raster PNG badge, change the '.svg' to '.png' in the link

Markdown

Textile

RDoc

HTML

Rst

pending completion
16

Pull #10

travis-ci

web-flow
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: {
   ... (continued)
Pull Request #10: Abstract Relationships

107 of 136 branches covered (78.68%)

Branch coverage included in aggregate %.

15 of 15 new or added lines in 5 files covered. (100.0%)

211 of 244 relevant lines covered (86.48%)

126.48 hits per line

Jobs
ID Job ID Ran Files Coverage
5 16.5 07 Dec 2018 11:52PM UTC 0
83.68
Travis Job 16.5
Source Files on build 16
Detailed source file information is not available for this build.
  • Back to Repo
  • Travis Build #16
  • Pull Request #10
  • PR Base - master (#14)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc