Skip to content

Commit

Permalink
fix db parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanoff committed Aug 11, 2017
1 parent 272d9e7 commit 91aee42
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 28 deletions.
29 changes: 7 additions & 22 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
'use strict';

var config = require('./lib/config');
const Api = require('../');
const api = new Api();

//var Api = require('create-rest-api');
var Api = require('../');
var api = new Api(config, {validation: true});
api.model('comments');

api.registerModel('categories', {
name: { type: 'string', required: true },
});

api.registerModel('directors', {
name: { type: 'string', required: true },
birthday: { type: 'date' },
});
api.verify();

api.registerModel('stars', {
name: { type: 'string', required: true },
birthday: { type: 'date' },
});
api.model('stars');

api.registerModel('movies', {
name: { type: 'string', required: true },
year: { type: 'integer', required: true },
director: { type: 'uuid', link: 'directors' },
stars: { type: 'array', link: 'stars' },
categories: { type: 'array', link: 'categories' },
api.model('movies', {
stars: { link: 'stars' },
});

api.start();
44 changes: 44 additions & 0 deletions example/index2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

var config = require('./lib/config');

//var Api = require('create-rest-api');
var Api = require('../');
//var api = new Api(config, {validation: true});
const api = new Api({
listen : {
host : '127.0.0.1',
port : 8877
},
db : {
mongo : {
url : '127.0.0.1',
port : 27017,
name : 'test'
}
},
}, {validation: true});

api.model('categories', {
name: { type: 'string', required: true },
});

api.registerModel('directors', {
name: { type: 'string', required: true },
birthday: { type: 'date' },
});

api.registerModel('stars', {
name: { type: 'string', required: true },
birthday: { type: 'date' },
});

api.registerModel('movies', {
name: { type: 'string', required: true },
year: { type: 'integer', required: true },
director: { type: 'uuid', link: 'directors' },
stars: { type: 'array', link: 'stars' },
categories: { type: 'array', link: 'categories' },
});

api.start();
25 changes: 25 additions & 0 deletions example/index3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

var config = require('./lib/config');

//var Api = require('create-rest-api');
var Api = require('../');
var api = new Api(config);

//api.registerModel('messages');

//api.verify({ login: 'users.login', password: 'users.password', default: {CRUD: 'root', R: /.*+/} });

api.registerModel('users', {CRUD:'root'});

api.registerModel('categories');
api.registerModel('directors');
api.registerModel('stars');

api.registerModel('movies', {
director: { link: 'directors' },
stars: { link: 'stars' },
categories: { link: 'categories' }
});

api.start();
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ exports = module.exports = function (config, options) {
_config: config,
_app: app,
registerModel: app.registerModel,
model: app.model,
verify: app.verify,
start: start,
};
};
Expand All @@ -34,8 +36,8 @@ function start(config) {
: cfgDb.login ? cfgDb.login + ':' + cfgDb.password + '@'
: '';
var dbUrl = process.env.DB_URL || cfgDb.url || 'localhost';
if(cfgDb.port) dbUrl += ':' + cfgDb.port;
if(cfgDb.name) dbUrl += '/' + cfgDb.name;
if(cfgDb.port && !dbUrl.match(/:/)) dbUrl += ':' + cfgDb.port;
if(cfgDb.name && !dbUrl.match(/\//)) dbUrl += '/' + cfgDb.name;
dbUrl = 'mongodb://' + dbAuth + dbUrl;

app._start(HOST, PORT, dbUrl);
Expand Down
1 change: 0 additions & 1 deletion lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var winston = require('winston');
var moment = require('moment-timezone');

var logLevel = process.env.LOG_LEVEL || 'info';

var Logger = winstonObj;

Logger.getInstance = function () {
Expand Down
14 changes: 11 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ app._start = function (HOST, PORT, dbUrl) {
// Connect to DB and start server
app._db.connect(dbUrl, function (err) {
if (typeof err !== 'undefined') {
app.log.error('server start failed. datbase connection error: %s. Add DB_URL and DB_AUTH globals or update config file.', err);
app.log.error('server start failed. datbase connection on %s error: %s. Try to add DB_URL and/or DB_AUTH globals or update config file.', dbUrl, err);
} else {
app.log.info('datbase connected');
app.log.info('datbase connected on %s', dbUrl);
app.listen(PORT, HOST, function () {
app.log.info('server started on %s:%d', HOST || '*', PORT);
});
Expand All @@ -97,7 +97,7 @@ app._start = function (HOST, PORT, dbUrl) {

};

app.registerModel = function (name, model) {
app.model = function (name, model) {
app.models[name] = new DefaultModel(name, model, app._db);
app.controllers[name] = new DefaultController(name, app.models[name]);
app.routes[name] = new DefaultRoutes(name, app.controllers[name], app);
Expand Down Expand Up @@ -135,4 +135,12 @@ app.registerModel = function (name, model) {
}
};

app.registerModel = function (name, model) {
console.warn('registerModel is depricated. Use model instead.');
return app.model(name, model);
}

app.verify = function (options) {
}

module.exports = app;

0 comments on commit 91aee42

Please sign in to comment.