Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Commit

Permalink
Add models directory for new generators
Browse files Browse the repository at this point in the history
  • Loading branch information
DesignByOnyx committed Feb 1, 2017
1 parent cf512e9 commit 01fc2a3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 37 deletions.
16 changes: 16 additions & 0 deletions generators/connection/templates/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ module.exports = function() {
const app = this;
const connectionString = app.get('sequelize');
const sequelize = new Sequelize(connectionString);
const oldSetup = app.setup;

app.set('sequelizeClient', sequelize);

app.setup = function(...args) {
// Set up data relationships
const models = sequelize.models;
Object.keys(models).forEach(name => {
if ('associate' in models[name]) {
models[name].associate(models);
}
});

// Sync to the database
sequelize.sync();

return oldSetup.apply(this, args);
}
};
2 changes: 1 addition & 1 deletion generators/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ module.exports = class ServiceGenerator extends Generator {
// Copy the model
this.fs.copyTpl(
this.templatePath('model', `${type}.js`),
this.destinationPath(this.libDirectory, 'services', kebabName, `${kebabName}.model.js`),
this.destinationPath(this.libDirectory, 'models', `${context.modelName}.js`),
context
);
}
Expand Down
9 changes: 7 additions & 2 deletions generators/service/templates/model/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ module.exports = function(app) {
type: Sequelize.STRING,
allowNull: false
}
}, {
classMethods: {
associate (models) {
// Define associations here
// See http://docs.sequelizejs.com/en/latest/docs/associations/
}
}
});

<%= camelName %>.sync();

return <%= camelName %>;
};
7 changes: 4 additions & 3 deletions generators/service/templates/service.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

// Initializes the `<%= name %>` service on path `/<%= path %>`
const createService = require('<%= serviceModule %>');
<% if(modelName) { %>const createModel = require('./<%= modelName %>');<% } %>
const createService = require('<%= serviceModule %>');<% if(modelName) { %>
const createModel = require('../../models/<%= modelName %>');<% } %>
const hooks = require('./<%= kebabName %>.hooks');
const filters = require('./<%= kebabName %>.filters');

Expand All @@ -11,7 +11,8 @@ module.exports = function() {
const Model = createModel(app);<% } %>
const paginate = app.get('paginate');

const options = {<% if(modelName) { %>
const options = {
name: '<%= kebabName %>',<% if(modelName) { %>
Model,<% } %>
paginate
};
Expand Down
31 changes: 0 additions & 31 deletions generators/service/templates/types/knex.js

This file was deleted.

0 comments on commit 01fc2a3

Please sign in to comment.