more db stuff
This commit is contained in:
parent
5a7d073c63
commit
f49b7fad08
5 changed files with 74 additions and 12 deletions
6
.sequelizerc
Normal file
6
.sequelizerc
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
const path = require("path");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
config: path.resolve("src", "back", "database", "config.js"),
|
||||||
|
"migrations-path": path.resolve("src", "back", "database", "migrations"),
|
||||||
|
};
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
const CONSTANTS = require("./constants");
|
const CONSTANTS = require("./constants");
|
||||||
const { getDb } = require("./db");
|
const { getDb } = require("./database/db");
|
||||||
|
|
||||||
const buildApp = () => {
|
const buildApp = () => {
|
||||||
const db = getDb();
|
const db = getDb();
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,13 @@
|
||||||
const { Sequelize } = require("sequelize");
|
|
||||||
const dotenv = require("dotenv");
|
const dotenv = require("dotenv");
|
||||||
|
const CONSTANTS = require("../constants");
|
||||||
const CONSTANTS = require("./constants");
|
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
const getDb = () => {
|
module.exports = {
|
||||||
const sequelize = new Sequelize({
|
development: {
|
||||||
dialect: CONSTANTS.DB_ENGINE,
|
dialect: CONSTANTS.DB_ENGINE,
|
||||||
host: process.env.POSTGRES_HOST,
|
host: process.env.POSTGRES_HOST,
|
||||||
port: process.env.POSTGRES_PORT,
|
port: process.env.POSTGRES_PORT,
|
||||||
database: process.env.POSTGRES_DB,
|
database: process.env.POSTGRES_DB,
|
||||||
username: process.env.POSTGRES_USER,
|
username: process.env.POSTGRES_USER,
|
||||||
password: process.env.POSTGRES_PASSWORD,
|
password: process.env.POSTGRES_PASSWORD,
|
||||||
|
|
@ -19,9 +17,5 @@ const getDb = () => {
|
||||||
underscored: true,
|
underscored: true,
|
||||||
quoteIdentifiers: false,
|
quoteIdentifiers: false,
|
||||||
},
|
},
|
||||||
});
|
},
|
||||||
|
|
||||||
return sequelize;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {getDb};
|
|
||||||
13
src/back/database/db.js
Normal file
13
src/back/database/db.js
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
const { Sequelize } = require("sequelize");
|
||||||
|
const dotenv = require("dotenv");
|
||||||
|
const config = require("./config");
|
||||||
|
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
const getDb = () => {
|
||||||
|
const sequelize = new Sequelize(config.development);
|
||||||
|
|
||||||
|
return sequelize;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = { getDb };
|
||||||
49
src/back/database/migrations.js
Normal file
49
src/back/database/migrations.js
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
up: (queryInterface, Sequelize) => {
|
||||||
|
return queryInterface.sequelize.transaction((t) => {
|
||||||
|
return Promise.all([
|
||||||
|
queryInterface.createTable(
|
||||||
|
"hello_world",
|
||||||
|
{
|
||||||
|
first_name: {
|
||||||
|
type: Sequelize.STRING,
|
||||||
|
allowNull: false,
|
||||||
|
unique: true,
|
||||||
|
primaryKey: true,
|
||||||
|
},
|
||||||
|
last_name: {
|
||||||
|
type: Sequelize.STRING,
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
created_at: {
|
||||||
|
type: Sequelize.DATE,
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ transaction: t }
|
||||||
|
),
|
||||||
|
queryInterface.bulkInsert(
|
||||||
|
"hello_world",
|
||||||
|
[
|
||||||
|
{
|
||||||
|
first_name: "John",
|
||||||
|
last_name: "Doe",
|
||||||
|
created_at: new Date(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
first_name: "Jane",
|
||||||
|
last_name: "Smith",
|
||||||
|
created_at: new Date(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
{ transaction: t }
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
down: (queryInterface, Sequelize) => {
|
||||||
|
return queryInterface.dropTable("hello_world");
|
||||||
|
},
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue