@ocap/indexdb-memory 1.29.14 → 1.29.16

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/esm/db/base.d.mts CHANGED
@@ -1,5 +1,5 @@
1
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";
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, TSearchResult } from "@ocap/types";
3
3
 
4
4
  //#region src/db/base.d.ts
5
5
  declare class LocalBaseIndexDB extends BaseIndexDB implements IIndexDB {
@@ -15,6 +15,10 @@ declare class LocalBaseIndexDB extends BaseIndexDB implements IIndexDB {
15
15
  listRollupBlocks(params?: Partial<TRequestListRollupBlocks>): IListRollupBlocksResult;
16
16
  listRollupValidators(params?: Partial<TRequestListRollupValidators>): IListRollupValidatorsResult;
17
17
  listDelegations(params?: Partial<TRequestListDelegations>): IListDelegationsResult;
18
+ /**
19
+ * Search entities by semantic fields (moniker, name, symbol, description)
20
+ */
21
+ search(keyword: string): TSearchResult[];
18
22
  }
19
23
  //#endregion
20
24
  export { LocalBaseIndexDB as default };
package/esm/db/base.mjs CHANGED
@@ -9,6 +9,14 @@ import omit from "lodash/omit.js";
9
9
  //#region src/db/base.ts
10
10
  const debug = debugFactory(name);
11
11
  const MAX_REQUEST_FACTORY_ADDRESS_SIZE = 100;
12
+ const SEARCH_LIMIT_PER_TABLE = 5;
13
+ /** Score a match: 3=exact, 2=prefix, 1=contains */
14
+ function scoreMatch(fieldValue, keyword) {
15
+ const lower = fieldValue.toLowerCase();
16
+ if (lower === keyword) return 3;
17
+ if (lower.startsWith(keyword)) return 2;
18
+ return 1;
19
+ }
12
20
  /** Helper to safely get collection from IIndexTable */
13
21
  function getCollection(table) {
14
22
  return table.collection;
@@ -435,6 +443,191 @@ var LocalBaseIndexDB = class extends BaseIndexDB {
435
443
  paging: buildPagingResult(total, pagination, delegations.length)
436
444
  };
437
445
  }
446
+ /**
447
+ * Search entities by semantic fields (moniker, name, symbol, description)
448
+ */
449
+ search(keyword) {
450
+ const trimmed = keyword?.trim();
451
+ if (!trimmed) return [];
452
+ const lowerKw = trimmed.toLowerCase();
453
+ const typePriority = {
454
+ token: 0,
455
+ account: 1,
456
+ factory: 2,
457
+ asset: 3,
458
+ tokenFactory: 4,
459
+ stake: 5
460
+ };
461
+ const results = [];
462
+ const tokenMatches = getCollection(this.token).chain().where((x) => {
463
+ const name$1 = x.name;
464
+ const symbol = x.symbol;
465
+ const description = x.description;
466
+ return (name$1 ? name$1.toLowerCase().includes(lowerKw) : false) || (symbol ? symbol.toLowerCase().includes(lowerKw) : false) || (description ? description.toLowerCase().includes(lowerKw) : false);
467
+ }).limit(SEARCH_LIMIT_PER_TABLE).data();
468
+ for (const row of tokenMatches) {
469
+ const nameScore = row.name ? scoreMatch(row.name, lowerKw) : 0;
470
+ const symbolScore = row.symbol ? scoreMatch(row.symbol, lowerKw) : 0;
471
+ const descScore = row.description ? scoreMatch(row.description, lowerKw) : 0;
472
+ const score = Math.max(nameScore, symbolScore, descScore);
473
+ const title = nameScore >= symbolScore ? row.name || row.symbol || "" : row.symbol || row.name || "";
474
+ results.push({
475
+ type: "token",
476
+ id: row.address,
477
+ title,
478
+ score,
479
+ priority: typePriority.token
480
+ });
481
+ }
482
+ const accountMatches = getCollection(this.account).chain().where((x) => {
483
+ const moniker = x.moniker;
484
+ return moniker ? moniker.toLowerCase().includes(lowerKw) : false;
485
+ }).limit(SEARCH_LIMIT_PER_TABLE).data();
486
+ for (const row of accountMatches) {
487
+ const score = scoreMatch(row.moniker, lowerKw);
488
+ results.push({
489
+ type: "account",
490
+ id: row.address,
491
+ title: row.moniker || "",
492
+ score,
493
+ priority: typePriority.account
494
+ });
495
+ }
496
+ const factoryMatches = getCollection(this.factory).chain().where((x) => {
497
+ const name$1 = x.name;
498
+ const description = x.description;
499
+ return (name$1 ? name$1.toLowerCase().includes(lowerKw) : false) || (description ? description.toLowerCase().includes(lowerKw) : false);
500
+ }).limit(SEARCH_LIMIT_PER_TABLE).data();
501
+ for (const row of factoryMatches) {
502
+ const nameScore = row.name ? scoreMatch(row.name, lowerKw) : 0;
503
+ const descScore = row.description ? scoreMatch(row.description, lowerKw) : 0;
504
+ const score = Math.max(nameScore, descScore);
505
+ results.push({
506
+ type: "factory",
507
+ id: row.address,
508
+ title: row.name || "",
509
+ score,
510
+ priority: typePriority.factory
511
+ });
512
+ }
513
+ const assetMatches = getCollection(this.asset).chain().where((x) => {
514
+ const moniker = x.moniker;
515
+ const tags = x.tags;
516
+ return (moniker ? moniker.toLowerCase().includes(lowerKw) : false) || Array.isArray(tags) && tags.some((t) => t.toLowerCase().includes(lowerKw));
517
+ }).limit(SEARCH_LIMIT_PER_TABLE).data();
518
+ for (const row of assetMatches) {
519
+ const monikerScore = row.moniker ? scoreMatch(row.moniker, lowerKw) : 0;
520
+ const tags = row.tags;
521
+ const tagScore = Array.isArray(tags) ? Math.max(0, ...tags.map((t) => scoreMatch(t, lowerKw))) : 0;
522
+ const score = Math.max(monikerScore, tagScore);
523
+ results.push({
524
+ type: "asset",
525
+ id: row.address,
526
+ title: row.moniker || "",
527
+ score,
528
+ priority: typePriority.asset
529
+ });
530
+ }
531
+ const tokenFactoryMatches = getCollection(this.tokenFactory).chain().where((x) => {
532
+ const name$1 = x.name;
533
+ const moniker = x.moniker;
534
+ return (name$1 ? name$1.toLowerCase().includes(lowerKw) : false) || (moniker ? moniker.toLowerCase().includes(lowerKw) : false);
535
+ }).limit(SEARCH_LIMIT_PER_TABLE).data();
536
+ for (const row of tokenFactoryMatches) {
537
+ const nameScore = row.name ? scoreMatch(row.name, lowerKw) : 0;
538
+ const monikerScore = row.moniker ? scoreMatch(row.moniker, lowerKw) : 0;
539
+ const score = Math.max(nameScore, monikerScore);
540
+ const title = nameScore >= monikerScore ? row.name || row.moniker || "" : row.moniker || row.name || "";
541
+ results.push({
542
+ type: "tokenFactory",
543
+ id: row.address,
544
+ title,
545
+ score,
546
+ priority: typePriority.tokenFactory
547
+ });
548
+ }
549
+ const stakeMatches = getCollection(this.stake).chain().where((x) => {
550
+ const message = x.message;
551
+ return message ? message.toLowerCase().includes(lowerKw) : false;
552
+ }).limit(SEARCH_LIMIT_PER_TABLE).data();
553
+ for (const row of stakeMatches) {
554
+ const score = scoreMatch(row.message, lowerKw);
555
+ results.push({
556
+ type: "stake",
557
+ id: row.address,
558
+ title: row.message || "",
559
+ score,
560
+ priority: typePriority.stake
561
+ });
562
+ }
563
+ if (/^(did:abt:)?(z[1-9A-HJ-NP-Za-km-z]|0x[0-9a-fA-F])/.test(trimmed)) {
564
+ const didPrefix = trimmed.replace(/^did:abt:/i, "");
565
+ if (didPrefix.length >= 4) {
566
+ const seen = new Set(results.map((r) => `${r.type}:${r.id}`));
567
+ const titleField = {
568
+ account: "moniker",
569
+ token: "name",
570
+ asset: "moniker",
571
+ factory: "name",
572
+ tokenFactory: "name",
573
+ stake: "message"
574
+ };
575
+ const collections = [
576
+ {
577
+ collection: this.account,
578
+ type: "account"
579
+ },
580
+ {
581
+ collection: this.token,
582
+ type: "token"
583
+ },
584
+ {
585
+ collection: this.asset,
586
+ type: "asset"
587
+ },
588
+ {
589
+ collection: this.factory,
590
+ type: "factory"
591
+ },
592
+ {
593
+ collection: this.tokenFactory,
594
+ type: "tokenFactory"
595
+ },
596
+ {
597
+ collection: this.stake,
598
+ type: "stake"
599
+ }
600
+ ];
601
+ for (const { collection, type } of collections) {
602
+ const matches = getCollection(collection).chain().where((x) => {
603
+ const addr = x.address;
604
+ return addr ? addr.startsWith(didPrefix) : false;
605
+ }).limit(SEARCH_LIMIT_PER_TABLE).data();
606
+ for (const row of matches) {
607
+ const addr = row.address;
608
+ const key = `${type}:${addr}`;
609
+ if (!seen.has(key)) {
610
+ seen.add(key);
611
+ const title = row[titleField[type]] || addr;
612
+ results.push({
613
+ type,
614
+ id: addr,
615
+ title,
616
+ score: 2,
617
+ priority: typePriority[type] ?? 99
618
+ });
619
+ }
620
+ }
621
+ }
622
+ }
623
+ }
624
+ results.sort((a, b) => b.score - a.score || a.priority - b.priority);
625
+ return results.map(({ type, id, title }) => ({
626
+ type,
627
+ id,
628
+ title
629
+ }));
630
+ }
438
631
  };
