Comments (10)
+1 for sequelize.js
from generator-ng-fullstack.
+1 for sequelize.js
from generator-ng-fullstack.
I have never used it, but it's a good idea.
I'd need help with the dev, though. I don't know its best practices.
from generator-ng-fullstack.
Hi @ericmdantas,
I'm glad to help out soon, but I'm currently busy and don't have much time to spare.
In https://github.com/DaftMonk/generator-angular-fullstack/tree/canary they have included it into the project. I'm using the generator for a current project, but have diverted a little bit from how it was implemented there. I keep all models in a model folder which are are loaded in via the index.js in the same folder. There was a reason for this though, since I had to build on top of an existing database and I used sequelize-auto to auto import all the models. But I generally like to have the models all in one folder.
Hope this helps
'use strict';
var fs = require("fs");
var path = require("path");
var config = require('../config/environment');
var Sequelize = require("sequelize");
//var sequelize = new Sequelize(config.dbUrl);
var sequelize = new Sequelize(config.dbUrl.database, config.dbUrl.user, config.dbUrl.password,
{
host: config.dbUrl.host,
dialect: 'postgres',
sync: { force: true },
logging: config.dbUrl.logging,
});
var db = {};
fs
.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf(".") !== 0) && (file.indexOf('.spec.') === -1) && (file !== "index.js");
})
.forEach(function(file) {
var model = sequelize["import"](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(function(modelName) {
if ("associate" in db[modelName]) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
The app.js:
'use strict';
// Set default node environment to development
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var express = require('express');
var models = require('./models');
var config = require('./config/environment');
// Setup server
var app = express();
var server = require('http').createServer(app);
var socketio = require('socket.io')(server, {
serveClient: (config.env === 'production') ? false : true,
path: '/socket.io-client'
});
require('./config/socketio')(socketio);
require('./config/express')(app);
require('./routes')(app);
var forcedrop = (config.seedDB) ? true : false;
models.sequelize.sync({ force: forcedrop }).success(function () {
// Populate DB with sample data
if (config.seedDB) {
require('./config/seed');
}
});
exports = module.exports = app;
A typical model would look like this:
address.js
'use strict';
module.exports = function(sequelize, DataTypes) {
var Address = sequelize.define('address', {
lat: {
type: DataTypes.DECIMAL(10,8),
allowNull: true,
defaultValue: null,
validate: { min: -90, max: 90 }
},
lng: {
type: DataTypes.DECIMAL(10,8),
allowNull: true,
defaultValue: null,
validate: { min: -180, max: 180 }
},
customer_id: {
type: DataTypes.INTEGER,
allowNull: false
}
},{
underscored: true,
paranoid: true,
validate: {
bothCoordsOrNone: function() {
if ((this.lat === null) === (this.lng === null)) {
throw new Error('Require either both lat and lng or neither')
}
}
},
classMethods: {
associate: function (models) {
Address.belongsTo(models.customer, { foreignKey:'customer_id'})
}
}
});
return Address;
}
from generator-ng-fullstack.
@khwerhahn, that's very helpful!
I'll take a closer look asap and I'll see where this fits. Thanks!
from generator-ng-fullstack.
I'm also tight on time right now due to a deadline closing soon, but one thing I would like to help with would be integrating waterline. Waterline is a multi-adapter ORM with official support for MySQL, PostgreSQL, Mongo, Redis, local memory and disk. Community adapters exist for less mainstream data stores.
Perhaps the biggest disadvantage of waterline compared to sequelize is that it currently doesn't support transactions (balderdashy/waterline#755). Still, there are talks about using knexjs as core component of their SQL adapters (balderdashy/waterline#605) so people could get the best of both worlds.
In the project I'm working now we are using generator-angular-fullstack with waterline instead of mongoose and so far I'm enjoying it :)
from generator-ng-fullstack.
@dmarcelino I personally have never used it, but it'd be great to have such integration.
I also have been very busy, but I'd definetely merge a PR on the subject.
from generator-ng-fullstack.
SQL is also a requirement for my latest project. Sequelize.js support out-of-the-box would definitely help here.
from generator-ng-fullstack.
+1 for sequelize
from generator-ng-fullstack.
Could anyone use the base of generator as of now and switch from MongoDB/Mongoose to Sequelize? I'd gladly accept the PR and land it on 1.9.
from generator-ng-fullstack.
Related Issues (20)
- test db helper - mongoose connection HOT 5
- Todo app isnt' saving to Mongo's DB HOT 5
- [ng1/tasks/gulp.watch] - Should not watch changes in bower_components HOT 3
- Do not use generators in Koa server HOT 1
- require-dir is broken on node 8 HOT 1
- Heroku Error Gulp is not defined HOT 1
- Task 'client.build:dist' is not in your gulpfile HOT 2
- Just Question HOT 7
- Name convention problems upon using generator HOT 5
- No scss-file is generated when using ngf generate HOT 2
- Koa 2 HOT 1
- webpack HOT 9
- Samples For Multiple Client Pages Accessing Multiple Server Endpoint HOT 3
- bower -> npm/yarn HOT 4
- v2
- vue2 + cordova HOT 2
- No Showing On Yeoman HOT 2
- Error running project HOT 4
- Duplicate - please ignore this.
- Troubles continued from #297 HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from generator-ng-fullstack.