Skip to content

Instantly share code, notes, and snippets.

@pegli
Last active December 19, 2015 11:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pegli/5951901 to your computer and use it in GitHub Desktop.
Save pegli/5951901 to your computer and use it in GitHub Desktop.
prototype methods for attachment management
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