Saturday, 23 July 2022

DDD: using db layer as a singleton, could this be problematic

I recently downloaded a ddd boilerplate to get an example of a javascript functional approach to ddd. As I went through and analyzed the code I reached the awilix ioc setup and am a little confused on this approach. The code below is directly from the container.js file (https://github.com/joshuaalpuerto/node-ddd-boilerplate):

const { createContainer, asValue, asFunction } = require('awilix')
// you can do this
const app = require('./app')
const server = require('./interfaces/http/server')
const router = require('./interfaces/http/router')
const auth = require('./interfaces/http/auth')
const config = require('../config')
const logger = require('./infra/logging/logger')
const database = require('./infra/database')
const jwt = require('./infra/jwt')
const response = require('./infra/support/response')
const date = require('./infra/support/date')
const repository = require('./infra/repositories')

const container = createContainer()

// SYSTEM
container
  .register({
    app: asFunction(app).singleton(),
    server: asFunction(server).singleton(),
    router: asFunction(router).singleton(),
    logger: asFunction(logger).singleton(),
    database: asFunction(database).singleton(),
    auth: asFunction(auth).singleton(),
    jwt: asFunction(jwt).singleton(),
    response: asFunction(response).singleton(),
    date: asFunction(date).singleton(),
    config: asValue(config),
    repository: asFunction(repository).singleton()
  })

module.exports = container

If endpoints are being accessed that access the db layer via a repository layer, should the database layer be a singleton? I would think this could open the door to unwanted side effects if all users are accessing the same db connection. Granted, I am probably missing something as this is my first time seeing awilix being used but it sure looks like only one instance of the database layer is being utilized. Am I missing something here and is this actually safe or could this really lead to issues?



from DDD: using db layer as a singleton, could this be problematic

No comments:

Post a Comment