439
632
  var base_default = LocalBaseIndexDB;
440
633
 
package/lib/db/base.cjs CHANGED
@@ -13,6 +13,14 @@ lodash_omit = require_rolldown_runtime.__toESM(lodash_omit);
13
13
  //#region src/db/base.ts
14
14
  const debug$1 = (0, debug.default)(require_package.name);
15
15
  const MAX_REQUEST_FACTORY_ADDRESS_SIZE = 100;
16
+ const SEARCH_LIMIT_PER_TABLE = 5;
17
+ /** Score a match: 3=exact, 2=prefix, 1=contains */
18
+ function scoreMatch(fieldValue, keyword) {
19
+ const lower = fieldValue.toLowerCase();
20
+ if (lower === keyword) return 3;
21
+ if (lower.startsWith(keyword)) return 2;
22
+ return 1;
23
+ }
16
24
  /** Helper to safely get collection from IIndexTable */
17
25
  function getCollection(table) {
18
26
  return table.collection;
@@ -439,6 +447,191 @@ var LocalBaseIndexDB = class extends _ocap_indexdb.BaseIndexDB {
439
447
  paging: buildPagingResult(total, pagination, delegations.length)
440
448
  };
441
449
  }
450
+ /**
451
+ * Search entities by semantic fields (moniker, name, symbol, description)
452
+ */
453
+ search(keyword) {
454
+ const trimmed = keyword?.trim();
455
+ if (!trimmed) return [];
456
+ const lowerKw = trimmed.toLowerCase();
457
+ const typePriority = {
458
+ token: 0,
459
+ account: 1,
460
+ factory: 2,
461
+ asset: 3,
462
+ tokenFactory: 4,
463
+ stake: 5
464
+ };
465
+ const results = [];
466
+ const tokenMatches = getCollection(this.token).chain().where((x) => {
467
+ const name$1 = x.name;
468
+ const symbol = x.symbol;
469
+ const description = x.description;
470
+ return (name$1 ? name$1.toLowerCase().includes(lowerKw) : false) || (symbol ? symbol.toLowerCase().includes(lowerKw) : false) || (description ? description.toLowerCase().includes(lowerKw) : false);
471
+ }).limit(SEARCH_LIMIT_PER_TABLE).data();
472
+ for (const row of tokenMatches) {
473
+ const nameScore = row.name ? scoreMatch(row.name, lowerKw) : 0;
474
+ const symbolScore = row.symbol ? scoreMatch(row.symbol, lowerKw) : 0;
475
+ const descScore = row.description ? scoreMatch(row.description, lowerKw) : 0;
476
+ const score = Math.max(nameScore, symbolScore, descScore);
477
+ const title = nameScore >= symbolScore ? row.name || row.symbol || "" : row.symbol || row.name || "";
478
+ results.push({
479
+ type: "token",
480
+ id: row.address,
481
+ title,
482
+ score,
483
+ priority: typePriority.token
484
+ });
485
+ }
486
+ const accountMatches = getCollection(this.account).chain().where((x) => {
487
+ const moniker = x.moniker;
488
+ return moniker ? moniker.toLowerCase().includes(lowerKw) : false;
489
+ }).limit(SEARCH_LIMIT_PER_TABLE).data();
490
+ for (const row of accountMatches) {
491
+ const score = scoreMatch(row.moniker, lowerKw);
492
+ results.push({
493
+ type: "account",
494
+ id: row.address,
495
+ title: row.moniker || "",
496
+ score,
497
+ priority: typePriority.account
498
+ });
499
+ }
500
+ const factoryMatches = getCollection(this.factory).chain().where((x) => {
501
+ const name$1 = x.name;
502
+ const description = x.description;
503
+ return (name$1 ? name$1.toLowerCase().includes(lowerKw) : false) || (description ? description.toLowerCase().includes(lowerKw) : false);
504
+ }).limit(SEARCH_LIMIT_PER_TABLE).data();
505
+ for (const row of factoryMatches) {
506
+ const nameScore = row.name ? scoreMatch(row.name, lowerKw) : 0;
507
+ const descScore = row.description ? scoreMatch(row.description, lowerKw) : 0;
508
+ const score = Math.max(nameScore, descScore);
509
+ results.push({
510
+ type: "factory",
511
+ id: row.address,
512
+ title: row.name || "",
513
+ score,
514
+ priority: typePriority.factory
515
+ });
516
+ }
517
+ const assetMatches = getCollection(this.asset).chain().where((x) => {
518
+ const moniker = x.moniker;
519
+ const tags = x.tags;
520
+ return (moniker ? moniker.toLowerCase().includes(lowerKw) : false) || Array.isArray(tags) && tags.some((t) => t.toLowerCase().includes(lowerKw));
521
+ }).limit(SEARCH_LIMIT_PER_TABLE).data();
522
+ for (const row of assetMatches) {
523
+ const monikerScore = row.moniker ? scoreMatch(row.moniker, lowerKw) : 0;
524
+ const tags = row.tags;
525
+ const tagScore = Array.isArray(tags) ? Math.max(0, ...tags.map((t) => scoreMatch(t, lowerKw))) : 0;
526
+ const score = Math.max(monikerScore, tagScore);
527
+ results.push({
528
+ type: "asset",
529
+ id: row.address,
530
+ title: row.moniker || "",
531
+ score,
532
+ priority: typePriority.asset
533
+ });
534
+ }
535
+ const tokenFactoryMatches = getCollection(this.tokenFactory).chain().where((x) => {
536
+ const name$1 = x.name;
537
+ const moniker = x.moniker;
538
+ return (name$1 ? name$1.toLowerCase().includes(lowerKw) : false) || (moniker ? moniker.toLowerCase().includes(lowerKw) : false);
539
+ }).limit(SEARCH_LIMIT_PER_TABLE).data();
540
+ for (const row of tokenFactoryMatches) {
541
+ const nameScore = row.name ? scoreMatch(row.name, lowerKw) : 0;
542
+ const monikerScore = row.moniker ? scoreMatch(row.moniker, lowerKw) : 0;
543
+ const score = Math.max(nameScore, monikerScore);
544
+ const title = nameScore >= monikerScore ? row.name || row.moniker || "" : row.moniker || row.name || "";
545
+ results.push({
546
+ type: "tokenFactory",
547
+ id: row.address,
548
+ title,
549
+ score,
550
+ priority: typePriority.tokenFactory
551
+ });
552
+ }
553
+ const stakeMatches = getCollection(this.stake).chain().where((x) => {
554
+ const message = x.message;
555
+ return message ? message.toLowerCase().includes(lowerKw) : false;
556
+ }).limit(SEARCH_LIMIT_PER_TABLE).data();
557
+ for (const row of stakeMatches) {
558
+ const score = scoreMatch(row.message, lowerKw);
559
+ results.push({
560
+ type: "stake",
561
+ id: row.address,
562
+ title: row.message || "",
563
+ score,
564
+ priority: typePriority.stake
565
+ });
566
+ }
567
+ if (/^(did:abt:)?(z[1-9A-HJ-NP-Za-km-z]|0x[0-9a-fA-F])/.test(trimmed)) {
568
+ const didPrefix = trimmed.replace(/^did:abt:/i, "");
569
+ if (didPrefix.length >= 4) {
570
+ const seen = new Set(results.map((r) => `${r.type}:${r.id}`));
571
+ const titleField = {
572
+ account: "moniker",
573
+ token: "name",
574
+ asset: "moniker",
575
+ factory: "name",
576
+ tokenFactory: "name",
577
+ stake: "message"
578
+ };
579
+ const collections = [
580
+ {
581
+ collection: this.account,
582
+ type: "account"
583
+ },
584
+ {
585
+ collection: this.token,
586
+ type: "token"
587
+ },
588
+ {
589
+ collection: this.asset,
590
+ type: "asset"
591
+ },
592
+ {
593
+ collection: this.factory,
594
+ type: "factory"
595
+ },
596
+ {
597
+ collection: this.tokenFactory,
598
+ type: "tokenFactory"
599
+ },
600
+ {
601
+ collection: this.stake,
602
+ type: "stake"
603
+ }
604
+ ];
605
+ for (const { collection, type } of collections) {
606
+ const matches = getCollection(collection).chain().where((x) => {
607
+ const addr = x.address;
608
+ return addr ? addr.startsWith(didPrefix) : false;
609
+ }).limit(SEARCH_LIMIT_PER_TABLE).data();
610
+ for (const row of matches) {
611
+ const addr = row.address;
612
+ const key = `${type}:${addr}`;
613
+ if (!seen.has(key)) {
614
+ seen.add(key);
615
+ const title = row[titleField[type]] || addr;
616
+ results.push({
617
+ type,
618
+ id: addr,
619
+ title,
620
+ score: 2,
621
+ priority: typePriority[type] ?? 99
622
+ });
623
+ }
624
+ }
625
+ }
626
+ }
627
+ }
628
+ results.sort((a, b) => b.score - a.score || a.priority - b.priority);
629
+ return results.map(({ type, id, title }) => ({
630
+ type,
631
+ id,
632
+ title
633
+ }));
634
+ }
442
635
  };
