├── .babelrc
├── .editorconfig
├── .eslintignore
├── .eslintrc
├── .gitattributes
├── .github
└── FUNDING.yml
├── .gitignore
├── .npmrc
├── .prettierrc
├── LICENSE
├── README.md
├── package.json
├── rollup.config.js
├── src
└── index.js
├── tests
├── ExistingTodosDataFunction.vue
├── TodosDataFunctionWithSelector.vue
├── emptyDataFunction.vue
├── emptyDataObject.vue
├── global-mocks.js
├── noDataFunctionOrObject.vue
└── testData.spec.js
└── types
└── index.d.ts
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": ["@babel/preset-env"]
3 | }
4 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | # EditorConfig http://EditorConfig.org
2 |
3 | # Root EditorConfig file
4 | root = true
5 |
6 | # Universal
7 | [*]
8 | end_of_line = lf
9 | charset = utf-8
10 | trim_trailing_whitespace = true
11 | insert_final_newline = true
12 | indent_style = space
13 | indent_size = 4
14 |
15 | [*.txt]
16 | charset = utf-8
17 |
18 | # JSON
19 | [*.json]
20 | end_of_line = lf
21 | indent_style = space
22 | indent_size = 4
23 |
24 | # PHP - PSR-2 standards
25 | [**.php]
26 | indent_style = space
27 | indent_size = 4
28 |
29 | # HTML
30 | [**.html]
31 | indent_style = space
32 | indent_size = 4
33 |
34 | # SVG
35 | [**.svg]
36 | indent_style = space
37 | indent_size = 4
38 |
39 | # JavaScript
40 | [**.js]
41 | indent_style = space
42 | indent_size = 4
43 |
44 | # CSS
45 | [**.css]
46 | indent_style = space
47 | indent_size = 4
48 |
49 | # SASS
50 | [**.scss]
51 | indent_style = space
52 | indent_size = 4
53 |
54 | # YAML
55 | [**.yml]
56 | indent_style = space
57 | indent_size = 2
58 |
--------------------------------------------------------------------------------
/.eslintignore:
--------------------------------------------------------------------------------
1 | lib
2 |
--------------------------------------------------------------------------------
/.eslintrc:
--------------------------------------------------------------------------------
1 | /* Highlighting
2 | 0 = OFF
3 | 1 = WARNING
4 | 2 = ERROR
5 | */
6 |
7 | {
8 | "extends": "eslint:recommended",
9 | "rules": {
10 | "indent": [
11 | 2,
12 | 4,
13 | { "SwitchCase": 1 }
14 | ],
15 | "quotes": [
16 | 2,
17 | "single",
18 | "avoid-escape"
19 | ],
20 | "brace-style": [2, "1tbs"],
21 | "comma-dangle": [2, "always-multiline"],
22 | "consistent-return": 2,
23 | "linebreak-style": [
24 | 2,
25 | "unix"
26 | ],
27 | "semi": [
28 | 2,
29 | "always"
30 | ],
31 | "no-console": 0,
32 | "no-undef": 0,
33 | "no-shadow": 0,
34 | "no-bitwise": 2,
35 | "eol-last": 2,
36 | "dot-notation": 2,
37 | "dot-location": [2, "property"],
38 | "eqeqeq": [2, "allow-null"],
39 | "no-inner-declarations": [2, "functions"],
40 | "no-multi-spaces": 2,
41 | "no-unused-expressions": 2,
42 | "no-unused-vars": 0,
43 | "keyword-spacing": 2,
44 | "space-before-blocks": 2,
45 | "space-before-function-paren": [2, {"anonymous": "never", "named": "never"}],
46 | "strict": [2, "global"]
47 | },
48 | "env": {
49 | "browser": true
50 | },
51 | "parserOptions": {
52 | "sourceType": "module",
53 | "ecmaVersion": 2017
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Automatically normalize line endings for all text-based files
2 | # http://git-scm.com/docs/gitattributes#_end_of_line_conversion
3 | * text=auto
4 |
5 | # For the following file types, normalize line endings to LF on
6 | # checkin and prevent conversion to CRLF when they are checked out
7 | # (this is required in order to prevent newline related issues like,
8 | # for example, after the build script is run)
9 | .* text eol=lf
10 | *.css text eol=lf
11 | *.html text eol=lf
12 | *.js text eol=lf
13 | *.json text eol=lf
14 | *.md text eol=lf
15 | *.sh text eol=lf
16 | *.txt text eol=lf
17 | *.xml text eol=lf
18 |
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | custom: ['https://www.paypal.me/mdslktr']
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | lib
3 | .idea
4 |
--------------------------------------------------------------------------------
/.npmrc:
--------------------------------------------------------------------------------
1 | package-lock=false
2 |
--------------------------------------------------------------------------------
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "singleQuote": true,
3 | "tabWidth": 4,
4 | "trailingComma": "es5"
5 | }
6 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Simon Kunz
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Pouch Vue
2 |
3 | ## This Plugin is not under active development anymore since none of the maintaining members are actively using it.
4 |
5 | ##### Basic structure copied from https://github.com/buhrmi/vue-pouch with a lot of api changes though. TypeScript support included too.
6 |
7 | ## Installation
8 | Make sure to have `pouchdb-browser` (or `pouchdb` depending on what you need) `pouchdb-find` and `pouchdb-live-find` installed
9 | ````sh
10 | npm i pouchdb-browser pouchdb-live-find pouchdb-find
11 | ````
12 |
13 | Install via npm:
14 | ```sh
15 | npm install --save pouch-vue
16 | ```
17 |
18 | The only requirement is that `pouchdb-live-find` is installed:
19 | ```javascript
20 | import PouchDB from 'pouchdb-browser'
21 | import PouchFind from 'pouchdb-find'
22 | import PouchLiveFind from 'pouchdb-live-find'
23 |
24 | PouchDB.plugin(PouchFind)
25 | PouchDB.plugin(PouchLiveFind)
26 | ```
27 |
28 | If you want to use remote databases (CouchDB, Cloudant, etc.), you should also install the authentication plugin:
29 | ```javascript
30 | PouchDB.plugin(require('pouchdb-authentication'));
31 | ```
32 | Then, plug VuePouch into Vue:
33 | ```javascript
34 | import Vue from 'vue';
35 | import PouchVue from 'pouch-vue';
36 |
37 | Vue.use(PouchVue, {
38 | pouch: PouchDB, // optional if `PouchDB` is available on the global object
39 | defaultDB: 'remoteDbName', // this is used as a default connect/disconnect database
40 | optionDB: {}, // this is used to include a custom fetch() method (see TypeScript example)
41 | debug: '*' // optional - See `https://pouchdb.com/api.html#debug_mode` for valid settings (will be a separate Plugin in PouchDB 7.0)
42 | });
43 | ```
44 | ### Known issue with PouchDB v7.0
45 |
46 | PouchDB v7.0 introduced [an issue with fetch using different defaults than XHR for cross-domain requests](https://github.com/pouchdb/pouchdb/issues/7391). The issue was fixed in PouchDB v7.1.1 so that fetch defaults now include 'credentials' just as XHR defaults come with credentials. If you are using PouchDB v7.0 you will get a 401 Unauthorized error. The workaround for PouchDB v7.0 is to override the fetch function in the defaults:
47 |
48 | ```javascript
49 | Vue.use(pouchVue,{
50 | pouch: PouchDB,
51 | defaultDB: 'todos',
52 | optionsDB: {
53 | fetch: function (url:any, opts:any) {
54 | opts.credentials = 'include';
55 | return PouchDB.fetch(url, opts);
56 | }
57 | }
58 | })
59 | ```
60 | ## API
61 | ### $pouch
62 |
63 | `$pouch` is made available on all vue instances and implements most of pouchdbs current API (https://pouchdb.com/api.html).
64 | Default events are mounted on each db you connect to: https://pouchdb.com/api.html#events. When a database is created `pouchdb-db-created` is emitted and `pouchdb-db-destroyed` when it's destroyed (which you can listen to with `this.$on(EVENT_NAME)`).
65 |
66 | #### Methods
67 | All Methods return a promise and mirror or extend the API from pouchdb.
68 |
69 | * `$pouch.getSession(OPTIONAL db)`: Returns the current session if already logged in to the defaultDB or given remote DB.
70 | * `$pouch.connect(username, password, OPTIONAL db)`: Connects you to the defaultDB or given remote DB and returns the user object on success.
71 | * `$pouch.disconnect(OPTIONAL db)`: Disconnects you from the defaultDB or given remote DB and clears the session data.
72 | * `$pouch.createUser(name, password, OPTIONAL db)`: Creates a user in the defaultDB or given remote DB and starts a new session.
73 | * `$pouch.putUser(name, OPTIONAL metadata, OPTIONAL db)`: Update a user in the defaultDB or given remote DB and returns the user object on success. [pouchdb-authentication API : putUser](https://github.com/pouchdb-community/pouchdb-authentication/blob/master/docs/api.md#dbputuserusername-opts--callback)
74 | * `$pouch.deleteUser(name, OPTIONAL db)`: Delete a user in the defaultDB or given remote DB and returns response. [pouchdb-authentication API : deleteUser](https://github.com/pouchdb-community/pouchdb-authentication/blob/master/docs/api.md#dbdeleteuserusername-opts--callback)
75 | * `$pouch.signUpAdmin(adminUsername, adminPassword, OPTIONAL db)`: Sign up a new admin and returns response. [pouchdb-authentication API : signUpAdmin](https://github.com/pouchdb-community/pouchdb-authentication/blob/master/docs/api.md#dbsignupadminusername-password--options--callback)
76 | * `$pouch.deleteAdmin(name, OPTIONAL db)`:Delete an admin and returns response. [pouchdb-authentication API : deleteAdmin](https://github.com/pouchdb-community/pouchdb-authentication/blob/master/docs/api.md#dbdeleteadminusername-opts--callback)
77 | ___
78 | * `$pouch.destroy(OPTIONAL db)`: same as https://pouchdb.com/api.html#delete_database
79 | * `$pouch.defaults(options)`: same as https://pouchdb.com/api.html#defaults
80 | ___
81 | * `$pouch.sync(localDatabase, OPTIONAL remoteDatabase, OPTIONAL options)`: The optional remoteDatabase parameter will use the default db set in the pouch options initially. Basically the same as PouchDB.sync(local, remote, {live: true, retry: true}). Also, if the browser has an active session cookie, it will fetch session data (username, etc) from the remote server. **BONUS:** If your remote database runs CouchDB 2.0 or higher, you can also specify a Mango Selector that is used to filter documents coming from the remote server. Callback functions will be invoked with the name `pouchdb-[method]-[type]`. So in this case you can use `this.$on('pouchdb-sync-change', callback(data))` to listen when a change occurs. See https://pouchdb.com/api.html#sync for a full list of events you can use.
82 |
83 | **default options (will be merged with the options passed in)**:
84 | ```javascript
85 | {
86 | live: true,
87 | retry: true,
88 | back_off_function: (delay) => {
89 | if (delay === 0) {
90 | return 1000;
91 | }
92 | return delay * 3;
93 | },
94 | }
95 | ```
96 | **For example:**
97 | ```javascript
98 | $pouch.sync('complaints', 'https:/42.233.1.44/complaints', {
99 | selector: {
100 | type: 'complaint',
101 | assignee: this.session.name
102 | }
103 | });
104 |
105 | ```
106 | * `$pouch.push(localDatabase, OPTIONAL remoteDatabase, OPTIONAL options)`: The optional remoteDatabase parameter will use the default db set in the pouch options initially. Like https://pouchdb.com/api.html#replication - replicate-to. Also, if the browser has an active session cookie, it will fetch session data (username, etc) from the remote server.
107 | * `$pouch.pull(localDatabase, OPTIONAL remoteDatabase, OPTIONAL options)`: The optional remoteDatabase parameter will use the default db set in the pouch options initially. Like https://pouchdb.com/api.html#replication - replicate-from. Also, if the browser has an active session cookie, it will fetch session data (username, etc) from the remote server.
108 | * `$pouch.changes(OPTIONAL options, OPTIONAL db)`: Listens for change on a db like: https://pouchdb.com/api.html#changes
109 | * `$pouch.put(object, OPTIONAL options, OPTIONAL db)`: https://pouchdb.com/api.html#create_document
110 | * `$pouch.post(object, OPTIONAL options, OPTIONAL db)`: https://pouchdb.com/api.html#create_document
111 | * `$pouch.remove(id, rev, OPTIONAL db)`: https://pouchdb.com/api.html#delete_document
112 | * `$pouch.get(object, OPTIONAL options, OPTIONAL db)`: https://pouchdb.com/api.html#create_document
113 | * `$pouch.query('map/reduce function', OPTIONAL options, OPTIONAL db)`: like https://pouchdb.com/api.html#query_database
114 | * `$pouch.allDocs(OPTIONAL options, OPTIONAL db)`: like https://pouchdb.com/api.html#batch_fetch but `include_docs` is set to true by default. You can however overwrite it of course.
115 | * `$pouch.bulkDocs(docs, OPTIONAL options, OPTIONAL db)`: https://pouchdb.com/api.html#batch_create
116 | * `$pouch.compact(OPTIONAL options, OPTIONAL db)`: https://pouchdb.com/api.html#compaction
117 | * `$pouch.viewCleanup(OPTIONAL db)`: https://pouchdb.com/api.html#view_cleanup
118 | * `$pouch.info(OPTIONAL db)`: like https://pouchdb.com/api.html#database_information
119 | * `$pouch.find(request, OPTIONAL db)`: like https://pouchdb.com/api.html#query_index
120 | * `$pouch.createIndex(index, OPTIONAL db)`: like https://pouchdb.com/api.html#create_index
121 | * `$pouch.putAttachment(docId, [rev], attachmentObject(id,data,type), OPTIONAL db)`: like https://pouchdb.com/api.html#save_attachment
122 | * `$pouch.getAttachment(docId, attachmentId, OPTIONAL db)`: like https://pouchdb.com/api.html#get_attachment
123 | * `$pouch.deleteAttachment(docId, attachmentId, docRev, OPTIONAL db)`: like https://pouchdb.com/api.html#delete_attachment
124 | * `$pouch.close(OPTIONAL db)`: https://pouchdb.com/api.html#close_database
125 |
126 | #### Non-Reactive Properties
127 | * `vm.$databases`: the pouchdb instances which are shared across all components.
128 |
129 | ## Examples
130 |
131 | ```vue
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
156 | ```
157 |
158 | ### Reactive & Live Selectors (Mango Queries)
159 |
160 | ```vue
161 |
162 | Show people that are years old.
163 |
164 | {{ person.name }}
165 |
166 |
167 |
168 |
197 | ```
198 |
199 | ### Single documents
200 |
201 | If you only want to sync a single document that matches a selector, use `first: true`:
202 |
203 | ```javascript
204 | module.exports = {
205 | // ...
206 | pouch: {
207 | projectDetails() {
208 | return {
209 | database: 'mydatabase',
210 | selector: {_id: this.selectedProjectId},
211 | first: true
212 | }
213 | }
214 | }
215 | // ...
216 | }
217 | ```
218 |
219 | ### TypeScript
220 | TypeScript example with a TypeScript file to include the pouch-vue plugin and a Single File Component
221 | using the plugin.
222 |
223 | main.ts
224 | ```typescript
225 |
226 | import { Component, Vue } from 'vue-property-decorator';
227 | import PouchDB from 'pouchdb-browser';
228 | import lf from 'pouchdb-find';
229 | import plf from 'pouchdb-live-find';
230 | import auth from 'pouchdb-authentication';
231 |
232 | import pouchVue from 'pouch-vue';
233 |
234 | // PouchDB plugins: pouchdb-find (included in the monorepo) and LiveFind (external plugin)
235 | PouchDB.plugin(lf);
236 | PouchDB.plugin(plf);
237 | PouchDB.plugin(auth);
238 |
239 | Vue.use(pouchVue,{
240 | pouch: PouchDB,
241 | defaultDB: 'todos',
242 | optionsDB: {
243 | fetch: function (url:any, opts:any) {
244 | opts.credentials = 'include';
245 | return PouchDB.fetch(url, opts);
246 | }
247 | }
248 | })
249 |
250 | new Vue({});
251 |
252 | ```
253 | Todos.vue
254 | ```vue
255 |
256 |