Meteor-Files: Docs say to extend default schema, but default schema is not availble till after creation

I have a suggestion:

Describe your feature / request

Export the default schema so it can be extended before it is passed to the constructor.

How you going to use it? Give a usage example(s)

To simplify the process of extending the default schema in a collection like so:

import { FilesCollection, defaultSchema } from 'meteor/ostrio:files';
import SimpleSchema from 'simpl-schema';

export const Images = new FilesCollection({
  collectionName: 'Images',
  schema: new SimpleSchema ({
    ...defaultSchema,
    tags: Array,
    createdBy: {
        type: String,
        denyUpdate: true,
        denyInsert: true,
        optional: true,
        autoValue() {
            if (this.isInsert) {
                return Meteor.userId();
            } else if (this.isUpsert) {
                return { $setOnInsert: Meteor.userId() };
            } else {
                this.unset();
            }
        },
    },
  }),
});
Images.collection.attachSchema(new SimpleSchema(Images.schema));

Happy to write a PR for this if you’re happy for this to be added

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (14 by maintainers)

Commits related to this issue

Most upvoted comments

Testing it in an app I’m building now. Everything seems okay. I’ll let it run for a bit before I submit the PR to be sure

Closing, published as v1.9.6

@coagmano merged and published as v1.9.3

Hello @coagmano ,

Great! Let’s dig into it. I suggest to make a schema as static property of FilesCollectionServer class. As we expect plain Object, perhaps it should be static getter. Actually I thought it’s already made this way 😛

So we will be able:

import { FilesCollection } from 'meteor/ostrio:files';
import SimpleSchema from 'simpl-schema';

export const Images = new FilesCollection({
  collectionName: 'Images',
  schema: new SimpleSchema ({
    ...FilesCollection.schema,
    tags: Array,
    createdBy: {
        type: String,
        denyUpdate: true,
        denyInsert: true,
        optional: true,
        autoValue() {
            if (this.isInsert) {
                return Meteor.userId();
            } else if (this.isUpsert) {
                return { $setOnInsert: Meteor.userId() };
            } else {
                this.unset();
            }
        },
    },
  }),
});

Images.collection.attachSchema(new SimpleSchema(Images.schema));

WDYT?