Commit c6c577a9 authored by Matheus Horstmann's avatar Matheus Horstmann 🐴

Modularize models and discover-and-build.js

Signed-off-by: Matheus Horstmann's avatarMatheus Horstmann <mch15@inf.ufpr.br>
parent 3fc410a2
var path = require('path');
var app = require(path.resolve(__dirname, '../server/server'));
var ds = app.datasources.ensalamento;
ds.automigrate('Bloco', function(err) {
if (err) throw err;
var blocos = [
{localizacao: '0,0', tamanho: 10},
{localizacao: '0,0', tamanho: 10}
];
var count = blocos.length;
blocos.forEach(function(bloco) {
app.models.Bloco.create(bloco, function(err, model) {
if (err) throw err;
console.log('Created:', model);
count--;
if (count === 0)
ds.disconnect();
});
});
});
ds.automigrate('Sala', function(err) {
if (err) throw err;
var salas = [
{localizacao: '41.12,-71.34', capacidade: 10, tipo:"teste", restrita:false},
{localizacao: '41.12,-71.34', capacidade: 10, tipo:"teste", restrita:true}
];
var count = salas.length;
salas.forEach(function(sala) {
app.models.Sala.create(sala, function(err, model) {
if (err) throw err;
console.log('Created:', model);
count--;
if (count === 0)
ds.disconnect();
});
});
});
ds.automigrate('User', function(err) {
if (err) throw err;
app.models.User.create([
{username: 'admin', email: 'admin@admin.com', password: '123mudar'}
],function(err, users) {
if (err) throw err;
console.log('Created users:', users);
app.models.Role.create({
name: 'admin'
}, function(err, role) {
if (err) throw err;
console.log('Created role:', role);
role.principals.create({
principalType: RoleMapping.USER,
principalId: users[0].id
}, function(err, principal) {
if (err) throw err;
console.log('Assign admin Role to initial User:', users[0].email);
});
});
});
ds.disconnect();
});
......@@ -3,163 +3,32 @@ var async = require('async');
var app = require(path.resolve(__dirname, '../server/server'));
var ds = app.datasources.ensalamento;
var models = require(path.resolve(__dirname,'../common/models/models.js'))
var functions = [discoverAndBuildUser, discoverAndBuildRole, discoverAndBuildSala,
discoverAndBuildSala, discoverAndBuildBloco, discoverAndBuildDisciplina,
discoverAndBuildEquivalenciadisciplina, discoverAndBuildAccessToken,
discoverAndBuildACL,discoverAndBuildRoleMapping
]
async.parallel(functions,function(error){
if (err) throw err;
var lbTables = models.models_lower;
ds.disconnect();
});
function discoverAndBuildUser(){
ds.discoverAndBuildModels('user', {schema: 'public'}, function(err, models) {
if (err) throw err;
models.User.find(function(err, users) {
if (err) return console.log(err);
console.log(users);
});
});
var count = lbTables.length;
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
function discoverAndBuildAccessToken(){
lbTables.forEach(function(table) {
ds.discoverAndBuildModels('accesstoken', {schema: 'public'}, function(err, models) {
ds.discoverAndBuildModels(table, {schema: 'public'}, function(err, models) {
if (err) throw err;
models.Accesstoken.find(function(err, accesstokens) {
if (err) return console.log(err);
console.log(accesstokens);
});
});
}
function discoverAndBuildACL(){
models[table.capitalize()].find(function(err, model) {
ds.discoverAndBuildModels('acl', {schema: 'public'}, function(err, models) {
if (err) throw err;
models.Acl.find(function(err, acls) {
if (err) return console.log(err);
console.log(acls);
console.log(model);
});
});
}
function discoverAndBuildRoleMapping(){
ds.discoverAndBuildModels('rolemapping', {schema: 'public'}, function(err, models) {
if (err) throw err;
models.Rolemapping.find(function(err, rolesmappings) {
if (err) return console.log(err);
console.log(rolesmappings);
});
});
}
function discoverAndBuildRole(){
ds.discoverAndBuildModels('role', {schema: 'public'}, function(err, models) {
if (err) throw err;
models.Role.find(function(err, roles) {
if (err) return console.log(err);
console.log(roles);
});
});
}
function discoverAndBuildSala(){
ds.discoverAndBuildModels('sala', {schema: 'public'}, function(err, models) {
if (err) throw err;
models.Sala.find(function(err, salas) {
if (err) return console.log(err);
console.log(salas);
});
});
}
function discoverAndBuildBloco(){
ds.discoverAndBuildModels('bloco', {schema: 'public'}, function(err, models) {
if (err) throw err;
models.Bloco.find(function(err, blocos) {
if (err) return console.log(err);
console.log(blocos);
});
});
}
function discoverAndBuildDisciplina(){
ds.discoverAndBuildModels('disciplina', {schema: 'public'}, function(err, models) {
if (err) throw err;
models.Disciplina.find(function(err, disciplinas) {
if (err) return console.log(err);
console.log(disciplinas);
});
});
}
function discoverAndBuildEquivalenciadisciplina(){
ds.discoverAndBuildModels('equivalenciadisciplina', {schema: 'public'}, function(err, models) {
if (err) throw err;
models.Equivalenciadisciplina.find(function(err, equivalenciadisciplinas) {
if (err) return console.log(err);
console.log(equivalenciadisciplinas);
});
});
}
count--;
if (count === 0)
ds.disconnect();
});
......@@ -2,8 +2,10 @@ var path = require('path');
var app = require(path.resolve(__dirname, '../server/server'));
var ds = app.datasources.ensalamento;
var lbTables = ['user','accesstoken','acl','rolemapping','role','sala','bloco','disciplina','equivalenciadisciplina'];
var models = require(path.resolve(__dirname,'../common/models/models.js'))
var lbTables = models.models_lower;
var count = lbTables.length;
lbTables.forEach(function(table) {
ds.discoverSchema(table, {schema: "public"}, function(err, schema) {
......
"use strict";
/**
Models are used in a lot of script, so we created this file to unify.
**/
var models = [
'User',
'AccessToken',
......@@ -18,4 +20,12 @@ var models = [
'Tipodesala'
];
/**
Some scripts need the lower case version of it
**/
var models_lower = models.map(function(x){
return x.toLowerCase();
});
exports.models = models;
exports.models_lower = models_lower
......@@ -5,16 +5,15 @@ var models = require(path.resolve(__dirname,'../common/models/models.js'))
var models = models.models;
var models_lower = models.models_lower;
var app = require(path.resolve(__dirname, '../server/server'));
var ds = app.datasources.ensalamento;
var new_models = models.map(function(x){
return x.toLowerCase();
});
ds.isActual(models, function(err, actual) {
console.log('Is actual?: ', actual);
if (!actual) {
ds.autoupdate(models, function(err, result) {
ds.autoupdate(models_lower, function(err, result) {
new_models.forEach(function(model){
ds.discoverModelProperties(model, function (err, props) {
console.log(props);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment