Skip to content

Commit 0b7ebc6

Browse files
authored
Merge pull request #265 from jlami/indexeddb
Use pouchdb indexeddb adapter instead of the old idb adapter to use native views.
2 parents fd7ea36 + 872f36d commit 0b7ebc6

File tree

11 files changed

+117
-41
lines changed

11 files changed

+117
-41
lines changed

.github/workflows/node.js.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- name: Install Nod
2424
uses: actions/setup-node@v3
2525
with:
26-
node-version: 12.x
26+
node-version: 14.x
2727
# cache: npm
2828
- name: Install dependencies
2929
run: |
@@ -47,7 +47,7 @@ jobs:
4747
- uses: actions/checkout@v3
4848
- uses: actions/setup-node@v3
4949
with:
50-
node-version: 12.x
50+
node-version: 14.x
5151
# cache: npm
5252
- name: Install Dependencies
5353
run: npm install --no-shrinkwrap
@@ -66,6 +66,7 @@ jobs:
6666
- ember-lts-3.16
6767
- ember-lts-3.24
6868
- ember-lts-3.28
69+
- ember-lts-4.4
6970
- ember-release
7071
- ember-beta
7172
- ember-canary
@@ -78,7 +79,7 @@ jobs:
7879
- name: Install Node
7980
uses: actions/setup-node@v3
8081
with:
81-
node-version: 12.x
82+
node-version: 14.x
8283
# cache: npm
8384
- name: Install dependencies
8485
run: |
@@ -90,4 +91,4 @@ jobs:
9091
npm i
9192
fi
9293
- name: Run Tests
93-
run: ./node_modules/.bin/ember try:one ${{ matrix.try-scenario }}
94+
run: ./node_modules/.bin/ember try:one ${{ matrix.try-scenario }}

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 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)
22

3-
[**Changelog**](#changelog)
3+
- [**Changelog**](#changelog)
4+
- [**Upgrading**](#upgrading)
45

56
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.
67

@@ -36,7 +37,6 @@ npm install [email protected] --save-dev
3637

3738
This provides
3839

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

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

70+
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.
71+
It also installs the required packages.
72+
7073
## Configuring /app/adapters/application.js
7174

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

120123
### Adapter
121124

122-
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.
125+
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.
123126

124127
```
125-
ember g pouch-adapter application
128+
ember g pouch-adapter foo
126129
```
127130

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

138+
This blueprint is run on installation for the `application` adapter.
139+
140+
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.
141+
135142
## Relationships
136143

137144
EmberPouch supports both `hasMany` and `belongsTo` relationships.
@@ -544,6 +551,18 @@ Following the CouchDB consistency model, we have introduced `ENV.emberPouch.even
544551
`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.
545552
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)
546553

554+
## Upgrading
555+
556+
### Version 8
557+
558+
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).
559+
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)
560+
561+
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.
562+
`npm install pouchdb-core pouchdb-adapter-indexeddb pouchdb-adapter-http pouchdb-mapreduce pouchdb-replication pouchdb-find relational-pouch --save-dev`
563+
564+
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.
565+
547566
## Installation
548567

549568
- `git clone` this repository
@@ -572,7 +591,11 @@ This project was originally based on the [ember-data-hal-adapter](https://github
572591
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)!
573592

574593
## Changelog
575-
594+
- **8.0.0-beta.2**
595+
- Set PouchDb indexeddb adapter as default instead of idb adapter to use native views
596+
- Generate adapters/application.js at installation
597+
- move package.json dependencies to default blueprint
598+
- 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.
576599
- **8.0.0-beta.1**
577600
- Updated to support latest Ember 4.x (fixed isDeleted issues)
578601
- Switch to PouchDB 7.3.0. Getting ready to use the indexeddb-adapter

addon/adapters/pouch.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
//});
2626

