Comparing version 0.1.14 to 0.1.15
@@ -47,2 +47,3 @@ import AxiomObject from "./AxiomObject"; | ||
createIndex(blob: any): Promise<void>; | ||
findDocs(query: Query, includeDeleted: boolean): Promise<any[]>; | ||
find(query: Query): Promise<AxiomObject[]>; | ||
@@ -49,0 +50,0 @@ load(): void; |
@@ -178,14 +178,13 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var answer, result, _i, _a, row, sm; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
var docs, answer, _i, docs_1, doc, sm; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.wrap(this.findDocs({ selector: {} }, true))]; | ||
case 1: | ||
docs = _a.sent(); | ||
answer = []; | ||
return [4 /*yield*/, this.wrap(this.db.allDocs({ include_docs: true }))]; | ||
case 1: | ||
result = _b.sent(); | ||
for (_i = 0, _a = result.rows; _i < _a.length; _i++) { | ||
row = _a[_i]; | ||
for (_i = 0, docs_1 = docs; _i < docs_1.length; _i++) { | ||
doc = docs_1[_i]; | ||
try { | ||
sm = this.documentToSignedMessage(row.doc); | ||
sm = this.documentToSignedMessage(doc); | ||
answer.push(sm); | ||
@@ -195,3 +194,3 @@ } | ||
// There's something invalid in the database. | ||
console.error("invalid database document:", row.doc); | ||
console.error("invalid database document:", doc); | ||
console.error(e); | ||
@@ -206,3 +205,2 @@ } | ||
// You don't have to await this. | ||
// TODO: make this use queries or something smarter | ||
Database.prototype.onMessage = function (callback) { | ||
@@ -479,13 +477,20 @@ return __awaiter(this, void 0, void 0, function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var messages, sms, _i, sms_2, sm; | ||
var selector, messages, docs, _i, docs_2, doc, sm; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
selector = m.selector || {}; | ||
messages = []; | ||
return [4 /*yield*/, this.allSignedMessages()]; | ||
return [4 /*yield*/, this.findDocs({ selector: selector }, false)]; | ||
case 1: | ||
sms = _a.sent(); | ||
for (_i = 0, sms_2 = sms; _i < sms_2.length; _i++) { | ||
sm = sms_2[_i]; | ||
messages.push(sm.serialize()); | ||
docs = _a.sent(); | ||
for (_i = 0, docs_2 = docs; _i < docs_2.length; _i++) { | ||
doc = docs_2[_i]; | ||
try { | ||
sm = this.documentToSignedMessage(doc); | ||
messages.push(sm.serialize()); | ||
} | ||
catch (e) { | ||
// There's an invalid database message | ||
} | ||
} | ||
@@ -685,4 +690,4 @@ if (messages.length === 0) { | ||
}; | ||
// Returns a list of AxiomObject | ||
Database.prototype.find = function (query) { | ||
// Returns a list of pouch documents | ||
Database.prototype.findDocs = function (query, includeDeleted) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -700,10 +705,10 @@ var start, response, answer, _i, _a, doc, ms, s; | ||
doc = _a[_i]; | ||
if (doc.metadata.type === "Delete") { | ||
if (!includeDeleted && doc.metadata.type === "Delete") { | ||
continue; | ||
} | ||
answer.push(this.documentToObject(doc)); | ||
answer.push(doc); | ||
} | ||
ms = new Date().getTime() - start.getTime(); | ||
s = (ms / 1000).toFixed(3); | ||
this.log(this.name + " handled query " + JSON.stringify(query) + " in " + s + "s"); | ||
this.log(this.name + " handled query " + JSON.stringify(query.selector) + " in " + s + "s"); | ||
return [2 /*return*/, answer]; | ||
@@ -714,3 +719,30 @@ } | ||
}; | ||
// TODO: let this use queries somehow | ||
// Returns a list of AxiomObject | ||
Database.prototype.find = function (query) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var docs, answer, _i, docs_3, doc, obj; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.findDocs(query, false)]; | ||
case 1: | ||
docs = _a.sent(); | ||
answer = []; | ||
for (_i = 0, docs_3 = docs; _i < docs_3.length; _i++) { | ||
doc = docs_3[_i]; | ||
try { | ||
obj = this.documentToObject(doc); | ||
answer.push(obj); | ||
} | ||
catch (e) { | ||
// There's an invalid database message | ||
} | ||
} | ||
return [2 /*return*/, answer]; | ||
} | ||
}); | ||
}); | ||
}; | ||
// TODO: let this use queries somehow. | ||
// Note that this does nothing if we have not yet connected to other nodes | ||
// in our channel. | ||
Database.prototype.load = function () { | ||
@@ -717,0 +749,0 @@ this.log("loading " + this.name + " db"); |
{ | ||
"name": "axiom-api", | ||
"version": "0.1.14", | ||
"version": "0.1.15", | ||
"description": "API for interacting with the Axiom.org platform", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -121,11 +121,11 @@ import PouchDB from "pouchdb"; | ||
async allSignedMessages(): Promise<SignedMessage[]> { | ||
let docs = await this.wrap(this.findDocs({ selector: {} }, true)); | ||
let answer = []; | ||
let result = await this.wrap(this.db.allDocs({ include_docs: true })); | ||
for (let row of result.rows) { | ||
for (let doc of docs) { | ||
try { | ||
let sm = this.documentToSignedMessage(row.doc); | ||
let sm = this.documentToSignedMessage(doc); | ||
answer.push(sm); | ||
} catch (e) { | ||
// There's something invalid in the database. | ||
console.error("invalid database document:", row.doc); | ||
console.error("invalid database document:", doc); | ||
console.error(e); | ||
@@ -138,3 +138,2 @@ } | ||
// You don't have to await this. | ||
// TODO: make this use queries or something smarter | ||
async onMessage(callback: DatabaseCallback) { | ||
@@ -374,6 +373,12 @@ let sms = await this.allSignedMessages(); | ||
async handleQuery(peer: Peer, m: Message) { | ||
let selector = m.selector || {}; | ||
let messages = []; | ||
let sms = await this.allSignedMessages(); | ||
for (let sm of sms) { | ||
messages.push(sm.serialize()); | ||
let docs = await this.findDocs({ selector }, false); | ||
for (let doc of docs) { | ||
try { | ||
let sm = this.documentToSignedMessage(doc); | ||
messages.push(sm.serialize()); | ||
} catch (e) { | ||
// There's an invalid database message | ||
} | ||
} | ||
@@ -489,4 +494,4 @@ if (messages.length === 0) { | ||
// Returns a list of AxiomObject | ||
async find(query: Query): Promise<AxiomObject[]> { | ||
// Returns a list of pouch documents | ||
async findDocs(query: Query, includeDeleted: boolean): Promise<any[]> { | ||
let start = new Date(); | ||
@@ -496,14 +501,33 @@ let response = await this.wrap(this.db.find(query)); | ||
for (let doc of response.docs) { | ||
if (doc.metadata.type === "Delete") { | ||
if (!includeDeleted && doc.metadata.type === "Delete") { | ||
continue; | ||
} | ||
answer.push(this.documentToObject(doc)); | ||
answer.push(doc); | ||
} | ||
let ms = new Date().getTime() - start.getTime(); | ||
let s = (ms / 1000).toFixed(3); | ||
this.log(`${this.name} handled query ${JSON.stringify(query)} in ${s}s`); | ||
this.log( | ||
`${this.name} handled query ${JSON.stringify(query.selector)} in ${s}s` | ||
); | ||
return answer; | ||
} | ||
// TODO: let this use queries somehow | ||
// Returns a list of AxiomObject | ||
async find(query: Query): Promise<AxiomObject[]> { | ||
let docs = await this.findDocs(query, false); | ||
let answer = []; | ||
for (let doc of docs) { | ||
try { | ||
let obj = this.documentToObject(doc); | ||
answer.push(obj); | ||
} catch (e) { | ||
// There's an invalid database message | ||
} | ||
} | ||
return answer; | ||
} | ||
// TODO: let this use queries somehow. | ||
// Note that this does nothing if we have not yet connected to other nodes | ||
// in our channel. | ||
load() { | ||
@@ -510,0 +534,0 @@ this.log(`loading ${this.name} db`); |
@@ -107,2 +107,4 @@ # Message formats for the peer-to-peer network | ||
`selector` is a CouchDB selector to use. | ||
We'll have to extend this later. | ||
@@ -109,0 +111,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
362584
7084