@ocap/indexdb-memory 1.28.9 → 1.29.1

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.
@@ -0,0 +1,29 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) {
13
+ __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ }
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
+ value: mod,
24
+ enumerable: true
25
+ }) : target, mod));
26
+
27
+ //#endregion
28
+
29
+ exports.__toESM = __toESM;
@@ -0,0 +1,434 @@
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
3
+ const require_package = require('../package.cjs');
4
+ let _ocap_indexdb = require("@ocap/indexdb");
5
+ let debug = require("debug");
6
+ debug = require_rolldown_runtime.__toESM(debug);
7
+ let _arcblock_did_lib_type = require("@arcblock/did/lib/type");
8
+ let _ocap_util = require("@ocap/util");
9
+ let _ocap_util_lib_constant = require("@ocap/util/lib/constant");
10
+ let lodash_omit = require("lodash/omit");
11
+ lodash_omit = require_rolldown_runtime.__toESM(lodash_omit);
12
+
13
+ //#region src/db/base.ts
14
+ const debug$1 = (0, debug.default)(require_package.name);
15
+ const MAX_REQUEST_FACTORY_ADDRESS_SIZE = 100;
16
+ /** Helper to safely get collection from IIndexTable */
17
+ function getCollection(table) {
18
+ return table.collection;
19
+ }
20
+ function applyTimeFilter(query, timeFilter, defaultField, validFields) {
21
+ let { startDateTime, endDateTime, field = defaultField } = timeFilter;
22
+ startDateTime = (0, _ocap_indexdb.parseDateTime)(startDateTime);
23
+ endDateTime = (0, _ocap_indexdb.parseDateTime)(endDateTime);
24
+ if (!validFields.includes(field)) field = defaultField;
25
+ if (startDateTime && endDateTime) query.where((x) => x[field] > startDateTime && x[field] <= endDateTime);
26
+ else if (startDateTime) query.where((x) => x[field] > startDateTime);
27
+ else if (endDateTime) query.where((x) => x[field] <= endDateTime);
28
+ }
29
+ function applyArrayFilter(query, items, fieldName, isArrayField) {
30
+ if (!items.length) return;
31
+ if (isArrayField) query.where((x) => x[fieldName].some((f) => items.includes(f)));
32
+ else query.where((x) => items.includes(x[fieldName]));
33
+ }
34
+ function buildPagingResult(total, pagination, itemCount) {
35
+ return {
36
+ cursor: String(pagination.cursor + itemCount),
37
+ next: itemCount >= pagination.size,
38
+ total
39
+ };
40
+ }
41
+ var LocalBaseIndexDB = class extends _ocap_indexdb.BaseIndexDB {
42
+ listTransactions(params = {}) {
43
+ const { addressFilter = {}, paging = {}, timeFilter = {}, typeFilter = {}, assetFilter = {}, factoryFilter = {}, tokenFilter = {}, accountFilter = {}, validityFilter = {}, txFilter = {}, rollupFilter = {}, stakeFilter = {}, delegationFilter = {}, tokenFactoryFilter = {}, includeItxData = false } = params;
44
+ const query = getCollection(this.tx).chain();
45
+ const { sender, receiver, direction = "UNION" } = addressFilter;
46
+ const { types = [] } = typeFilter;
47
+ const { factories = [] } = factoryFilter;
48
+ const { assets = [] } = assetFilter;
49
+ const { tokens = [] } = tokenFilter;
50
+ const { accounts = [] } = accountFilter;
51
+ const { txs = [] } = txFilter;
52
+ const { rollups = [] } = rollupFilter;
53
+ const { stakes = [] } = stakeFilter;
54
+ const { delegations = [] } = delegationFilter;
55
+ const { tokenFactories = [] } = tokenFactoryFilter;
56
+ const pagination = (0, _ocap_indexdb.formatPagination)({
57
+ paging,
58
+ defaultSortField: "time"
59
+ });
60
+ debug$1("listTransactions", {
61
+ sender,
62
+ receiver,
63
+ pagination,
64
+ types,
65
+ factories,
66
+ assets,
67
+ tokens,
68
+ accounts,
69
+ rollups,
70
+ stakes,
71
+ delegations,
72
+ includeItxData
73
+ });
74
+ if (sender && receiver) {
75
+ if (direction === "MUTUAL") query.where((x) => x.sender === sender && x.receiver === receiver || x.sender === receiver && x.receiver === sender);
76
+ else if (direction === "ONE_WAY") query.where((x) => x.sender === sender && x.receiver === receiver);
77
+ else if (direction === "UNION") query.where((x) => x.sender === sender || x.receiver === receiver);
78
+ } else if (sender) query.where((x) => x.sender === sender);
79
+ else if (receiver) query.where((x) => x.receiver === receiver);
80
+ applyArrayFilter(query, types, "type", false);
81
+ applyArrayFilter(query, factories, "factories", true);
82
+ applyArrayFilter(query, assets, "assets", true);
83
+ applyArrayFilter(query, tokens, "tokens", true);
84
+ applyArrayFilter(query, accounts, "accounts", true);
85
+ applyArrayFilter(query, tokenFactories, "tokenFactories", true);
86
+ applyArrayFilter(query, txs, "hash", false);
87
+ applyArrayFilter(query, rollups, "rollups", true);
88
+ applyArrayFilter(query, stakes, "stakes", true);
89
+ applyArrayFilter(query, delegations, "delegations", true);
90
+ applyTimeFilter(query, timeFilter, "time", ["time"]);
91
+ const { validity } = validityFilter;
92
+ if (validity === "VALID") query.where((x) => x.valid === true);
93
+ else if (validity === "INVALID") query.where((x) => x.valid === false);
94
+ let transactions = query.simplesort(pagination.order.field, pagination.order.type === "desc").offset(pagination.cursor).limit(pagination.size).data();
95
+ if (!includeItxData) transactions = transactions.map((tx) => {
96
+ if (tx.tx && tx.tx?.itxJson) return {
97
+ ...tx,
98
+ tx: {
99
+ ...tx.tx,
100
+ itxJson: (0, lodash_omit.default)(tx.tx.itxJson, ["data"])
101
+ }
102
+ };
103
+ return tx;
104
+ });
105
+ const total = query.count();
106
+ return {
107
+ transactions,
108
+ paging: buildPagingResult(total, pagination, transactions.length)
109
+ };
110
+ }
111
+ async getRelatedAddresses(address) {
112
+ let account = await this.account.get(address);
113
+ if (!account) return [];
114
+ const related = [address];
115
+ while (account?.migratedFrom && related.length <= 8) {
116
+ const migratedFrom = account.migratedFrom;
117
+ related.push(migratedFrom);
118
+ account = await this.account.get(migratedFrom);
119
+ }
120
+ return related.filter(Boolean);
121
+ }
122
+ async listAssets(params = {}) {
123
+ const { ownerAddress, factoryAddress, paging, timeFilter = {} } = params;
124
+ if (!ownerAddress && !factoryAddress) return {
125
+ assets: [],
126
+ account: null,
127
+ paging: {
128
+ cursor: "0",
129
+ next: false,
130
+ total: 0
131
+ }
132
+ };
133
+ const pagination = (0, _ocap_indexdb.formatPagination)({
134
+ paging,
135
+ defaultSortField: "renaissanceTime"
136
+ });
137
+ const { field = "renaissanceTime" } = timeFilter;
138
+ if ([
139
+ "genesisTime",
140
+ "renaissanceTime",
141
+ "consumedTime"
142
+ ].includes(field) === false) throw new Error("invalid field specified in timeFilter");
143
+ const query = getCollection(this.asset).chain();
144
+ if (ownerAddress) {
145
+ const possibleOwners = await this.getRelatedAddresses(ownerAddress);
146
+ if (possibleOwners.length) query.where((x) => possibleOwners.includes(x.owner));
147
+ else return {
148
+ assets: [],
149
+ account: null,
150
+ paging: {
151
+ cursor: "0",
152
+ next: false,
153
+ total: 0
154
+ }
155
+ };
156
+ }
157
+ if (factoryAddress) query.where((x) => x.parent === factoryAddress);
158
+ applyTimeFilter(query, timeFilter, "renaissanceTime", [
159
+ "genesisTime",
160
+ "renaissanceTime",
161
+ "consumedTime"
162
+ ]);
163
+ debug$1("listAssets", {
164
+ ownerAddress,
165
+ factoryAddress,
166
+ timeFilter,
167
+ paging,
168
+ pagination
169
+ });
170
+ const assets = query.simplesort(pagination.order.field, pagination.order.type === "desc").offset(pagination.cursor).limit(pagination.size).data();
171
+ const total = query.count();
172
+ return {
173
+ assets,
174
+ account: ownerAddress ? await this.account.get(ownerAddress) : null,
175
+ paging: (0, _ocap_indexdb.formatNextPagination)(total, pagination)
176
+ };
177
+ }
178
+ async listTopAccounts(params = {}) {
179
+ const { paging, tokenAddress } = params;
180
+ const query = getCollection(this.account).chain();
181
+ const pagination = (0, _ocap_indexdb.formatPagination)({
182
+ paging,
183
+ defaultSortField: "balance",
184
+ supportedSortFields: [
185
+ "genesisTime",
186
+ "renaissanceTime",
187
+ "balance",
188
+ "moniker"
189
+ ]
190
+ });
191
+ debug$1("listTopAccounts", {
192
+ tokenAddress,
193
+ paging,
194
+ pagination
195
+ });
196
+ query.where((x) => {
197
+ const owned = x.tokens.find((t) => t.address === tokenAddress);
198
+ return owned ? owned.balance > "0" : false;
199
+ });
200
+ if (pagination.order.field === "balance") {
201
+ const descending = pagination.order.type === "desc";
202
+ query.sort((a, b) => {
203
+ const tokenA = a.tokens.find((t) => t.address === tokenAddress);
204
+ const tokenB = b.tokens.find((t) => t.address === tokenAddress);
205
+ const balanceA = new _ocap_util.BN(tokenA ? tokenA.balance : "0");
206
+ const balanceB = new _ocap_util.BN(tokenB ? tokenB.balance : "0");
207
+ if (balanceB.gt(balanceA)) return descending ? 1 : -1;
208
+ if (balanceB.eq(balanceA)) return 0;
209
+ return descending ? -1 : 1;
210
+ });
211
+ } else query.simplesort(pagination.order.field, pagination.order.type === "desc");
212
+ const accounts = query.offset(pagination.cursor).limit(pagination.size).data();
213
+ const total = query.count();
214
+ accounts.forEach((account) => {
215
+ const acc = account;
216
+ if (Array.isArray(acc.tokens) && acc.tokens.length) acc.tokens = acc.tokens.map((token) => {
217
+ token.decimal = typeof token.decimal === "undefined" ? _ocap_util_lib_constant.DEFAULT_TOKEN_DECIMAL : token.decimal;
218
+ return token;
219
+ });
220
+ });
221
+ return {
222
+ accounts,
223
+ paging: (0, _ocap_indexdb.formatNextPagination)(total, pagination)
224
+ };
225
+ }
226
+ async listTokens(params = {}) {
227
+ const { issuerAddress, paging } = params;
228
+ const conditions = {};
229
+ if (issuerAddress) conditions.issuer = issuerAddress;
230
+ const pagination = (0, _ocap_indexdb.formatPagination)({
231
+ paging,
232
+ defaultSortField: "renaissanceTime",
233
+ supportedSortFields: [
234
+ "genesisTime",
235
+ "renaissanceTime",
236
+ "issuer",
237
+ "symbol",
238
+ "totalSupply"
239
+ ]
240
+ });
241
+ debug$1("listTokens", {
242
+ issuerAddress,
243
+ paging,
244
+ conditions,
245
+ pagination
246
+ });
247
+ let tokens = getCollection(this.token).chain().find(conditions).simplesort(pagination.order.field, pagination.order.type === "desc").offset(pagination.cursor).limit(pagination.size).data();
248
+ tokens = tokens.map((token) => {
249
+ const t = token;
250
+ t.decimal = typeof t.decimal === "undefined" ? _ocap_util_lib_constant.DEFAULT_TOKEN_DECIMAL : t.decimal;
251
+ return t;
252
+ });
253
+ const total = this.token.count(Object.keys(conditions).length ? conditions : void 0);
254
+ return {
255
+ tokens,
256
+ paging: (0, _ocap_indexdb.formatNextPagination)(total, pagination)
257
+ };
258
+ }
259
+ async listTokenFactories(params = {}) {
260
+ const { owner, reserveAddress, tokenAddress, paging } = params;
261
+ const conditions = {};
262
+ if (owner) conditions.owner = owner;
263
+ if (reserveAddress) conditions.reserveAddress = reserveAddress;
264
+ if (tokenAddress) conditions.tokenAddress = tokenAddress;
265
+ const pagination = (0, _ocap_indexdb.formatPagination)({
266
+ paging,
267
+ defaultSortField: "renaissanceTime",
268
+ supportedSortFields: [
269
+ "genesisTime",
270
+ "renaissanceTime",
271
+ "reserveBalance",
272
+ "currentSupply"
273
+ ]
274
+ });
275
+ debug$1("listTokenFactories", {
276
+ owner,
277
+ reserveAddress,
278
+ tokenAddress,
279
+ paging,
280
+ conditions,
281
+ pagination
282
+ });
283
+ return {
284
+ tokenFactories: getCollection(this.tokenFactory).chain().find(conditions).simplesort(pagination.order.field, pagination.order.type === "desc").offset(pagination.cursor).limit(pagination.size).data(),
285
+ paging: (0, _ocap_indexdb.formatNextPagination)(this.tokenFactory.count(Object.keys(conditions).length ? conditions : void 0), pagination)
286
+ };
287
+ }
288
+ async listFactories(params = {}) {
289
+ const { ownerAddress, addressList, paging } = params;
290
+ const conditions = {};
291
+ if (ownerAddress) conditions.owner = ownerAddress;
292
+ if (Array.isArray(addressList) && addressList.length > 0) {
293
+ if (addressList.length > MAX_REQUEST_FACTORY_ADDRESS_SIZE) throw new Error(`The length of 'addressList' cannot exceed the length of ${MAX_REQUEST_FACTORY_ADDRESS_SIZE}`);
294
+ conditions.address = { $in: addressList };
295
+ }
296
+ const pagination = (0, _ocap_indexdb.formatPagination)({
297
+ paging,
298
+ defaultSortField: "renaissanceTime",
299
+ supportedSortFields: [
300
+ "genesisTime",
301
+ "renaissanceTime",
302
+ "owner",
303
+ "numMinted",
304
+ "limit",
305
+ "name"
306
+ ]
307
+ });
308
+ debug$1("listFactories", {
309
+ ownerAddress,
310
+ paging,
311
+ conditions,
312
+ pagination
313
+ });
314
+ return {
315
+ factories: getCollection(this.factory).chain().find(conditions).simplesort(pagination.order.field, pagination.order.type === "desc").offset(pagination.cursor).limit(pagination.size).data(),
316
+ paging: (0, _ocap_indexdb.formatNextPagination)(this.factory.count(Object.keys(conditions).length ? conditions : void 0), pagination)
317
+ };
318
+ }
319
+ listStakes(params = {}) {
320
+ const { addressFilter = {}, paging = {}, timeFilter = {}, assetFilter = {} } = params;
321
+ const query = getCollection(this.stake).chain();
322
+ const { sender, receiver } = addressFilter;
323
+ const { assets = [] } = assetFilter;
324
+ const pagination = (0, _ocap_indexdb.formatPagination)({
325
+ paging,
326
+ defaultSortField: "renaissanceTime",
327
+ supportedSortFields: ["genesisTime", "renaissanceTime"]
328
+ });
329
+ if (sender && receiver) query.where((x) => x.sender === sender && x.receiver === receiver);
330
+ else if (sender) query.where((x) => x.sender === sender);
331
+ else if (receiver) query.where((x) => x.receiver === receiver);
332
+ applyArrayFilter(query, assets, "assets", true);
333
+ applyTimeFilter(query, timeFilter, "renaissanceTime", ["genesisTime", "renaissanceTime"]);
334
+ const stakes = query.simplesort(pagination.order.field, pagination.order.type === "desc").offset(pagination.cursor).limit(pagination.size).data();
335
+ return {
336
+ stakes,
337
+ paging: buildPagingResult(query.count(), pagination, stakes.length)
338
+ };
339
+ }
340
+ async listRollups(params = {}) {
341
+ const { paging, tokenAddress = "", erc20TokenAddress = "", foreignTokenAddress = "" } = params;
342
+ const pagination = (0, _ocap_indexdb.formatPagination)({
343
+ paging,
344
+ defaultSortField: "renaissanceTime",
345
+ supportedSortFields: ["genesisTime", "renaissanceTime"]
346
+ });
347
+ const query = getCollection(this.rollup).chain();
348
+ if (tokenAddress) query.where((x) => x.tokenAddress === tokenAddress);
349
+ const foreignTokenAddr = foreignTokenAddress || erc20TokenAddress;
350
+ if (erc20TokenAddress) query.where((x) => x.foreignToken.contractAddress === (0, _arcblock_did_lib_type.toChecksumAddress)(foreignTokenAddr));
351
+ debug$1("listRollups", {
352
+ paging,
353
+ pagination
354
+ });
355
+ return {
356
+ rollups: query.simplesort(pagination.order.field, pagination.order.type === "desc").offset(pagination.cursor).limit(pagination.size).data(),
357
+ paging: (0, _ocap_indexdb.formatNextPagination)(query.count(), pagination)
358
+ };
359
+ }
360
+ listRollupBlocks(params = {}) {
361
+ const { paging = {}, rollupAddress = "", tokenAddress = "", proposer = "", height = "", timeFilter = {}, txFilter = {}, validatorFilter = {} } = params;
362
+ const query = getCollection(this.rollupBlock).chain();
363
+ const { txs = [] } = txFilter;
364
+ const { validators = [] } = validatorFilter;
365
+ const pagination = (0, _ocap_indexdb.formatPagination)({
366
+ paging,
367
+ defaultSortField: "genesisTime",
368
+ supportedSortFields: ["genesisTime", "renaissanceTime"]
369
+ });
370
+ if (rollupAddress) query.where((x) => x.rollup === rollupAddress);
371
+ if (Number(height) > 0) query.where((x) => Number(x.height) === Number(height));
372
+ if (proposer) query.where((x) => x.proposer === proposer);
373
+ if (tokenAddress) query.where((x) => x.tokenInfo.address === tokenAddress);
374
+ applyArrayFilter(query, txs, "txs", true);
375
+ applyArrayFilter(query, validators, "validators", true);
376
+ applyTimeFilter(query, timeFilter, "genesisTime", ["genesisTime", "renaissanceTime"]);
377
+ const blocks = query.simplesort(pagination.order.field, pagination.order.type === "desc").offset(pagination.cursor).limit(pagination.size).data();
378
+ return {
379
+ blocks,
380
+ paging: buildPagingResult(query.count(), pagination, blocks.length)
381
+ };
382
+ }
383
+ listRollupValidators(params = {}) {
384
+ const { paging = {}, rollupAddress = "" } = params;
385
+ const query = getCollection(this.rollupValidator).chain();
386
+ const pagination = (0, _ocap_indexdb.formatPagination)({
387
+ paging,
388
+ defaultSortField: "genesisTime",
389
+ supportedSortFields: [
390
+ "joinTime",
391
+ "leaveTime",
392
+ "genesisTime",
393
+ "renaissanceTime"
394
+ ]
395
+ });
396
+ if (rollupAddress) query.where((x) => x.rollup === rollupAddress);
397
+ const validators = query.simplesort(pagination.order.field, pagination.order.type === "desc").offset(pagination.cursor).limit(pagination.size).data();
398
+ return {
399
+ validators,
400
+ paging: buildPagingResult(query.count(), pagination, validators.length)
401
+ };
402
+ }
403
+ listDelegations(params = {}) {
404
+ const { from, to, paging = {}, timeFilter = {} } = params;
405
+ if (!from && !to) return {
406
+ delegations: [],
407
+ paging: {
408
+ cursor: "0",
409
+ next: false,
410
+ total: 0
411
+ }
412
+ };
413
+ const query = getCollection(this.delegation).chain();
414
+ const pagination = (0, _ocap_indexdb.formatPagination)({
415
+ paging,
416
+ defaultSortField: "renaissanceTime",
417
+ supportedSortFields: ["genesisTime", "renaissanceTime"]
418
+ });
419
+ if (from && to) query.where((x) => x.from === from && x.to === to);
420
+ else if (from) query.where((x) => x.from === from);
421
+ else if (to) query.where((x) => x.to === to);
422
+ applyTimeFilter(query, timeFilter, "renaissanceTime", ["genesisTime", "renaissanceTime"]);
423
+ const delegations = query.simplesort(pagination.order.field, pagination.order.type === "desc").offset(pagination.cursor).limit(pagination.size).data();
424
+ const total = query.count();
425
+ return {
426
+ delegations: delegations.map((d) => (0, _ocap_indexdb.formatDelegationAfterRead)(d)),
427
+ paging: buildPagingResult(total, pagination, delegations.length)
428
+ };
429
+ }
430
+ };
431
+ var base_default = LocalBaseIndexDB;
432
+
433
+ //#endregion
434
+ exports.default = base_default;
@@ -0,0 +1,20 @@
1
+ import { BaseIndexDB } from "@ocap/indexdb";
2
+ import { IIndexDB, IListAccountsResult, IListAssetsResult, IListDelegationsResult, IListFactoriesResult, IListRollupBlocksResult, IListRollupValidatorsResult, IListRollupsResult, IListStakesResult, IListTokenFactoriesResult, IListTokensResult, IListTransactionsResult, TRequestListAssets, TRequestListDelegations, TRequestListFactories, TRequestListRollupBlocks, TRequestListRollupValidators, TRequestListRollups, TRequestListStakes, TRequestListTokenFactories, TRequestListTokens, TRequestListTopAccounts, TRequestListTransactions } from "@ocap/types";
3
+
4
+ //#region src/db/base.d.ts
5
+ declare class LocalBaseIndexDB extends BaseIndexDB implements IIndexDB {
6
+ listTransactions(params?: Partial<TRequestListTransactions>): IListTransactionsResult;
7
+ getRelatedAddresses(address: string): Promise<string[]>;
8
+ listAssets(params?: Partial<TRequestListAssets>): Promise<IListAssetsResult>;
9
+ listTopAccounts(params?: Partial<TRequestListTopAccounts>): Promise<IListAccountsResult>;
10
+ listTokens(params?: Partial<TRequestListTokens>): Promise<IListTokensResult>;
11
+ listTokenFactories(params?: Partial<TRequestListTokenFactories>): Promise<IListTokenFactoriesResult>;
12
+ listFactories(params?: Partial<TRequestListFactories>): Promise<IListFactoriesResult>;
13
+ listStakes(params?: Partial<TRequestListStakes>): IListStakesResult;
14
+ listRollups(params?: Partial<TRequestListRollups>): Promise<IListRollupsResult>;
15
+ listRollupBlocks(params?: Partial<TRequestListRollupBlocks>): IListRollupBlocksResult;
16
+ listRollupValidators(params?: Partial<TRequestListRollupValidators>): IListRollupValidatorsResult;
17
+ listDelegations(params?: Partial<TRequestListDelegations>): IListDelegationsResult;
18
+ }
19
+ //#endregion
20
+ export { LocalBaseIndexDB as default };
@@ -0,0 +1,40 @@
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
3
+ const require_package = require('../package.cjs');
4
+ const require_table_base = require('../table/base.cjs');
5
+ const require_table_transaction = require('../table/transaction.cjs');
6
+ const require_db_base = require('./base.cjs');
7
+ let _ocap_util_lib_md5 = require("@ocap/util/lib/md5");
8
+ let lokijs = require("lokijs");
9
+ lokijs = require_rolldown_runtime.__toESM(lokijs);
10
+
11
+ //#region src/db/index.ts
12
+ let instanceCounter = 0;
13
+ var MemoryIndexDB = class extends require_db_base.default {
14
+ constructor() {
15
+ super();
16
+ this.name = require_package.name;
17
+ this.version = require_package.version;
18
+ this.md5 = _ocap_util_lib_md5.md5;
19
+ instanceCounter += 1;
20
+ this.db = new lokijs.default(`ocap-memory-indexdb-${instanceCounter}.db`);
21
+ this.account = new require_table_base.default("account", "address", this.db);
22
+ this.asset = new require_table_base.default("asset", "address", this.db);
23
+ this.delegation = new require_table_base.default("delegation", "address", this.db);
24
+ this.tx = new require_table_transaction.default("tx", "hash", this.db);
25
+ this.factory = new require_table_base.default("factory", "address", this.db);
26
+ this.token = new require_table_base.default("token", "address", this.db);
27
+ this.stake = new require_table_base.default("stake", "address", this.db);
28
+ this.rollup = new require_table_base.default("rollup", "address", this.db);
29
+ this.rollupBlock = new require_table_base.default("rollupBlock", "hash", this.db);
30
+ this.rollupValidator = new require_table_base.default("rollupValidator", "address", this.db);
31
+ this.tokenDistribution = new require_table_base.default("tokenDistribution", "tokenAddress", this.db);
32
+ this.balance = new require_table_base.default("balance", ["address", "tokenAddress"], this.db);
33
+ this.tokenFactory = new require_table_base.default("tokenFactory", "address", this.db);
34
+ this.attachReadyListeners();
35
+ }
36
+ };
37
+ var db_default = MemoryIndexDB;
38
+
39
+ //#endregion
40
+ exports.default = db_default;
@@ -0,0 +1,27 @@
1
+ import LocalBaseIndexDB from "./base.cjs";
2
+ import { IIndexDB, IIndexTable, IndexTableTypeMap } from "@ocap/types";
3
+ import { md5 } from "@ocap/util/lib/md5";
4
+ import Lokijs from "lokijs";
5
+
6
+ //#region src/db/index.d.ts
7
+ declare class MemoryIndexDB extends LocalBaseIndexDB implements IIndexDB {
8
+ name: string;
9
+ version: string;
10
+ md5: typeof md5;
11
+ db: Lokijs;
12
+ tx: IIndexTable<IndexTableTypeMap['tx']>;
13
+ account: IIndexTable<IndexTableTypeMap['account']>;
14
+ asset: IIndexTable<IndexTableTypeMap['asset']>;
15
+ token: IIndexTable<IndexTableTypeMap['token']>;
16
+ factory: IIndexTable<IndexTableTypeMap['factory']>;
17
+ stake: IIndexTable<IndexTableTypeMap['stake']>;
18
+ delegation: IIndexTable<IndexTableTypeMap['delegation']>;
19
+ rollup: IIndexTable<IndexTableTypeMap['rollup']>;
20
+ rollupBlock: IIndexTable<IndexTableTypeMap['rollupBlock']>;
21
+ rollupValidator: IIndexTable<IndexTableTypeMap['rollupValidator']>;
22
+ tokenDistribution: IIndexTable<IndexTableTypeMap['tokenDistribution']>;
23
+ tokenFactory: IIndexTable<IndexTableTypeMap['tokenFactory']>;
24
+ constructor();
25
+ }
26
+ //#endregion
27
+ export { MemoryIndexDB as default };
package/lib/main.cjs ADDED
@@ -0,0 +1,10 @@
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ const require_db_base = require('./db/base.cjs');
3
+ const require_db_index = require('./db/index.cjs');
4
+
5
+ //#region src/main.ts
6
+ var main_default = require_db_index.default;
7
+
8
+ //#endregion
9
+ exports.LocalBaseIndexDB = require_db_base.default;
10
+ exports.default = main_default;
package/lib/main.d.cts ADDED
@@ -0,0 +1,3 @@
1
+ import LocalBaseIndexDB from "./db/base.cjs";
2
+ import MemoryIndexDB from "./db/index.cjs";
3
+ export { LocalBaseIndexDB, MemoryIndexDB as default };
@@ -0,0 +1,18 @@
1
+
2
+ //#region package.json
3
+ var name = "@ocap/indexdb-memory";
4
+ var version = "1.20.2";
5
+
6
+ //#endregion
7
+ Object.defineProperty(exports, 'name', {
8
+ enumerable: true,
9
+ get: function () {
10
+ return name;
11
+ }
12
+ });
13
+ Object.defineProperty(exports, 'version', {
14
+ enumerable: true,
15
+ get: function () {
16
+ return version;
17
+ }
18
+ });
@@ -0,0 +1,55 @@
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
3
+ const require_package = require('../package.cjs');
4
+ let _ocap_indexdb = require("@ocap/indexdb");
5
+ let debug = require("debug");
6
+ debug = require_rolldown_runtime.__toESM(debug);
7
+
8
+ //#region src/table/base.ts
9
+ const debug$1 = (0, debug.default)(require_package.name);
10
+ var MemoryIndex = class MemoryIndex extends _ocap_indexdb.BaseIndex {
11
+ /**
12
+ * @param name table name
13
+ * @param uniqIndex primary key(s)
14
+ * @param db LokiJS database instance
15
+ */
16
+ constructor(name$1, uniqIndex, db) {
17
+ super(name$1, uniqIndex);
18
+ if (!db) throw new Error("db is required for MemoryIndex");
19
+ this.collection = db.addCollection(name$1, {
20
+ unique: [this.primaryKey],
21
+ clone: true
22
+ });
23
+ this.markReady();
24
+ }
25
+ count(...args) {
26
+ return this.collection.count(...args);
27
+ }
28
+ _insert(row) {
29
+ debug$1(`insert ${this.name}`, row);
30
+ const id = this.generatePrimaryKey(row);
31
+ return this.collection.insert({
32
+ [this.primaryKey]: id,
33
+ ...row
34
+ });
35
+ }
36
+ _get(key) {
37
+ const id = this.generatePrimaryKey(key);
38
+ return key ? this.collection.by(this.primaryKey, id) ?? null : null;
39
+ }
40
+ _update(key, updates) {
41
+ const id = this.generatePrimaryKey(key);
42
+ const doc = MemoryIndex.prototype._get.call(this, id);
43
+ if (!doc) throw new Error(`${this.name} does not exists: ${key}`);
44
+ Object.assign(doc, updates);
45
+ this.collection.update(doc);
46
+ return doc;
47
+ }
48
+ _reset() {
49
+ this.collection.removeWhere({});
50
+ }
51
+ };
52
+ var base_default = MemoryIndex;
53
+
54
+ //#endregion
55
+ exports.default = base_default;
@@ -0,0 +1,22 @@
1
+ import { BaseIndex } from "@ocap/indexdb";
2
+ import { IIndexTable } from "@ocap/types";
3
+ import Lokijs from "lokijs";
4
+
5
+ //#region src/table/base.d.ts
6
+ type UniqueIndex = string | string[];
7
+ declare class MemoryIndex<T = unknown> extends BaseIndex<T> implements IIndexTable<T> {
8
+ collection: Lokijs.Collection<T & Record<string, unknown>>;
9
+ /**
10
+ * @param name table name
11
+ * @param uniqIndex primary key(s)
12
+ * @param db LokiJS database instance
13
+ */
14
+ constructor(name: string, uniqIndex: UniqueIndex, db: Lokijs);
15
+ count(...args: Parameters<Lokijs.Collection['count']>): number;
16
+ _insert(row: Record<string, unknown>): T & Record<string, unknown>;
17
+ _get(key: string | Record<string, unknown>): (T & Record<string, unknown>) | null;
18
+ _update(key: string | Record<string, unknown>, updates: Record<string, unknown>): T & Record<string, unknown>;
19
+ _reset(): void;
20
+ }
21
+ //#endregion
22
+ export { MemoryIndex as default };