Last active
December 19, 2015 11:59
-
-
Save pegli/5951901 to your computer and use it in GitHub Desktop.
prototype methods for attachment management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports.afterModelCreate = function(Model) { | |
Model = Model || {}; | |
Model.prototype.idAttribute = '_id'; // true for all TouchDB documents | |
Model.prototype.config.Model = Model; // needed for fetch operations to initialize the collection from persistent store | |
Model.prototype.attachmentNamed = function(name) { | |
var doc = db.documentWithID(this.id); | |
if (doc) { | |
return doc.currentRevision.attachmentNamed(name); | |
} | |
}; | |
Model.prototype.addAttachment = function(name, contentType, content) { | |
var doc = db.documentWithID(this.id); | |
if (doc) { | |
var rev = doc.newRevision(); | |
rev.addAttachment(name, contentType, content); | |
rev.save(); | |
} | |
}; | |
Model.prototype.removeAttachment = function(name) { | |
var doc = db.documentWithID(this.id); | |
if (doc) { | |
var rev = doc.newRevision(); | |
rev.removeAttachment(name); | |
rev.save(); | |
} | |
}; | |
Model.prototype.attachmentNames = function() { | |
var doc = db.documentWithID(this.id); | |
return doc ? doc.attachmentNames : []; | |
} | |
return Model; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment