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 givencmdandargvthrough the givendbconnection.
See json-schema-sequelizer for usage info.
DB
Public methods static
registered(name)— Returnstrueif the named connection is already defined.register(name, params)— Add a named connection to the registry where params is an object withconfig,refsandcwdoptions.bundle(options)— Returns a repository built on top of givenmodelsdirectory anddatabaseoptions as connection. It have the following methods/props:disconnect(),connect(),sync(),get(model),connection,sequelize,schemas,modelsand$refs.
Private* props static
_registry— All registered database connections.
Entity
Public methods static
define(name, params)— Returns a newEntitymodel from the givennameandparamsarguments.connect(options[, refs, cwd])— Set the connection for the given options;refsandcwdare for dereferencing.getSchema(id[, refs])— Returns the wrapped schema, givenidis lookup throughdefinitionsschemas;refsare 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ìdand validates the givendatainput;refandrefsare optional._fakeFrom(id, refs, opts)— Generates examples based on the json-schema found byid-_through(id, refs)— Used to resolve fromdefinitionsreturning the same methods as_schema(...)._schema(id, refs)— Used to resove from regular schemas; returnsfakeOne,fakeMany,assert, andvalidatemethods._wrap(id, def[, refs])— Use to decorate any given model with thegetSchema(...)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; requiresprefix,databaseandoptionsto be defined.from(Model, params, options)— Returns a RESTful resource for the given model; requiresdatabaseto be defined. RESTful resources have six methods only:findAll,findOne,destroy,update,countandcreate.
See formator for usage info.