Skip to content

Commit

Permalink
Merge pull request #265 from jlami/indexeddb
Browse files Browse the repository at this point in the history
Use pouchdb indexeddb adapter instead of the old idb adapter to use native views.
  • Loading branch information
broerse authored Dec 5, 2022
2 parents fd7ea36 + 872f36d commit 0b7ebc6
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 41 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Install Nod
uses: actions/setup-node@v3
with:
node-version: 12.x
node-version: 14.x
# cache: npm
- name: Install dependencies
run: |
Expand All @@ -47,7 +47,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 12.x
node-version: 14.x
# cache: npm
- name: Install Dependencies
run: npm install --no-shrinkwrap
Expand All @@ -66,6 +66,7 @@ jobs:
- ember-lts-3.16
- ember-lts-3.24
- ember-lts-3.28
- ember-lts-4.4
- ember-release
- ember-beta
- ember-canary
Expand All @@ -78,7 +79,7 @@ jobs:
- name: Install Node
uses: actions/setup-node@v3
with:
node-version: 12.x
node-version: 14.x
# cache: npm
- name: Install dependencies
run: |
Expand All @@ -90,4 +91,4 @@ jobs:
npm i
fi
- name: Run Tests
run: ./node_modules/.bin/ember try:one ${{ matrix.try-scenario }}
run: ./node_modules/.bin/ember try:one ${{ matrix.try-scenario }}
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Ember Pouch [![Build Status](https://travis-ci.org/pouchdb-community/ember-pouch.svg)](https://travis-ci.org/pouchdb-community/ember-pouch) [![GitHub version](https://badge.fury.io/gh/pouchdb-community%2Fember-pouch.svg)](https://badge.fury.io/gh/pouchdb-community%2Fember-pouch) [![Ember Observer Score](https://emberobserver.com/badges/ember-pouch.svg)](https://emberobserver.com/addons/ember-pouch)

[**Changelog**](#changelog)
- [**Changelog**](#changelog)
- [**Upgrading**](#upgrading)

Ember Pouch is a PouchDB/CouchDB adapter for Ember Data 3.16+. For older Ember Data versions down to 2.0+ use Ember Pouch version 7.0 For Ember Data versions lower than 2.0+ use Ember Pouch version 3.2.2.

Expand Down Expand Up @@ -36,7 +37,6 @@ npm install ember-pouch@3.2.2 --save-dev

This provides

- `import PouchDB from 'ember-pouch/pouchdb';`
- `import {Model, Adapter, Serializer} from 'ember-pouch'`

`Ember-Pouch` requires you to add a `@attr('string') rev` field to all your models. This is for PouchDB/CouchDB to handle revisions:
Expand Down Expand Up @@ -67,6 +67,9 @@ export default class TodoModel extends Model {
}
```

The installation creates a file `adapters/application.js` that you can use by default to setup the database connection. Look at the [Adapter blueprint](#adapter) section to see the settings that you have to set in your config file to work with this adapter.
It also installs the required packages.

## Configuring /app/adapters/application.js

A local PouchDB that syncs with a remote CouchDB looks like this:
Expand Down Expand Up @@ -119,10 +122,10 @@ Replace `<model-name>` with the name of your model and the file will automatical

### Adapter

You can now create an adapter using ember-cli's blueprint functionality. Once you've installed `ember-pouch` into your ember-cli app you can run the following command to automatically generate an application adapter.
You can now create an adapter using ember-cli's blueprint functionality. Once you've installed `ember-pouch` into your ember-cli app you can run the following command to automatically generate an adapter.

```
ember g pouch-adapter application
ember g pouch-adapter foo
```

Now you can store your localDb and remoteDb names in your ember-cli's config. Just add the following keys to the `ENV` object:
Expand All @@ -132,6 +135,10 @@ ENV.emberPouch.localDb = 'test';
ENV.emberPouch.remoteDb = 'http://localhost:5984/my_couch';
```

This blueprint is run on installation for the `application` adapter.

You can use multiple adapters, but be warned that doing the `.plugin` calls in multiple adapter files will result in errors: `TypeError: Cannot redefine property: replicate`. In this case it is better to move the `PouchDB.plugin` calls to a separate file.

## Relationships

EmberPouch supports both `hasMany` and `belongsTo` relationships.
Expand Down Expand Up @@ -544,6 +551,18 @@ Following the CouchDB consistency model, we have introduced `ENV.emberPouch.even
`findRecord` now returns a long running Promise if the record is not found. It only rejects the promise if a deletion of the record is found. Otherwise this promise will wait for eternity to resolve.
This makes sure that belongsTo relations that have been loaded in an unexpected order will still resolve correctly. This makes sure that ember-data does not set the belongsTo to null if the Pouch replicate would have loaded the related object later on. (This only works for async belongsTo, sync versions will need this to be implemented in relational-pouch)

## Upgrading

### Version 8

Version 8 introduces the custom PouchDB setup in the adapter instead of having a default setup in `addon/pouchdb.js`. So if you used `import PouchDB from 'ember-pouch/pouchdb'` in your files, you now have to make your own 'PouchDB bundle' in the same way we do it in the default adapter blueprint. The simplest way to do this is to run the blueprint by doing `ember g ember-pouch` (which will overwrite your application adapter, so make sure to commit that file first) and take the `import PouchDB from...` until the final `.plugin(...)` line and put that into your original adapter (or a separate file if you use more than one adapter).
You can also copy the lines from [the blueprint file in git](https://github.com/pouchdb-community/ember-pouch/blob/master/blueprints/pouch-adapter/files/__root__/adapters/__name__.js)

We also removed the pouchdb-browser package and relational-pouch as a package.json dependency, so you will have to install the packages since the lines above depend upon.
`npm install pouchdb-core pouchdb-adapter-indexeddb pouchdb-adapter-http pouchdb-mapreduce pouchdb-replication pouchdb-find relational-pouch --save-dev`

This way you can now decide for yourself which PouchDB plugins you want to use. You can even remove the http or indexeddb ones if you just want to work offline or online.

## Installation

- `git clone` this repository
Expand Down Expand Up @@ -572,7 +591,11 @@ This project was originally based on the [ember-data-hal-adapter](https://github
And of course thanks to all our wonderful contributors, [here](https://github.com/pouchdb-community/ember-pouch/graphs/contributors) and [in Relational Pouch](https://github.com/pouchdb-community/relational-pouch/graphs/contributors)!

## Changelog

- **8.0.0-beta.2**
- Set PouchDb indexeddb adapter as default instead of idb adapter to use native views
- Generate adapters/application.js at installation
- move package.json dependencies to default blueprint
- removed Adapter `fixDeleteBug` flag. We now do a client side `destroyRecord` with custom `adapterOptions` to signal to the ember-data store that the record is deleted. So no more hacking ember-data is needed in the addon to support the server pushed deletes.
- **8.0.0-beta.1**
- Updated to support latest Ember 4.x (fixed isDeleted issues)
- Switch to PouchDB 7.3.0. Getting ready to use the indexeddb-adapter
Expand Down
20 changes: 7 additions & 13 deletions addon/adapters/pouch.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
//});

export default class PouchAdapter extends RESTAdapter.extend({
fixDeleteBug: true,
coalesceFindRequests: false,

// The change listener ensures that individual records are kept up to date
Expand Down Expand Up @@ -134,19 +133,12 @@ export default class PouchAdapter extends RESTAdapter.extend({
}

if (change.deleted) {
if (this.fixDeleteBug) {
if (
recordInStore._internalModel._recordData &&
recordInStore._internalModel._recordData.setIsDeleted
) {
recordInStore._internalModel._recordData.setIsDeleted(true);
}
recordInStore._internalModel.transitionTo('deleted.saved'); //work around ember-data bug
} else {
store.unloadRecord(recordInStore);
}
if (!recordInStore.isSaving && !recordInStore.isDeleted)
return recordInStore.destroyRecord({
adapterOptions: { serverPush: true },
});
} else {
recordInStore.reload();
return recordInStore.reload();
}
},

Expand Down Expand Up @@ -561,6 +553,8 @@ export default class PouchAdapter extends RESTAdapter.extend({
},

deleteRecord: async function (store, type, record) {
if (record.adapterOptions && record.adapterOptions.serverPush) return;

await this._init(store, type);
var data = this._recordToData(store, type, record);
return this.db.rel
Expand Down
7 changes: 0 additions & 7 deletions addon/pouchdb.js

This file was deleted.

23 changes: 23 additions & 0 deletions blueprints/ember-pouch/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const path = require('path');

module.exports = {
normalizeEntityName() {
return 'application';
},

filesPath() {
return path.join(this.path, '../pouch-adapter/files');
},

afterInstall(/*options*/) {
this.addPackagesToProject([
{ name: 'pouchdb-core' },
{ name: 'pouchdb-adapter-indexeddb' },
{ name: 'pouchdb-adapter-http' },
{ name: 'pouchdb-mapreduce' },
{ name: 'pouchdb-replication' },
{ name: 'pouchdb-find' },
{ name: 'relational-pouch' },
]);
},
};
16 changes: 15 additions & 1 deletion blueprints/pouch-adapter/files/__root__/adapters/__name__.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import config from '<%= dasherizedPackageName %>/config/environment';
import PouchDB from 'ember-pouch/pouchdb';
import { Adapter } from 'ember-pouch';
import { assert } from '@ember/debug';
import { isEmpty } from '@ember/utils';

import PouchDB from 'pouchdb-core';
import PouchDBFind from 'pouchdb-find';
import PouchDBRelational from 'relational-pouch';
import indexeddb from 'pouchdb-adapter-indexeddb';
import HttpPouch from 'pouchdb-adapter-http';
import mapreduce from 'pouchdb-mapreduce';
import replication from 'pouchdb-replication';

PouchDB.plugin(PouchDBFind)
.plugin(PouchDBRelational)
.plugin(indexeddb)
.plugin(HttpPouch)
.plugin(mapreduce)
.plugin(replication);

export default class ApplicationAdapter extends Adapter {

constructor() {
Expand Down
11 changes: 10 additions & 1 deletion config/ember-try.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { embroiderSafe, embroiderOptimized } = require('@embroider/test-setup');

module.exports = async function () {
return {
useYarn: true,
useYarn: false,
scenarios: [
{
name: 'ember-lts-3.16',
Expand Down Expand Up @@ -34,6 +34,15 @@ module.exports = async function () {
},
},
},
{
name: 'ember-lts-4.4',
npm: {
devDependencies: {
'ember-source': '~4.4.0',
'ember-data': '~4.4.0',
},
},
},
{
name: 'ember-release',
npm: {
Expand Down
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ember-pouch",
"version": "8.0.0-beta.1",
"version": "8.0.0-beta.2",
"description": "PouchDB adapter for Ember Data",
"keywords": [
"ember-addon",
Expand Down Expand Up @@ -40,10 +40,7 @@
},
"dependencies": {
"ember-auto-import": "~2.4.2",
"ember-cli-babel": "^7.26.11",
"pouchdb-browser": "^7.3.0",
"pouchdb-find": "^7.3.0",
"relational-pouch": "^4.0.0"
"ember-cli-babel": "^7.26.11"
},
"devDependencies": {
"@ember/optional-features": "^2.0.0",
Expand All @@ -53,7 +50,6 @@
"@glimmer/tracking": "^1.0.4",
"babel-eslint": "^10.1.0",
"broccoli-asset-rev": "^3.0.0",
"ember-auto-import": "~2.4.2",
"ember-cli": "~4.3.0",
"ember-cli-app-version": "~5.0.0",
"ember-cli-dependency-checker": "^3.2.0",
Expand Down Expand Up @@ -83,6 +79,13 @@
"license-checker": "^25.0.1",
"loader.js": "^4.7.0",
"npm-run-all": "^4.1.5",
"pouchdb-core": "^7.3.1",
"pouchdb-adapter-indexeddb": "^7.3.1",
"pouchdb-adapter-http": "^7.3.1",
"pouchdb-mapreduce": "^7.3.1",
"pouchdb-replication": "^7.3.1",
"pouchdb-find": "^7.3.1",
"relational-pouch": "^4.0.1",
"prettier": "^2.6.1",
"qunit": "^2.18.0",
"qunit-dom": "^2.0.0",
Expand Down
6 changes: 3 additions & 3 deletions tests/dummy/app/adapters/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defer } from 'rsvp';
import { assert } from '@ember/debug';
import { isEmpty } from '@ember/utils';
import { Adapter } from 'ember-pouch';
import PouchDB from 'ember-pouch/pouchdb';
import PouchDB from 'dummy/pouchdb';
import config from 'dummy/config/environment';

function createDb() {
Expand Down Expand Up @@ -43,9 +43,9 @@ export default class ApplicationAdapter extends Adapter {
}

onChangeListenerTest = null;
onChange() {
async onChange() {
if (super.onChange) {
super.onChange(...arguments);
await super.onChange(...arguments);
}
if (this.onChangeListenerTest) {
this.onChangeListenerTest(...arguments);
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/app/adapters/taco-salad.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { run } from '@ember/runloop';
import { assert } from '@ember/debug';
import { isEmpty } from '@ember/utils';
import { Adapter } from 'ember-pouch';
import PouchDB from 'ember-pouch/pouchdb';
import PouchDB from 'dummy/pouchdb';
import config from 'dummy/config/environment';

function createDb() {
Expand Down
16 changes: 16 additions & 0 deletions tests/dummy/app/pouchdb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import PouchDB from 'pouchdb-core';
import PouchDBFind from 'pouchdb-find';
import PouchDBRelational from 'relational-pouch';
import indexeddb from 'pouchdb-adapter-indexeddb';
import HttpPouch from 'pouchdb-adapter-http';
import mapreduce from 'pouchdb-mapreduce';
import replication from 'pouchdb-replication';

PouchDB.plugin(PouchDBFind)
.plugin(PouchDBRelational)
.plugin(indexeddb)
.plugin(HttpPouch)
.plugin(mapreduce)
.plugin(replication);

export default PouchDB;

0 comments on commit 0b7ebc6

Please sign in to comment.