|
| 1 | +import { append, map, foldl } from 'funcadelic'; |
| 2 | +import { NumberType, StringType } from './types'; |
| 3 | +import { create as _create } from '../index'; |
| 4 | +import { view, At } from '../src/lens'; |
| 5 | +import { link, valueOf, pathOf, atomOf, typeOf, metaOf, ownerOf } from '../src/meta'; |
| 6 | + |
| 7 | +import faker from 'faker'; |
| 8 | + |
| 9 | +class Person { |
| 10 | + firstName = StringType; |
| 11 | + lastName = StringType; |
| 12 | +} |
| 13 | + |
| 14 | +class Blog { |
| 15 | + title = StringType; |
| 16 | + author = belongsTo(Person, "people"); |
| 17 | +} |
| 18 | + |
| 19 | +class Comment {} |
| 20 | + |
| 21 | +function Table(Type, factory = {}) { |
| 22 | + |
| 23 | + return class Table { |
| 24 | + static Type = Type; |
| 25 | + static name = `Table<${Type.name}>`; |
| 26 | + |
| 27 | + nextId = NumberType; |
| 28 | + |
| 29 | + get length() { return Object.keys(this.records).length; } |
| 30 | + |
| 31 | + get latest() { return this.records[this.latestId]; } |
| 32 | + |
| 33 | + get latestId() { return this.nextId.state - 1; } |
| 34 | + |
| 35 | + get records() { |
| 36 | + return map((value, key) => ln(Type, pathOf(this).concat(["records", key]), this), this.state.records); |
| 37 | + } |
| 38 | + |
| 39 | + get state() { |
| 40 | + return valueOf(this) || { nextId: 0, records: {}}; |
| 41 | + } |
| 42 | + |
| 43 | + create(overrides = {}) { |
| 44 | + let id = this.nextId.state; |
| 45 | + let record = createAt(this.nextId.increment(), Type, ["records", id], { id }); |
| 46 | + |
| 47 | + let created = foldl((record, { key, value: attr }) => { |
| 48 | + if (record[key]) { |
| 49 | + let attrFn = typeof attr === 'function' ? attr : () => attr; |
| 50 | + |
| 51 | + // create a local link of the DB that returns itself. to pass into |
| 52 | + // the factory function. |
| 53 | + let db = link(_create(DB), DB, pathOf(this).slice(0, -1), atomOf(record)); |
| 54 | + |
| 55 | + let result = attrFn(overrides[key], db); |
| 56 | + |
| 57 | + let next = metaOf(result) ? link(record, Type, pathOf(record), atomOf(result)) : record; |
| 58 | + |
| 59 | + return next[key].set(result); |
| 60 | + } else { |
| 61 | + return record; |
| 62 | + } |
| 63 | + }, record, append(factory, overrides)); |
| 64 | + |
| 65 | + let owner = ownerOf(this); |
| 66 | + return link(this, typeOf(this), pathOf(this), atomOf(created), owner.Type, owner.path); |
| 67 | + } |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +class DB { |
| 74 | + people = Table(Person, { |
| 75 | + firstName: () => faker.name.firstName(), |
| 76 | + lastName: () => faker.name.lastName() |
| 77 | + }); |
| 78 | + blogs = Table(Blog, { |
| 79 | + title: () => faker.random.words(), |
| 80 | + author: (attrs, db) => { |
| 81 | + return db.people.create(attrs) |
| 82 | + .people.latest; |
| 83 | + } |
| 84 | + }); |
| 85 | + comments = Table(Comment); |
| 86 | +} |
| 87 | + |
| 88 | +function createAt(parent, Type, path, value) { |
| 89 | + return link(_create(Type), Type, pathOf(parent).concat(path), atomOf(parent)).set(value); |
| 90 | +} |
| 91 | + |
| 92 | +function ln(Type, path, owner) { |
| 93 | + return link(_create(Type), Type, path, atomOf(owner), typeOf(owner), pathOf(owner)); |
| 94 | +} |
| 95 | + |
| 96 | +import Relationship from '../src/relationship'; |
| 97 | + |
| 98 | +function linkTo(Type, path) { |
| 99 | + return new Relationship(resolve); |
| 100 | + |
| 101 | + function resolve(origin, originType, originPath /*, relationshipName */) { |
| 102 | + |
| 103 | + return { |
| 104 | + Type, |
| 105 | + path: path.reduce((path, element) => { |
| 106 | + if (element === '..') { |
| 107 | + return path.slice(0, -1); |
| 108 | + } else if (element === '.') { |
| 109 | + return path; |
| 110 | + } else { |
| 111 | + return path.concat(element); |
| 112 | + } |
| 113 | + }, originPath) |
| 114 | + }; |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +function belongsTo(T, tableName) { |
| 119 | + return new Relationship(resolve); |
| 120 | + |
| 121 | + function BelongsTo(originType, originPath, foreignKey) { |
| 122 | + return class BelongsTo extends T { |
| 123 | + set(value) { |
| 124 | + let origin = ln(originType, originPath, value); |
| 125 | + let id = valueOf(value).id; |
| 126 | + return origin.set(append(valueOf(origin), { |
| 127 | + [foreignKey]: id |
| 128 | + })); |
| 129 | + } |
| 130 | + }; |
| 131 | + } |
| 132 | + |
| 133 | + function resolve(origin, originType, originPath, relationshipName) { |
| 134 | + let foreignKey = `${relationshipName}Id`; |
| 135 | + let id = view(At(foreignKey), valueOf(origin)); |
| 136 | + let Type = BelongsTo(originType, originPath, foreignKey); |
| 137 | + let { resolve } = linkTo(Type, ["..", "..", "..", tableName, "records", id]); |
| 138 | + return resolve(origin, originType, originPath, relationshipName); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +export default _create(DB, {}); |
0 commit comments