const Sequelize = require('sequelize'); const Config = require('./config.js'); if (!Config.config.db_url) { console.error("No database url found. please set `db_url` in config.json"); process.exit(); } const db = new Sequelize(Config.config.db_url); const User = db.define('User', { id: { type: Sequelize.DataTypes.UUID, defaultValue: Sequelize.UUIDV4, allowNull: false, primaryKey: true, unique: true }, email: { type: Sequelize.DataTypes.STRING, allowNull: false, unique: true }, password_hash: { type: Sequelize.DataTypes.STRING, allowNull: true } }); const Todo = db.define('Todo', { id: { type: Sequelize.DataTypes.UUID, defaultValue: Sequelize.UUIDV4, allowNull: false, primaryKey: true, unique: true }, content: { type: Sequelize.DataTypes.TEXT, allowNull: false } }); const Tag = db.define('Tag', { id: { type: Sequelize.DataTypes.UUID, defaultValue: Sequelize.UUIDV4, allowNull: false, primaryKey: true, unique: true }, content: { type: Sequelize.DataTypes.STRING, allowNull: false } }); User.hasMany(Todo); Todo.hasMany(Tag); let options = { alter: false }; if (Config.config.alter_db) { options.alter = true; } User.sync(options); module.exports = { db: db, constructors: { user: () => { return User.build(); } }, schemas: { user: User } }