How to check Mongo collections in Meteor with Flowtype?

I am experimenting using Flowtype in a Meteor + React app. Adding types to my various functions and classes seems to work well, however I would really like the type of validation to also be checked against various collections.

The idea would be to indicate that all items in the Books collection will have at least certain fields (defined as an Array type), ideally to test this when it reads data from Mongo (at least in development ) and then he would know that if I did

const a = Meteor.books.findOne(id) 

then a will be of type Book.

I am currently browsing data through both Meteor.createCollection and Meteor.find (). fetch () or Meteor.findOne ().

Ideas are welcome!

+6
source share
1 answer

I think that it will not be so simple (at the moment), because the Meteor kernel must somehow support this function.

So, Meteor.findOne() returns a simple JavaScript object, and Meteor.find().fetch() returns a JavaScript array. Perhaps you can try the 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 must first return data at application startup, so this example is really useful.

Another option is to use third-party packages to validate the collection against the schema. There are two packages of competitors:

Perhaps this solution is better than using Flowtype

+1
source

Source: https://habr.com/ru/post/1012534/


All Articles