wallet-storage-client
Version:
Client only Wallet Storage
116 lines • 4.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StorageReader = void 0;
const index_client_1 = require("../index.client");
const getSyncChunk_1 = require("./methods/getSyncChunk");
/**
* The `StorageReader` abstract class is the base of the concrete wallet storage provider classes.
*
* It is the minimal interface required to read all wallet state records and is the base class for sync readers.
*
* The next class in the heirarchy is the `StorageReaderWriter` which supports sync readers and writers.
*
* The last class in the heirarchy is the `Storage` class which supports all active wallet operations.
*
* The ability to construct a properly configured instance of this class implies authentication.
* As such there are no user specific authenticated access checks implied in the implementation of any of these methods.
*/
class StorageReader {
get dbtype() { var _a; return (_a = this._settings) === null || _a === void 0 ? void 0 : _a.dbtype; }
constructor(options) {
this.chain = options.chain;
}
isAvailable() {
return !!this._settings;
}
async makeAvailable() {
return this._settings = await this.readSettings();
}
getSettings() {
if (!this._settings)
throw new index_client_1.sdk.WERR_INVALID_OPERATION('must call "makeAvailable" before accessing "settings"');
return this._settings;
}
isStorageProvider() { return false; }
async findUserByIdentityKey(key) {
return (0, index_client_1.verifyOneOrNone)(await this.findUsers({ partial: { identityKey: key } }));
}
async getSyncChunk(args) {
return (0, getSyncChunk_1.getSyncChunk)(this, args);
}
/**
* Force dates to strings on SQLite and Date objects on MySQL
* @param date
* @returns
*/
validateEntityDate(date) {
if (!this.dbtype)
throw new index_client_1.sdk.WERR_INTERNAL('must call verifyReadyForDatabaseAccess first');
let r = this.validateDate(date);
switch (this.dbtype) {
case 'MySQL':
break;
case 'SQLite':
r = r.toISOString();
break;
default: throw new index_client_1.sdk.WERR_INTERNAL(`Invalid dateScheme ${this.dbtype}`);
}
return r;
}
/**
*
* @param date
* @param useNowAsDefault if true and date is null or undefiend, set to current time.
* @returns
*/
validateOptionalEntityDate(date, useNowAsDefault) {
if (!this.dbtype)
throw new index_client_1.sdk.WERR_INTERNAL('must call verifyReadyForDatabaseAccess first');
let r = this.validateOptionalDate(date);
if (!r && useNowAsDefault)
r = new Date();
switch (this.dbtype) {
case 'MySQL':
break;
case 'SQLite':
if (r)
r = r.toISOString();
break;
default: throw new index_client_1.sdk.WERR_INTERNAL(`Invalid dateScheme ${this.dbtype}`);
}
return r;
}
validateDate(date) {
let r;
if (date instanceof Date)
r = date;
else
r = new Date(date);
return r;
}
validateOptionalDate(date) {
if (date === null || date === undefined)
return undefined;
return this.validateDate(date);
}
validateDateForWhere(date) {
if (!this.dbtype)
throw new index_client_1.sdk.WERR_INTERNAL('must call verifyReadyForDatabaseAccess first');
if (typeof date === 'number')
date = (0, index_client_1.validateSecondsSinceEpoch)(date);
const vdate = (0, index_client_1.verifyTruthy)(this.validateDate(date));
let r;
switch (this.dbtype) {
case 'MySQL':
r = vdate;
break;
case 'SQLite':
r = vdate.toISOString();
break;
default: throw new index_client_1.sdk.WERR_INTERNAL(`Invalid dateScheme ${this.dbtype}`);
}
return r;
}
}
exports.StorageReader = StorageReader;
//# sourceMappingURL=StorageReader.js.map