Meteor-Community-Packages / meteor-collection-hooks

Meteor Collection Hooks
https://atmospherejs.com/matb33/collection-hooks
MIT License
657 stars 92 forks source link

CollectionFS support #84

Closed ccorcos closed 9 years ago

ccorcos commented 9 years ago

Would it be possible to add support for hooks on CollectionFS?

matb33 commented 9 years ago

Interesting idea! I assume it's easy enough, probably just a matter of doing CollectionHooks.wrapCollection(FS.Collection, FS.Collection); and having the right api.use in package.js.

ccorcos commented 9 years ago

Here's an app you can test it with.

https://github.com/ccorcos/meteor-boilerplate

fpoirier1 commented 9 years ago

+1

fpoirier1 commented 9 years ago

After some digging, the Meteor/Mongo.collection resides in the files property of the FS.Collection. So something like this should be working.

Pictures = new FS.Collection('pictures', ...);
Pictures.files.before.insert(function(userId, picture){...});
afuggini commented 9 years ago

@fpoirier1 You rock! Thanks!

ccorcos commented 9 years ago

Still a quirk:

I've tried offloading some client work onto the server:

      # This now happens in the .before.insert hook
      # img.metadata =  
      #   date: Date.now()
      #   ownerId: Meteor.userId()

      Images.insert img,  (err, fileObj) ->
        if err
          console.log err

On the server, I try to take care of it:

  Images.files.before.insert (userId, doc) ->
    doc.metadata =  
      date: Date.now()
      ownerId: userId
    console.log "before", doc
    return doc

And I'm checking for the metadata here:

Images.allow
  insert: (userId, doc) ->
    console.log "insert", doc
    doc.metadata?.ownerId is userId

Based on the console log statements, when I insert an image, this is what happens:

