vigetlabs / microcosm

Flux with actions at center stage. Write optimistic updates, cancel requests, and track changes with ease.
http://code.viget.com/microcosm/
MIT License
487 stars 29 forks source link

[micromanage] Use JSON schemas. Start Collection #498

Closed nhunzaker closed 6 years ago

nhunzaker commented 6 years ago

Changes the way schemas are parsed such that they use json-schema. It also begins the idea of a Collection domain.

This isn't complete:

Highlights

import { Entity, Collection } from 'micromanage'

export class Planet extends Entity {
  static schema = {
    title: 'Planet',
    type: 'object',
    required: ['name'],
    properties: {
      name: { type: 'string', title: 'Name' },
      weight: { type: 'number', title: 'Weight', default: 100, min: 0 },
      belt: { type: 'string', enum: ['inner', 'outer'], default: 'inner' }
    }
  }
}

export class Planets extends Collection(Planet) {
  all(matcher) {
    return this.map(Object.values)
  }
}

The neat thing about this is that you can use the schema as an API for other libraries, like form builders:

import React from 'react'
import Form from 'react-jsonschema-form'
import { Presenter } from 'microcosm-dom'
import { Planet, Planets } from '../domains/planets'

export class PlanetForm extends Presenter {
  getModel(repo) {
    return {
      planets: repo.domains.planets.all()
    }
  }

  render() {
    return (
      <div>
        <Form schema={Planet.schema} onSubmit={this.onSubmit.bind(this)} />
        <ul>{this.model.planets.map(p => <li key={p._id}>{p.name}</li>)}</ul>
      </div>
    )
  }

  onSubmit({ formData }) {
    this.repo.push(Planets.create, formData)
  }
}
codecov-io commented 6 years ago

Codecov Report

Merging #498 into master will not change coverage. The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #498   +/-   ##
=======================================
  Coverage   98.78%   98.78%           
=======================================
  Files          30       30           
  Lines         738      738           
  Branches      147      147           
=======================================
  Hits          729      729           
  Misses          9        9

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 52cb4be...2bad325. Read the comment docs.