[solved] How to skip SimpleSchema ``autoValue`` for specific fields on insert?

Hi guys,

I am generating fixtures for tests and I want to MANUALLY set doc.createdAt & doc.updatedAt values - WITHOUT SimpleSchema doing its “autoValue”-thing.

My SimpleSchema is complex and has OTHER fields WITH autoValue-functions that NEED to run when inserting, so I can NOT set getAutoValues as suggested in the docs:
To skip adding automatic values, set the getAutoValues option to false when you call insert or update. This works only in server code.

This is how the field is defined in the schema:

  // other fields with ``autoValue``-functions that NEED to run
  //  ..

  // HOW TO SKIP autoValue ONLY for this field when inserting a doc?
  updatedAt: {
    type: Date,
    autoValue: function() {
      return new Date();  // always add date
    },
  },

Is there a way to achieve this? I guess I’ll have to hack the schema before the insert and reset it afterwards?

I found a solution - so this is for you when you stumble upon the same challange:

  1. Read collection2’s docs about “Attaching Multiple Schemas to the Same Collection”: https://github.com/aldeed/meteor-collection2.
  2. Simply create 2 Schemas - one WITH autoValues (the standard-one you use for your app) PLUS one WITHOUT autoValues (the one you use to insert fixtures). Note: keep it DRY and use this syntax new SimpleSchema([UseCaseSchema, updatedCreatedAtSchema]) to combine 2 schemas together.
  3. BEFORE inserting your fixtures attach “Schema WITHOUT autoValues” to Collection. use { replace: true })-option
  4. AFTER inserting your fixtures attach “Standardschema WITH autoValues” to Collection. use { replace: true })-option