@clearroad/api
Advanced tools
Comparing version 2.5.0 to 2.6.0
@@ -5,2 +5,17 @@ # Change Log | ||
<a name="2.6.0"></a> | ||
# [2.6.0](https://github.com/clearroad/clearroad-api/compare/v2.5.0...v2.6.0) (2018-12-06) | ||
### Bug Fixes | ||
* **clearroad:** use options.minDate to restrict sync in time ([a7a4a14](https://github.com/clearroad/clearroad-api/commit/a7a4a14)) | ||
### Features | ||
* **clearroad:** add method to query messages by state ([f469f98](https://github.com/clearroad/clearroad-api/commit/f469f98)) | ||
<a name="2.5.0"></a> | ||
@@ -7,0 +22,0 @@ # [2.5.0](https://github.com/clearroad/clearroad-api/compare/v2.4.1...v2.5.0) (2018-11-22) |
@@ -5,2 +5,3 @@ "use strict"; | ||
var jIO = require('jio').jIO; | ||
var all = require('rsvp').all; | ||
var index_1 = require("./definitions/index"); | ||
@@ -37,3 +38,3 @@ var queue_1 = require("./queue"); | ||
* When a message is processed by the ClearRoad platform, it will create a new message with a validation state. | ||
* When the message has not been sent to the platform yet, the state is "unsynced". | ||
* When the message has not been sent to the platform yet, the state is "not_processed". | ||
*/ | ||
@@ -45,3 +46,3 @@ var ValidationStates; | ||
ValidationStates["Submitted"] = "submitted"; | ||
ValidationStates["Unsynced"] = "unsynced"; | ||
ValidationStates["Unprocessed"] = "not_processed"; | ||
})(ValidationStates = exports.ValidationStates || (exports.ValidationStates = {})); | ||
@@ -175,6 +176,6 @@ var queryValidationStates = [ | ||
*/ | ||
ClearRoad.prototype.queryMaxDate = function () { | ||
ClearRoad.prototype.queryMinDate = function () { | ||
// only retrieve the data since xxx | ||
if (this.options.maxDate) { | ||
var from = new Date(this.options.maxDate); | ||
if (this.options.minDate) { | ||
var from = new Date(this.options.minDate); | ||
return "modification_date: >= \"" + from.toJSON() + "\""; | ||
@@ -271,3 +272,3 @@ } | ||
exports.queryGroupingReference + ": \"" + GroupingReferences.Data + "\"", | ||
this.queryMaxDate() | ||
this.queryMinDate() | ||
]); | ||
@@ -317,3 +318,3 @@ var signatureStorage = this.signatureSubStorage(this.databaseName + "-messages-signatures"); | ||
"validation_state: (" + queryValidationStates + ")", | ||
this.queryMaxDate() | ||
this.queryMinDate() | ||
]); | ||
@@ -364,3 +365,3 @@ var signatureStorage = this.signatureSubStorage(this.databaseName + "-ingestion-signatures"); | ||
"\"" + PortalTypes.RoadTransaction + "\"" | ||
].join(' OR ') + ")", this.queryMaxDate()]); | ||
].join(' OR ') + ")", this.queryMinDate()]); | ||
var signatureStorage = this.signatureSubStorage(this.databaseName + "-directory-signatures"); | ||
@@ -409,3 +410,3 @@ var localStorage = this.localSubStorage(refKey); | ||
exports.queryPortalType + ": (\"" + PortalTypes.File + "\")", | ||
this.queryMaxDate() | ||
this.queryMinDate() | ||
]); | ||
@@ -532,3 +533,3 @@ var signatureStorage = this.signatureSubStorage(this.databaseName + "-files-signatures"); | ||
} | ||
return ValidationStates.Unsynced; | ||
return ValidationStates.Unprocessed; | ||
}); | ||
@@ -560,2 +561,61 @@ }; | ||
/** | ||
* Query the messages with a specific state. | ||
* @param state The state to query for | ||
* @param options Set { sort_on, limit } on the results | ||
*/ | ||
ClearRoad.prototype.queryByState = function (state, options) { | ||
var _this = this; | ||
if (options === void 0) { options = {}; } | ||
var sort_on = options.sort_on, limit = options.limit; | ||
// query for data message without corresponding report message | ||
if (state === ValidationStates.Unprocessed) { | ||
return this.allDocs({ | ||
query: exports.queryGroupingReference + ": \"" + GroupingReferences.Data + "\"", | ||
select_list: [exports.queryPortalType], | ||
sort_on: sort_on, | ||
limit: limit | ||
}) | ||
.push(function (results) { | ||
return all(results.data.rows.map(function (result) { | ||
return _this.allDocs({ | ||
query: exports.queryGroupingReference + ": \"" + GroupingReferences.Report + "\" AND " + exports.querySourceReference + ": \"" + result.id + "\"" | ||
}).push(function (docs) { | ||
return docs.data.rows.length === 0 ? result : null; | ||
}); | ||
})); | ||
}) | ||
.push(function (results) { | ||
var rows = results.filter(function (result) { return result !== null; }); | ||
return { | ||
data: { | ||
rows: rows, | ||
total_rows: rows.length | ||
} | ||
}; | ||
}); | ||
} | ||
return this.allDocs({ | ||
query: "state: \"" + state + "\" AND " + exports.queryGroupingReference + ": \"" + GroupingReferences.Report + "\"", | ||
select_list: [exports.queryPortalType, exports.querySourceReference], | ||
sort_on: sort_on, | ||
limit: limit | ||
}).push(function (results) { | ||
var rows = results.data.rows.map(function (row) { | ||
var _a; | ||
return { | ||
id: row.value[exports.querySourceReference], | ||
value: (_a = {}, | ||
_a[exports.queryPortalType] = row.value[exports.queryPortalType], | ||
_a) | ||
}; | ||
}); | ||
return { | ||
data: { | ||
rows: rows, | ||
total_rows: rows.length | ||
} | ||
}; | ||
}); | ||
}; | ||
/** | ||
* Query for documents in the local storage. Make sure `.sync()` is called before. | ||
@@ -562,0 +622,0 @@ * @param options Query options. If none set, return all documents. |
@@ -24,3 +24,3 @@ import { portalType } from './message-types'; | ||
* When a message is processed by the ClearRoad platform, it will create a new message with a validation state. | ||
* When the message has not been sent to the platform yet, the state is "unsynced". | ||
* When the message has not been sent to the platform yet, the state is "not_processed". | ||
*/ | ||
@@ -31,3 +31,3 @@ export declare enum ValidationStates { | ||
Submitted = "submitted", | ||
Unsynced = "unsynced" | ||
Unprocessed = "not_processed" | ||
} | ||
@@ -55,16 +55,30 @@ /** | ||
type: localStorageType | string; | ||
/** | ||
* Access token of the storage. | ||
*/ | ||
accessToken?: string; | ||
/** | ||
* Primary database name. Default to 'clearroad' | ||
* Primary database name. Default to 'clearroad'. | ||
*/ | ||
database?: string; | ||
}; | ||
maxDate?: Date | number | string; | ||
/** | ||
* Messages updated before this date will not be synchronized. | ||
* If not set, all messages will be synchronized. | ||
* Improves speed of synchronisation for big sets. | ||
* @example | ||
* ``` | ||
* const today = new Date(); | ||
* const from = today.setMonth(today.getMonth() - 1); // one month synchronization only | ||
* new ClearRoad(url, token, {minDate: from}) | ||
* ``` | ||
*/ | ||
minDate?: Date | number | string; | ||
/** | ||
* Force using a query storage around the localStorage. | ||
* Needed if the storage can not query data directly. See information on the storage | ||
* Needed if the storage can not query data directly. See information on the storage. | ||
*/ | ||
useQueryStorage?: boolean; | ||
/** | ||
* Turn on debugging mode to console | ||
* Log to console replication steps between local and remote storage. | ||
*/ | ||
@@ -174,2 +188,8 @@ debug?: boolean; | ||
/** | ||
* Query the messages with a specific state. | ||
* @param state The state to query for | ||
* @param options Set { sort_on, limit } on the results | ||
*/ | ||
queryByState(state: ValidationStates, options?: Partial<IJioQueryOptions>): IQueue<IJioQueryResults>; | ||
/** | ||
* Query for documents in the local storage. Make sure `.sync()` is called before. | ||
@@ -176,0 +196,0 @@ * @param options Query options. If none set, return all documents. |
const Rusha = require('rusha'); | ||
const jIO = require('jio').jIO; | ||
const { all } = require('rsvp'); | ||
import { validateDefinition } from './definitions/index'; | ||
@@ -34,3 +35,3 @@ import { getQueue } from './queue'; | ||
* When a message is processed by the ClearRoad platform, it will create a new message with a validation state. | ||
* When the message has not been sent to the platform yet, the state is "unsynced". | ||
* When the message has not been sent to the platform yet, the state is "not_processed". | ||
*/ | ||
@@ -42,3 +43,3 @@ export var ValidationStates; | ||
ValidationStates["Submitted"] = "submitted"; | ||
ValidationStates["Unsynced"] = "unsynced"; | ||
ValidationStates["Unprocessed"] = "not_processed"; | ||
})(ValidationStates || (ValidationStates = {})); | ||
@@ -167,6 +168,6 @@ const queryValidationStates = [ | ||
*/ | ||
queryMaxDate() { | ||
queryMinDate() { | ||
// only retrieve the data since xxx | ||
if (this.options.maxDate) { | ||
const from = new Date(this.options.maxDate); | ||
if (this.options.minDate) { | ||
const from = new Date(this.options.minDate); | ||
return `modification_date: >= "${from.toJSON()}"`; | ||
@@ -262,3 +263,3 @@ } | ||
`${queryGroupingReference}: "${GroupingReferences.Data}"`, | ||
this.queryMaxDate() | ||
this.queryMinDate() | ||
]); | ||
@@ -308,3 +309,3 @@ const signatureStorage = this.signatureSubStorage(`${this.databaseName}-messages-signatures`); | ||
`validation_state: (${queryValidationStates})`, | ||
this.queryMaxDate() | ||
this.queryMinDate() | ||
]); | ||
@@ -355,3 +356,3 @@ const signatureStorage = this.signatureSubStorage(`${this.databaseName}-ingestion-signatures`); | ||
`"${PortalTypes.RoadTransaction}"` | ||
].join(' OR ')})`, this.queryMaxDate()]); | ||
].join(' OR ')})`, this.queryMinDate()]); | ||
const signatureStorage = this.signatureSubStorage(`${this.databaseName}-directory-signatures`); | ||
@@ -399,3 +400,3 @@ const localStorage = this.localSubStorage(refKey); | ||
`${queryPortalType}: ("${PortalTypes.File}")`, | ||
this.queryMaxDate() | ||
this.queryMinDate() | ||
]); | ||
@@ -521,3 +522,3 @@ const signatureStorage = this.signatureSubStorage(`${this.databaseName}-files-signatures`); | ||
} | ||
return ValidationStates.Unsynced; | ||
return ValidationStates.Unprocessed; | ||
}); | ||
@@ -547,2 +548,58 @@ } | ||
/** | ||
* Query the messages with a specific state. | ||
* @param state The state to query for | ||
* @param options Set { sort_on, limit } on the results | ||
*/ | ||
queryByState(state, options = {}) { | ||
const { sort_on, limit } = options; | ||
// query for data message without corresponding report message | ||
if (state === ValidationStates.Unprocessed) { | ||
return this.allDocs({ | ||
query: `${queryGroupingReference}: "${GroupingReferences.Data}"`, | ||
select_list: [queryPortalType], | ||
sort_on, | ||
limit | ||
}) | ||
.push(results => { | ||
return all(results.data.rows.map(result => { | ||
return this.allDocs({ | ||
query: `${queryGroupingReference}: "${GroupingReferences.Report}" AND ${querySourceReference}: "${result.id}"` | ||
}).push(docs => { | ||
return docs.data.rows.length === 0 ? result : null; | ||
}); | ||
})); | ||
}) | ||
.push(results => { | ||
const rows = results.filter(result => result !== null); | ||
return { | ||
data: { | ||
rows, | ||
total_rows: rows.length | ||
} | ||
}; | ||
}); | ||
} | ||
return this.allDocs({ | ||
query: `state: "${state}" AND ${queryGroupingReference}: "${GroupingReferences.Report}"`, | ||
select_list: [queryPortalType, querySourceReference], | ||
sort_on, | ||
limit | ||
}).push(results => { | ||
const rows = results.data.rows.map(row => { | ||
return { | ||
id: row.value[querySourceReference], | ||
value: { | ||
[queryPortalType]: row.value[queryPortalType] | ||
} | ||
}; | ||
}); | ||
return { | ||
data: { | ||
rows, | ||
total_rows: rows.length | ||
} | ||
}; | ||
}); | ||
} | ||
/** | ||
* Query for documents in the local storage. Make sure `.sync()` is called before. | ||
@@ -549,0 +606,0 @@ * @param options Query options. If none set, return all documents. |
{ | ||
"name": "@clearroad/api", | ||
"version": "2.5.0", | ||
"version": "2.6.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/commonjs/index.js", |
Sorry, the diff of this file is not supported yet
191106
3357