Skip to content
Permalink
Browse files

Add sequelize files

  • Loading branch information...
nlaz committed Feb 28, 2019
1 parent c49c86b commit b3eec46727beb34828e03c065671365d5987d1c9
@@ -1,7 +1,7 @@
const express = require("express");

const models = require("../models");
const config = require("../../config");
const User = require("../models/user");
const GitHub = require("../services/github");

const router = express.Router();
@@ -28,7 +28,7 @@ router.get("/callback/github", async function(req, res) {
return res.render("404");
}

const user = User.find_or_create_from_token(access_token);
const user = models.User.find_or_create_from_token(access_token);

return res.redirect("/");
});
@@ -4,24 +4,33 @@ 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 env = process.env.NODE_ENV || "development";
const config = require(__dirname + "/../../config/databases.json")[env];
const db = {};

const settings = databases[config.env];
const sequelize = new Sequelize(settings.database, settings.username, settings.password, settings);
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}

// 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) };
fs.readdirSync(__dirname)
.filter(file => {
return file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js";
})
.forEach(file => {
const model = sequelize["import"](path.join(__dirname, file));
db[model.name] = model;
});

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

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = models;
module.exports = db;

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,35 @@
"use strict";
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable("Users", {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
username: {
type: Sequelize.STRING,
allowNull: false,
unique: true
},
avatar_url: {
type: Sequelize.STRING
},
github_id: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable("Users");
}
};
@@ -1,29 +1,35 @@
const Sequelize = require("sequelize");
const GitHub = require("../services/GitHub");

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 }
);
}
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define(
"User",
{
username: { type: DataTypes.STRING, unqiue: true, allowNull: false },
avatar_url: DataTypes.STRING,
github_id: DataTypes.STRING
},
{ sequelize }
);

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

static async find_or_create_from_token(access_token) {
User.find_or_create_from_token = async function(access_token) {
const data = await GitHub.get_user_from_token(access_token);

/* Find existing user or create new User instances */
}
}
const instance = await this.findOrCreate({
where: { username: data["login"] },
defaults: {
username: data["login"],
avatar_url: data["avatar_url"],
github_id: data["id"]
}
});

module.exports = User;
return instance;
};

return User;
};

0 comments on commit b3eec46

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