Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Last active August 27, 2023 00:45
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 nolanlawson/b6d6164035f1fa0d38a8 to your computer and use it in GitHub Desktop.
Save nolanlawson/b6d6164035f1fa0d38a8 to your computer and use it in GitHub Desktop.
Putting and getting a plaintext attachment
<html>
<body>
<pre id="display"></pre>
<script src="//cdn.jsdelivr.net/pouchdb/latest/pouchdb.min.js"></script>
<script src="index.js"></script>
</body>
</html>
// Destroy the database before doing anything, because I want
// you to see the same thing if you reload.
// Ignore the man behind the curtain!
new PouchDB('sample').destroy().then(function () {
return new PouchDB('sample');
}).then(function (db) {
//
// IMPORTANT CODE STARTS HERE
//
db.put({
_id: 'mydoc',
_attachments: {
'myattachment.txt': {
content_type: 'text/plain',
data: 'aGVsbG8gd29ybGQ='
}
}
}).then(function () {
return db.get('mydoc', {attachments: true});
}).then(function (doc) {
var display = document.getElementById('display');
display.innerHTML = "Got our doc! It looks like this: " + JSON.stringify(doc);
display.innerHTML += '\nAnd our attachment, decoded, looks like this: ' + JSON.stringify(atob(doc._attachments['myattachment.txt'].data));
console.log(doc);
});
//
// IMPORTANT CODE ENDS HERE
//
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment