forked from PolymerElements/app-pouchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-pouchdb-document.html
205 lines (177 loc) · 5.6 KB
/
app-pouchdb-document.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!--
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../app-storage/app-storage-behavior.html">
<link rel="import" href="app-pouchdb-database-behavior.html">
<link rel="import" href="pouchdb.html">
<!--
`app-pouchdb-document` is an implementation of `Polymer.AppStorageBehavior`
for reading and writing to individual PouchDB documents.
In order to refer to a PouchDB document, provide the name of the database
(both local and remote databases are supported) and the ID of the document.
For example:
```html
<app-pouchdb-document
db-name="cats"
doc-id="parsnip">
</app-pouchdb-document>
```
In the above example, a PouchDB instance will be created to connect to the
local database named "cats". Then it will check to see if a document with the
ID "parsnip" exists. If it does, the `data` property of the document will be
set to the value of the document. If it does not, then any subsequent
assignments to the `data` property will cause a document with ID "parsnip" to
be created.
Here is an example of a simple form that can be used to read and write to a
PouchDB document:
```html
<app-pouchdb-document
db-name="cats"
doc-id="parsnip"
data="{{cat}}">
</app-pouchdb-document>
<input
is="iron-input"
bind-value="{{cat.name}}">
</input>
```
-->
<script>
(function() {
'use strict';
Polymer({
is: 'app-pouchdb-document',
behaviors: [
Polymer.AppStorageBehavior,
Polymer.AppPouchDBDatabaseBehavior
],
properties: {
/**
* The value of the _id (Pouch/Couch unique identifier) of the PouchDB
* document that this element's data should refer to.
*/
docId: {
type: String,
value: null
},
/**
* The current _rev (revision) of the PouchDB document that this
* element's data refers to, if the document is not new.
*/
rev: {
type: String,
readOnly: true,
value: null
},
/**
* A changes event emitter. Notifies of changes to the PouchDB document
* referred to by `docId`, if a `docId` has been provided.
*/
changes: {
type: Object,
computed: '__computeChanges(db, docId)'
}
},
observers: [
'__docRevChanged(data._rev)',
'__docChanged(db, data, docId, _readied)'
],
/** @override */
get isNew() {
return this.docId == null;
},
/** @override */
get zeroValue() {
return {};
},
/** @override */
save: function() {
if (!this.db) {
return Promise.reject('No PouchDB instance available!');
}
if (this.docId) {
this.data._id = this.docId;
}
return this._upsert('data', this.data, this.data)
.then(function(response) {
this.syncToMemory(function() {
this.docId = response.id;
this.set('data._id', response.id);
this.set('data._rev', response.rev);
});
}.bind(this));
},
/** @override */
reset: function() {
this.docId = null;
this.data = this.zeroValue;
},
/** @override */
destroy: function() {
this.set('data._deleted', true);
return this.transactionsComplete.then(function() {
return this.reset();
});
},
/** @override */
getStoredValue: function(storagePath) {
return this.db.get(this.docId).then(function(doc) {
return this.get(storagePath, {
data: doc
});
}.bind(this)).catch(function(error) {
if (error && error.status === 404) {
return;
} else {
throw error;
}
});
},
/** @override */
setStoredValue: function(storagePath, value) {
if (storagePath === 'data' && value == null) {
return Promise.reject(
['Unsupported attempt to unset PouchDB document by assigning null value.',
'Perhaps you meant to set `data._deleted = true`?'].join(' '));
}
return this._put(storagePath, value, this.data);
},
__docChanged: function(db, doc, docId) {
this._log('Doc / ID changed', doc, docId);
if (db && doc != null && docId != null && doc._id != docId) {
doc._id = docId;
this._initializeStoredValue();
}
},
__docRevChanged: function() {
this._setRev(this.data != null ? this.data._rev : null);
},
__computeChanges: function(db, docId) {
if (this.changes) {
this.changes.removeAllListeners();
}
if (db == null || docId == null) {
return null;
}
return db.changes({
since: 'now',
live: true,
doc_ids: [docId],
include_docs: true,
}).on('change', function(change) {
var doc = change.doc;
this.syncToMemory(function() {
this.set('data', doc);
});
}.bind(this));
}
});
})();
</script>