The module is still under development, and not fully tested. I am going to use it with a project by Jan 2017, and still doing experiments with it currently.
Developed under NodeJS 6.9.1 with ES6. Will have a compatability test for some older versions, which show obvious compatable possibilties from http://node.green/. Here is a list of ES6 features used in the module:
- arrow function
- class
- object literals
- promise
- spread operator
Install the connector for your project.
$ npm install loopback-connector-contentful --save
Add the following configuration to the datasources.json.
"ds_contentful": {
"name": "ds_contentful",
"connector": "contentful",
"accessToken": "<access token>",
"spaceId": "<space id>",
"locale": "en-US",
"debug": true | false
}
Specify the datasource for your model in model-config.json.
"Product": {
"dataSource": "ds_contentful",
"options": {
"spaceId": "<space id>",
"locale": "en-US"
}
}
Model Definition
{
"name": "Product",
"options": {
"displayField": "productName"
},
"properties": {
"productName": "text",
"productDescription": {
"type": "text",
"required": false,
"contentful": {
"name": "Product Description",
"dataType": "Text"
"localized": false,
"validations": [{"size": {"min": 20, "max": 200}}],
"disabled": false,
"omitted": false
}
},
"tags": {
"type": [
"string"
]
}
},
"relations": {
"categories": {
"type": "hasMany",
"model": "Category",
"contentful": {
"name": "Product Categories",
"localized": false,
"validations": [],
"disabled": false,
"omitted": false
}
},
"brand": {
"type": "belongsTo",
"model": "Brand"
}
}
}
SpaceId MUST be defined in datasources.json file. When saving or retrieving a model, its space is resolved in the following order:
- Use spaceId defined in model-config.json. Raise error if no matching content type within the space.
- Use spaceId defined in datasources.json. Raise error if no matching content type within the space.
- Raise error if spaceId is not defined above.
When saving or retrieving a model, its local is resolved in the following order:
- Use locale defined in model-config.json.
- Use locale defined in datasources.json.
- If locale is not defined above, use the default locale of the space to save and retrieve models from contentful
module.exports = function(app) {
app.datasources['contentful'].autoupdate(function(err, result) {
});
};
LoopBack Type | Contentful Type |
---|---|
Text/JSON/Any | Text |
String | Symbol |
Number | Number |
Date | Date |
Boolean | Boolean |
GeoPoint | Location |
Object | Object |
["string"] | Array |
The following relations can be automatically created during autoupdate phase:
Loopback Relations | Status |
---|---|
belongsTo | Supported |
hasMany | Supported |
hasManyThrough | Not supported yet |
hasAndBelongsToMany | Not supported yet |
- connector.autoupdate
- connector.create
- connector.all
- connector.update
- connector.updateOrCreate (optional, but see below)
- connector.replaceOrCreate (a new feature - work in progress)
- connector.findOrCreate (optional, but see below)
- connector.buildNearFilter
- connector.destroyAll
- connector.count
- connector.save
- connector.destroy
- connector.replaceById (a new feature - work in progress)
- connector.updateAttributes
The following references are used, while building the module:
- loopback guide on building a connector
- loopback official connector loopback-connector-mongodb
- contentful management API
- contentful delivery API