From f647e9e2d1f0c7dacc53eb716930b81b46c5d725 Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Tue, 26 Sep 2017 16:59:24 +0200 Subject: [PATCH] =?UTF-8?q?Modelos=20y=20c=C3=B3digo=20com=C3=BAn=20para?= =?UTF-8?q?=20servicios=20loopback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gulpfile.js | 66 +++--- gulpfiles/services.json | 12 - services/auth/common/models/account.json | 46 ---- services/auth/common/models/user.json | 11 - services/auth/server/boot/root.js | 6 +- services/auth/server/model-config.json | 1 + services/client/common/models/account.json | 46 ---- services/client/common/models/my-model.js | 220 ------------------ services/client/common/models/my-model.json | 4 - services/client/common/models/user.json | 14 -- services/client/server/boot/root.js | 13 +- services/client/server/model-config.json | 1 + .../production/common/models/account.json | 46 ---- services/production/common/models/my-model.js | 220 ------------------ .../production/common/models/my-model.json | 4 - services/production/common/models/user.json | 14 -- services/production/server/boot/root.js | 6 +- services/production/server/model-config.json | 1 + services/salix/common/models/user.json | 11 - services/salix/server/boot/root.js | 6 +- services/salix/server/model-config.json | 1 + services/service/boot/root.js | 15 ++ .../common => service}/models/account.js | 3 +- 23 files changed, 51 insertions(+), 716 deletions(-) delete mode 100644 gulpfiles/services.json delete mode 100644 services/auth/common/models/account.json delete mode 100644 services/auth/common/models/user.json delete mode 100644 services/client/common/models/account.json delete mode 100644 services/client/common/models/my-model.js delete mode 100644 services/client/common/models/my-model.json delete mode 100644 services/client/common/models/user.json delete mode 100644 services/production/common/models/account.json delete mode 100644 services/production/common/models/my-model.js delete mode 100644 services/production/common/models/my-model.json delete mode 100644 services/production/common/models/user.json delete mode 100644 services/salix/common/models/user.json create mode 100644 services/service/boot/root.js rename services/{client/common => service}/models/account.js (95%) diff --git a/gulpfile.js b/gulpfile.js index 1e53d15fa..866a65599 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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() { diff --git a/gulpfiles/services.json b/gulpfiles/services.json deleted file mode 100644 index 9e0f4da68..000000000 --- a/gulpfiles/services.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "services": [ - "./services/client", - "./services/production" - ], - "files": [ - "account.json", - "my-model.js", - "my-model.json", - "user.json" - ] -} diff --git a/services/auth/common/models/account.json b/services/auth/common/models/account.json deleted file mode 100644 index 4b2bbf0dc..000000000 --- a/services/auth/common/models/account.json +++ /dev/null @@ -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" - } - ] -} diff --git a/services/auth/common/models/user.json b/services/auth/common/models/user.json deleted file mode 100644 index 0fe53764b..000000000 --- a/services/auth/common/models/user.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "user", - "base": "User", - "properties": { - "id": { - "id": true, - "type": "Number", - "forceId": false - } - } -} diff --git a/services/auth/server/boot/root.js b/services/auth/server/boot/root.js index 2b24dd71a..1be3ac1e7 100644 --- a/services/auth/server/boot/root.js +++ b/services/auth/server/boot/root.js @@ -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); }; diff --git a/services/auth/server/model-config.json b/services/auth/server/model-config.json index a35d7196f..5b54bc063 100644 --- a/services/auth/server/model-config.json +++ b/services/auth/server/model-config.json @@ -3,6 +3,7 @@ "sources": [ "loopback/common/models", "loopback/server/models", + "../../service/models", "../common/models", "./models" ], diff --git a/services/client/common/models/account.json b/services/client/common/models/account.json deleted file mode 100644 index 7b3fd0739..000000000 --- a/services/client/common/models/account.json +++ /dev/null @@ -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" - } - ] -} diff --git a/services/client/common/models/my-model.js b/services/client/common/models/my-model.js deleted file mode 100644 index aa02ff156..000000000 --- a/services/client/common/models/my-model.js +++ /dev/null @@ -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 === ""; -} - diff --git a/services/client/common/models/my-model.json b/services/client/common/models/my-model.json deleted file mode 100644 index 7a0a975bb..000000000 --- a/services/client/common/models/my-model.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "name": "MyModel", - "base": "PersistedModel" -} diff --git a/services/client/common/models/user.json b/services/client/common/models/user.json deleted file mode 100644 index 2f79cebdb..000000000 --- a/services/client/common/models/user.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "name": "user", - "base": "User", - "properties": { - "id": { - "id": true, - "type": "Number", - "forceId": false - }, - "username":{ - "type": "string" - } - } -} \ No newline at end of file diff --git a/services/client/server/boot/root.js b/services/client/server/boot/root.js index c541f82d2..1be3ac1e7 100644 --- a/services/client/server/boot/root.js +++ b/services/client/server/boot/root.js @@ -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); }; diff --git a/services/client/server/model-config.json b/services/client/server/model-config.json index 986f3b4df..7a1565916 100644 --- a/services/client/server/model-config.json +++ b/services/client/server/model-config.json @@ -3,6 +3,7 @@ "sources": [ "loopback/common/models", "loopback/server/models", + "../../service/models", "../common/models", "./models" ], diff --git a/services/production/common/models/account.json b/services/production/common/models/account.json deleted file mode 100644 index 7b3fd0739..000000000 --- a/services/production/common/models/account.json +++ /dev/null @@ -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" - } - ] -} diff --git a/services/production/common/models/my-model.js b/services/production/common/models/my-model.js deleted file mode 100644 index aa02ff156..000000000 --- a/services/production/common/models/my-model.js +++ /dev/null @@ -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 === ""; -} - diff --git a/services/production/common/models/my-model.json b/services/production/common/models/my-model.json deleted file mode 100644 index 7a0a975bb..000000000 --- a/services/production/common/models/my-model.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "name": "MyModel", - "base": "PersistedModel" -} diff --git a/services/production/common/models/user.json b/services/production/common/models/user.json deleted file mode 100644 index 2f79cebdb..000000000 --- a/services/production/common/models/user.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "name": "user", - "base": "User", - "properties": { - "id": { - "id": true, - "type": "Number", - "forceId": false - }, - "username":{ - "type": "string" - } - } -} \ No newline at end of file diff --git a/services/production/server/boot/root.js b/services/production/server/boot/root.js index 6adce90ad..a158fdfe5 100644 --- a/services/production/server/boot/root.js +++ b/services/production/server/boot/root.js @@ -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); }; diff --git a/services/production/server/model-config.json b/services/production/server/model-config.json index 6a741b661..4f7b637a2 100644 --- a/services/production/server/model-config.json +++ b/services/production/server/model-config.json @@ -3,6 +3,7 @@ "sources": [ "loopback/common/models", "loopback/server/models", + "../../service/models", "../common/models", "./models" ], diff --git a/services/salix/common/models/user.json b/services/salix/common/models/user.json deleted file mode 100644 index f13d4c168..000000000 --- a/services/salix/common/models/user.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "user", - "base": "User", - "properties": { - "id": { - "id": true, - "type": "Number", - "forceId": false - } - } -} \ No newline at end of file diff --git a/services/salix/server/boot/root.js b/services/salix/server/boot/root.js index 2b24dd71a..1be3ac1e7 100644 --- a/services/salix/server/boot/root.js +++ b/services/salix/server/boot/root.js @@ -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); }; diff --git a/services/salix/server/model-config.json b/services/salix/server/model-config.json index fc5e58edc..2b0772109 100644 --- a/services/salix/server/model-config.json +++ b/services/salix/server/model-config.json @@ -3,6 +3,7 @@ "sources": [ "loopback/common/models", "loopback/server/models", + "../../service/models", "../common/models", "./models" ], diff --git a/services/service/boot/root.js b/services/service/boot/root.js new file mode 100644 index 000000000..8fcde107c --- /dev/null +++ b/services/service/boot/root.js @@ -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!'); + }); +*/ +}; diff --git a/services/client/common/models/account.js b/services/service/models/account.js similarity index 95% rename from services/client/common/models/account.js rename to services/service/models/account.js index 5b47bdaa9..edc6f684d 100644 --- a/services/client/common/models/account.js +++ b/services/service/models/account.js @@ -1,8 +1,7 @@ module.exports = function(Account) { - // Validations Account.validatesUniquenessOf('name', { message: 'Ya existe un usuario con ese nombre' }); -}; \ No newline at end of file +};