I20150115-17:17:42.358(-8)? insert { createdByTransform: true,
I20150115-17:17:42.359(-8)?   original: { type: 'image/png', size: 122935 },
I20150115-17:17:42.359(-8)?   metadata: { date: 1421371062351, ownerId: 'KtMrS3DhBbqCzyTzm' },
I20150115-17:17:42.359(-8)?   _id: '5yev5oapPzK7ADmBe',
I20150115-17:17:42.360(-8)?   collectionName: 'images' }
I20150115-17:17:42.360(-8)? before { original: { type: 'image/png', size: 122935 },
I20150115-17:17:42.360(-8)?   metadata: { date: 1421371062358, ownerId: 'KtMrS3DhBbqCzyTzm' },
I20150115-17:17:42.360(-8)?   _id: '5yev5oapPzK7ADmBe' }
I20150115-17:17:42.460(-8)? insert { createdByTransform: true,
I20150115-17:17:42.460(-8)?   _id: '5yev5oapPzK7ADmBe',
I20150115-17:17:42.460(-8)?   chunkCount: 0,
I20150115-17:17:42.460(-8)?   chunkSize: 2097152,
I20150115-17:17:42.460(-8)?   chunkSum: 1,
I20150115-17:17:42.460(-8)?   metadata: { date: 1421371062358, ownerId: 'KtMrS3DhBbqCzyTzm' },
I20150115-17:17:42.461(-8)?   original: { type: 'image/png', size: 122935 },
I20150115-17:17:42.461(-8)?   collectionName: 'images',
I20150115-17:17:42.461(-8)?   collection: 
I20150115-17:17:42.461(-8)?    { storesLookup: { images: [Object] },
I20150115-17:17:42.461(-8)?      primaryStore: 
I20150115-17:17:42.461(-8)?       { mongoUrl: 'mongodb://127.0.0.1:3001/meteor',
I20150115-17:17:42.461(-8)?         mongoOptions: [Object],
I20150115-17:17:42.461(-8)?         name: 'images',
I20150115-17:17:42.462(-8)?         typeName: 'storage.gridfs',
I20150115-17:17:42.462(-8)?         adapter: [Object],
I20150115-17:17:42.462(-8)?         _removeAsync: [Function],
I20150115-17:17:42.462(-8)?         remove: [Function],
I20150115-17:17:42.462(-8)?         _transform: [Object],
I20150115-17:17:42.462(-8)?         _events: [Object] },
I20150115-17:17:42.462(-8)?      options: { filter: [Object], stores: [Object], chunkSize: null },
I20150115-17:17:42.463(-8)?      name: 'images',
I20150115-17:17:42.463(-8)?      files: 
I20150115-17:17:42.463(-8)?       { _makeNewID: [Function],
I20150115-17:17:42.463(-8)?         _transform: [Function],
I20150115-17:17:42.463(-8)?         _connection: [Object],
I20150115-17:17:42.463(-8)?         _collection: [Object],
I20150115-17:17:42.463(-8)?         _name: 'cfs.images.filerecord',
I20150115-17:17:42.463(-8)?         _restricted: true,
I20150115-17:17:42.463(-8)?         _insecure: undefined,
I20150115-17:17:42.464(-8)?         _validators: [Object],
I20150115-17:17:42.464(-8)?         _prefix: '/cfs.images.filerecord/',
I20150115-17:17:42.464(-8)?         before: [Object],
I20150115-17:17:42.464(-8)?         _hookAspects: [Object],
I20150115-17:17:42.464(-8)?         after: [Object],
I20150115-17:17:42.464(-8)?         hookOptions: [Object],
I20150115-17:17:42.464(-8)?         direct: [Object] },
I20150115-17:17:42.464(-8)?      _validators: { download: [Object] } },
I20150115-17:17:42.464(-8)?   domain: null,
I20150115-17:17:42.465(-8)?   _events: {},
I20150115-17:17:42.465(-8)?   _maxListeners: 10,
I20150115-17:17:42.465(-8)?   attachData: [Function: fsFileAttachData],
I20150115-17:17:42.465(-8)?   uploadProgress: [Function],
I20150115-17:17:42.465(-8)?   controlledByDeps: [Function],
I20150115-17:17:42.465(-8)?   getCollection: [Function],
I20150115-17:17:42.465(-8)?   isMounted: [Function],
I20150115-17:17:42.465(-8)?   getFileRecord: [Function],
I20150115-17:17:42.465(-8)?   update: [Function],
I20150115-17:17:42.465(-8)?   _saveChanges: [Function],
I20150115-17:17:42.466(-8)?   remove: [Function],
I20150115-17:17:42.466(-8)?   getExtension: [Function],
I20150115-17:17:42.466(-8)?   isImage: [Function],
I20150115-17:17:42.466(-8)?   isVideo: [Function],
I20150115-17:17:42.466(-8)?   isAudio: [Function],
I20150115-17:17:42.466(-8)?   formattedSize: [Function: fsFileFormattedSize],
I20150115-17:17:42.466(-8)?   isUploaded: [Function],
I20150115-17:17:42.467(-8)?   hasStored: [Function],
I20150115-17:17:42.467(-8)?   hasCopy: [Function],
I20150115-17:17:42.467(-8)?   getCopyInfo: [Function],
I20150115-17:17:42.467(-8)?   _getInfo: [Function],
I20150115-17:17:42.467(-8)?   _setInfo: [Function],
I20150115-17:17:42.467(-8)?   name: [Function],
I20150115-17:17:42.467(-8)?   extension: [Function],
I20150115-17:17:42.467(-8)?   size: [Function],
I20150115-17:17:42.467(-8)?   type: [Function],
I20150115-17:17:42.468(-8)?   updatedAt: [Function],
I20150115-17:17:42.468(-8)?   logCopyFailure: [Function],
I20150115-17:17:42.468(-8)?   failedPermanently: [Function],
I20150115-17:17:42.468(-8)?   createReadStream: [Function],
I20150115-17:17:42.468(-8)?   createWriteStream: [Function],
I20150115-17:17:42.468(-8)?   copy: [Function],
I20150115-17:17:42.468(-8)?   url: [Function],
I20150115-17:17:42.469(-8)?   setMaxListeners: [Function: setMaxListeners],
I20150115-17:17:42.469(-8)?   emit: [Function: emit],
I20150115-17:17:42.469(-8)?   addListener: [Function: addListener],
I20150115-17:17:42.469(-8)?   on: [Function: addListener],
I20150115-17:17:42.469(-8)?   once: [Function: once],
I20150115-17:17:42.469(-8)?   removeListener: [Function: removeListener],
I20150115-17:17:42.469(-8)?   removeAllListeners: [Function: removeAllListeners],
I20150115-17:17:42.470(-8)?   listeners: [Function: listeners] }

The insert gets called twice and the before happens in the middle...

matb33 commented 9 years ago

I wrote some tests in v0.7.9 to verify that hooks work with CollectionFS. I'm not getting any double inserts... I do get several update before/after hooks firing but that makes sense since CollectionFS updates properties as the file uploads.

yasaricli commented 9 years ago

@fpoirier1 :+1:

matb33 commented 9 years ago

@ccorcos are you still experiencing any issues? I'm not sure what to do at this point since my tests are passing... if you've resolved your issue I would be able to close this one off