-
Notifications
You must be signed in to change notification settings - Fork 32
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
Changes Support #24
base: master
Are you sure you want to change the base?
Changes Support #24
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,6 +124,64 @@ couch.mango(dbName, mangoQuery, parameters).then(({data, headers, status}) => { | |
}); | ||
``` | ||
|
||
## Trigger A Design Update | ||
```javascript | ||
const update = { | ||
firstname: 'Rita', | ||
middlename: '__delete__' | ||
}; | ||
|
||
couch.design_update('database', 'someDocId', 'default', 'partialUpdate', update).then(({data, headers, status}) => { | ||
// data is json response | ||
// headers is an object with all response headers | ||
// status is statusCode number | ||
}, err => { | ||
// either request error occured | ||
// ...or err.code=EDOCMISSING if document is missing | ||
// ...or err.code=EUNKNOWN if statusCode is unexpected | ||
}); | ||
``` | ||
|
||
## Subscribe To Changes | ||
```javascript | ||
const dbName = "database"; | ||
const mangoQuery = { | ||
selector: { | ||
$gte: {firstname: 'Ann'}, | ||
$lt: {firstname: 'George'} | ||
} | ||
}; | ||
|
||
// This method will first be called with a null change and valid context, before any changes are posted. | ||
// The context has an unsubscribe() method that will release resources and disconnect. | ||
function changeCallback (change, context) { | ||
if (!change) { | ||
// Store the context, and call context.unsubscribe() to release the socket when done listening. | ||
} | ||
else { | ||
// change is the changed row | ||
// heartbeats are filtered out | ||
} | ||
}; | ||
|
||
// These are the defaults | ||
const parameters = { | ||
feed: 'continuous', | ||
heartbeat: true, | ||
since: 'now' | ||
}; | ||
|
||
couch.changes(dbName, mangoQuery, parameters, callback, 'selector', selectorFilter).then(({data, headers, status}) => { | ||
// data is json response | ||
// headers is an object with all response headers | ||
// status is statusCode number | ||
}, err => { | ||
// either request error occured | ||
// ...or err.code=EDOCMISSING if document is missing | ||
// ...or err.code=EUNKNOWN if statusCode is unexpected | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit tricky API and it's more similar to publish-subscribe, than to promises which get fulfilled one time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I use the promise to return the result of the subscribe request, and the callback for the actual subscription. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean exactly this. Promise represents the asynchronous operation which finishes either with some useful result (resolve) or fails with some error (reject). Publish/Subscribe (EventEmitter, Rx.Observable) represent continuous operations which can emit different events. Is there any need for result of subscribe request? How should the callback code work if subscribe request fails? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The callback is not invoked at all if the promise fails. We could return an interface that has a subscribe method in the promise, but then we would have to cache any intermediate data. I think you need both the promise and subscription. There is an async operation and a separate subscription. I guess we could send an error to the subscription if the connection fails; Then you would not need to return a promise at all. But that would break chaining operations, and make the API less consistent. |
||
``` | ||
|
||
## Insert a document | ||
```javascript | ||
couch.insert("databaseName", { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this to another PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#26