@ocap/indexdb 1.28.8 → 1.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.cjs ADDED
@@ -0,0 +1,96 @@
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
3
+ let _ocap_util_lib_ready = require("@ocap/util/lib/ready");
4
+ let node_crypto = require("node:crypto");
5
+ node_crypto = require_rolldown_runtime.__toESM(node_crypto);
6
+ let kareem = require("kareem");
7
+ kareem = require_rolldown_runtime.__toESM(kareem);
8
+ let lodash_omit = require("lodash/omit");
9
+ lodash_omit = require_rolldown_runtime.__toESM(lodash_omit);
10
+
11
+ //#region src/index.ts
12
+ /**
13
+ * Base class for index tables
14
+ * Provides hook support (pre/post) and common operations
15
+ * Subclasses must implement _insert, _get, _update, _reset, count
16
+ */
17
+ var BaseIndex = class extends _ocap_util_lib_ready.Ready {
18
+ constructor(name, uniqIndex) {
19
+ super();
20
+ this.name = name || "";
21
+ this.ready = false;
22
+ this.readyCallbacks = [];
23
+ this.uniqIndex = uniqIndex || "id";
24
+ this.primaryKey = typeof uniqIndex === "string" ? uniqIndex : "id";
25
+ const hooks = new kareem.default();
26
+ Object.defineProperty(this, "pre", { value: (hookName, fn) => hooks.pre(hookName, fn) });
27
+ Object.defineProperty(this, "post", { value: (hookName, fn) => hooks.post(hookName, fn) });
28
+ [
29
+ "insert",
30
+ "get",
31
+ "update",
32
+ "reset"
33
+ ].forEach((opName) => {
34
+ Object.defineProperty(this, opName, { value: async (...args) => {
35
+ hooks.execPreSync(opName, args);
36
+ const result = await this[`_${opName}`](...args);
37
+ hooks.execPostSync(opName, result);
38
+ if (["insert", "update"].includes(opName)) this.emit(opName, (0, lodash_omit.default)(result, "$loki"));
39
+ return result;
40
+ } });
41
+ });
42
+ }
43
+ /**
44
+ * Count documents matching query
45
+ * Must be implemented by subclasses
46
+ */
47
+ count(_query) {
48
+ throw new Error("`count` must be implemented in sub indexdb");
49
+ }
50
+ /**
51
+ * Internal insert implementation
52
+ * Called by the public `insert` method after pre-hooks
53
+ */
54
+ _insert(_doc) {
55
+ throw new Error("`_insert` must be implemented in sub indexdb");
56
+ }
57
+ /**
58
+ * Internal get implementation
59
+ * Called by the public `get` method after pre-hooks
60
+ */
61
+ _get(_key) {
62
+ throw new Error("`_get` must be implemented in sub indexdb");
63
+ }
64
+ /**
65
+ * Internal update implementation
66
+ * Called by the public `update` method after pre-hooks
67
+ */
68
+ _update(_key, _updates) {
69
+ throw new Error("`_update` must be implemented in sub indexdb");
70
+ }
71
+ /**
72
+ * Internal reset implementation
73
+ * Called by the public `reset` method after pre-hooks
74
+ */
75
+ _reset() {
76
+ throw new Error("`_reset` must be implemented in sub indexdb");
77
+ }
78
+ /**
79
+ * Generate primary key from document or key object
80
+ * For composite keys, generates MD5 hash of joined values
81
+ * @param doc - String key or document/key object
82
+ * @returns Primary key string
83
+ */
84
+ generatePrimaryKey(doc) {
85
+ if (typeof doc === "string") return doc;
86
+ if (Array.isArray(this.uniqIndex)) {
87
+ const key = this.uniqIndex.map((id) => doc[id]).map((item) => typeof item === "object" ? JSON.stringify(item) : item).join("-");
88
+ return node_crypto.default.createHash("md5").update(key).digest("hex");
89
+ }
90
+ return doc[this.uniqIndex];
91
+ }
92
+ };
93
+ var src_default = BaseIndex;
94
+
95
+ //#endregion
96
+ exports.default = src_default;
@@ -0,0 +1,58 @@
1
+ import { IIndexTable, IndexTableHookFn, Promisable } from "@ocap/types";
2
+ import { Ready } from "@ocap/util/lib/ready";
3
+
4
+ //#region src/index.d.ts
5
+ type UniqueIndex = string | string[];
6
+ /**
7
+ * Base class for index tables
8
+ * Provides hook support (pre/post) and common operations
9
+ * Subclasses must implement _insert, _get, _update, _reset, count
10
+ */
11
+ declare class BaseIndex<T = unknown> extends Ready implements IIndexTable<T> {
12
+ readonly name: string;
13
+ readonly primaryKey: string;
14
+ readonly uniqIndex: UniqueIndex;
15
+ ready: boolean;
16
+ readyCallbacks: Array<() => void>;
17
+ pre: (hookName: string, fn: IndexTableHookFn) => void;
18
+ post: (hookName: string, fn: IndexTableHookFn) => void;
19
+ insert: (doc: T | Record<string, unknown>) => Promise<T>;
20
+ get: (key: string | Record<string, unknown>) => Promise<T | null>;
21
+ update: (key: string | Record<string, unknown>, updates: Partial<T>) => Promise<T>;
22
+ reset: () => Promise<void>;
23
+ constructor(name?: string, uniqIndex?: UniqueIndex);
24
+ /**
25
+ * Count documents matching query
26
+ * Must be implemented by subclasses
27
+ */
28
+ count(_query?: unknown): Promisable<number>;
29
+ /**
30
+ * Internal insert implementation
31
+ * Called by the public `insert` method after pre-hooks
32
+ */
33
+ _insert(_doc: T | Record<string, unknown>): Promisable<T>;
34
+ /**
35
+ * Internal get implementation
36
+ * Called by the public `get` method after pre-hooks
37
+ */
38
+ _get(_key: string | Record<string, unknown>): Promisable<T | null>;
39
+ /**
40
+ * Internal update implementation
41
+ * Called by the public `update` method after pre-hooks
42
+ */
43
+ _update(_key: string | Record<string, unknown>, _updates: Partial<T>): Promisable<T>;
44
+ /**
45
+ * Internal reset implementation
46
+ * Called by the public `reset` method after pre-hooks
47
+ */
48
+ _reset(): Promisable<void>;
49
+ /**
50
+ * Generate primary key from document or key object
51
+ * For composite keys, generates MD5 hash of joined values
52
+ * @param doc - String key or document/key object
53
+ * @returns Primary key string
54
+ */
55
+ generatePrimaryKey(doc: string | Record<string, unknown>): string;
56
+ }
57
+ //#endregion
58
+ export { BaseIndex as default };
package/lib/main.cjs ADDED
@@ -0,0 +1,25 @@
1
+ const require_db = require('./db.cjs');
2
+ const require_index = require('./index.cjs');
3
+ const require_util = require('./util.cjs');
4
+
5
+ exports.BaseIndex = require_index.default;
6
+ exports.BaseIndexDB = require_db.default;
7
+ exports.createIndexedAccount = require_util.createIndexedAccount;
8
+ exports.createIndexedAsset = require_util.createIndexedAsset;
9
+ exports.createIndexedDelegation = require_util.createIndexedDelegation;
10
+ exports.createIndexedFactory = require_util.createIndexedFactory;
11
+ exports.createIndexedRollup = require_util.createIndexedRollup;
12
+ exports.createIndexedRollupBlock = require_util.createIndexedRollupBlock;
13
+ exports.createIndexedStake = require_util.createIndexedStake;
14
+ exports.createIndexedToken = require_util.createIndexedToken;
15
+ exports.createIndexedTokenDistribution = require_util.createIndexedTokenDistribution;
16
+ exports.createIndexedTokenFactory = require_util.createIndexedTokenFactory;
17
+ exports.createIndexedTransaction = require_util.createIndexedTransaction;
18
+ exports.formatDelegationAfterRead = require_util.formatDelegationAfterRead;
19
+ exports.formatNextPagination = require_util.formatNextPagination;
20
+ exports.formatPagination = require_util.formatPagination;
21
+ exports.formatTokenMeta = require_util.formatTokenMeta;
22
+ exports.formatTxAfterRead = require_util.formatTxAfterRead;
23
+ exports.formatTxBeforeInsert = require_util.formatTxBeforeInsert;
24
+ exports.isDefaultTokenChanged = require_util.isDefaultTokenChanged;
25
+ exports.parseDateTime = require_util.parseDateTime;
package/lib/main.d.cts ADDED
@@ -0,0 +1,4 @@
1
+ import BaseIndexDB from "./db.cjs";
2
+ import BaseIndex from "./index.cjs";
3
+ import { DelegationContext, FactoryContext, FormatPaginationParams, FormattedPagination, IndexDBForTx, PagingInput, RollupBlockContext, RollupContext, TokenFactoryContext, TokenStateInfo, TokenWithBalance, TxData, createIndexedAccount, createIndexedAsset, createIndexedDelegation, createIndexedFactory, createIndexedRollup, createIndexedRollupBlock, createIndexedStake, createIndexedToken, createIndexedTokenDistribution, createIndexedTokenFactory, createIndexedTransaction, formatDelegationAfterRead, formatNextPagination, formatPagination, formatTokenMeta, formatTxAfterRead, formatTxBeforeInsert, isDefaultTokenChanged, parseDateTime } from "./util.cjs";
4
+ export { BaseIndex, BaseIndexDB, DelegationContext, FactoryContext, FormatPaginationParams, FormattedPagination, IndexDBForTx, PagingInput, RollupBlockContext, RollupContext, TokenFactoryContext, TokenStateInfo, TokenWithBalance, TxData, createIndexedAccount, createIndexedAsset, createIndexedDelegation, createIndexedFactory, createIndexedRollup, createIndexedRollupBlock, createIndexedStake, createIndexedToken, createIndexedTokenDistribution, createIndexedTokenFactory, createIndexedTransaction, formatDelegationAfterRead, formatNextPagination, formatPagination, formatTokenMeta, formatTxAfterRead, formatTxBeforeInsert, isDefaultTokenChanged, parseDateTime };