NodejsHouston / scavenger

MIT License
0 stars 6 forks source link

DB - Initialize DB #4

Open bpanicker13 opened 8 years ago

bpanicker13 commented 8 years ago

Initialize the Database in the /server project.

Acceptance Criteria:

poeticninja commented 8 years ago

Here is an example hapi plugin that works with sequalize.

import Sequelize from "sequelize";

const register = (server, options, next) => {

  const sequelize = new Sequelize('database', 'username', 'password', {
    host: 'localhost',
    dialect: 'sqlite',

    pool: {
      max: 5,
      min: 0,
      idle: 10000
    },

    // SQLite only
    storage: './database.sqlite'
  });

  server.expose("sequalize", sequelize);
  // example access to variable server.plugins["db"].sequalize

  return next();
};

register.attributes = {
  name: 'db'
};

export default register;
quellhorst commented 8 years ago

Here is code for the user model

var User = sequelize.define('user', {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV1,
    primaryKey: true
  },
  email: {
    type: Sequelize.STRING
  },
  name: {
    type: Sequelize.STRING
  },
  password: {
    type: Sequelize.STRING
  },
  locked: {
    type: Sequelize.BOOLEAN
  },
  lastLogin: {
    type: Sequelize.DATE,
    field: 'last_login'
  },
  lastUpdatedBy: {
    type: Sequelize.DATE,
    field: 'last_updated_by'
  },
  lastUpdated: {
    type: Sequelize.DATE,
    field: 'last_updated'
  },
  createdDate: {
    type: Sequelize.DATE,
    field: 'created_date'
  },
  createdBy: {
    type: Sequelize.STRING,
    field: 'created_by'
  },
}, {
  freezeTableName: true // Model tableName will be the same as the model name
});

User.sync({force: true}).then(function () {
  // Table created
  /* Could create test user here
  return User.create({
    firstName: 'John',
    lastName: 'Hancock'
  });
  */
});