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 givencmd
andargv
through the givendb
connection.
See json-schema-sequelizer for usage info.
DB
Public methods static
registered(name)
— Returnstrue
if the named connection is already defined.register(name, params)
— Add a named connection to the registry where params is an object withconfig
,refs
andcwd
options.bundle(options)
— Returns a repository built on top of givenmodels
directory anddatabase
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 newEntity
model from the givenname
andparams
arguments.connect(options[, refs, cwd])
— Set the connection for the given options;refs
andcwd
are for dereferencing.getSchema(id[, refs])
— Returns the wrapped schema, givenid
is lookup throughdefinitions
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 givendata
input;ref
andrefs
are optional._fakeFrom(id, refs, opts)
— Generates examples based on the json-schema found byid
-_through(id, refs)
— Used to resolve fromdefinitions
returning the same methods as_schema(...)
._schema(id, refs)
— Used to resove from regular schemas; returnsfakeOne
,fakeMany
,assert
, andvalidate
methods._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
,database
andoptions
to be defined.from(Model, params, options)
— Returns a RESTful resource for the given model; requiresdatabase
to be defined. RESTful resources have six methods only:findAll
,findOne
,destroy
,update
,count
andcreate
.
See formator for usage info.