Meteor method to add new field with type to collection


#1

I’m using node simple schema to define a schema to use for an API for example:

export const Data = new Mongo.Collection('data');
schema = {}
export const schema.data = new SimpleSchema({
    name: {
        type: String,
        label: "Device's Name",
    },
    message: {
        type: String,
        label: "Message",
    },
    data: {
        type: Date,
        autoValue: function() {
            if (this.isUpdate) {
                return new Date();
            }
        },
    },

}, { tracker: Tracker });

Data.attachSchema(data.schema);

Now I want users to be able to define and add new field with type to the data collection using a predefined method from the server that user can call on the API-Client. This method should add a new field with type to the data collection. Ex:
api.call.addData( surName: {type: Array, label: “User Surname”, OtherSimpleSchema_attribute…}). But the defined field should be available only to the actual user.
Any help


#2

Which one are you actually using?

  1. simpl-schema by aldeed, 2 months old, 17000 dlds / mth.
  2. node-simple-schema by yourcelf , 2 years old, 670 dlds / mth.

Personally I make sure that my schema thoroughly defines what should go to MongoDB, leaving nothing for user choice.

I have needed extra fields on forms (eg, “confirm password”), that I remove before saving. To do that with SimpleSchema I use Lodash deepClone() of the schema, extend the clone with those attributes and then pass the clone to the form.

You can learn about extending in the README : Extending Schemas


#3

@warehouseman thanks for your reply. I’m using node-simple-schama. What I’m trying to do is not to extend the schema, but to add new field for example:

let field = "someNewField";
let fieldType = "Array";
let fieldLabel = "Custom Label";
const CustomSchema = new SimpleSchema({
    field: {
        type: fieldType,
        label: fieldLabel,
    }
});

and then Collection.attachSchema(CustomSchema );