Skip to content

Commit

Permalink
Revert "Bodyparser (#143)" (#144)
Browse files Browse the repository at this point in the history
This reverts commit 79c7ce6.
  • Loading branch information
timelf123 committed May 2, 2018
1 parent 79c7ce6 commit 2150443
Show file tree
Hide file tree
Showing 28 changed files with 598 additions and 612 deletions.
21 changes: 0 additions & 21 deletions .editorconfig

This file was deleted.

3 changes: 1 addition & 2 deletions .gitignore
Expand Up @@ -33,5 +33,4 @@ lib/core_modules/server/node_modules
*.swp

/.idea
*.iml
/docs/
*.iml
2 changes: 1 addition & 1 deletion gulpfile.js
Expand Up @@ -7,4 +7,4 @@ gulp.task('test', function(){

return gulp.src('./test/**/*.spec.js', {read: false})
.pipe(mocha());
});
});
17 changes: 0 additions & 17 deletions jsdoc.conf.json

This file was deleted.

69 changes: 28 additions & 41 deletions lib/core_modules/db/index.js
@@ -1,42 +1,38 @@
'use strict';

var mongoose = require('mongoose'),
q = require('q'),
var mongoose = require ('mongoose'),
Q = require('q'),
Schema = mongoose.Schema,
_ = require('lodash');

mongoose.Promise = q.Promise;
mongoose.Promise = Q.Promise;

