Hello @eakenbor,
You can add as many models as you want. Mongoose v8.0.0: Models
The following pattern works normally, but this is more of a mongoose issue.
connection.model("ModelName", schema)
connection.model("ModelName2", schema2)
...
Are you talking about automatically creating models based on actual data in your No-SQL DB?
Have you use forest
CLI to generate your project ? I
It creates it for you. (You can try it using Advanced Onboarding setup and following the Mongo DB setup)
How does it create them.
index.js
const fs = require('fs');
const path = require('path');
const Mongoose = require('mongoose');
const connection = Mongoose.createConnection(process.env.DATABASE_URL);
fs
.readdirSync(__dirname)
.filter((file) => file !== 'index.js')
.forEach((file) => {
try {
const { schema, modelName, collectionName } = require(path.join(__dirname, file));
connection.model(modelName, schema, collectionName);
} catch (error) {
console.error(`Model creation error: ${error}`);
}
});
module.exports = connection;
model.js
const Mongoose = require('mongoose');
const schema = new Mongoose.Schema({
address: {
streetNumber: Number,
streetName: String,
city: String,
country: String,
},
lastname: String,
firstname: String,
storeId: Number,
bills: [{
title: String,
amount: Number,
issueDate: Date,
items: [{
importance: String,
title: String,
amount: Number,
}],
}],
}, {
timestamps: false,
});
module.exports = {
collectionName: 'accounts',
modelName: 'accounts',
schema,
};
If you are talking without defining any models at all.
This is something we are working on but No-SQL is not that straightforward to create the models from since the data can be anything. We have some ideas on how to solve this (basically taking a sample of the DB and analyzing it) but it will take us some more time to finalize it.
I see that you’ve overridden the default toJSON blogPostSchema.methods.toJSON
method. It could be the cause.
(Or the fact that you refer to User and it doesn’t look to be declared following your first message )
In general in forest admin, you should prefer using Computed field to do this. It allows computing asynchronous resources (in an optimized way) instead of doing the computation one by one.
You should have more logs in your agent (backend), if not you can enable debugging with loggerLevel: 'Debug',
(more in info about logger options)
Kind regards,
Morgan