Skip to content

Commit

Permalink
feat: sequelize integration part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLzq committed Jul 30, 2022
1 parent 272907f commit a233630
Show file tree
Hide file tree
Showing 31 changed files with 2,468 additions and 468 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dirsfirst",
"eslintignore",
"fullname",
"hstore",
"LGPL",
"nargs",
"nodemon",
Expand Down
4 changes: 4 additions & 0 deletions example/fastify/.sequelizerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
config: './src/database/postgres/config.js',
'migrations-path': './src/database/postgres/migrations/'
}
11 changes: 0 additions & 11 deletions example/fastify/nodemon.json

This file was deleted.

31 changes: 28 additions & 3 deletions example/fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
"start": "node dist/index.js",
"release": "standard-version",
"test:ci": "jest --ci -i",
"test:local": "NODE_ENV=local jest --ci -i"
"test:local": "NODE_ENV=local jest --ci -i",
"migrations:run": "sequelize db:migrate --to",
"migrations:run:last": "sequelize db:migrate --to $(ls src/database/postgres/migrations | sort -r | head -n 1)",
"migration": "ts-node -r dotenv/config src/scripts/migration.ts"
},
"author": "AnthonyLzq <sluzquinosa@uni.pe>",
"license": "MIT",
Expand All @@ -23,13 +26,20 @@
"fastify": "^3",
"http-errors": "^2.0.0",
"mongoose": "^6.4.4",
"pino-pretty": "^8.1.0"
"pg": "^8.7.3",
"pg-hstore": "^2.3.4",
"pino-pretty": "^8.1.0",
"reflect-metadata": "^0.1.13",
"sequelize": "^6.21.3",
"sequelize-typescript": "^2.1.3",
"sequelize-typescript-migration-lts": "^3.1.5"
},
"devDependencies": {
"@jest/types": "^28.1.3",
"@types/http-errors": "^1.8.2",
"@types/jest": "^28.1.5",
"@types/node": "^18.0.4",
"@types/node": "^18.0.6",
"@types/validator": "^13.7.4",
"@typescript-eslint/eslint-plugin": "^5.30.6",
"@typescript-eslint/parser": "^5.30.6",
"axios": "^0.27.2",
Expand All @@ -47,6 +57,7 @@
"jest-unit": "^0.0.1",
"nodemon": "^2.0.19",
"prettier": "^2.7.1",
"sequelize-cli": "^6.4.1",
"standard-version": "^9.5.0",
"ts-jest": "^28.0.6",
"ts-loader": "^9.3.1",
Expand All @@ -57,5 +68,19 @@
"webpack": "^5.73.0",
"webpack-cli": "^4.10.0",
"webpack-node-externals": "^3.0.0"
},
"nodemonConfig": {
"ignore": [
"test",
".git",
"node_modules"
],
"watch": [
"src",
"package.json",
".env"
],
"exec": "npx ts-node -r dotenv/config ./src/index",
"ext": "ts"
}
}
1 change: 1 addition & 0 deletions example/fastify/src/@types/models/user.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
interface UserDBO {
id: number
name: string
lastName: string
createdAt: Date
Expand Down
2 changes: 1 addition & 1 deletion example/fastify/src/database/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './mongo'
export * from './postgres'
55 changes: 0 additions & 55 deletions example/fastify/src/database/mongo/connection.ts

This file was deleted.

28 changes: 0 additions & 28 deletions example/fastify/src/database/mongo/models/user.ts

This file was deleted.

61 changes: 0 additions & 61 deletions example/fastify/src/database/mongo/queries/user.ts

This file was deleted.

12 changes: 12 additions & 0 deletions example/fastify/src/database/postgres/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require('dotenv').config()

module.exports = {
development: {
url: process.env.DB_URI,
dialect: 'postgres'
},
production: {
url: process.env.DB_URI,
dialect: 'postgres'
}
}
46 changes: 46 additions & 0 deletions example/fastify/src/database/postgres/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Sequelize } from 'sequelize-typescript'
import { FastifyLoggerInstance } from 'fastify'

import * as models from './models'
import { join } from 'path'

let sequelize: Sequelize

const dbConnection = async (
logger?: FastifyLoggerInstance
): Promise<{
connect: () => Promise<Sequelize>
disconnect: () => Promise<void>
createMigration: (migrationName: string) => Promise<void>
}> => {
return {
connect: async () => {
if (!sequelize) {
sequelize = new Sequelize(process.env.DB_URI as string, {
models: Object.values(models)
})
logger?.info('Postgres connection established.')
}

return sequelize
},
disconnect: () => {
logger?.info('Postgres connection closed.')

return sequelize?.close()
},
createMigration: async (migrationName: string) => {
const { SequelizeTypescriptMigration } = await import(
'sequelize-typescript-migration-lts'
)

await SequelizeTypescriptMigration.makeMigration(sequelize, {
outDir: join(__dirname, './migrations'),
migrationName,
preview: false
})
}
}
}

export { dbConnection }
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './connection'
export * from './models'
export * from './queries'
export * from './connection'
Loading

0 comments on commit a233630

Please sign in to comment.