Modelos y código común para servicios loopback

This commit is contained in:
Juan Ferrer Toribio 2017-09-26 16:59:24 +02:00
parent 3a666ef764
commit f647e9e2d1
23 changed files with 51 additions and 716 deletions

View File

@ -11,8 +11,6 @@ var fs = require('fs');
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var services = require("./gulpfiles/services.json");
// Configuration
var srcDir = './client';
@ -36,33 +34,41 @@ gulp.task('client', ['clean'], function() {
return gulp.start('watch', 'routes', 'locales', 'webpack-dev-server');
});
gulp.task('copy', function() {
var streams = [];
for (i = 0; i < services.services.length; i++) {
var service = services.services[i];
for (j = 0; j < services.files.length; j++) {
var file = services.files[j];
streams.push(gulp.src("./services/service/models/" + file)
.pipe(gulp.dest(service + "/common/models/")));
}
}
return merge(streams);
});
gulp.task('services', ['copy'], function() {
require('./services/auth/server/server.js').start();
require('./services/salix/server/server.js').start();
gulp.task('services', function() {
require('./services/mailer/server.js').start();
for (i = 0; i < services.services.length; i++) {
require(services.services[i] + "/server/server.js").start();
}
var lbServices = [
'auth',
'salix',
'client',
'production'
];
for (var service of lbServices)
require(`./services/${service}/server/server.js`).start();
});
gulp.task('clean', function() {
return del([`${buildDir}/*`, `!${buildDir}/templates`, `!${buildDir}/images`], {force: true});
});
gulp.task('install', () => {
const pathServices = './services/';
const fileJson = [];
const services = fs.readdirSync(pathServices);
services.push('..');
services.forEach(service => {
fileJson.push(pathServices.concat(service, '/package.json'));
});
return gulp.src(fileJson)
.pipe(print(filepath => {
return `Installing packages in ${filepath}`;
}))
.pipe(install({
npm: ['--no-package-lock']
}));
});
// Spliting
var splitingFiles = './spliting/*';
@ -161,24 +167,6 @@ gulp.task('routes', function() {
.pipe(gulp.dest(buildDir));
});
// install packages from package.json in folders.
gulp.task('install', () => {
const pathServices = './services/';
const fileJson = [];
const services = fs.readdirSync(pathServices);
services.push('..');
services.forEach((service) => {
fileJson.push(pathServices.concat(service, '/package.json'));
});
return gulp.src(fileJson)
.pipe(print((filepath) => {
return `Installing packages in ${filepath}`;
}))
.pipe(install({
npm: ['--no-package-lock']
}));
});
// Watch
gulp.task('watch', function() {

View File

@ -1,12 +0,0 @@
{
"services": [
"./services/client",
"./services/production"
],
"files": [
"account.json",
"my-model.js",
"my-model.json",
"user.json"
]
}

View File

@ -1,46 +0,0 @@
{
"name": "Account",
"base": "PersistedModel",
"validateUpsert": true,
"properties": {
"id": {
"type": "number",
"required": true
},
"name": {
"type": "string",
"required": true
},
"password": {
"type": "string",
"required": true
},
"active": {
"type": "boolean"
},
"email": {
"type": "string",
"required": true
},
"created": {
"type": "date"
},
"updated": {
"type": "date"
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
}
]
}

View File

@ -1,11 +0,0 @@
{
"name": "user",
"base": "User",
"properties": {
"id": {
"id": true,
"type": "Number",
"forceId": false
}
}
}

View File

@ -1,8 +1,4 @@
module.exports = function(server) {
server.enableAuth();
var router = server.loopback.Router();
router.get('/status', server.loopback.status());
server.use(router);
require ('../../../service/boot/root.js')(server);
};

View File

@ -3,6 +3,7 @@
"sources": [
"loopback/common/models",
"loopback/server/models",
"../../service/models",
"../common/models",
"./models"
],

View File

@ -1,46 +0,0 @@
{
"name": "Account",
"base": "PersistedModel",
"validateUpsert": true,
"properties": {
"id": {
"type": "number",
"required": true
},
"name": {
"type": "string",
"required": true
},
"password": {
"type": "string",
"required": true
},
"active": {
"type": "boolean"
},
"email": {
"type": "string",
"required": true
},
"created": {
"type": "date"
},
"updated": {
"type": "date"
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "root",
"permission": "ALLOW"
}
]
}

View File

@ -1,220 +0,0 @@
module.exports = function(self) {
self.setup = function() {
self.super_.setup.call(this);
let disableMethods = {
create: true,
replaceOrCreate: true,
patchOrCreate: true,
upsert: true,
updateOrCreate: true,
exists: true,
find: true,
findOne: true,
findById: true,
deleteById: true,
replaceById: true,
updateAttributes: false,
createChangeStream: true,
updateAll: true,
upsertWithWhere: true,
count: true
};
for (let method in disableMethods) {
// this.disableRemoteMethod(method, disableMethods[method]);
}
};
self.defineScope = function(serverFilter) {
this.remoteMethodCtx('list', {
accepts: [
{
arg: 'filter',
type: 'object',
description: 'Filter defining where'
}
],
returns: {
type: [this.modelName],
root: true
},
http: {
verb: 'get',
path: '/list'
}
});
this.list = function(ctx, clientFilter, cb) {
var clientFields = (clientFilter && clientFilter.fields) ? clientFilter.fields : [];
var serverFields = (serverFilter && serverFilter.fields) ? serverFilter.fields : [];
var fields = clientFields.filter(itemC => {
return serverFields.some(itemS => itemS === itemC);
});
var and = [];
(clientFilter && clientFilter.where) && and.push(clientFilter.where);
(serverFilter && serverFilter.where) && and.push(serverFilter.where);
var order;
var limit;
if (clientFilter && clientFilter.order)
order = clientFilter.order;
else if (serverFilter && serverFilter.order)
order = serverFilter.order;
if (serverFilter && serverFilter.limit)
limit = serverFilter.limit;
else if (clientFilter && clientFilter.limit)
limit = clientFilter.limit;
var filter = {order: order, limit: limit};
filter.where = (and.length > 0) && {and: and};
filter.fields = fields;
this.find(filter, function(err, states) {
(err) ? cb(err, null) : cb(null, states);
});
};
};
self.rawSql = function(query, params, cb) {
var connector = this.dataSource.connector;
return new Promise(function(resolve, reject) {
connector.execute(query, params, function(error, response) {
if (error && !reject)
cb(error, null);
else if (error && reject)
reject(error);
else
resolve(response);
});
});
};
self.remoteMethodCtx = function(methodName, args) {
var ctx = {
arg: 'context',
type: 'object',
http: function(ctx) {
return ctx;
}
};
if (args.accepts === undefined)
args.accepts = [];
else if (!Array.isArray(args.accepts))
args.accepts = [args.accepts];
args.accepts.unshift(ctx);
this.remoteMethod(methodName, args);
};
self.connectToService = function(ctx, dataSource) {
this.app.dataSources[dataSource].connector.remotes.auth = {
bearer: new Buffer(ctx.req.accessToken.id).toString('base64'),
sendImmediately: true
};
};
self.disconnectFromService = function(dataSource) {
this.app.dataSources[dataSource].connector.remotes.auth = {
bearer: new Buffer("").toString('base64'),
sendImmediately: true
};
};
self.installMethod = function(methodName, filterCb) {
this.remoteMethod(methodName, {
description: 'List items using a filter',
accessType: 'READ',
accepts: [
{
arg: 'filter',
type: 'object',
required: true,
description: 'Filter defining where',
http: function(ctx) {
return ctx.req.query;
}
}
],
returns: {
arg: 'data',
type: [this.modelName],
root: true
},
http: {
verb: 'get',
path: `/${methodName}`
}
});
this[methodName] = (params, cb) => {
let filter = removeEmpty(filterCb(params));
var response = {};
function returnValues() {
if (response.instances !== undefined && response.count !== undefined)
cb(null, response);
}
function error() {
cb(null, response);
}
this.find(filter, function(err, instances) {
if (err) {
error();
} else {
response.instances = instances;
returnValues();
}
});
this.count(filter.where, function(err, totalCount) {
if (err) {
error();
} else {
response.count = totalCount;
returnValues();
}
});
};
};
self.validateBinded = function(propertyName, validatorFn, options) {
var customValidator = function(err) {
if (!validatorFn(this[propertyName])) err();
};
options.isExportable = true;
options.bindedFunction = validatorFn;
this.validate(propertyName, customValidator, options);
};
};
function removeEmpty(o) {
if (Array.isArray(o)) {
let array = [];
for (let item of o) {
let i = removeEmpty(item);
if (!isEmpty(item))
array.push(item);
}
if (array.length > 0)
return array;
} else if (typeof o === 'object') {
let object = {};
for (let key in o) {
let i = removeEmpty(o[key]);
if (!isEmpty(i))
object[key] = i;
}
if (Object.keys(object).length > 0)
return object;
} else if (!isEmpty(o))
return o;
return undefined;
}
function isEmpty(value) {
return value === undefined || value === "";
}

View File

@ -1,4 +0,0 @@
{
"name": "MyModel",
"base": "PersistedModel"
}

View File

@ -1,14 +0,0 @@
{
"name": "user",
"base": "User",
"properties": {
"id": {
"id": true,
"type": "Number",
"forceId": false
},
"username":{
"type": "string"
}
}
}

View File

@ -1,15 +1,4 @@
module.exports = function(server) {
server.enableAuth();
let router = server.loopback.Router();
router.get('/status', server.loopback.status());
server.use(router);
/*
let ds = server.dataSources.auth;
//ds.automigrate(function() {
ds.autoupdate(function() {
console.log('Tables migrated!');
});
*/
require ('../../../service/boot/root.js')(server);
};

View File

@ -3,6 +3,7 @@
"sources": [
"loopback/common/models",
"loopback/server/models",
"../../service/models",
"../common/models",
"./models"
],

View File

@ -1,46 +0,0 @@
{
"name": "Account",
"base": "PersistedModel",
"validateUpsert": true,
"properties": {
"id": {
"type": "number",
"required": true
},
"name": {
"type": "string",
"required": true
},
"password": {
"type": "string",
"required": true
},
"active": {
"type": "boolean"
},
"email": {
"type": "string",
"required": true
},
"created": {
"type": "date"
},
"updated": {
"type": "date"
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "root",
"permission": "ALLOW"
}
]
}

View File

@ -1,220 +0,0 @@
module.exports = function(self) {
self.setup = function() {
self.super_.setup.call(this);
let disableMethods = {
create: true,
replaceOrCreate: true,
patchOrCreate: true,
upsert: true,
updateOrCreate: true,
exists: true,
find: true,
findOne: true,
findById: true,
deleteById: true,
replaceById: true,
updateAttributes: false,
createChangeStream: true,
updateAll: true,
upsertWithWhere: true,
count: true
};
for (let method in disableMethods) {
// this.disableRemoteMethod(method, disableMethods[method]);
}
};
self.defineScope = function(serverFilter) {
this.remoteMethodCtx('list', {
accepts: [
{
arg: 'filter',
type: 'object',
description: 'Filter defining where'
}
],
returns: {
type: [this.modelName],
root: true
},
http: {
verb: 'get',
path: '/list'
}
});
this.list = function(ctx, clientFilter, cb) {
var clientFields = (clientFilter && clientFilter.fields) ? clientFilter.fields : [];
var serverFields = (serverFilter && serverFilter.fields) ? serverFilter.fields : [];
var fields = clientFields.filter(itemC => {
return serverFields.some(itemS => itemS === itemC);
});
var and = [];
(clientFilter && clientFilter.where) && and.push(clientFilter.where);
(serverFilter && serverFilter.where) && and.push(serverFilter.where);
var order;
var limit;
if (clientFilter && clientFilter.order)
order = clientFilter.order;
else if (serverFilter && serverFilter.order)
order = serverFilter.order;
if (serverFilter && serverFilter.limit)
limit = serverFilter.limit;
else if (clientFilter && clientFilter.limit)
limit = clientFilter.limit;
var filter = {order: order, limit: limit};
filter.where = (and.length > 0) && {and: and};
filter.fields = fields;
this.find(filter, function(err, states) {
(err) ? cb(err, null) : cb(null, states);
});
};
};
self.rawSql = function(query, params, cb) {
var connector = this.dataSource.connector;
return new Promise(function(resolve, reject) {
connector.execute(query, params, function(error, response) {
if (error && !reject)
cb(error, null);
else if (error && reject)
reject(error);
else
resolve(response);
});
});
};
self.remoteMethodCtx = function(methodName, args) {
var ctx = {
arg: 'context',
type: 'object',
http: function(ctx) {
return ctx;
}
};
if (args.accepts === undefined)
args.accepts = [];
else if (!Array.isArray(args.accepts))
args.accepts = [args.accepts];
args.accepts.unshift(ctx);
this.remoteMethod(methodName, args);
};
self.connectToService = function(ctx, dataSource) {
this.app.dataSources[dataSource].connector.remotes.auth = {
bearer: new Buffer(ctx.req.accessToken.id).toString('base64'),
sendImmediately: true
};
};
self.disconnectFromService = function(dataSource) {
this.app.dataSources[dataSource].connector.remotes.auth = {
bearer: new Buffer("").toString('base64'),
sendImmediately: true
};
};
self.installMethod = function(methodName, filterCb) {
this.remoteMethod(methodName, {
description: 'List items using a filter',
accessType: 'READ',
accepts: [
{
arg: 'filter',
type: 'object',
required: true,
description: 'Filter defining where',
http: function(ctx) {
return ctx.req.query;
}
}
],
returns: {
arg: 'data',
type: [this.modelName],
root: true
},
http: {
verb: 'get',
path: `/${methodName}`
}
});
this[methodName] = (params, cb) => {
let filter = removeEmpty(filterCb(params));
var response = {};
function returnValues() {
if (response.instances !== undefined && response.count !== undefined)
cb(null, response);
}
function error() {
cb(null, response);
}
this.find(filter, function(err, instances) {
if (err) {
error();
} else {
response.instances = instances;
returnValues();
}
});
this.count(filter.where, function(err, totalCount) {
if (err) {
error();
} else {
response.count = totalCount;
returnValues();
}
});
};
};
self.validateBinded = function(propertyName, validatorFn, options) {
var customValidator = function(err) {
if (!validatorFn(this[propertyName])) err();
};
options.isExportable = true;
options.bindedFunction = validatorFn;
this.validate(propertyName, customValidator, options);
};
};
function removeEmpty(o) {
if (Array.isArray(o)) {
let array = [];
for (let item of o) {
let i = removeEmpty(item);
if (!isEmpty(item))
array.push(item);
}
if (array.length > 0)
return array;
} else if (typeof o === 'object') {
let object = {};
for (let key in o) {
let i = removeEmpty(o[key]);
if (!isEmpty(i))
object[key] = i;
}
if (Object.keys(object).length > 0)
return object;
} else if (!isEmpty(o))
return o;
return undefined;
}
function isEmpty(value) {
return value === undefined || value === "";
}

View File

@ -1,4 +0,0 @@
{
"name": "MyModel",
"base": "PersistedModel"
}

View File

@ -1,14 +0,0 @@
{
"name": "user",
"base": "User",
"properties": {
"id": {
"id": true,
"type": "Number",
"forceId": false
},
"username":{
"type": "string"
}
}
}

View File

@ -1,8 +1,4 @@
'use strict';
module.exports = function(server) {
// Install a `/` route that returns server status
var router = server.loopback.Router();
router.get('/', server.loopback.status());
server.use(router);
require ('../../../service/boot/root.js')(server);
};

View File

@ -3,6 +3,7 @@
"sources": [
"loopback/common/models",
"loopback/server/models",
"../../service/models",
"../common/models",
"./models"
],

View File

@ -1,11 +0,0 @@
{
"name": "user",
"base": "User",
"properties": {
"id": {
"id": true,
"type": "Number",
"forceId": false
}
}
}

View File

@ -1,8 +1,4 @@
module.exports = function(server) {
server.enableAuth();
var router = server.loopback.Router();
router.get('/status', server.loopback.status());
server.use(router);
require ('../../../service/boot/root.js')(server);
};

View File

@ -3,6 +3,7 @@
"sources": [
"loopback/common/models",
"loopback/server/models",
"../../service/models",
"../common/models",
"./models"
],

View File

@ -0,0 +1,15 @@
module.exports = function(app) {
app.enableAuth();
var router = app.loopback.Router();
router.get('/status', app.loopback.status());
app.use(router);
/*
let ds = app.dataSources.auth;
//ds.automigrate(function() {
ds.autoupdate(function() {
console.log('Tables migrated!');
});
*/
};

View File

@ -1,8 +1,7 @@
module.exports = function(Account) {
// Validations
Account.validatesUniquenessOf('name', {
message: 'Ya existe un usuario con ese nombre'
});
};
};