Skip to content
Permalink
Browse files

Add sequelize models and migrations

  • Loading branch information...
nlaz committed Feb 27, 2019
1 parent dc01c03 commit c49c86b5c7fdce5c56a6026ea7118a05f7834722
Showing with 108 additions and 2,190 deletions.
  1. +7 −0 .sequelizerc
  2. +27 −0 app/models/index.js
  3. +37 −0 app/models/migrations/20190227174624-create-user-2.js
  4. +19 −11 app/models/user.js
  5. +14 −0 config/databases.json
  6. +4 −1 package.json
  7. +0 −2,178 yarn.lock
@@ -0,0 +1,7 @@
const path = require('path');

module.exports = {
"config": path.resolve('./config', 'databases.json'),
"models-path": path.resolve('./app/models'),
"migrations-path": path.resolve('./app/models/migrations'),
};
@@ -0,0 +1,27 @@
"use strict";

const fs = require("fs");
const path = require("path");
const Sequelize = require("sequelize");
const basename = path.basename(__filename);
const config = require("../../config");
const databases = require("../../config/databases");

const settings = databases[config.env];
const sequelize = new Sequelize(settings.database, settings.username, settings.password, settings);

// Initialize each model file in /models
const models = fs
.readdirSync(__dirname)
.filter(file => file.indexOf(".") !== 0 && file !== "index.js")
.map(file => {
const model = require(path.join(__dirname, file));
return { [model.name]: model.init(sequelize) };
});

// Load model associations
for (const model of Object.keys(models)) {
typeof models[model].associate === "function" && models[model].associate(models);
}

module.exports = models;
@@ -0,0 +1,37 @@
const tablename = "Users";

module.exports.up = (queryInterface, Sequelize) => {
return queryInterface.createTable(tablename, {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
username: {
type: Sequelize.STRING,
allowNull: false,
unique: true
},
avatar_url: {
type: Sequelize.STRING,
allowNull: false
},
github_id: {
type: Sequelize.STRING,
allowNull: false
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
};

module.exports.down = (queryInterface, Sequelize) => {
return queryInterface.dropTable("Users");
};
@@ -1,18 +1,26 @@
const GitHub = require("../services/github");
const Sequelize = require("sequelize");

class User {
constructor(username, avatar_url, github_id) {
this.username = username;
this.avatar_url = avatar_url;
this.github_id = github_id;
class User extends Sequelize.Model {
static init(sequelize) {
return super.init(
{
username: { type: Sequelize.STRING, unique: true, allowNull: false },
avatar_url: { type: Sequelize.STRING, allowNull: false },
github_id: { type: Sequelize.STRING, allowNull: false }
},
{ sequelize }
);
}

static associate(models) {
// Create associations with other models here
// http://docs.sequelizejs.com/manual/tutorial/associations.html
//
// this.hasMany(models.Repository);
}

static async find_or_create_from_token(access_token) {
try {
const data = await GitHub.get_user_from_token(access_token);
} catch (error) {
console.log("ERROR", error);
}
const data = await GitHub.get_user_from_token(access_token);

/* Find existing user or create new User instances */
}
@@ -0,0 +1,14 @@
{
"development": {
"database": "nodejs-starter-dev",
"host": "127.0.0.1",
"port": 5432,
"dialect": "postgres"
},
"test": {
"database": "nodejs-starter-test",
"host": "127.0.0.1",
"port": 5432,
"dialect": "postgres"
}
}
@@ -12,7 +12,10 @@
"dotenv": "^6.2.0",
"express": "^4.16.4",
"express-handlebars": "^3.0.2",
"morgan": "^1.9.1"
"morgan": "^1.9.1",
"pg": "^7.8.1",
"pg-hstore": "^2.3.2",
"sequelize": "^4.42.1"
},
"devDependencies": {
"nodemon": "^1.18.10"
Oops, something went wrong.

0 comments on commit c49c86b

Please sign in to comment.
You can’t perform that action at this time.