简体   繁体   中英

How to check Mongo collections in Meteor with Flowtype?

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 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM