Home > Software engineering >  Express Javascript: Is there a way to set schema attribute for a controller?
Express Javascript: Is there a way to set schema attribute for a controller?

Time:11-25

I'm new to stack overflow and new to dev.. and have been teaching myself to code using react and Express... so, apologies if this question is foundationally in-plausiable.. I have fundamental gaps in my knowledge :)

I have a bunch (25 likely to grow) collections in a mongoDB, and each collection has a mongoose defined schema that is unique to each collection.

The Express API end points are below, with the suffix correlating with the colection:

  • https://localhost:5030/mySchema/...
  • https://localhost:5030/myShema2/... -.... etc

I'm currently refactoring the code to strip out the control and route code. I had the logic appended after the verb in teh route path - which works, but is messy and cumbersome, and limits re-use is a pain to debug and update, so I'm spliting out the controller logic.

For each collection / API endpoint, I have common URLs / API functions applied to each route:

  • getAll
  • findByName
  • sumamry (filtering out attributes I don't want to display)
  • etc

If we use the following example code:

const mySchema = require('../models/mySchema'),
const mySchema1 = require('../models/mySchema1'),
const mySchema2 = require('../models/mySchema2'),
const mySchema3 = require('../models/mySchema3'),
const mySchema4 = require('../models/mySchema4'),
const mySchema5 = require('../models/mySchema5'),
const mySchema6 = require('../models/mySchema6'),


const getAll = (req, res) => {
  console.log('Request made to Fetch assets data')
    try {
        mySchema.find()
        then((resultsFound) => res.json(resultsFound))
        console.log('Results Found are', resultsFound)
        } catch (error) {
            next(err) //In-Built Express error Handling
        }
}

Is there a way of variabilising the schema name so this controller code can be applicable across the schema definitions for mySchema, myschema1, mySchema2 etc...

I've really only tried manually templating the config.

A hack way I thought of to achieve this is to strip out the suffix of the URL from the request, and set that as a variable... but that has a limited use-case it solves only those instances where the schema definition resides in the identical place in the URL.

Is there a more extensible way using things I don't know, and probably won't understand taht would achieve this dynamic scheam attribution in a controller?

CodePudding user response:

My advice would be to change the architecture of code, so that every separate route has its own controller (that works with appropriate schema model).

It would be a lot easier later if you need to do any other db queries or anything else with the schema model later.

I think I get what you are trying to achieve there, having one same controller for all getAll routes, but I'm not so sure that is the best solution.

Also, I suggest that you look up repository pattern. About code architecture, dividing routes and controllers, and all node js best practices checkout this

CodePudding user response:

create a schema without configuring it and export all of it in a single file and then do the configuration

const { model } = require('mongoose');
const mySchema = require('../models/mySchema')
const mySchema1 = require('../models/mySchema1')
const mySchema2 = require('../models/mySchema2')
const mySchema3 = require('../models/mySchema3')
const mySchema4 = require('../models/mySchema4')


export default allModels = {
    modelName: model("modelName", mySchema),
    modelName: model("modelName", mySchema1),
    modelName: model("modelName", mySchema2),
    modelName: model("modelName", mySchema3),
    modelName: model("modelName", mySchema4),
}

you can import by using

import model.myschema1 from './path of those models' || 
import model.myschema1 as myschema1 from './path of those models'
  • Related