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 { getDb } = require("./db");
|
||||
const { getDb } = require("./database/db");
|
||||
|
||||
const buildApp = () => {
|
||||
const db = getDb();
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
const { Sequelize } = require("sequelize");
|
||||
const dotenv = require("dotenv");
|
||||
|
||||
const CONSTANTS = require("./constants");
|
||||
const CONSTANTS = require("../constants");
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const getDb = () => {
|
||||
const sequelize = new Sequelize({
|
||||
module.exports = {
|
||||
development: {
|
||||
dialect: CONSTANTS.DB_ENGINE,
|
||||
host: process.env.POSTGRES_HOST,
|
||||
port: process.env.POSTGRES_PORT,
|
||||
|
|
@ -19,9 +17,5 @@ const getDb = () => {
|
|||
underscored: true,
|
||||
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