Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
feat: sequelize integration
Browse files Browse the repository at this point in the history
  • Loading branch information
AuHau committed Jan 30, 2020
1 parent 85a3371 commit a52e341
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 15 deletions.
2 changes: 0 additions & 2 deletions config/default.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"host": "localhost",
"port": 3030,
"public": "../public/",
"paginate": {
"default": 10,
Expand Down
8 changes: 8 additions & 0 deletions config/development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"host": "localhost",
"port": 3030,
"db": {
"dialect": "sqlite",
"storage": "../db.sqlite"
}
}
10 changes: 9 additions & 1 deletion config/production.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"host": "@rsksmart/rif-martketplace-cache-app.feathersjs.com",
"port": "PORT"
"port": "PORT",
"db": {
"dialect": "postgres",
"host": "PG_HOST",
"port": "PG_PORT",
"username": "PG_USER",
"password": "PG_PASS",
"database": "PG_DB",
}
}
70 changes: 59 additions & 11 deletions package-lock.json

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

11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
"types",
"lib"
],
"directories": {
"lib": "src",
"test": "test/",
"config": "config/"
},
"main": "lib/index.js",
"repository": {
"type": "git",
Expand All @@ -45,16 +50,19 @@
"@feathersjs/transport-commons": "^4.5.1",
"compression": "^1.7.4",
"cors": "^2.8.5",
"feathers-sequelize": "^6.1.0",
"helmet": "^3.21.2",
"pg": "^7.17.1",
"pg-hstore": "^2.3.3",
"sequelize": "^5.21.3",
"sequelize-typescript": "^1.1.0",
"serve-favicon": "^2.5.0",
"web3": "^1.2.5",
"winston": "^3.2.1"
},
"devDependencies": {
"@feathersjs/cli": "^4.2.5",
"@types/bluebird": "^3.5.29",
"@types/chai": "^4.2.7",
"@types/chai-as-promised": "^7.1.2",
"@types/compression": "^1.0.1",
Expand All @@ -63,9 +71,10 @@
"@types/dirty-chai": "^2.0.2",
"@types/helmet": "0.0.45",
"@types/mocha": "^5.2.7",
"@types/node": "^13.5.0",
"@types/node": "^13.5.1",
"@types/node-fetch": "^2.5.4",
"@types/serve-favicon": "^2.5.0",
"@types/validator": "^10.11.3",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"dirty-chai": "^2.0.1",
Expand Down
3 changes: 3 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import middleware from './middleware';
import services from './services';
import appHooks from './app.hooks';
import channels from './channels';
import sequelize from './sequelize';
// Don't remove this comment. It's needed to format import lines nicely.

const app: Application = express(feathers());
Expand All @@ -36,6 +37,8 @@ app.use('/', express.static(app.get('public')));
app.configure(express.rest());
app.configure(socketio());

app.configure(sequelize);

// Configure other middleware (see `middleware/index.js`)
app.configure(middleware);
// Set up our services (see `services/index.js`)
Expand Down
34 changes: 34 additions & 0 deletions src/sequelize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Sequelize } from 'sequelize-typescript'
import { Application } from './declarations'
import path from 'path'

export default function (app: Application): void {
const dbSettings = Object.assign({
models: [path.join(__dirname, '/models/**/*.model.ts')],
modelMatch: (filename: string, member: string): boolean => {
return filename.substring(0, filename.indexOf('.model')) === member.toLowerCase()
}
}, app.get('db'))

const sequelize = new Sequelize(dbSettings)
const oldSetup = app.setup

app.set('sequelizeClient', sequelize)

app.setup = function (...args): ReturnType<Application['setup']> {
const result = oldSetup.apply(this, args)

// Set up data relationships
const models = sequelize.models
Object.keys(models).forEach(name => {
if ('associate' in models[name]) {
(models[name] as any).associate(models)
}
})

// Sync to the database
app.set('sequelizeSync', sequelize.sync())

return result
}
}

0 comments on commit a52e341

Please sign in to comment.