Git Product home page Git Product logo

plugin-seed's Introduction

PouchDB Plugin Seed

Build Status

Fork this project to build your first PouchDB plugin. It contains everything you need to test in Node, WebSQL, and IndexedDB. It also includes a Travis config file so you can automatically run the tests in Travis.

Building

npm install
npm run build

Your plugin is now located at dist/pouchdb.mypluginname.js and dist/pouchdb.mypluginname.min.js and is ready for distribution.

Getting Started

First, change the name in package.json to whatever you want to call your plugin. Change the build script so that it writes to the desired filename (e.g. pouchdb.mypluginname.js). Also, change the authors, description, git repo, etc.

Next, modify the index.js to do whatever you want your plugin to do. Right now it just adds a pouch.sayHello() function that says hello:

exports.sayHello = utils.toPromise(function (callback) {
  callback(null, 'hello');
});

Optionally, you can add some tests in tests/test.js. These tests will be run both in the local database and a remote CouchDB, which is expected to be running at localhost:5984 in "Admin party" mode.

The sample test is:

it('should say hello', function () {
  return db.sayHello().then(function (response) {
    response.should.equal('hello');
  });
});

Testing

In Node

This will run the tests in Node using LevelDB:

npm test

You can also check for 100% code coverage using:

npm run coverage

If you don't like the coverage results, change the values from 100 to something else in package.json, or add /*istanbul ignore */ comments.

If you have mocha installed globally you can run single test with:

TEST_DB=local mocha --reporter spec --grep search_phrase

The TEST_DB environment variable specifies the database that PouchDB should use (see package.json).

In the browser

Run npm run dev and then point your favorite browser to http://127.0.0.1:8001/test/index.html.

The query param ?grep=mysearch will search for tests matching mysearch.

Automated browser tests

You can run e.g.

CLIENT=selenium:firefox npm test
CLIENT=selenium:phantomjs npm test

This will run the tests automatically and the process will exit with a 0 or a 1 when it's done. Firefox uses IndexedDB, and PhantomJS uses WebSQL.

What to tell your users

Below is some boilerplate you can use for when you want a real README for your users.

To use this plugin, include it after pouchdb.js in your HTML page:

<script src="pouchdb.js"></script>
<script src="pouchdb.mypluginname.js"></script>

Or to use it in Node.js, just npm install it:

npm install pouchdb-myplugin

And then attach it to the PouchDB object:

var PouchDB = require('pouchdb');
PouchDB.plugin(require('pouchdb-myplugin'));

plugin-seed's People

Contributors

nolanlawson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

plugin-seed's Issues

Best way to have private variables in plugin

I've tried a bunch of different things to try and implement private variables in my plugin that are not shared among different pouch instances. The following works, but it is a little ugly as it requires extra calls; calls to persistInit(). Is there something better?

in index.js

exports.persistInit = function () {
  var pouch = this;
  var y = 7;
  pouch.setY = function (_y_) {
    y = _y_;
  };
  pouch.getY = function () {
    return y;
  };
};

then in test.js

var db1 = new Pouch('test1');
db1.persistInit();
var db2 = new Pouch('test2');
db2.persistInit();
db1.setY(8);
console.log('db1.getY()=' + db1.getY()); // outputs db1.getY()=8
console.log('db2.getY()=' + db2.getY()); // outputs db2.getY()=7

the following doesn't work as the private variable is shared among both instances of pouch:

var y = 7;
exports.persistInit = function() { }; // just so test.js remains the same
exports.setY = function (_y_) {
  y = _y_;
};
exports.getY = function () {
  return y;
};

Attach plugin code to pouch constructor

I have some code that I would like to automatically run when my plugin is instantiated, e.g.

db.on('create', onCreate);

or in other words, I'd like to automatically set an event handler for create

I currently have a function in my plugin that must be explicitly called to set this up, but ideally it would be setup automatically. Is there an easy way to do this?

Windows 10 issue with browserify | ./bin/es3ify.js

I had a error with the pipeline :

browserify index.js | ./bin/es3ify.js | derequire > dist/pouchdb.mypluginname.js
./bin/es3ify.js wasn't a recommanded command. It looks like, it wasn't detected and included as a script in my prompt.
To solve that problem I implemented Gulp which has a es3ify module + a uglify module.
So now, the es3ify pipe and uglify pipe is done in gulp.

The commit : Shocoben@77566c3

Is all the dist code really needed?

I'm working on a new plugin for persistent replication called pouchdb-persist. As this is my second fork of the plugin-seed, I'm looking a little deeper into how things are working. Eventually, my "app" will be using 3 plugins (delta-pouch, filter-pouch and pouchdb-persist).

  1. If you build a cloned copy of plugin-seed and take a look at the un-minified build file in dist, you'll see that there are approximately 1,700 "extra" lines in there. Is all this code really necessary for the plugin? If not, is there a recommended way to remove it? I thought I would ask before I start trying to hack things.
  2. I've made an assumption that multiple plugins can be applied to the same pouch instance. Is this assumption correct?
  3. There appears to be a lot of shared code between different plugins. Is there something like a pouchdb-plugin-commons file that can be included so that the finished "app" doesn't need to include duplicate code?

I'd be happy to help if work needs to be done--just let me know!

How to define additional events

I'm trying to define a new delta-update event for my plugin. I'd then like the user to be able to do something like:

db.on('delta-update', onUpdate);

In my plugin I have code that triggers the event like db.emit('delta-update', someobject) but the event appears to get stopped in pouch as a handler is then undefined.

I can do something like the following in my plugin and it is triggered by the emit(), but I'd like to reuse on() if possible:

db.addListener('delta-change', onUpdate);

Is there a way to reuse on() with my custom event?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.