2727
export default class PouchAdapter extends RESTAdapter.extend({
28-
fixDeleteBug: true,
2928
coalesceFindRequests: false,
3029

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

136135
if (change.deleted) {
137-
if (this.fixDeleteBug) {
138-
if (
139-
recordInStore._internalModel._recordData &&
140-
recordInStore._internalModel._recordData.setIsDeleted
141-
) {
142-
recordInStore._internalModel._recordData.setIsDeleted(true);
143-
}
144-
recordInStore._internalModel.transitionTo('deleted.saved'); //work around ember-data bug
145-
} else {
146-
store.unloadRecord(recordInStore);
147-
}
136+
if (!recordInStore.isSaving && !recordInStore.isDeleted)
137+
return recordInStore.destroyRecord({
138+
adapterOptions: { serverPush: true },
139+
});
148140
} else {
149-
recordInStore.reload();
141+
return recordInStore.reload();
150142
}
151143
},
152144

@@ -561,6 +553,8 @@ export default class PouchAdapter extends RESTAdapter.extend({
561553
},
562554

563555
deleteRecord: async function (store, type, record) {
556+
if (record.adapterOptions && record.adapterOptions.serverPush) return;
557+
564558
await this._init(store, type);
565559
var data = this._recordToData(store, type, record);
566560
return this.db.rel

addon/pouchdb.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

blueprints/ember-pouch/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
normalizeEntityName() {
5+
return 'application';
6+
},
7+
8+
filesPath() {
9+
return path.join(this.path, '../pouch-adapter/files');
10+
},
11+
12+
afterInstall(/*options*/) {
13+
this.addPackagesToProject([
14+
{ name: 'pouchdb-core' },
15+
{ name: 'pouchdb-adapter-indexeddb' },
16+
{ name: 'pouchdb-adapter-http' },
17+
{ name: 'pouchdb-mapreduce' },
18+
{ name: 'pouchdb-replication' },
19+
{ name: 'pouchdb-find' },
20+
{ name: 'relational-pouch' },
21+
]);
22+
},
23+
};

blueprints/pouch-adapter/files/__root__/adapters/__name__.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
import config from '<%= dasherizedPackageName %>/config/environment';
2-
import PouchDB from 'ember-pouch/pouchdb';
32
import { Adapter } from 'ember-pouch';
43
import { assert } from '@ember/debug';
54
import { isEmpty } from '@ember/utils';
65

6+
import PouchDB from 'pouchdb-core';
7+
import PouchDBFind from 'pouchdb-find';
8+
import PouchDBRelational from 'relational-pouch';
9+
import indexeddb from 'pouchdb-adapter-indexeddb';
10+
import HttpPouch from 'pouchdb-adapter-http';
11+
import mapreduce from 'pouchdb-mapreduce';
12+
import replication from 'pouchdb-replication';
13+
14+
PouchDB.plugin(PouchDBFind)
15+
.plugin(PouchDBRelational)
16+
.plugin(indexeddb)
17+
.plugin(HttpPouch)
18+
.plugin(mapreduce)
19+
.plugin(replication);
20+
721
export default class ApplicationAdapter extends Adapter {
822

923
constructor() {

config/ember-try.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { embroiderSafe, embroiderOptimized } = require('@embroider/test-setup');
55

66
module.exports = async function () {
77
return {
8-
useYarn: true,
8+
useYarn: false,
99
scenarios: [
1010
{
1111
name: 'ember-lts-3.16',
@@ -34,6 +34,15 @@ module.exports = async function () {
3434
},
3535
},
3636
},
37+
{
38+
name: 'ember-lts-4.4',
39+
npm: {
40+
devDependencies: {
41+
'ember-source': '~4.4.0',
42+
'ember-data': '~4.4.0',
43+
},
44+
},
45+
},
3746
{
3847
name: 'ember-release',
3948
npm: {

package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ember-pouch",
3-
"version": "8.0.0-beta.1",
3+
"version": "8.0.0-beta.2",
44
"description": "PouchDB adapter for Ember Data",
55
"keywords": [
66
"ember-addon",
@@ -40,10 +40,7 @@
4040
},
4141
"dependencies": {
4242
"ember-auto-import": "~2.4.2",
43-
"ember-cli-babel": "^7.26.11",
44-
"pouchdb-browser": "^7.3.0",
45-
"pouchdb-find": "^7.3.0",
46-
"relational-pouch": "^4.0.0"
43+
"ember-cli-babel": "^7.26.11"
4744
},
4845
"devDependencies": {
4946
"@ember/optional-features": "^2.0.0",
@@ -53,7 +50,6 @@
5350
"@glimmer/tracking": "^1.0.4",
5451
"babel-eslint": "^10.1.0",
5552
"broccoli-asset-rev": "^3.0.0",
56-
"ember-auto-import": "~2.4.2",
5753
"ember-cli": "~4.3.0",
5854
"ember-cli-app-version": "~5.0.0",
5955
"ember-cli-dependency-checker": "^3.2.0",
@@ -83,6 +79,13 @@
8379
"license-checker": "^25.0.1",
8480
"loader.js": "^4.7.0",
8581
"npm-run-all": "^4.1.5",
82+
"pouchdb-core": "^7.3.1",
83+
"pouchdb-adapter-indexeddb": "^7.3.1",
84+
"pouchdb-adapter-http": "^7.3.1",
85+
"pouchdb-mapreduce": "^7.3.1",
86+
"pouchdb-replication": "^7.3.1",
87+
"pouchdb-find": "^7.3.1",
88+
"relational-pouch": "^4.0.1",
8689
"prettier": "^2.6.1",
8790
"qunit": "^2.18.0",
8891
"qunit-dom": "^2.0.0",

tests/dummy/app/adapters/application.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { defer } from 'rsvp';
22
import { assert } from '@ember/debug';
33
import { isEmpty } from '@ember/utils';
44
import { Adapter } from 'ember-pouch';
5-
import PouchDB from 'ember-pouch/pouchdb';
5+
import PouchDB from 'dummy/pouchdb';
66
import config from 'dummy/config/environment';
77

88
function createDb() {
@@ -43,9 +43,9 @@ export default class ApplicationAdapter extends Adapter {
4343
}
4444

4545
onChangeListenerTest = null;
46-
onChange() {
46+
async onChange() {
4747
if (super.onChange) {
48-
super.onChange(...arguments);
48+
await super.onChange(...arguments);
4949
}
5050
if (this.onChangeListenerTest) {
5151
this.onChangeListenerTest(...arguments);

tests/dummy/app/adapters/taco-salad.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { run } from '@ember/runloop';
22
import { assert } from '@ember/debug';
33
import { isEmpty } from '@ember/utils';
44
import { Adapter } from 'ember-pouch';
5-
import PouchDB from 'ember-pouch/pouchdb';
5+
import PouchDB from 'dummy/pouchdb';
66
import config from 'dummy/config/environment';
77

88
function createDb() {

0 commit comments

Comments
 (0)