function filterDBAliases(v) {
function filterDBAliases (v) {
return mongoose.alias_MEANIODB_exists(v);
}

function applyModels(schema, model, collection, dbalias) {
function applyModels (schema, model, collection, dbalias) {
mongoose.get_MEANIODB_connection(dbalias).model(model, schema, collection);
return mongoose.get_MEANIODB_connection(dbalias).model(model);
}

function connectDb(alias, path) {
var defer = q.defer();
var connection = mongoose.createConnection(path);
connection.once('connected', connectDbOk.bind(null, defer, {
path: path,
alias: alias,
connection: connection
}));
function connectDb (alias, path) {
var defer = Q.defer();
var connection = mongoose.createConnection (path);
connection.once ('connected', connectDbOk.bind(null, defer, {path:path, alias: alias, connection: connection}));
connection.once('error', connectDbFailed.bind(null, defer, {}));
return defer.promise;
}

function connectDbOk(defer, s) {
function connectDbOk (defer, s) {
defer.resolve(s);
}

function connectDbFailed(defer, s) {
defer.reject(s);
}

function dataBasesReady(done, database, connection_pool) {
function dataBasesReady (done, database, connection_pool) {
var alias_map = {};
for (var i in connection_pool) {
if (connection_pool[i].state !== 'fulfilled') continue;
Expand All @@ -58,33 +54,27 @@ function dataBasesReady(done, database, connection_pool) {

var lazyModelsMap = {};

function createModelStructure(schema, model, collection, db) {
function createModelStructure (schema, model, collection, db) {
db = db || 'default';
if (!lazyModelsMap[db]) lazyModelsMap[db] = {};
if (!lazyModelsMap[db][model]) lazyModelsMap[db][model] = {
pre: [],
post: [],
virtual: [],
indices: []
};
if (!lazyModelsMap[db][model]) lazyModelsMap[db][model] = {pre:[], post:[], virtual: [], indices: []};

var mc = lazyModelsMap[db][model];
mc.collection = collection;
mc.fields = _.merge(mc.fields || {}, schema.fields);
mc.methods = _.assign(mc.methods || {}, schema.methods);
mc.statics = _.assign(mc.statics || {}, schema.statics);

if (schema.options) mc.options = _.assign(mc.options || {}, schema.options);
if (schema.indices) Array.prototype.push(mc.indices, schema.indices);
if (schema.pre) mc.pre.push(schema.pre);
if (schema.virtual) mc.virtual.push(schema.virtual);
mc.fields = _.merge (mc.fields || {}, schema.fields);
mc.methods = _.assign (mc.methods || {}, schema.methods);
mc.statics = _.assign (mc.statics || {}, schema.statics);

if (schema.options) mc.options = _.assign (mc.options || {}, schema.options);
if (schema.indices) Array.prototype.push (mc.indices, schema.indices);
if (schema.pre) mc.pre.push (schema.pre);
if (schema.virtual) mc.virtual.push (schema.virtual);
}

function bindIndices(s, i) {
function bindIndices (s, i) {
s.index(i);
}

function bindVirtuals(s, vr) {
function bindVirtuals (s, vr) {
for (var name in vr) {
var v = s.virtual(name);
console.log('create virtual ', name);
Expand All @@ -93,7 +83,7 @@ function bindVirtuals(s, vr) {
}
}

function bindHook(s, type, rec) {
function bindHook (s, type, rec) {
for (var name in rec) {
console.log('create hook', name);
s[type](name, rec[name]);
Expand All @@ -103,7 +93,7 @@ function bindHook(s, type, rec) {
function onInstance(meanioinstance, defer) {
var config = meanioinstance.getConfig();
mongoose.set('debug', config.mongoose && config.mongoose.debug);
var database = mongoose.connect(config.db || '', config.dbOptions || {}, function (err) {
var database = mongoose.connect(config.db || '', config.dbOptions || {}, function(err) {
if (err) {
console.error('Error:', err.message);
return console.error('**Could not connect to MongoDB. Please ensure mongod is running and restart MEAN app.**');
Expand All @@ -118,7 +108,7 @@ function onInstance(meanioinstance, defer) {
for (var i in config.dbs) {
db_promises.push(connectDb(i, config.dbs[i]));
}
q.allSettled(db_promises).then(dataBasesReady.bind(null, defer, database));
Q.allSettled(db_promises).then(dataBasesReady.bind(null, defer, database));
});
}

Expand All @@ -138,10 +128,7 @@ function supportDB(Meanio) {
rec.post.forEach(bindHook.bind(null, s, 'post'));
rec.indices.forEach(bindIndices.bind(null, s));
var m = applyModels(s, model, rec.collection, db);
Meanio.Singleton.events.emit('lazy_model_ready', {
model: m,
db: db
});
Meanio.Singleton.events.emit ('lazy_model_ready', {model: m, db: db});
}
}
Meanio.Singleton.events.emit('lazy_models_ready');
Expand All @@ -166,7 +153,7 @@ function supportDB(Meanio) {
}

//ok, now form structures for lazy model creation
itm.dbs.forEach(createModelStructure.bind(null, itm.schema, itm.model, itm.collection));
itm.dbs.forEach( createModelStructure.bind(null, itm.schema, itm.model, itm.collection) );
}
};
}
Expand Down
3 changes: 3 additions & 0 deletions lib/core_modules/db/package.json
@@ -0,0 +1,3 @@
{
"name": "db"
}
54 changes: 24 additions & 30 deletions lib/core_modules/menu/MenuItem.js
@@ -1,16 +1,10 @@
'use strict';

var _ = require('lodash');

//MenuItem class
function MenuItem(options) {
options = _.assign({
name: null,
title: null,
link: null,
roles: null
}, options);
options.name = options.name || (options.link ? options.link.replace('/', '_') : undefined) || options.title;
function MenuItem (options) {
options = _.assign ({name: null, title:null, link:null, roles:null}, options);
options.name = options.name || (options.link ? options.link.replace('/','_') : undefined) || options.title;
this.name = options.name;
this.title = options.title;
this.link = options.link;
Expand All @@ -23,9 +17,9 @@ function MenuItem(options) {
MenuItem.prototype.strip = function () {
return {
name: this.name,
title: this.title,
title:this.title,
link: this.link,
roles: this.roles,
roles:this.roles,
icon: this.icon,
weight: this.weight,
submenus: this.submenus.map(mapDoStrip)
Expand All @@ -40,11 +34,11 @@ MenuItem.hasRole = function (role, roles) {
MenuItem.prototype.props = function () {
return {
name: this.name,
title: this.title,
link: this.link,
title:this.title,
link:this.link,
icon: this.icon,
weight: this.weight,
roles: this.roles
roles:this.roles
};
};

Expand All @@ -55,7 +49,7 @@ MenuItem.prototype.findOrCreate = function (path) {
if (index > -1) return this.submenus[index].findOrCreate(path);
var n = new MenuItem();
n.name = p;
this.submenus.push(n);
this.submenus.push (n);
return n.findOrCreate(path);
};

Expand All @@ -66,30 +60,30 @@ MenuItem.prototype.list = function () {
MenuItem.prototype.get = function (roles, path) {
roles = roles ? roles.slice() : [];
if (roles.indexOf('anonymous') < 0 && roles.indexOf('authenticated') < 0) {
roles.push('authenticated');
roles.push ('authenticated');
}
if (roles.indexOf('all') < 0) roles.push('all');

var list = this.list();
if (path) {
if (!path.length) return this;
var n = path.shift();
var index = list.indexOf(n);
return this.submenus[index] ? this.submenus[index].get(roles, path) : undefined;
var index = list.indexOf (n);
return this.submenus[index] ? this.submenus[index].get(roles,path) : undefined;
}

if (this.roles) {
if (!_.intersection(this.roles, roles).length) return undefined;
}

return new MenuItem({
return new MenuItem ({
roles: this.roles || null,
link: this.link || null,
title: this.title || null,
name: this.name || null,
icon: this.icon || null,
link : this.link || null,
title:this.title || null,
name : this.name || null,
icon : this.icon || null,
weight: this.weight || null,
submenus: this.submenus.map(get_get.bind(null, roles)).filter(remove_nulls),
submenus : this.submenus.map(get_get.bind(null, roles)).filter(remove_nulls),
});
};

Expand All @@ -100,27 +94,27 @@ MenuItem.prototype.add = function (mi) {
var ts = mi.props();
itm = this.submenus[index];
for (var i in ts) itm[i] = ts[i];
} else {
}else{
itm = mi;
this.submenus.push(itm);
this.submenus.push (itm);
}
return itm;
};

//helper functions
function extractNames(v) {
function extractNames (v) {
return v.name;
}

function get_get(roles, v) {
function get_get (roles,v) {
return v.get(roles);
}

function remove_nulls(v) {
function remove_nulls (v) {
return v;
}

function mapDoStrip(v) {
function mapDoStrip (v) {
return v ? v.strip() : undefined;
}

Expand Down
29 changes: 14 additions & 15 deletions lib/core_modules/menu/index.js
@@ -1,17 +1,14 @@
'use strict';
var _ = require('lodash');
var MenuItem = require('./MenuItem.js');

var _ = require('lodash'),
MenuItem = require('./MenuItem.js'),
allMenus = new MenuItem();
var allMenus = new MenuItem(),
_ = require('lodash');

function Menus() {}

Menus.prototype.add = add;
Menus.prototype.get = get;

module.exports = Menus;
function Menus() {
}

function add(options) {
Menus.prototype.add = function (options) {
if (arguments.length === 0) return this;
if (options instanceof Array) {
options.forEach(Menus.prototype.add.bind(this));
Expand All @@ -29,16 +26,16 @@ function add(options) {

options = _.assign({
path: 'main',
roles: ['anonymous']
roles: ['anonymous'],
},
options);
options.path = options.path.replace(/^\//, '');
var item = allMenus.findOrCreate(options.path.split('/'));
item.add(new MenuItem(options));
return this;
}
};

function get(options) {
Menus.prototype.get = function (options) {
options = options || {};
options.menu = options.menu || 'main';
options.roles = options.roles || ['anonymous'];
Expand All @@ -51,8 +48,10 @@ function get(options) {
}
var ret = sm.get(options.roles);
return ret ? options.defaultMenu.concat(ret.submenus.map(mapDoStrip)) : options.defaultMenu;
}
};

function mapDoStrip(v) {
function mapDoStrip (v) {
return v ? v.strip() : undefined;
}

module.exports = Menus;

0 comments on commit 2150443

Please sign in to comment.