443
636
  var base_default = LocalBaseIndexDB;
444
637
 
package/lib/db/base.d.cts CHANGED
@@ -1,5 +1,5 @@
1
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";
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, TSearchResult } from "@ocap/types";
3
3
 
4
4
  //#region src/db/base.d.ts
5
5
  declare class LocalBaseIndexDB extends BaseIndexDB implements IIndexDB {
@@ -15,6 +15,10 @@ declare class LocalBaseIndexDB extends BaseIndexDB implements IIndexDB {
15
15
  listRollupBlocks(params?: Partial<TRequestListRollupBlocks>): IListRollupBlocksResult;
16
16
  listRollupValidators(params?: Partial<TRequestListRollupValidators>): IListRollupValidatorsResult;
17
17
  listDelegations(params?: Partial<TRequestListDelegations>): IListDelegationsResult;
18
+ /**
19
+ * Search entities by semantic fields (moniker, name, symbol, description)
20
+ */
21
+ search(keyword: string): TSearchResult[];
18
22
  }
19
23
  //#endregion
20
24
  export { LocalBaseIndexDB as default };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ocap/indexdb-memory",
3
3
  "description": "OCAP indexdb adapter that uses memory as backend, just for test purpose",
4
- "version": "1.29.14",
4
+ "version": "1.29.16",
5
5
  "type": "module",
6
6
  "author": "wangshijun <shijun@arcblock.io> (https://www.arcblock.io)",
7
7
  "bugs": {
@@ -15,7 +15,7 @@
15
15
  "wangshijun <shijun@arcblock.io> (https://www.arcblock.io)"
16
16
  ],
17
17
  "devDependencies": {
18
- "@ocap/indexdb-test": "1.29.14",
18
+ "@ocap/indexdb-test": "1.29.16",
19
19
  "@types/debug": "^4.1.12",
20
20
  "@types/lodash": "^4.17.10",
21
21
  "@types/lokijs": "^1.5.14",
@@ -79,10 +79,10 @@
79
79
  },
80
80
  "gitHead": "87990c8b5e215107fc587c1ced0d6b3e2cd2483e",
81
81
  "dependencies": {
82
- "@arcblock/did": "1.29.14",
83
- "@ocap/indexdb": "1.29.14",
84
- "@ocap/types": "1.29.14",
85
- "@ocap/util": "1.29.14",
82
+ "@arcblock/did": "1.29.16",
83
+ "@ocap/indexdb": "1.29.16",
84
+ "@ocap/types": "1.29.16",
85
+ "@ocap/util": "1.29.16",
86
86
  "debug": "^4.4.3",
87
87
  "lodash": "^4.17.23",
88
88
  "lokijs": "^1.5.12"