Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
nolanlawson / tests.broken.js
Created February 11, 2014 17:24
Deliberately broken test.basics.js test
it('update_seq persists', function(start) {
var name = testHelpers.name;
testUtils.initTestDB(name, function(err, db) {
db.post({test:"somestuff"}, function (err, info) {
new PouchDB(name, function(err, db) {
db.info(function(err, info) {
equal(info.doc_count, 2, 'Doc Count persists'); // should be 1
start();
});
});
@nolanlawson
nolanlawson / couchperuser.md
Last active March 13, 2021 21:08
Solving "one database per user" in CouchDB/IrisCouch/Cloudant

Background

Security in a single CouchDB can only be set up to do either:

  • Everyone can read/write everything (admin party)
  • Everyone can read, some can write
  • Some can read everything, and those same people can write everything

So in the very common situation where you want user data to be private, the current best practice is to give every user a database. This sounds nuts at first, but it turns out that databases are cheap in CouchDB; Cloudant boasts that 100k databases in a single Couch is not uncommon (source).

@nolanlawson
nolanlawson / print_pouchdb_size.py
Created April 9, 2014 18:46
print_pouchdb_size.py
#!/usr/bin/env python
#
# Prints the pouchdb.min.js and gzipped file sizes for
# all PouchDB builds in the past several months.
#
import requests, zlib
def main():
print 'Commit','GitHub link','Date','pouchdb.min.js size','gzipped size'
@nolanlawson
nolanlawson / size_diffs.md
Last active August 29, 2015 13:58
Top 20 most size-increasing and size-reducing pouchdb commits
@nolanlawson
nolanlawson / inconsistent_results_couchdb_reduce_error.js
Created April 10, 2014 02:56
inconsistent_results_couchdb_reduce_error.js
it('should handle user errors in reduce functions', function () {
return new Pouch(dbName).then(function (db) {
function getKey(row) {return row.key; }
db.on('error', function () { /* noop */ });
return createView(db, {
map : function (doc) {
emit(doc.name, doc.data);
},
reduce : function (keys, values) {
return values[0].foo.bar;
@nolanlawson
nolanlawson / _blob-shim.md
Last active February 20, 2017 17:34
HTML5 Blob shim

HTML5 Blob shim

Small JavaScript function that abstracts constructing a Blob object, so it works in older browsers that don't support the native Blob constructor (e.g. old versions of QtWebKit).

Usage

The function createBlob() simply replaces new Blob():

@nolanlawson
nolanlawson / _pouch_2.2.0_perf.md
Last active April 28, 2017 14:31
PouchDB 2.2.0 performance report, April 2014

PouchDB 2.2.0 performance report

TL;DR

PouchDB 2.2.0, which will be released in early May, will boast an 80x-100x speed improvement in query() calls in IndexedDB (Chrome, Firefox, IE) and a roughly 40x speed improvement in WebSQL (Safari, Cordova). This requires that you first save the view before querying (so it can be indexed), but we will provide sugar to make that easy.

The old-style query() calls with temporary views, however, will be up to 100x slower due to no longer being performed in-memory. So developers will need to upgrade carefully.

@nolanlawson
nolanlawson / pouchdb_index_api.md
Last active August 29, 2015 14:00
PouchDB 2.3.0 index API proposal

PouchDB 2.3.0 index API proposal

Wherein we hack up our dream map/reduce API, slated to be introduced in PouchDB 2.3.0.

Basic

new PouchDB('mydb', {indexes: ['title']});
var start = document.getElementById('start');
var stuff = document.getElementById('stuff');
var remote = new PouchDB('http://registry.npmjs.org/', {cache: true});
var local = new PouchDB('npm');
start.addEventListener('click', function (){
remote.info().then(function (a) {
return a.doc_count;
}).then(function (count) {
remote.replicate.to(local, {
batch_size: 100
@nolanlawson
nolanlawson / websql_example.js
Created April 26, 2014 16:13
websql_example.js
openDatabase('mydatabase', 1, 'mydatabase', 5000000, function (db) {
function onTransactionSuccess() {
console.log('yay, transaction succeeded!');
}
function onTransactionError() {
console.log('boo, transaction failed!');
}
db.transaction(function (tx) {