Comments (6)
Coming from Mongoose I was expecting defaultValue to be always applied. It is not. Contract broken.
How's it not applied?
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
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?
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.
from meteor-collection2.
Please provide a clear code example of what you're trying to achieve.
meteor create --minimal defaults
cd defaults
meteor npm i mongoose simpl-schema
meteor add mongo
meteor add aldeed:collection2
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());
});
$ meteor run
Collection2:
[
{ _id: ObjectID { _str: '613f1af210ed928c897f4956' } },
{ _id: ObjectID { _str: '613f1b018b16e18cf59f5beb' } },
{ _id: ObjectID { _str: '613f1b4fa07c418e95ca8489' } },
{ _id: ObjectID { _str: '613f1b5c9f34a68f65578d53' } },
{ _id: ObjectID { _str: '613f1b7a177d0e900cf0669a' } },
{ _id: ObjectID { _str: '613f1b810295cb904fbe0488' } },
{ _id: ObjectID { _str: '613f1b931d346f90c248cfea' } },
{ _id: ObjectID { _str: '613f1c8c979bba91a3fb47e4' } },
{ _id: ObjectID { _str: '613f1cc1d0872b92783c7ad7' } },
{ _id: ObjectID { _str: '613f1cca89a5ee92dc71d47e' } },
{ _id: ObjectID { _str: '613f1cd348bdf7933396c57d' } }
]
Mongoose:
[
{ title: '', _id: new ObjectId("613f1af210ed928c897f4956") },
{ title: '', _id: new ObjectId("613f1b018b16e18cf59f5beb") },
{ title: '', _id: new ObjectId("613f1b4fa07c418e95ca8489") },
{ title: '', _id: new ObjectId("613f1b5c9f34a68f65578d53") },
{ title: '', _id: new ObjectId("613f1b7a177d0e900cf0669a") },
{ title: '', _id: new ObjectId("613f1b810295cb904fbe0488") },
{ title: '', _id: new ObjectId("613f1b931d346f90c248cfea") },
{ title: '', _id: new ObjectId("613f1c8c979bba91a3fb47e4") },
{ title: '', _id: new ObjectId("613f1cc1d0872b92783c7ad7") },
{ title: '', _id: new ObjectId("613f1cca89a5ee92dc71d47e") },
{ title: '', _id: new ObjectId("613f1cd348bdf7933396c57d") }
]
to ensure consistent data according to your defined schema
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 title
in the database Mongoose returns one. This makes changing the schema 🔥 without having to write migrations all the time just to add a default to existing docs.
from meteor-collection2.
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.
cc @StorytellerCZ @coagmano @copleykj
from meteor-collection2.
Well first off, I wanna thank you for taking the time to recommend ways to improve Collection2. I really appreciate it! :)
Thanks for looking into them!
but I'm unsure how this would affect current usage
It could be an option that defaults to false
(maybe turn it true
in a major release) 👍
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
set all of them to false
, including removeEmptyStrings
. Someone that uses removeEmptyStrings
wouldn't expect to receive an empty string when fetching objects either. Not sure about other options (haven't used simpl-schema before stumbling into Meteor and Collection2), so maybe it would be as simple as calling clean
on fetched object, assuming we can hook into fetching (I didn't look into how Collection2 intercepts insert/update calls)
from meteor-collection2.
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.
from meteor-collection2.
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.
from meteor-collection2.
Related Issues (20)
- Upsert no longer works in 3.0.5
- Duplicate key errors not correctly reporting (no Meteor.Error) HOT 3
- Migration from Meteor 1.4 to 1.9, Collection 2 from 2.10 to 3.0, SimpleSchema with meteor-collection2 error on unique option HOT 3
- Can't update the collection HOT 3
- Dynamic Schema with Validation HOT 10
- Is it possible to create field type that takes key and value pair? HOT 4
- Writing a robust updatedAt autoValue HOT 4
- CONTRIBUTING.md is missing HOT 1
- Breaking change in v3.2.0 for extending schemas HOT 2
- Delete the name of collections in error messages HOT 5
- Question: A way to log all errors without actually throwing them? HOT 1
- Does not work with `$[]` positional operator HOT 1
- relplace lodash with underscore HOT 3
- FLE-Encryption HOT 3
- Expose defaultCleanOptions HOT 10
- Stub exceptions are logged instead of thrown in Meteor Methods, preventing throwStubExceptions from working as intended
- Meteor 3.0 potential bug HOT 7
- 4.0 load function is not dynamic HOT 2
- Insert with 'bypassCollection2: true' throws an exception HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from meteor-collection2.