-
Notifications
You must be signed in to change notification settings - Fork 6
Usage with Swagger Server
Now that we have the swagger generated server in place, we can start adding Caminte structure to it. What we need for that is the caminte-cli package installed with --global (-g). Please note, that for the sake of simplicity this tutorial will use SQLite 3.
$ npm install -g caminte-cli
This will install the respective packages and uses the upcoming stable release of caminte. We can now let the caminte CLI initialize the project's directory:
$ cd swagger-generated-server/
$ caminte --init -a sqlite3
Running this command will create the folders config, migrations and models, like that
.
|
|-- models
| `-- ...
|-- test
| |-- model
| | `-- ...
| |-- unit
| | `-- ...
| `-- tests.js
|-- models.js
`-- database.js
As an example application we will create a very basic and simple todo tool, which allows the creation of users and the management of their tasks.
In order to create a maintainable application, we will put all the database logic into the models folder. When the application gets fired up, caminte will async the models with the database and afterwards start the server. This way we don't clutter the application while making use of caminte's features.
Add the following strings in index.js
.
var models = require('./models');
models.init(app);
All models of our application are located as separate files in the models folder. If you want to add a new model, just add it to this folder and everything will work automagically. Also you can use the caminte CLI's caminte --model name [fields]
, more details
$ caminte -m User active:bool name email password note:text created:date
Will be create two files:
.
`-- models
`-- UserModel.js
Accessing a Model in Service
var User = caminte.model('User');
Example implementation
var caminte = require('caminte');
exports.countUsers = function(args, res, next) {
var User = caminte.model('User');
var opts = {
where: {}
};
User.count(opts.where, function (err, count) {
res.setHeader('Content-Type', 'application/json');
if (err) {
// some logic for handle error
}
res.end(JSON.stringify({
count: count
}, null, 2));
});
}
$ SET AUTOUPDATE=1 & npm start