How to create `Collection Behaviours` with native extend collection?


#1

I would like to try to create Collection Behaviours: timestamp (don’t depend on collection extend package) such as lai:collection-extensions.
Please help me with example???


#2

One example is to make a subclass of collection and override the insert/update methods to add timestamps:

class AutoTimestampCollection extends Mongo.Collection {
  
    insert(document, ...rest) {
    document.createdAt = new Date();
    super(document, ...rest);
  }

  update(selector, modifier, ...rest) {
    modifier.$set = modifier.$set || {};
    modifier.$set.updatedAt = new Date();
    super(selector, modifier, ...rest);
  }

}

And make your Collections instances of this new class:

const Todos = new AutoTimestampCollection('todos');

Note: This code is an example and untested, use caution when implementing. You should probably check out the code in timestampable to see how it works and implement similar


#3

Very thanks, I will try.
If I have many collection extends such as: softRemovable, restorable.
Excuse me, could you example one more for softRemovable/restorable behaviours?
:blush:


#4

document is undefined

class AutoTimestampCollection extends Mongo.Collection {
    insert(document, callback) {
       document.createdAt = new Date();
       console.log(document)
       return super.insert(document, callback);
   }

#5

As I mentioned in my note, that was just example code. You should add your own parameter checking logic (if you want it). As an example, I didn’t point the super calls in the methods to the right place (should be super.insert( not super(

Calling insert on a database without a document to insert will always throw an error.
How are you calling the insert function?


Here’s an example with the other behaviours:
Note: untested and you should adjust for your needs

class AutoTimestampCollection extends Mongo.Collection {
  
    insert(document, ...rest) {
    document.createdAt = new Date();
    super.insert(document, ...rest);
  }

  update(selector, modifier, ...rest) {
    modifier.$set = modifier.$set || {};
    modifier.$set.updatedAt = new Date();
    super.update(selector, modifier, ...rest);
  }

  remove(selector, ...rest) {
    // Note we're updating calling update on the super-class (Mongo.Collection) instead of remove
    super.update(selector, { $set: { deleted: true, deletedAt: new Date() } }, ...rest)
  }

  restore(selector, ...rest) {
    // Adjust based on how you want to track deletes/restores etc.
    super.update(selector, { $set: { deleted: false, restoredAt: new Date() } }, ...rest);
  }

}

#6

Thanks again, I will try


#7

Now It work fine :blush:


#8

Now have problem with Meteor.userId() in collection extends

class AutoTimestampCollection extends Mongo.Collection {
   constructor(name){
      super(name);
      this.userId = Meteor.userId(); // don't work
   }
  insert(document, ...rest) {
    document.createdAt = new Date();
    document.createdBy = Meteor.userId(); // don't work
    super.insert(document, ...rest);
  }

#9

and have problem with Simple Schema, bc I don’t have createdAt , createdBy field definde

export default new SimpleSchema({
  refNo: {
    type: String,
  },
  name: {
    type: String,
  },
  memo: {
    type: String,
    optional: true,
  }