Skip to content

Commit

Permalink
DB adjusted
Browse files Browse the repository at this point in the history
  • Loading branch information
SaddockAime committed May 27, 2024
1 parent 182ba14 commit 7108216
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 319 deletions.
16 changes: 4 additions & 12 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
PORT=
NODE_ENV=
NODE=
DATABASE_URL_TEST=
DATABASE_URL_PRO=
DOCKER_DATABASE_HOST_AUTH_METHOD=
DOCKER_DATABASE_NAME=
DOCKER_DATABASE_PASSWORD=
DATABASE_PORT_DEV=
DATABASE_HOST_DEV=
DATABASE_NAME_DEV=
DATABASE_USERNAME_DEV=
DATABASE_PASSWORD_DEV=
DATABASE_PORT_TEST=
DATABASE_HOST_TEST=
DATABASE_NAME_TEST=
DATABASE_USERNAME_TEST=
DATABASE_PASSWORD_TEST=
DATABASE_URL_DEV=
DATABASE_URL_TEST=
DATABASE_URL_PRO=
DB_HOST_TYPE=
20 changes: 16 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ on:
push:
branches:
- develop

env:
PORT: ${{ secrets.PORT }}
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
DATABASE_TEST_URL: ${{ secrets.DATABASE_TEST_URL }}
DATABASE_URL_TEST: ${{ secrets.DATABASE_URL_TEST }}
NODE_EN: ${{ secrets.NODE_EN }}
DATABASE_URL_PRO: ${{ secrets.DATABASE_URL_PRO }}
API_KEY: ${{ secrets.API_KEY }}
API_SECRET: ${{ secrets.API_SECRET }}
CLOUD_NAME: ${{ secrets.CLOUD_NAME }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}

jobs:
build:
Expand All @@ -24,22 +28,30 @@ jobs:

steps:
- uses: actions/checkout@v3

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
- run: npm install
- run: npm ci

- name: Install dependencies
run: npm install

- name: Set NODE_ENV to test
run: echo "NODE_ENV=test" >> $GITHUB_ENV

- run: npm run build --if-present

- run: npm test --if-present

- run: npm run coverage --if-present

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

- name: Upload coverage to Coveralls
uses: coverallsapp/github-action@v2
env:
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"coverage": " nyc mocha --require ts-node/register 'src/**/*.spec.ts' --timeout 600000 --exit",
"lint": "eslint . --ext .ts",
"lint-staged": "lint-staged",
"prepare": "husky"
"prepare": "husky",
"postinstall": "husky install"
},
"nyc": {
"extends": "@istanbuljs/nyc-config-typescript",
Expand Down
80 changes: 21 additions & 59 deletions src/databases/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,36 @@
// import dotenv from "dotenv"
// dotenv.config()
import dotenv from "dotenv";

// module.exports = {
// test: {
// logging: false,
// dialect: "postgres",
// port: process.env.DATABASE_PORT_TEST,
// host: process.env.DATABASE_HOST_TEST,
// database: process.env.DATABASE_NAME_TEST,
// username: process.env.DATABASE_USERNAME_TEST,
// password: process.env.DATABASE_PASSWORD_TEST,
// dialectOptions: {
// ssl: {
// require: true,
// rejectUnauthorized: true
// }
// }

// },

// development: {
// logging: false,
// dialect: "postgres",
// port: process.env.DATABASE_PORT_DEV,
// host: process.env.DATABASE_HOST_DEV,
// database: process.env.DATABASE_NAME_DEV,
// username: process.env.DATABASE_USERNAME_DEV,
// password: process.env.DATABASE_PASSWORD_DEV
// },

// production: {
// logging: false,
// dialect: "postgres",
// url: process.env.DATABASE_URL_PRO,
// dialectOptions: {
// ssl: {
// require: true,
// rejectUnauthorized: true
// }
// }
// }
// }

import dotenv from "dotenv"

dotenv.config()
dotenv.config();

const commonDatabaseConfig = {
dialect: "postgres",
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false
}
}
}
dialect: "postgres"
};

const sequelizeConfig = {
development: {
...commonDatabaseConfig,
url: process.env.DEV_DATABASE_URL
url: process.env.DATABASE_URL_DEV
},
test: {
...commonDatabaseConfig,
url: process.env.DATABASE_TEST_URL
url: process.env.DATABASE_URL_TEST,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false
}
}
},
production: {
...commonDatabaseConfig,
url: process.env.DATABASE_URL_PRO
url: process.env.DATABASE_URL_PRO,
dialectOptions: {
ssl: {
require: true,
rejectUnauthorized: false
}
}
}
}
};

module.exports = sequelizeConfig
module.exports = sequelizeConfig;
8 changes: 4 additions & 4 deletions src/databases/config/db.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ const DB_HOST_MODE: string = process.env.DB_HOST_TYPE || "remote"
*/
function getDbUri(): string {
switch (NODE_ENV) {
case "development":
return process.env.DATABASE_URL_DEV as string
case "test":
return process.env.DATABASE_TEST_URL as string
case "production":
return process.env.DATABASE_URL_PRO as string
return process.env.DATABASE_URL_TEST as string
default:
return process.env.DEV_DATABASE_URL as string
return process.env.DATABASE_URL_PRO as string
}
}

Expand Down
53 changes: 47 additions & 6 deletions src/databases/migrations/20240520180022-create-users.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { QueryInterface, DataTypes } from "sequelize";

export default {
up: async (queryInterface: QueryInterface) => {
const [results] = await queryInterface.sequelize.query(
"SELECT 1 FROM pg_type WHERE typname = 'enum_users_gender';"
);
if (!results.length) {
await queryInterface.sequelize.query(
"CREATE TYPE \"enum_users_gender\" AS ENUM('male', 'female');"
);
}

await queryInterface.createTable("users", {
id: {
type: DataTypes.INTEGER,
Expand All @@ -10,11 +20,11 @@ export default {
},
firstName: {
type: new DataTypes.STRING(128),
allowNull: false
allowNull: true
},
lastName: {
type: new DataTypes.STRING(128),
allowNull: false
allowNull: true
},
email: {
type: new DataTypes.STRING(128),
Expand All @@ -26,17 +36,42 @@ export default {
},
phone: {
type: new DataTypes.BIGINT,
allowNull: false
allowNull: true
},
profilePicture: {
type: new DataTypes.STRING(128),
allowNull: true
},
gender: {
type: new DataTypes.ENUM("male", "female"),
allowNull: true
},
birthDate: {
type: new DataTypes.DATE,
allowNull: true
},
language: {
type: new DataTypes.STRING(128),
allowNull: true
},
currency: {
type: new DataTypes.STRING(128),
allowNull: true
},
role: {
type: new DataTypes.STRING(128),
allowNull: false
allowNull: true
},
isVerified: {
type: new DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
},
is2FAEnabled: {
type: new DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
},
status: {
type: new DataTypes.BOOLEAN,
allowNull: false,
Expand All @@ -45,17 +80,23 @@ export default {
createdAt: {
allowNull: false,
type: DataTypes.DATE,
defaultValue: DataTypes.DATE
defaultValue: DataTypes.NOW
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE,
defaultValue: DataTypes.DATE
defaultValue: DataTypes.NOW
}
});
},

down: async (queryInterface: QueryInterface) => {
// Drop the users table
await queryInterface.dropTable("users");

// Drop the enum type if it exists
await queryInterface.sequelize.query(`
DROP TYPE IF EXISTS "enum_users_gender";
`);
}
};
Loading

0 comments on commit 7108216

Please sign in to comment.