-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Override PouchDB.allDocs (issue #175) #176
Open
funblaster22
wants to merge
10
commits into
redgeoff:master
Choose a base branch
from
funblaster22:bulkGet
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Here is some code to test the changes. While waiting for a fix to pouch-db to allow overriding methods, I used pouchdb version 7 <script lang="ts">
import PouchDB from 'pouchdb-browser';
import DeltaPouch from 'delta-pouch';
import PouchFind from "pouchdb-find";
import PouchFindLive from "pouchdb-live-find";
import type {Readable, Subscriber, Unsubscriber} from "svelte/store";
PouchDB.plugin(DeltaPouch);
PouchDB.plugin(PouchFind);
PouchDB.plugin(PouchFindLive);
const upstreamLiveFind = PouchDB.prototype.liveFind;
PouchDB.plugin(PouchDB => {
PouchDB.prototype.liveFindStore = function(requestDef: Parameters<typeof upstreamLiveFind>[0]): Readable<{}> {
console.log(this);
const liveFeed = upstreamLiveFind.call(this, requestDef);
return {
subscribe(run: Subscriber<{}>, invalidate: Invalidator<{}> | undefined): Unsubscriber {
run([]);
liveFeed.on('update', (update, aggregate) => run(aggregate));
return liveFeed.cancel;
}
}
};
});
const db = new PouchDB<{}>("test");
db.deltaInit();
const liveFeed = db.liveFindStore({
selector: {}, // {series: 'Mario'}
sort: [], // [{series: 'desc'}, {name: 'desc'}]
aggregate: true
});
let newTodo: string;
function submit(id=undefined, title=undefined) {
db.save({ $id: id, title: title ?? newTodo }).then(function (doc) {
console.log(doc);
// doc.$id is the id of the created doc
});
}
</script>
<div>
<input bind:value={newTodo} /><button on:click={() => submit()}>Submit</button>
{#each $liveFeed as todo}
<div>{JSON.stringify(todo)}</div>
{todo.$id}
<input value={todo.title} on:change={ev => submit(todo.$id, ev.target.value)} />
<button on:click={() => db.delete(todo._id).then(console.log)}>❎</button>
{/each}
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I moved the logic from
.all
into.mergeAll
and made it keep the input shape (i.e. {docs: {}, key: 123, value: {}} stays the same, instead of becoming {...docs}). This is useful because this is the format.allDocs
expects I added an override for.allDocs
so that 3rd party plugins could take advantage of delta pouch's system without having to know about how the documents are combined together. I also bumped the version to 2.0 since it modifies the prototype of PouchDB, and people might not be expecting that, just to be safe.Closes #175