I am experimenting with using Flowtype in a Meteor+React app. Adding types to my various functions and classes seems to work well, however I would really like to type-check access to the different collections as well.
The idea would be to specify that all items in the collection "Books" would at least have certain fields (defined as the type Array), ideally to verify this whenever it reads data from Mongo (at least in development), and then it would know that if I did
const a = Meteor.books.findOne(id)
then a
would have the type Book.
Currently I'm accessing the data both through Meteor.createCollection, and through Meteor.find().fetch() or Meteor.findOne().
Ideas are welcome!
I think this wouldn't be so simple (for now), because Meteor core should somehow support this feature.
So Meteor.findOne()
returns simple JavaScript Object and Meteor.find().fetch()
returns JavaScript Array.
Maybe you can try example from Flow | Objects
docs:
type Book = { name: string, author: string, price: number };
const book = Meteor.books.findOne(id); //returns { name : 'Flowtype Handbook', author: 'renren89', price: 'free'}
( book : Book );
But as you can see Meteor should return data first while running application to get this example actually usable.
Another option is to use third party packages for Collection validation against Schema. There two competitor packages:
Maybe this solution is better than using Flowtype
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With