Confusing database with schema - node.js

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.

Related

How to load mongoose Schema from the collection name

Ideally, for populate to work during queries in mongoose, both the schemas of that which is been populated and the calling schema needs to be imported in same file under same repo.
Now, I have a scenario where a schema called AuthorSchema was created by another internal micro-service in another repo which is pointing to the same database as my own service and I wish to populate it during my queries.
Is there a way to automatically decode the AuthorSchema from the author collection name after I've connected to the database?
E.g
let posts = await Post.find({})
.populate("author", 'first_name last_name _id').limit(10)
Where I have created postSchema in my own service but authorSchema was created by another micro-service in another repo called author-management-microservice and as such I have no access to that schema.
What I would want is something like:
let AuthorSchema = mongoose.connection.decodeSchemaFromCollectionName('author');
Such that I can modify my query to something like
let posts = await Post.find({})
.populate({path:"author", model:AuthorSchema, select:'first_name last_name _id'}).limit(10)
The reason I want something like this is because I have been battling with MissingSchemaError: Schema hasn't been registered for model "Author". whenever I try to populate author and I don't want to copy around duplicate schema files across different micro-services.
To get the schema from a registered Mongoose model, you need to access the schema specifically:
const AuthorSchema = require('mongoose').model('Author').schema;
Is there a way to automatically decode the AuthorSchema from the author collection name after I've connected to the database?
No - the schema is saved at the code level, not the database level. There is no way to know the schema just from connecting to the database.

How to create new Schema same as an existing one in mongoDB

I have a question in mongoDB. I have a user schema with different fields and methods. But I want to when deleting a user from this schema to place it in another schema for example deleted-users schema.
Should I create the deleted-users schema with the same exact fields but different name ?
Or is there a better way to this

How do I specify multiple database schemas in Diesel?

How to specify multiple schemas in Diesel ORM (Rust). I desire to have several models specified into different schema according to their domain categories. I used a similar solution with hibernate and JPA in Java( you specify the default schema in the global configuration and the rest of the schema on each entity modal).
This is what I can currently configure in diesel.toml, but it is limited to a single schema only.
[print_schema]
file = "src/schema.rs"
schema = "shema-name"
Currently, the schema field in [print_schema] is for specifying only the default schema(If no value is provided, the public schema will be searched). For tables in a schema other than the default, the table name should be given as "schema_name.table_name". More is stated here

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.

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