Skip to content

Instantly share code, notes, and snippets.

@carbureted
Last active January 9, 2016 19:49
Show Gist options
  • Save carbureted/88d207ddfc9d2157902f to your computer and use it in GitHub Desktop.
Save carbureted/88d207ddfc9d2157902f to your computer and use it in GitHub Desktop.
Untested and somewhat ugly way to run pouchdb on react-native
/*
I just got a hacky, untested instance of pouchdb running on my
react-native IOS simulator five minutes ago. To do so, I installed
https://github.com/andpor/react-native-sqlite-storage and then pretended that it
was actually https://github.com/litehelpers/Cordova-sqlite-storage. This is
probably broken in some horribly subtle way, but the APIs I've manually tested
seem to work. Use very much at your own risk, and let me know how it goes.
I'm kind of shocked somebody hasn't come up with any other working solution, since
pouchdb and react-native are a perfect fit and there seems to be tons of demand.
*/
// hack to fool PouchDB's hacky cordova sqlite3 plugin detection
window.SQLitePlugin = true
window.sqlitePlugin = require('react-native-sqlite-storage')
// Pouchdb thinks it's in node if you do require('pouchdb')
var PouchDB = require('./node_modules/pouchdb/dist/pouchdb.min.js')
var db = new PouchDB('myDB', {adapter: 'websql'});
// Show that it's running
db.info().then(console.log.bind(console));
// Everything below here is saving dummy data. Refresh multiple times and watch the console!
function showTodos() {
db.allDocs({include_docs: true, descending: true}, function(err, doc) {
console.log(doc.rows, 'showing saved docs')
});
}
function addTodo(text) {
var todo = {
_id: new Date().toISOString(),
title: text,
completed: false
};
db.put(todo, function callback(err, result) {
if (!err) {
console.log('Successfully posted a todo!', result);
showTodos()
}
else {
console.log(err, 'err')
}
});
}
addTodo("derp")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment