-
-
Notifications
You must be signed in to change notification settings - Fork 109
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
Apply defaultValue
on fetch
#433
Comments
How's it not applied? Please provide a clear code example of what you're trying to achieve. A thorough explanation is always best. I look up "defaultValue" in mongoose docs but I was only able to find this. I'm not familiar with mongoose so please
Just to be clear, Collection2 is mere wrapper around simple-schema to ensure consistent data according to your defined schema. Maybe you need to look deeper into simpl-schema. |
server/main.js import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import SimpleSchema from 'simpl-schema';
import mongoose from 'mongoose';
mongoose.connect(process.env.MONGO_URL);
const Books = new Mongo.Collection('books');
Books.attachSchema(
new SimpleSchema({
title: {
type: String,
defaultValue: '',
},
})
);
const Bookoose = mongoose.model('books', {
title: {
type: String,
default: '',
},
});
Meteor.startup(async () => {
await Books.rawCollection().insert({});
console.log('Collection2:');
console.log(Books.find().fetch());
console.log('Mongoose:');
console.log(await Bookoose.find().exec());
});
But only on insert/update. Hence the feature request to apply this to documents when they are fetched as well. You can see that even though there is no |
Well first off, I wanna thank you for taking the time to recommend ways to improve Collection2. I really appreciate it! :) hmm, I could definitely see some value but I'm unsure how this would affect current usage. We need more opinions. |
Thanks for looking into them!
It could be an option that defaults to There definitely needs to be some thought put into this. It won't work with the default clean options, but that's perfectly fine. E.g. I've |
In my opinion this is not in the scope of this package. Migrations are the standard way to to ensure integrity when making schema modifications. If something prevents you from using migrations, I would suggest delegating this task to a model controller. |
I partially agree with @copleykj. Currently it is our of the scope of this package, but I would be open for discussing inclusion of this functionality in the next major version. |
Is your feature request related to a problem? Please describe.
I'm defining a schema as a contract between my application and the database. So that the application can be sure to always receive data according to the schema.
Coming from Mongoose I was expecting
defaultValue
to be always applied. It is not. Contract broken.Describe the solution you'd like
Always apply
defaultValue
Describe alternatives you've considered
I assume everyone is writing migrations for every new property? The beauty of default values in the schema is that you don't need to migrate every single document when adding a new optional property (it's optional after all, why force the default value to be persisted in the db?). They can just be missing in the db and you're good.
Additional context
I'm not sure if this is out-of-scope for this particular project? As I said, I'm used to Mongoose which does a lot more than just the schema. Even if it's in-scope, but not sure if this is even possible given the design of Meteor to intercept every fetch?
The text was updated successfully, but these errors were encountered: