UNPKG

chex-storage

Version:

A wrapper library for Chrome extension storage local - the standard storage in the chrome extension.

285 lines (279 loc) 6.71 kB
// src/web-application/provider.ts var ChexStorageProvider = class { constructor(extensionId, config) { this.config = { debug: false }; this.extensionId = extensionId; this.config = { ...this.config, ...config || {} }; } log(...args) { if (this.config.debug) { console.log(...args); } } async sendMessage(type, payload) { this.log("[ChexDatabaseWeb]: Payload", { type, payload }); return await new Promise((resolve, reject) => { if (!this.extensionId) { return; } try { chrome.runtime.sendMessage( this.extensionId, JSON.stringify({ type, payload, config: this.config }), (res) => { this.log("[ChexDatabaseWeb]: Data", res.data); resolve(res.data); } ); } catch (error) { console.error("[ChexDatabaseWeb]: Error", error); reject(error); } }); } }; var provider_default = ChexStorageProvider; // src/utils/result.ts function ChexStorageResult(items) { const newArray = new Array(...items); Object.setPrototypeOf(Array.prototype, ChexStorageResult.prototype); ChexStorageResult.prototype.sortBy = function(fieldName) { const field = fieldName.slice(0, -1); const self = this; if (fieldName.indexOf("+") > 0) { return self.sort( (a, b) => a[field] > b[field] ? 1 : -1 ); } return self.sort( (a, b) => a[field] > b[field] ? -1 : 1 ); }; return newArray; } var result_default = ChexStorageResult; // src/constant.ts var event = { INIT: "chex-storage-init", DEFINE_TABLE: "chex-storage-define-table", ADD: "chex-storage-add", GET: "chex-storage-get", GET_TABLE: "chex-storage-get-table", WHERE_EQUAL: "chex-storage-where-equal", SEARCH: "chex-storage-where-search", UPDATE: "chex-storage-update", UPDATE_OR_CREATE: "chex-storage-update-or-create", UPDATE_ALL: "chex-storage-update-all", DELETE: "chex-storage-delete", BULK_ADD: "chex-storage-bulk-add", BULK_DELETE: "chex-storage-bulk-delete" }; // src/web-application/_where.ts var ChexStorageWhereForOutsideMethods = class extends provider_default { #tableName; #keyWhere; constructor(tableName, keyWhere, extensionId) { super(extensionId); this.#tableName = tableName; this.#keyWhere = keyWhere; } /** * Query data where the specified key equals the given value * * @param val * @returns TData[] */ async equals(val) { const data = await this.sendMessage(event.WHERE_EQUAL, { value: val, table: this.#tableName, keyWhere: this.#keyWhere }); return data; } async search(pattern) { const data = await this.sendMessage(event.SEARCH, { pattern, table: this.#tableName, keyWhere: this.#keyWhere }); return result_default(data); } }; var where_default = ChexStorageWhereForOutsideMethods; // src/web-application/table.ts var ChexTableWeb = class extends provider_default { #tableName; #database; constructor(database, name, extensionId) { super(extensionId); this.#database = database; this.#tableName = name; } /** * Querying data with the specified key condition * * @param keyWhere * @returns ChexStorageWhereMethods */ where(keyWhere) { const whereMethod = new where_default( this.#tableName, keyWhere, this.extensionId ); return whereMethod; } /** * Adds new data to the table and returns the key of the added data. * * @param data * @returns Key value */ async add(data) { const key = await this.sendMessage(event.ADD, { data, table: this.#tableName }); return key; } /** * Adds multiple data entries to the table * * @param data */ async bulkAdd(data) { const res = await this.sendMessage(event.BULK_ADD, { data, table: this.#tableName }); return res; } async get(key) { const data = await this.sendMessage(event.GET, { key, table: this.#tableName }); return data; } /** * Updates data in the table by key and returns the key of the updated data * * @param key * @param change */ async update(keyValue, change) { const data = await this.sendMessage(event.UPDATE, { keyValue, change, table: this.#tableName }); return data; } /** * Update data for create new record * * @param keyValue * @param change * @param defaultValue * @returns */ async updateOrCreate(keyValue, change, defaultValue) { const data = await this.sendMessage(event.UPDATE_OR_CREATE, { keyValue, change, defaultValue, table: this.#tableName }); return data; } /** * Updates all data in the table with the specified changes. * * @param change */ async updateAll(change) { const data = await this.sendMessage(event.UPDATE_ALL, { change, table: this.#tableName }); return data; } /** * Deletes multiple data entries from the table by their keys * * @param keyValue */ async delete(keyValue) { const data = await this.sendMessage(event.DELETE, { keyValue, table: this.#tableName }); return data; } /** * Delete * * @param keyValues */ async bulkDelete(keyValues) { const data = await this.sendMessage(event.DELETE, { keyValues, table: this.#tableName }); return data; } /** * Query data with the same of filter factory function * * @param callback * @returns TData[] */ async filter(callback) { } }; var table_default = ChexTableWeb; // src/web-application/index.ts var ChexDatabaseWeb = class extends provider_default { constructor(databaseName, extensionId, initData = {}, config) { try { if (config?.debug) { console.log("[ChexDatabaseWeb]: constructor"); } super(extensionId, config); this.databaseName = databaseName; this.sendMessage(event.INIT, { databaseName, initData }); } catch (error) { console.error("[Chex storage] Initial fail: ", error); super(""); this.databaseName = ""; } } async tables(tables) { const self = this; await this.sendMessage(event.DEFINE_TABLE, { tables }); Object.keys(tables).forEach((table) => { const tableClass = new table_default( self.databaseName, table, this.extensionId ); self[table] = tableClass; }); } }; var web_application_default = ChexDatabaseWeb; export { web_application_default as default };