├── .gitignore ├── README.md ├── index.html ├── main.js ├── package.json ├── screenshot2.png └── script.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | mydb-* 3 | _pouch_mydb-sqlite 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hello Electron with Pouchdb 2 | 3 | This is a demo app that integrates PouchDB with Electron. 4 | 5 | It shows how to use any of the following PouchDB adapters: 6 | 7 | * IndexedDB (browser-style) 8 | * WebSQL (browser-style) 9 | * LevelDB (Node-style) 10 | * SQLite (Node-style) 11 | 12 | The app looks like this: 13 | 14 | ![screenshot](screenshot2.png) 15 | 16 | ## Install and run 17 | 18 | Check out the code: 19 | 20 | git clone https://github.com/nolanlawson/hello-electron-with-pouchdb 21 | cd hello-electron-with-pouchdb 22 | 23 | Then npm install: 24 | 25 | npm install 26 | 27 | And run: 28 | 29 | npm start 30 | 31 | If it doesn't work, you might not have the latest version of Node/npm. Try installing the latest using [nvm](https://github.com/creationix/nvm). 32 | 33 | ## Browser vs Node 34 | 35 | In order to get LevelDB to work propertly, we use [electron-rebuild](https://github.com/electron/electron-rebuild) to rebuild LevelDB for Electron. 36 | 37 | If this step doesn't work for you (e.g. because you are using an older version of Node, you're using Windows, etc.), you can remove the `postinstall` script from `package.json`, replace the `pouchdb` dependency with `pouchdb-browser`, and just use the browser adapters (IndexedDB/WebSQL) rather than the Node.js adapter (LevelDB). 38 | 39 | See [pouchdb-electron](https://github.com/nolanlawson/pouchdb-electron) for more installation instructions. 40 | 41 | ## node-websql (sqlite3) adpater 42 | 43 | If you want to run PouchDB in "Node.js style" but using SQLite, then you should use `pouchdb-adapter-node-websql`, as demonstrated in this project. 44 | 45 | Like LevelDB, this requires reinstalling a native module (in this case, [SQLite3](https://github.com/mapbox/node-sqlite3)). If this fails, you can just remove the `pouchdb-adapter-node-websql` dependency from `package.json` and also the `postinstall` script. 46 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | PouchDB ♥ Electron 5 | 6 | 7 | 8 |

PouchDB ♥ Electron

9 | We are using 10 | 15 | 16 |
17 |
18 |
19 |
20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /main.js: -------------------------------------------------------------------------------- 1 | const electron = require('electron') 2 | // Module to control application life. 3 | const app = electron.app 4 | // Module to create native browser window. 5 | const BrowserWindow = electron.BrowserWindow 6 | 7 | // Keep a global reference of the window object, if you don't, the window will 8 | // be closed automatically when the JavaScript object is garbage collected. 9 | let mainWindow 10 | 11 | function createWindow () { 12 | // Create the browser window. 13 | mainWindow = new BrowserWindow({width: 800, height: 600}) 14 | 15 | // and load the index.html of the app. 16 | mainWindow.loadURL(`file://${__dirname}/index.html`) 17 | 18 | // Open the DevTools. 19 | mainWindow.webContents.openDevTools() 20 | 21 | // Emitted when the window is closed. 22 | mainWindow.on('closed', function () { 23 | // Dereference the window object, usually you would store windows 24 | // in an array if your app supports multi windows, this is the time 25 | // when you should delete the corresponding element. 26 | mainWindow = null 27 | }) 28 | } 29 | 30 | // This method will be called when Electron has finished 31 | // initialization and is ready to create browser windows. 32 | // Some APIs can only be used after this event occurs. 33 | app.on('ready', createWindow) 34 | 35 | // Quit when all windows are closed. 36 | app.on('window-all-closed', function () { 37 | // On OS X it is common for applications and their menu bar 38 | // to stay active until the user quits explicitly with Cmd + Q 39 | if (process.platform !== 'darwin') { 40 | app.quit() 41 | } 42 | }) 43 | 44 | app.on('activate', function () { 45 | // On OS X it's common to re-create a window in the app when the 46 | // dock icon is clicked and there are no other windows open. 47 | if (mainWindow === null) { 48 | createWindow() 49 | } 50 | }) 51 | 52 | // In this file you can include the rest of your app's specific main process 53 | // code. You can also put them in separate files and require them here. 54 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hello-electron-with-pouchdb", 3 | "version": "1.0.0", 4 | "description": "A minimal Electron application, with PouchDB", 5 | "main": "main.js", 6 | "scripts": { 7 | "start": "electron .", 8 | "postinstall": "electron-rebuild" 9 | }, 10 | "devDependencies": { 11 | "electron": "^1.4.12", 12 | "electron-rebuild": "^1.4.0" 13 | }, 14 | "dependencies": { 15 | "pouchdb": "^6.1.0", 16 | "pouchdb-adapter-node-websql": "^6.1.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /screenshot2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nolanlawson/hello-electron-with-pouchdb/58c23356c6affc5c883b2ccc64940213c74b5dfc/screenshot2.png -------------------------------------------------------------------------------- /script.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | 3 | 'use strict'; 4 | 5 | var $ = document.querySelector.bind(document); 6 | 7 | // IndexedDB 8 | 9 | var db = new PouchDB('mydb-idb'); 10 | 11 | db.info().then(function (info) { 12 | $('#idb').innerHTML = '✔ We can use PouchDB with IndexedDB!'; 13 | }).catch(function (err) { 14 | $('#idb').innerHTML = 'Error for IndexedDB'; 15 | }); 16 | 17 | // WebSQL 18 | 19 | var websqlDB = new PouchDB('mydb-websql', {adapter: 'websql'}); 20 | 21 | websqlDB.info().then(function (info) { 22 | $('#websql').innerHTML = '✔ We can use PouchDB with WebSQL!'; 23 | }).catch(function (err) { 24 | $('#websql').innerHTML = 'Error for WebSQL'; 25 | }); 26 | 27 | // LevelDB 28 | 29 | var NodePouchDB = require('pouchdb'); 30 | 31 | var leveldbDB = new NodePouchDB('mydb-leveldb'); 32 | 33 | leveldbDB.info().then(function (info) { 34 | $('#leveldb').innerHTML = '✔ We can use PouchDB with LevelDB!'; 35 | }).catch(function (err) { 36 | $('#leveldb').innerHTML = 'Error for LevelDB'; 37 | }); 38 | 39 | // node-websql 40 | 41 | NodePouchDB.plugin(require('pouchdb-adapter-node-websql')); 42 | var sqliteDB = new NodePouchDB('mydb-sqlite', {adapter: 'websql'}); 43 | 44 | sqliteDB.info().then(function (info) { 45 | $('#sqlitedb').innerHTML = '✔ We can use PouchDB with node-websql (SQLite)!'; 46 | }).catch(function (err) { 47 | $('#sqlitedb').innerHTML = 'Error for node-websql (SQLite)'; 48 | }); 49 | 50 | })(); 51 | --------------------------------------------------------------------------------