Package Exports
- nxus-storage
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nxus-storage) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
nxus-storage
Storage Module
A storage framework for Nxus applications using waterline.
Configuration
"config": {
"storage": {
"adapter": {
"default": "sails-mongo"
},
"connections": {
"default": {
"adapter": "default",
"url": "mongodb://...."
}
},
"modelsDir": "./src/models"
}
}Creating models
Inherit your models from BaseModel
import {BaseModel} from '@nxus/storage'
var User = BaseModel.extend({
identity: 'user',
attributes: {
name: 'string'
}
})Register models
Either import your model class and pass it to model():
storage.model(modelClass)Or register all models in a directory with modelDir():
storage.modelDir(__dirname+"/models")Model events
The storage model emits events for create, update, and destroy, you can register a handler for all events:
storage.on('model.create', (identity, record) => {})
storage.on('model.update', (identity, record) => {})
storage.on('model.destroy', (identity, record) => {})Or just a specific model identity:
storage.on('model.create.user', (identity, record) => {})
storage.on('model.update.user', (identity, record) => {})
storage.on('model.destroy.user', (identity, record) => {})Lifecycle notes
load- Models should be registered during
load, e.g. var User = BaseModel.extend({ identity: 'user', ... }); application.get('storage').model(User)
- Models should be registered during
startupThe configured database is connected during
load.afterYou can query models from
startupand beyond, retrieve the model by the 'identity':application.get('storage').getModel('user').then((User) => { User.create(...); });
API
Storage
Extends NxusModule
Storage provides a common interface for defining models. Uses the Waterline ORM.
model
Register a model
Parameters
modelobject A Waterline-compatible model class
Examples
storage.model(...)getModel
Request a model based on its identity (name)
Parameters
Examples
storage.getModel('user')Returns Promise The model class(es)
modelDir
Register all models in a directory
Parameters
dirstring Directory containing model files
Examples
application.get('storage').model(...)Returns Promise Array of model identities
HasModels
Extends NxusModule
The HasModels class is a Base class for defining helper classes with Models.
All models contained in a ./models directory will be registered automatically, and are the
default list of model identities made available in the this.models object.
You may override or extend this list of model identities, or a mapping of model identities to variable names,
by overriding .modelNames()
modelNames
Deprecated: Override to define the model names to access
Examples
modelNames() {
return ['user']
}Returns (array | object) Model identities to add to this.models, or object of {identity: name}