Merge pull request #154 from MrBigDog2U/master

Add PouchDB definitions
This commit is contained in:
Boris Yankov
2013-01-02 16:37:30 -08:00
2 changed files with 358 additions and 0 deletions
+216
View File
@@ -0,0 +1,216 @@
// Type definitions for Pouch 0.1
// Project: http://pouchdb.com
// Definitions by: Bill Sears <https://github.com/MrBigDog2U/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
interface PouchError {
status: number;
error: string;
reason: string;
}
interface PouchApi {
id(): string;
}
interface PouchInfoResponse {
db_name: string;
doc_count: number;
update_seq: string;
}
interface PouchApi {
info(callback: (err: PouchError, res: PouchInfoResponse) => void): void;
}
interface PouchGetOptions {
rev?: string;
revs?: bool;
revs_info?: bool;
conflicts?: bool;
attachments?: bool;
}
interface PouchGetResponse {
_id: string;
_rev: string;
_attachments: any;
}
interface PouchAllDocsOptions {
startkey?: string;
endKey?: string;
descending?: bool;
include_docs?: bool;
conflicts?: bool;
}
interface PouchAllDocsItem {
id: string;
key: string;
value: any;
}
interface PouchAllDocsResponse {
total_rows: number;
rows: PouchAllDocsItem[];
}
interface PouchApi {
//
// get == select by id
//
get(id: string, opts: PouchGetOptions, callback: (err: PouchError, res: PouchGetResponse) => void): void;
get(id: string, callback: (err: PouchError, res: PouchGetResponse) => void): void;
allDocs(opts: PouchAllDocsOptions, callback: (err: PouchError, res: PouchAllDocsResponse) => void): void;
allDocs(callback: (err: PouchError, res: PouchAllDocsResponse) => void): void;
}
interface PouchUpdateOptions {
new_edits: bool;
}
interface PouchUpdateResponse {
ok: bool;
id: string;
rev: string;
}
interface PouchApi {
bulkDocs(req, opts: PouchUpdateOptions, callback: (err: PouchError, res: PouchUpdateResponse[]) => void): void;
bulkDocs(req, callback: (err: PouchError, res: PouchUpdateResponse[]) => void): void;
//
// post == insert (doc does not contain an _id)
//
post(doc: any, opts: PouchUpdateOptions, callback: (err: PouchError, res: PouchUpdateResponse) => void): void;
post(doc: any, callback: (err: PouchError, res: PouchUpdateResponse) => void): void;
//
// put == update (doc DOES contain an _id)
//
put(doc, opts: PouchUpdateOptions, callback: (err: PouchError, res: PouchUpdateResponse) => void): void;
put(doc, callback: (err: PouchError, res: PouchUpdateResponse) => void): void;
//
// remove == delete
//
remove(doc, opts: PouchUpdateOptions, callback: (err: PouchError, res: PouchUpdateResponse) => void): void;
remove(doc, callback: (err: PouchError, res: PouchUpdateResponse) => void): void;
}
interface PouchFilter {
map: (doc: any) => void;
reduce?: (key: string, value: any) => any;
}
interface PouchQueryOptions {
complete?: any;
include_docs?: bool;
error?: (err: PouchError) => void;
descending?: bool;
reduce?: bool;
}
interface PouchQueryResponse {
rows: any[];
}
interface PouchApi {
//
// query == select by other criteria
//
query(fun: string, opts: PouchQueryOptions, callback: (err: PouchError, res: PouchQueryResponse) => void): void;
query(fun: string, callback: (err: PouchError, res: PouchQueryResponse) => void): void;
query(fun: PouchFilter, opts: PouchQueryOptions, callback: (err: PouchError, res: PouchQueryResponse) => void): void;
query(fun: PouchFilter, callback: (err: PouchError, res: PouchQueryResponse) => void): void;
}
interface PouchAttachmentOptions {
decode?: bool;
}
interface PouchApi {
getAttachment(id: string, opts: PouchAttachmentOptions, callback: (err: PouchError, res: any) => void): void;
getAttachment(id: string, callback: (err: PouchError, res: any) => void): void;
putAttachment(id: string, rev: string, doc: any, type: string, callback: (err: PouchError, res: PouchUpdateResponse) => void): void;
removeAttachment(id: string, rev: string, callback: (err: PouchError, res: PouchUpdateResponse) => void): void;
}
interface PouchCancellable {
cancel: () => void;
}
interface PouchChangesOptions {
onChange: (change: PouchChange) => void;
complete?: (err: PouchError, res: PouchChanges) => void;
seq?: number;
since?: number;
descending?: bool;
filter?: PouchFilter;
continuous?: bool;
include_docs?: bool;
conflicts?: bool;
}
interface PouchChange {
changes: any;
doc: PouchGetResponse;
id: string;
seq: number;
}
interface PouchChanges {
results: PouchChange[];
}
interface PouchApi {
changes(opts: PouchChangesOptions, callback: (err: PouchError, res: PouchChanges) => void): PouchCancellable;
changes(callback: (err: PouchError, res: PouchChanges) => void): PouchCancellable;
}
interface PouchRevsDiffOptions {
}
interface PouchReplicateOptions {
continuous?: bool;
onChange?: (any) => void;
filter?: any; // Can be either string or PouchFilter
complete?: (err: PouchError, res: PouchChanges) => void;
}
interface PouchReplicateResponse {
ok: bool;
start_time: Date;
end_time: Date;
docs_read: number;
docs_written: number;
}
interface PouchReplicate {
from(url: string, opts: PouchReplicateOptions, callback: (err: PouchError, res: PouchReplicateResponse) => void): PouchCancellable;
from(url: string, callback: (err: PouchError, res: PouchReplicateResponse) => void): PouchCancellable;
to(dbName: string, opts: PouchReplicateOptions, callback: (err: PouchError, res: PouchReplicateResponse) => void): PouchCancellable;
to(dbName: string, callback: (err: PouchError, res: PouchReplicateResponse) => void): PouchCancellable;
}
interface PouchApi {
revsDiff(req: any, opts: PouchRevsDiffOptions, callback: (missing: any) => void): void;
revsDiff(req: any, callback: (missing: any) => void): void;
replicate: PouchReplicate;
}
interface PouchOptions {
name?: string;
adapter?: string;
}
interface Pouch extends PouchApi {
(name: string, opts: PouchOptions, callback: (err: PouchError, res: Pouch) => void ): Pouch;
(name: string, callback: (err: PouchError, res: Pouch) => void ): Pouch;
destroy(name: string, callback: (err: PouchError) => void): void;
}
var Pouch: Pouch;
//
// emit is the function that the PouchFilter.map function should call in order to add a particular item to
// a filter view.
//
declare function emit(key: any, value: any);
+142
View File
@@ -0,0 +1,142 @@
/// <reference path="../../scripts/framework/pouch.d.ts" />
declare var $: any;
function alert(thing: any) {
$('body').append('<div>' + thing + '</div>');
}
var pouch: Pouch;
function pouchTests() {
Pouch('idb://testdb', function (err: PouchError, res: Pouch) {
if (err) {
alert('Error ' + err.status + ' occurred ' + err.error + ' - ' + err.reason);
}
else {
pouch = res;
alert("database opened");
runTests();
}
});
}
var tests = [
setupTests,
testId,
testAllDocs,
testGet,
testUpdate,
testDelete,
deleteDb
];
var testIndex;
var revs: any = {};
function runTests() {
testIndex = 0;
tests[testIndex++]();
}
// each test function except the last one needs to call nextTest when it is finished doing its thing.
function nextTest() {
alert("starting test " + testIndex);
tests[testIndex++]();
}
function setupTests() {
alert('setupTests');
pouch.bulkDocs({
docs: [{ _id: '1', name: 'record 1' },
{ _id: '2', name: 'record 2' },
{ _id: '3', name: 'record 3' }
]
}, function (err: PouchError, res: PouchUpdateResponse[]) {
if (err) {
alert('Error ' + err.status + ' occurred ' + err.error + ' - ' + err.reason);
}
else {
for (var i = 0; i < res.length; i++) {
if (res[i].ok) {
revs[res[i].id] = res[i].rev;
}
}
alert("test records loaded");
}
nextTest();
});
}
function testId() {
alert('testId');
var id = pouch.id();
alert('Database Id = ' + id);
nextTest();
}
function testGet() {
alert('testGet');
pouch.get('1', function (err: PouchError, res: PouchGetResponse) {
if (err) {
alert('Error ' + err.status + ' occurred ' + err.error + ' - ' + err.reason);
}
else {
alert('Retrieved record with id=1, name=[' + res['name'] + ']');
}
nextTest();
});
}
function testAllDocs() {
alert('testAllDocs');
pouch.allDocs(function (err: PouchError, res: PouchAllDocsResponse) {
alert('allDocs resulted in ' + res.total_rows + ' results');
for (var i = 0; i < res.total_rows; i++) {
alert('Retrieved record with id=' + res.rows[i].id + ', rev=[' + res.rows[i].value.rev + ']');
}
nextTest();
});
}
function testUpdate() {
alert('testUpdate');
pouch.put({ _id: '2', _rev: revs['2'], name: 'record 2 updated' }, function (err: PouchError, res: PouchUpdateResponse) {
if (err) {
alert('Error ' + err.status + ' occurred ' + err.error + ' - ' + err.reason);
}
else {
alert('record updated id=' + res.id + ', rev=[' + res.rev + ']');
}
testAllDocs(); // spit out the db contents and then go on
});
}
function testDelete() {
alert('testDelete');
pouch.remove({ _id: '3', _rev: revs['3'] }, function (err: PouchError, res: PouchUpdateResponse) {
if (err) {
alert('Error ' + err.status + ' occurred ' + err.error + ' - ' + err.reason);
}
else {
alert('record deleted id=' + res.id + ', rev=[' + res.rev + ']');
}
testAllDocs(); // spit out the db contents and then go on
});
}
function deleteDb() {
alert('deleteDb');
if (pouch) {
pouch = null;
Pouch.destroy('idb://testdb', function (err: PouchError) {
if (err) {
alert('Error ' + err.status + ' occurred ' + err.error + ' - ' + err.reason);
}
else {
alert("database destroyed");
}
});
}
}