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

microstates / lab / 27 / 5
96%
master: 96%

Build:
Build:
LAST BUILD BRANCH: greenkeeper/monorepo.babel7-20200530201154
DEFAULT BRANCH: master
Ran 11 Dec 2018 07:38PM UTC
Files 10
Run time 2s
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

11 Dec 2018 07:34PM UTC coverage: 96.018% (-0.4%) from 96.429%
27.5

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

53 of 60 branches covered (88.33%)

Branch coverage included in aggregate %.

164 of 166 relevant lines covered (98.8%)

182.23 hits per line

Source Files on job 27.5
  • Tree
  • List 0
  • Changed 7
  • Source Changed 4
  • Coverage Changed 6
Coverage ∆ File Lines Relevant Covered Missed Hits/Line Branch Hits Branch Misses
  • Back to Build 53
  • Travis Job 27.5
  • 90024e74 on github
  • Prev Job for on cl/linker-architecture (#27.5)
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