Skip to content

Commit

Permalink
connection + update
Browse files Browse the repository at this point in the history
  • Loading branch information
aymericdo committed Jan 18, 2024
1 parent becdff4 commit 2f2a35b
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 34 deletions.
124 changes: 112 additions & 12 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"jest-environment-jsdom": "^29.6.2",
"jsdom": "^22.1.0",
"module-alias": "^2.2.3",
"mongoose": "^7.4.3",
"mongoose": "^8.1.0",
"node-cache": "^5.1.2",
"node-cron": "^3.0.2",
"point-in-polygon": "^1.1.0",
Expand Down Expand Up @@ -77,4 +77,4 @@
"ts-node-dev": "^2.0.0",
"typescript": "^5.1.6"
}
}
}
46 changes: 30 additions & 16 deletions src/db/db.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
import { createConnection } from 'mongoose'
import { PrettyLog } from '@services/helpers/pretty-log'

const localConnectionString = 'mongodb://localhost/loyer-paris-be'
const mongoose = require('mongoose')

const rentConnection = createConnection(process.env.MONGODB_URI || localConnectionString)
function makeNewConnection(uri: string) {
const db = mongoose.createConnection(uri)

const empriseBatieConnection = createConnection(process.env.MONGODB_URI_EMPRISE_BATIE || localConnectionString)
db.on('error', function (error) {
PrettyLog.call(`MongoDB :: connection ${this.name} ${JSON.stringify(error)}`, 'red')
db.close().catch(() => PrettyLog.call(`MongoDB :: failed to close connection ${this.name}`))
})

const encadrementAddress1Connection = createConnection(
process.env.MONGODB_URI_ENCADREMENT_ADDRESS1 || localConnectionString)
db.on('connected', function () {
if (process.env.CURRENT_ENV === 'dev') {
mongoose.set('debug', function (col, method, query, doc) {
PrettyLog.call(`MongoDB :: ${this.conn.name} ${col}.${method}(${JSON.stringify(query)},${JSON.stringify(doc)})`)
})
}
PrettyLog.call(`MongoDB :: connected ${this.name}`, 'green')
})

const encadrementAddress2Connection = createConnection(
process.env.MONGODB_URI_ENCADREMENT_ADDRESS2 || localConnectionString
)
db.on('disconnected', function () {
PrettyLog.call(`MongoDB :: disconnected ${this.name}`, 'green')
})

return db
}

// Use
const localConnectionString = 'mongodb://localhost/loyer-paris-be'

const rentConnection = makeNewConnection(process.env.MONGODB_URI || localConnectionString)
const empriseBatieConnection = makeNewConnection(process.env.MONGODB_URI_EMPRISE_BATIE || localConnectionString)
const encadrementAddress1Connection = makeNewConnection(process.env.MONGODB_URI_ENCADREMENT_ADDRESS1 || localConnectionString)
const encadrementAddress2Connection = makeNewConnection(process.env.MONGODB_URI_ENCADREMENT_ADDRESS2 || localConnectionString)

const rentSchema = require('./rent.model')
const incompleteRentSchema = require('./incomplete-rent.model')
Expand All @@ -27,22 +48,15 @@ const bordeauxAddressSchema = require('./bordeaux-address.model')
export const Rent = rentConnection.model('Rent', rentSchema)
export const IncompleteRent = rentConnection.model('IncompleteRent', incompleteRentSchema)
export const EmpriseBatie = empriseBatieConnection.model('Batie', empriseBatieSchema)

export const ParisAddress = encadrementAddress1Connection.model('parisaddress', parisAddressSchema)

export const LilleAddress = encadrementAddress1Connection.model('lilleaddress', lilleAddressSchema)

export const PlaineCommuneAddress = encadrementAddress1Connection.model(
'plainecommuneaddress',
plaineCommuneAddressSchema
)

export const LyonAddress = encadrementAddress2Connection.model('lyonaddress', lyonAddressSchema)

export const EstEnsembleAddress = encadrementAddress2Connection.model('estensembleaddress', estEnsembleAddressSchema)

export const MontpellierAddress = encadrementAddress2Connection.model('montpellieraddresses', montpellierAddressSchema)

export const BordeauxAddress = encadrementAddress2Connection.model('bordeauxaddresses', bordeauxAddressSchema)

export const closeAllConnections = async () => {
Expand Down
8 changes: 4 additions & 4 deletions src/services/helpers/sentry.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { captureMessage, Severity } from '@sentry/node'
import { captureMessage } from '@sentry/node'

export class SentryService {
error(message: string) {
captureMessage(message, Severity.Error)
captureMessage(message, 'error')
}

warning(message: string) {
captureMessage(message, Severity.Warning)
captureMessage(message, 'warning')
}

light(message: string) {
captureMessage(message, Severity.Log)
captureMessage(message, 'log')
}
}

0 comments on commit 2f2a35b

Please sign in to comment.