2
0
Fork 0
tcrud/example/lib/mongoose-blog.js
2017-06-10 16:53:29 +02:00

154 lines
3.6 KiB
JavaScript

var mongoose = require('mongoose');
var log = require('winston').loggers.get('main');
var tcrud = require('../../lib/node-ff-tcrud');
module.exports = {
setup: setup
};
function setup(tcrud,tcrudModel) {
var mongoUrl = 'mongodb://localhost:27017/blog';
var mongoOptions = {
db: {
fsync: false,
journal: false,
native_parser: true,
forceServerObjectId: true
},
server: {
poolSize: 4,
socketOptions: {
connectTimeoutMS: 500,
keepAlive: 1,
auto_reconnect: true
}
}
};
log.info('Connecting to: ' + mongoUrl);
var conn = mongoose.createConnection(mongoUrl,mongoOptions);
tcrud.plugin.backend.mongoose.registrate('mongoose/blog',conn);
conn.model('blog-state', modelSchemaBlogState, 'blog_state');
var tc = tcrud.config;
var crudAdmin = tc.createTEntityNode(tcrudModel,'admin');
crudAdmin.troles.push('admin');
var crudAdminModels = tcrud.plugin.backend.mongoose.buildTEntityModels(conn,crudAdmin);
log.info('crud admin models created: '+crudAdminModels.length);
crudAdminModels.forEach(function(model) {
model.tmeta.tmodel.tbackend = 'mongoose/blog';
});
}
var modelMetaBlogState = {
name: {
type: String,
trim: true,
index: { unique: true },
tfield: {
tvalidate: { io: 'string' },
},
},
type: {
type: String,
trim: true,
index: { unique: false },
tfield: {
tvalidate: { io: 'string' },
},
},
value: {
type: String,
trim: true,
tfield: {
xtype: 'textarea',
tvalidate: { io: 'string' },
},
},
description: {
type: String,
trim: true,
tfield: {
ttype: 'textarea',
tvalidate: { io: 'string' },
},
},
changed_date: {
type: Date,
default: Date.now,
tfield: {
tlist: { tenable: false },
},
},
created_date: {
type: Date,
default: Date.now,
tfield: {
tlist: { tenable: false },
},
}
};
var modelSchemaBlogState = new mongoose.Schema(modelMetaBlogState);
modelSchemaBlogState.statics = tcrud.plugin.backend.mongoose.buildStaticsModelValidated(modelMetaBlogState,modelSchemaBlogState, {
findLastChangedLimit5: function (callback) {
log.debug(modelBackend+'.findLastChangedLimit5');
this.find({}).sort('-changed_date').limit(5).exec(callback);
},
findOneByName: function (name, callback) {
log.debug(modelBackend+'.findByName name='+name);
this.findOne({name:name}).exec(callback);
},
ensureExcists: function (name, type, defaultValue, description, callback) {
this.findOneByName(name, function(err, xprop) {
if (err) {
callback(err);
return;
}
if (xprop == null) {
log.debug(modelBackend+'.getByName create name='+name+' defaultValue='+defaultValue);
var model = mongoose.model('blog-state');
xprop = new model();
xprop.name = name;
xprop.type = type;
xprop.value = defaultValue;
xprop.description = description;
xprop.save(function(err,xprop) {
if (callback) {
callback(err, xprop);
}
});
} else {
log.debug(modelBackend+'.getByName fetched name='+name);
if (callback) {
callback(null, xprop);
}
}
});
},
setByName: function (name, value, callback) {
this.findOneByName(name, function(err, xprop) {
if (err) { throw err }
log.debug(modelBackend+'.setByName name='+name+' valueNew='+value+' valueOld='+xprop.value);
xprop.value = value;
xprop.save(function(err) {
callback(err, xprop);
});
});
},
incByName: function (name, callback) {
this.findOneByName(name, function(err, xprop) {
if (err) { throw err }
xprop.value++;
log.debug(modelBackend+'.incByName name='+name+' value='+xprop.value);
xprop.save(function(err) {
callback(err, xprop);
});
});
},
});