SimpleSchema on Meteor 3

I’m trying to setup my first Meteor 3 app using SimpleSchema, Collection2, indexes and TypeScript. First off, thanks to the community for porting these important packages! However, I got a bit confused about how to setup this correctly.

The docs of aldeed:simple-schema say you should use version 2.0.0 or higher, but there’s only a 1.13.1 available. I picked this version, and it seems to work.

The docs of aldeed:collection2 say you should either initialize it using

import 'meteor/aldeed:collection2/static';

or

import 'meteor/aldeed:collection2/dynamic';

Collection2.load();

But they don’t state where to put this code.

I added the static version to a common.js file and this made Meteor find Mongo.Collection.attachSchema(), so I guess this was the right approach. However, TypeScript still complains about the missing method attachSchema(). Is there a way to get rid of this error?

If I try the “dynamic” approach, Meteor starts complaining about the missing method again. Maybe because attachSchema() is being called before Collection2.load() is ready. Should I place all schema attachments in a Meteor.startup() then? Also, calling a global Collection2 variable seems a bit odd to me.

The indexing feature seems to work right away, which is amazing.

However, I still have an issue with autoValues like these:

createdAt: {
    type: Date,
    optional: true,
    index: 1,
    autoValue: function() {
      if (this.isInsert) {
        return new Date();
      } else if (this.isUpsert) {
        return {
          $setOnInsert: new Date()
        };
      } else {
        this.unset();
      }
    }
  },

TypeScript complains that this.insert, this.upsert and this.unset are not valid. According to the docs, they should work fine. Maybe just the TypeScript notations are missing here?

Erm, yes. I’m using this?

It says version 2.0.0

https://packosphere.com/aldeed/simple-schema

1 Like

@waldgeist I use the following directly in main.js of the /server

import 'meteor/aldeed:collection2/static'
1 Like

Thanks, that’s what I did now as well. Yet I am still curious how the “dynamic” approach should work.

Interesting. I looked on github, there it was 1.13.1, and there was no other 2.0.0 branch.

@jkuester might be able to give a proper update here. I remember him preparing a release for Meteor 3.0

It’s all a bit confusing. SimpleSchema got moved to npm and we moved it back, because SimpleSchema 3 has dropped all Meteor support.

Consider the latest 1.13.1 release to be the last npm release that supported Meteor but wrapped as Meteor package.

Regarding collection 2 - the dynamic load is to use dynamic-import to load this package on the client. This is because beforehand the clients loaded the entire package immediately to the bundle. Therefore you have to await the load function.

You should use it, if you want to optimize bundle size but you need to make sure it’s loaded before your collections are initialized. If you don’t care about bundle size then use the static import route as @paulishca showed

2 Likes

Thanks. Does that mean that version 2.0.0 does not “live” on npm anymore but is available via Meteor’s package system, embedded in collection2? But why does my version file show “aldeed:simple-schema@1.13.1” then?

I only have collection2 and index in my package file:

image

But maybe I had added simple-schema directly before and that’s the reason?

I’m also wondering where the 2.0.0 would come from, as Packosphere points to the Github repo where only 1.13.1 can be found?