nestJS fetch data from a model dynamically i.e. model name as variable - node.js

How to fetch data from mongo model in nestjs only by name of the model.
i.e. instead of this.userModel.find() can we have some approach to get the model dynamically and then fetch data from there.
I tried,
const model = mongoose.model('User');
model.find({});
but it's not working
I have a requirement, where based on the input variable I have to get data of that collection.

Related

Confusing database with schema

I have defined db name in squelize constructor so that tables being generated by corresponding model files. Tables are successfully created in given db. I am confused with the schema which by default is public in models.So why to define schema in models if tables are properly generated in db in default postgresql server named PostgreSQL 15. if I change name of schema from public to some how ALGH it says schema doesnot exist. if it is referencing schema then what is the purpose of giving db name in sequelize constructor.
You can have multiple schemas in the same DB for different purposes like to divide sets of tables for different apps/services or by functionality.
You can indicate a schema in the Sequelize instance and it will be the default schema for all models that does not have an explicit schema OR you can indicate a schema for certain models only and other models will have the default public schema.
Of course, you need to create tables only after you decided and indicated all schemas (either in Sequelize instance and/or in certain models) you need otherwise you will get such errors you already mentioned in the post.

Changing collection name according to query parameters in Nestjs

Can we get data from different collections of same or multiple mongo databases according to the query parameters in nest js ?
For example if parameter says get data from collection A, then collection A data should be displayed if it says get data from collection B, then collection B data should be displayed.
Can we do it in same controller or we need to make multiple controllers ?
I got it, I just have to make two models from different collections and use the desired model according to query parameter by using simple if then else.

Why use mongoose schema in Node.js

I was learning Node.js and Mongoose. Then, I cam across the term schema which is defined as the description of the structure of the data, default values and validation. So, as we know schema looks basically like this:
var TaskSchema = new Schema({
name: String,
priority: Number
});
The question I would like to ask is Why should we care about the description of structure of data that the schema allows to achieve, I mean, what is the main point in the use of schemas in mongoose? if you say, validation, then can't we achieve that with express-validator package? or...
Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.
See the documentation for more information.
Schemas not only define the structure of your document and casting of properties, they also define document instance methods, static Model methods, compound indexes, and document lifecycle hooks called middleware.
This is what allows you to communicate with the database easily using mongoose. It's not just for validation.

Custom Mongoose Population

I have a microservices architecture (NodeJS) where the services have their own database (mongo). Each database has an userId reference (from another microservice). My question is if I can (and how) make a custom mongoose population function, where I can give it a field name and a function that iterates trough the field name and returns an object.
Items.find({}).populate('userId', getObj)
getObj(userId) {
return "object promise";
}

Why does the model name matter in Mongoose?

Why does the model name matter in Mongoose? I understand that given a model name, Mongoose can try to guess the collection name. However, for this application it seems better and easier to just explicitly specify the collection name instead of the model name.
What else is the model name used for in Mongoose?
Mongoose is an Object Data Mapping (ODM) library for node similar to Object Relational Mapping (ORM) in Ruby on Rails and some other frameworks. Therefore, it follows more or less the same rules (or guidelines, as you can override them):
ORM:
Class name capital and singular
Table name small and plural
Class object mapped/translated to table row and vice versa
Class and its objects have methods/hooks
ODM:
Model name capital and singular (e.g. User)
Collection name small and plural (e.g. users)
Model object mapped/translated to collection document and vice versa (e.g. object of model: new User({name: "abc"}), document in collection: {"name": "abc"})
Model and its objects have methods/hooks (e.g. pre, post hooks; validations etc)
The mapping is between incompatible types, like an object of Ruby class to a row of SQL table; and a JavaScript object of Mongoose model (can have functions, for instance) to a JSON document of mongodb (valid JSON can't have functions).
Since class and table, model and collection are different entities (though related) and serve different purpose, they are named differently (though similarly). Hence the need of model name in Mongoose!

Resources