Code Monkey home page Code Monkey logo

Comments (10)

amarewgs avatar amarewgs commented on August 25, 2024 3

+1 for sequelize.js

from generator-ng-fullstack.

itrethan avatar itrethan commented on August 25, 2024 1

+1 for sequelize.js

from generator-ng-fullstack.

ericmdantas avatar ericmdantas commented on August 25, 2024

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.

khwerhahn avatar khwerhahn commented on August 25, 2024

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.

ericmdantas avatar ericmdantas commented on August 25, 2024

@khwerhahn, that's very helpful!

I'll take a closer look asap and I'll see where this fits. Thanks!

from generator-ng-fullstack.

dmarcelino avatar dmarcelino commented on August 25, 2024

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.

ericmdantas avatar ericmdantas commented on August 25, 2024

@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.

justcfx2u avatar justcfx2u commented on August 25, 2024

SQL is also a requirement for my latest project. Sequelize.js support out-of-the-box would definitely help here.

from generator-ng-fullstack.

johnnyPescarul avatar johnnyPescarul commented on August 25, 2024

+1 for sequelize

from generator-ng-fullstack.

ericmdantas avatar ericmdantas commented on August 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.