cmbf-hapi-restmodel
Advanced tools
Comparing version 0.4.0 to 0.4.1
{ | ||
"name": "cmbf-hapi-restmodel", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "bluebird": "^3.0.6", |
@@ -12,3 +12,3 @@ # CMBF REST Model | ||
```npm install cmbf-restmod --save``` | ||
```npm install cmbf-hapi-restmodel --save``` | ||
@@ -61,1 +61,77 @@ With the CMBF launcher, you just add a server hook to load additional plugins like this : | ||
## Model | ||
Ideally, you define your model using the new ES6 class concept: | ||
``` | ||
var Joi = require('joi'); | ||
module.exports = function(server, config, log) { | ||
"use strict"; | ||
var BaseModel = server.plugins['cmbf-hapi-restmodel'].BaseModel; | ||
var clock = server.plugins['covistra-system'].clock; | ||
class Document extends BaseModel { | ||
static get endpoint() { | ||
return "documents"; | ||
} | ||
static get auth() { | ||
return "token"; | ||
} | ||
static get name() { | ||
return "document"; | ||
} | ||
static get collection() { | ||
return 'documents'; | ||
} | ||
static get Schema() { | ||
return Joi.object().keys({ | ||
key: Joi.string().required(), | ||
name: Joi.string().required(), | ||
ownerId: Joi.string(), | ||
customerId: Joi.string(), | ||
type: Joi.string(), | ||
tags: Joi.array().items(Joi.string()), | ||
content: Joi.any(), | ||
created_at: Joi.date().default(clock.nowTs, "Default to current time") | ||
}); | ||
} | ||
} | ||
return Document; | ||
}; | ||
``` | ||
You then register this model descriptor: | ||
``` | ||
var Document = require('./models/document')(server, config, log); | ||
modelManager.registerModel('document', Document`); | ||
``` | ||
This model will get automatically exposed when the server stats with all standard routes | ||
generated for you. | ||
### Overriding ID field | ||
Sometimes you need to override the field through which you model is referenced. By default, | ||
we use a ```id``` field, but you just have to override the idField property in your | ||
derived model: | ||
``` | ||
class MyModel extends BaseModel { | ||
static get idField() { | ||
return 'key'; | ||
} | ||
} | ||
``` | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30975
136