Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 622
Stian Håklev Avatar asked Nov 21 '16 15:11

Stian Håklev


1 Answers

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:

  • Astronomy
  • Simple Schema (less active and maintained)

Maybe this solution is better than using Flowtype

like image 69
Kifir Avatar answered Oct 11 '22 17:10

Kifir