-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What is the best way to manage "relational" defaults? #607
Comments
I guess you can indeed use
|
That would not work as I need the id of the default-created ModelB. This would assume that I store modelB nested in the document. However it is stored separately and referenced with the id and a hasOne relationship. |
You could still do something like that : Assuming you have express const defaultModelB = new ModelB(...)
app.locals.defaultModelB = defaultModelB
Note: you need either the id if you want to store |
The id would be undefined. Ids are not created until they are saved in the DB. const defaultModelB = new ModelB(...);
console.log(defaultModelB.id); // undefined |
You can use a middleware like that : app.use((req, res, next) => {
if (!defaultModelB.isSaved()) {
defaultModelB.save().then(() => next())
}
next()
}) |
Okay, but that's not what I am looking for. To be specific, I want to handle this all with only the Model. Including Express middlewares is not a solution for me. |
I think even in Eloquent there are no default relationships |
Question
Say I wanted to have a default for a model (ModelA) that was the id of another model (ModelB). The schemas look like this:
When I create a new modelA, I want it to automatically create a new modelB with some default attributes and add the id of that newly created modelB to the modelBId attribute of modelA. So it would work like this:
Is this possible? I know that
.default()
can accept a function but that function assumes a synchronous process because it has tonext
support like thepre
andpost
event hooks do.The text was updated successfully, but these errors were encountered: