Model

Declare and validate your models using JSON-Schema definitions.

// register extension
Grown.use(require('@grown/model'));

// declares a single entity model
const Example = Grown.Model.Entity.define('Test', {
  $schema: {
    id: 'TestExample',
    type: 'object',
    properties: {
      value: {
        type: 'string',
      },
    },
  },
});

// connect database for this model only
const ExampleModel = await Example.connect({
  dialect: 'sqlite',
  storage: ':memory:',
});

// direct access to your sequelize-models
await ExampleModel.sync();
await ExampleModel.count();

Repositories are built by scanning source files in any given directory, e.g.

// register extension
Grown.use(require('@grown/model'));

fixture`./models/Example/index.js
  module.exports = {
    $schema: require('./schema')
  };
`;

fixture`./models/Example/schema.json
  {
    "id": "Example",
    "type": "object",
    "properties": {
      "value": {
        "type": "string"
      }
    }
  }
`;

// scan for all model definitions within
const repo = Grown.Model.DB.bundle({
  models: __dirname + '/models',
  database: {
    identifier: 'test',
    config: {
      dialect: 'sqlite',
      storage: ':memory:'
    }
  }
});

// expose RESTful endpoint to your models
const API = Grown.Model.Formator({
  database: Grown.Model.DB.test,
  prefix: '/db',
});

// connect the repository
await repo.connect();
await repo.sync();

// access model as a RESTful resource
const TestRepo = API.from(repo.models.Example);

await TestRepo.create({ value: 'OSOM' });

const testCount = await TestRepo.count();

console.log(testCount);

// close connection
await repo.disconnect();

In the example above we're using API as our RESTful adapter, we can also plug() it in our server application, e.g. server.plug(API) — that would mount the /db endpoint.

Both examples does not work on Runkit, but they should work if you test them locally. 💣


CLI

Public methods static

  • usage(bin) — Prints out the usage info for the given binary.
  • execute(db, cmd, argv) — Run the given cmd and argv through the given db connection.

See json-schema-sequelizer for usage info.


DB

Public methods static

  • registered(name) — Returns true if the named connection is already defined.
  • register(name, params) — Add a named connection to the registry where params is an object with config, refs and cwd options.
  • bundle(options) — Returns a repository built on top of given models directory and database options as connection. It have the following methods/props: disconnect(), connect(), sync(), get(model), connection, sequelize, schemas, models and $refs.

Private* props static

  • _registry — All registered database connections.

Entity

Public methods static

  • define(name, params) — Returns a new Entity model from the given name and params arguments.
  • connect(options[, refs, cwd]) — Set the connection for the given options; refs and cwd are for dereferencing.
  • getSchema(id[, refs]) — Returns the wrapped schema, given id is lookup through definitions schemas; refs are optional schemas.

Private* methods static

  • _validateFrom(id, ref, refs, data) — Returns a promise with the validation results from calling _assertFrom(...).
  • _assertFrom(id, ref, refs, data) — Retrieve the named schema by ìd and validates the given data input; ref and refs are optional.
  • _fakeFrom(id, refs, opts) — Generates examples based on the json-schema found by id-
  • _through(id, refs) — Used to resolve from definitions returning the same methods as _schema(...).
  • _schema(id, refs) — Used to resove from regular schemas; returns fakeOne, fakeMany, assert, and validate methods.
  • _wrap(id, def[, refs]) — Use to decorate any given model with the getSchema(...) method, only if is not present already.

See json-schema-faker for usage info.


Formator

Public methods static

  • $install(ctx) — Auto-mount endpoint for general usage; requires prefix, database and options to be defined.
  • from(Model, params, options) — Returns a RESTful resource for the given model; requires database to be defined. RESTful resources have six methods only: findAll, findOne, destroy, update, count and create.

See formator for usage info.