@ocap/indexdb-sqlite 1.29.15 → 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.mjs CHANGED
@@ -761,45 +761,69 @@ var SqliteBaseIndexDB = class extends BaseIndexDB {
761
761
  const escaped = escapeLikePattern(trimmed);
762
762
  const lowerKw = trimmed.toLowerCase();
763
763
  const containsPattern = `%${escaped}%`;
764
+ const limit = sql.raw(String(SEARCH_LIMIT_PER_TABLE));
764
765
  const typePriority = {
765
766
  token: 0,
766
767
  account: 1,
767
768
  factory: 2,
768
769
  asset: 3,
769
- tokenFactory: 4
770
+ tokenFactory: 4,
771
+ stake: 5
770
772
  };
771
- const [tokenRows, accountRows, factoryRows, assetRows, tokenFactoryRows] = await Promise.all([
773
+ const isDidLike = /^(did:abt:)?(z[1-9A-HJ-NP-Za-km-z]|0x[0-9a-fA-F])/.test(trimmed);
774
+ const didPrefix = isDidLike ? trimmed.replace(/^did:abt:/i, "") : "";
775
+ const doDidSearch = isDidLike && didPrefix.length >= 4;
776
+ const queries = [
772
777
  sql`
773
- SELECT address, name, symbol FROM token
774
- WHERE LOWER(name) LIKE LOWER(${containsPattern}) ESCAPE '\\' OR LOWER(symbol) LIKE LOWER(${containsPattern}) ESCAPE '\\'
775
- LIMIT ${sql.raw(String(SEARCH_LIMIT_PER_TABLE))}
778
+ SELECT address, name, symbol, description FROM token
779
+ WHERE LOWER(name) LIKE LOWER(${containsPattern}) ESCAPE '\\' OR LOWER(symbol) LIKE LOWER(${containsPattern}) ESCAPE '\\' OR LOWER(description) LIKE LOWER(${containsPattern}) ESCAPE '\\'
780
+ LIMIT ${limit}
776
781
  `.execute(this.db),
777
782
  sql`
778
783
  SELECT address, moniker FROM account
779
784
  WHERE LOWER(moniker) LIKE LOWER(${containsPattern}) ESCAPE '\\'
780
- LIMIT ${sql.raw(String(SEARCH_LIMIT_PER_TABLE))}
785
+ LIMIT ${limit}
781
786
  `.execute(this.db),
782
787
  sql`
783
788
  SELECT address, name, description FROM factory
784
789
  WHERE LOWER(name) LIKE LOWER(${containsPattern}) ESCAPE '\\' OR LOWER(description) LIKE LOWER(${containsPattern}) ESCAPE '\\'
785
- LIMIT ${sql.raw(String(SEARCH_LIMIT_PER_TABLE))}
790
+ LIMIT ${limit}
786
791
  `.execute(this.db),
787
792
  sql`
788
- SELECT address, moniker FROM asset
789
- WHERE LOWER(moniker) LIKE LOWER(${containsPattern}) ESCAPE '\\'
790
- LIMIT ${sql.raw(String(SEARCH_LIMIT_PER_TABLE))}
793
+ SELECT address, moniker, tags FROM asset
794
+ WHERE LOWER(moniker) LIKE LOWER(${containsPattern}) ESCAPE '\\' OR LOWER(tags) LIKE LOWER(${containsPattern}) ESCAPE '\\'
795
+ LIMIT ${limit}
791
796
  `.execute(this.db),
792
797
  sql`
793
798
  SELECT address, name, moniker FROM tokenFactory
794
799
  WHERE LOWER(name) LIKE LOWER(${containsPattern}) ESCAPE '\\' OR LOWER(moniker) LIKE LOWER(${containsPattern}) ESCAPE '\\'
795
- LIMIT ${sql.raw(String(SEARCH_LIMIT_PER_TABLE))}
800
+ LIMIT ${limit}
801
+ `.execute(this.db),
802
+ sql`
803
+ SELECT address, message FROM stake
804
+ WHERE LOWER(message) LIKE LOWER(${containsPattern}) ESCAPE '\\'
805
+ LIMIT ${limit}
796
806
  `.execute(this.db)
797
- ]);
807
+ ];
808
+ if (doDidSearch) {
809
+ const prefixPattern = `${escapeLikePattern(didPrefix)}%`;
810
+ queries.push(sql`
811
+ SELECT * FROM (SELECT address, 'account' as type, COALESCE(moniker, address) as title FROM account WHERE address LIKE ${prefixPattern} ESCAPE '\\' LIMIT ${limit})
812
+ UNION ALL SELECT * FROM (SELECT address, 'token', COALESCE(name, address) FROM token WHERE address LIKE ${prefixPattern} ESCAPE '\\' LIMIT ${limit})
813
+ UNION ALL SELECT * FROM (SELECT address, 'asset', COALESCE(moniker, address) FROM asset WHERE address LIKE ${prefixPattern} ESCAPE '\\' LIMIT ${limit})
814
+ UNION ALL SELECT * FROM (SELECT address, 'factory', COALESCE(name, address) FROM factory WHERE address LIKE ${prefixPattern} ESCAPE '\\' LIMIT ${limit})
815
+ UNION ALL SELECT * FROM (SELECT address, 'tokenFactory', COALESCE(name, address) FROM tokenFactory WHERE address LIKE ${prefixPattern} ESCAPE '\\' LIMIT ${limit})
816
+ UNION ALL SELECT * FROM (SELECT address, 'stake', COALESCE(message, address) FROM stake WHERE address LIKE ${prefixPattern} ESCAPE '\\' LIMIT ${limit})
817
+ `.execute(this.db));
818
+ }
819
+ const allResults = await Promise.all(queries);
820
+ const [tokenRows, accountRows, factoryRows, assetRows, tokenFactoryRows, stakeRows] = allResults;
798
821
  const results = [];
799
822
  for (const row of tokenRows.rows) {
800
823
  const nameScore = row.name ? scoreMatch(row.name, lowerKw) : 0;
801
824
  const symbolScore = row.symbol ? scoreMatch(row.symbol, lowerKw) : 0;
802
- const score = Math.max(nameScore, symbolScore);
825
+ const descScore = row.description ? scoreMatch(row.description, lowerKw) : 0;
826
+ const score = Math.max(nameScore, symbolScore, descScore);
803
827
  const title = nameScore >= symbolScore ? row.name || row.symbol || "" : row.symbol || row.name || "";
804
828
  results.push({
805
829
  type: "token",
@@ -832,7 +856,13 @@ var SqliteBaseIndexDB = class extends BaseIndexDB {
832
856
  });
833
857
  }
834
858
  for (const row of assetRows.rows) {
835
- const score = scoreMatch(row.moniker, lowerKw);
859
+ const monikerScore = row.moniker ? scoreMatch(row.moniker, lowerKw) : 0;
860
+ let tagScore = 0;
861
+ if (row.tags) try {
862
+ const tags = typeof row.tags === "string" ? JSON.parse(row.tags) : row.tags;
863
+ if (Array.isArray(tags)) tagScore = Math.max(0, ...tags.map((t) => scoreMatch(t, lowerKw)));
864
+ } catch {}
865
+ const score = Math.max(monikerScore, tagScore);
836
866
  results.push({
837
867
  type: "asset",
838
868
  id: row.address,
@@ -854,6 +884,33 @@ var SqliteBaseIndexDB = class extends BaseIndexDB {
854
884
  priority: typePriority.tokenFactory
855
885
  });
856
886
  }
887
+ for (const row of stakeRows.rows) {
888
+ const score = scoreMatch(row.message, lowerKw);
889
+ results.push({
890
+ type: "stake",
891
+ id: row.address,
892
+ title: row.message || "",
893
+ score,
894
+ priority: typePriority.stake
895
+ });
896
+ }
897
+ if (doDidSearch) {
898
+ const didRows = allResults[6];
899
+ const seen = new Set(results.map((r) => `${r.type}:${r.id}`));
900
+ for (const row of didRows.rows) {
901
+ const key = `${row.type}:${row.address}`;
902
+ if (!seen.has(key)) {
903
+ seen.add(key);
904
+ results.push({
905
+ type: row.type,
906
+ id: row.address,
907
+ title: row.title || row.address,
908
+ score: 2,
909
+ priority: typePriority[row.type] ?? 99
910
+ });
911
+ }
912
+ }
913
+ }
857
914
  results.sort((a, b) => b.score - a.score || a.priority - b.priority);
858
915
  return results.map(({ type, id, title }) => ({
859
916
  type,
package/lib/db/base.cjs CHANGED
@@ -765,45 +765,69 @@ var SqliteBaseIndexDB = class extends _ocap_indexdb.BaseIndexDB {
765
765
  const escaped = escapeLikePattern(trimmed);
766
766
  const lowerKw = trimmed.toLowerCase();
767
767
  const containsPattern = `%${escaped}%`;
768
+ const limit = kysely.sql.raw(String(SEARCH_LIMIT_PER_TABLE));
768
769
  const typePriority = {
769
770
  token: 0,
770
771
  account: 1,
771
772
  factory: 2,
772
773
  asset: 3,
773
- tokenFactory: 4
774
+ tokenFactory: 4,
775
+ stake: 5
774
776
  };
775
- const [tokenRows, accountRows, factoryRows, assetRows, tokenFactoryRows] = await Promise.all([
777
+ const isDidLike = /^(did:abt:)?(z[1-9A-HJ-NP-Za-km-z]|0x[0-9a-fA-F])/.test(trimmed);
778
+ const didPrefix = isDidLike ? trimmed.replace(/^did:abt:/i, "") : "";
779
+ const doDidSearch = isDidLike && didPrefix.length >= 4;
780
+ const queries = [
776
781
  kysely.sql`
777
- SELECT address, name, symbol FROM token
778
- WHERE LOWER(name) LIKE LOWER(${containsPattern}) ESCAPE '\\' OR LOWER(symbol) LIKE LOWER(${containsPattern}) ESCAPE '\\'
779
- LIMIT ${kysely.sql.raw(String(SEARCH_LIMIT_PER_TABLE))}
782
+ SELECT address, name, symbol, description FROM token
783
+ WHERE LOWER(name) LIKE LOWER(${containsPattern}) ESCAPE '\\' OR LOWER(symbol) LIKE LOWER(${containsPattern}) ESCAPE '\\' OR LOWER(description) LIKE LOWER(${containsPattern}) ESCAPE '\\'
784
+ LIMIT ${limit}
780
785
  `.execute(this.db),
781
786
  kysely.sql`
782
787
  SELECT address, moniker FROM account
783
788
  WHERE LOWER(moniker) LIKE LOWER(${containsPattern}) ESCAPE '\\'
784
- LIMIT ${kysely.sql.raw(String(SEARCH_LIMIT_PER_TABLE))}
789
+ LIMIT ${limit}
785
790
  `.execute(this.db),
786
791
  kysely.sql`
787
792
  SELECT address, name, description FROM factory
788
793
  WHERE LOWER(name) LIKE LOWER(${containsPattern}) ESCAPE '\\' OR LOWER(description) LIKE LOWER(${containsPattern}) ESCAPE '\\'
789
- LIMIT ${kysely.sql.raw(String(SEARCH_LIMIT_PER_TABLE))}
794
+ LIMIT ${limit}
790
795
  `.execute(this.db),
791
796
  kysely.sql`
792
- SELECT address, moniker FROM asset
793
- WHERE LOWER(moniker) LIKE LOWER(${containsPattern}) ESCAPE '\\'
794
- LIMIT ${kysely.sql.raw(String(SEARCH_LIMIT_PER_TABLE))}
797
+ SELECT address, moniker, tags FROM asset
798
+ WHERE LOWER(moniker) LIKE LOWER(${containsPattern}) ESCAPE '\\' OR LOWER(tags) LIKE LOWER(${containsPattern}) ESCAPE '\\'
799
+ LIMIT ${limit}
795
800
  `.execute(this.db),
796
801
  kysely.sql`
797
802
  SELECT address, name, moniker FROM tokenFactory
798
803
  WHERE LOWER(name) LIKE LOWER(${containsPattern}) ESCAPE '\\' OR LOWER(moniker) LIKE LOWER(${containsPattern}) ESCAPE '\\'
799
- LIMIT ${kysely.sql.raw(String(SEARCH_LIMIT_PER_TABLE))}
804
+ LIMIT ${limit}
805
+ `.execute(this.db),
806
+ kysely.sql`
807
+ SELECT address, message FROM stake
808
+ WHERE LOWER(message) LIKE LOWER(${containsPattern}) ESCAPE '\\'
809
+ LIMIT ${limit}
800
810
  `.execute(this.db)
801
- ]);
811
+ ];
812
+ if (doDidSearch) {
813
+ const prefixPattern = `${escapeLikePattern(didPrefix)}%`;
814
+ queries.push(kysely.sql`
815
+ SELECT * FROM (SELECT address, 'account' as type, COALESCE(moniker, address) as title FROM account WHERE address LIKE ${prefixPattern} ESCAPE '\\' LIMIT ${limit})
816
+ UNION ALL SELECT * FROM (SELECT address, 'token', COALESCE(name, address) FROM token WHERE address LIKE ${prefixPattern} ESCAPE '\\' LIMIT ${limit})
817
+ UNION ALL SELECT * FROM (SELECT address, 'asset', COALESCE(moniker, address) FROM asset WHERE address LIKE ${prefixPattern} ESCAPE '\\' LIMIT ${limit})
818
+ UNION ALL SELECT * FROM (SELECT address, 'factory', COALESCE(name, address) FROM factory WHERE address LIKE ${prefixPattern} ESCAPE '\\' LIMIT ${limit})
819
+ UNION ALL SELECT * FROM (SELECT address, 'tokenFactory', COALESCE(name, address) FROM tokenFactory WHERE address LIKE ${prefixPattern} ESCAPE '\\' LIMIT ${limit})
820
+ UNION ALL SELECT * FROM (SELECT address, 'stake', COALESCE(message, address) FROM stake WHERE address LIKE ${prefixPattern} ESCAPE '\\' LIMIT ${limit})
821
+ `.execute(this.db));
822
+ }
823
+ const allResults = await Promise.all(queries);
824
+ const [tokenRows, accountRows, factoryRows, assetRows, tokenFactoryRows, stakeRows] = allResults;
802
825
  const results = [];
803
826
  for (const row of tokenRows.rows) {
804
827
  const nameScore = row.name ? scoreMatch(row.name, lowerKw) : 0;
805
828
  const symbolScore = row.symbol ? scoreMatch(row.symbol, lowerKw) : 0;
806
- const score = Math.max(nameScore, symbolScore);
829
+ const descScore = row.description ? scoreMatch(row.description, lowerKw) : 0;
830
+ const score = Math.max(nameScore, symbolScore, descScore);
807
831
  const title = nameScore >= symbolScore ? row.name || row.symbol || "" : row.symbol || row.name || "";
808
832
  results.push({
809
833
  type: "token",
@@ -836,7 +860,13 @@ var SqliteBaseIndexDB = class extends _ocap_indexdb.BaseIndexDB {
836
860
  });
837
861
  }
838
862
  for (const row of assetRows.rows) {
839
- const score = scoreMatch(row.moniker, lowerKw);
863
+ const monikerScore = row.moniker ? scoreMatch(row.moniker, lowerKw) : 0;
864
+ let tagScore = 0;
865
+ if (row.tags) try {
866
+ const tags = typeof row.tags === "string" ? JSON.parse(row.tags) : row.tags;
867
+ if (Array.isArray(tags)) tagScore = Math.max(0, ...tags.map((t) => scoreMatch(t, lowerKw)));
868
+ } catch {}
869
+ const score = Math.max(monikerScore, tagScore);
840
870
  results.push({
841
871
  type: "asset",
842
872
  id: row.address,
@@ -858,6 +888,33 @@ var SqliteBaseIndexDB = class extends _ocap_indexdb.BaseIndexDB {
858
888
  priority: typePriority.tokenFactory
859
889
  });
860
890
  }
891
+ for (const row of stakeRows.rows) {
892
+ const score = scoreMatch(row.message, lowerKw);
893
+ results.push({
894
+ type: "stake",
895
+ id: row.address,
896
+ title: row.message || "",
897
+ score,
898
+ priority: typePriority.stake
899
+ });
900
+ }
901
+ if (doDidSearch) {
902
+ const didRows = allResults[6];
903
+ const seen = new Set(results.map((r) => `${r.type}:${r.id}`));
904
+ for (const row of didRows.rows) {
905
+ const key = `${row.type}:${row.address}`;
906
+ if (!seen.has(key)) {
907
+ seen.add(key);
908
+ results.push({
909
+ type: row.type,
910
+ id: row.address,
911
+ title: row.title || row.address,
912
+ score: 2,
913
+ priority: typePriority[row.type] ?? 99
914
+ });
915
+ }
916
+ }
917
+ }
861
918
  results.sort((a, b) => b.score - a.score || a.priority - b.priority);
862
919
  return results.map(({ type, id, title }) => ({
863
920
  type,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ocap/indexdb-sqlite",
3
3
  "description": "OCAP indexdb adapter that uses SQLite as backend",
4
- "version": "1.29.15",
4
+ "version": "1.29.16",
5
5
  "author": "wangshijun <shijun@arcblock.io> (https://www.arcblock.io)",
6
6
  "bugs": {
7
7
  "url": "https://github.com/ArcBlock/blockchain/issues",
@@ -14,7 +14,7 @@
14
14
  "wangshijun <shijun@arcblock.io> (https://www.arcblock.io)"
15
15
  ],
16
16
  "devDependencies": {
17
- "@ocap/indexdb-test": "1.29.15"
17
+ "@ocap/indexdb-test": "1.29.16"
18
18
  },
19
19
  "homepage": "https://github.com/ArcBlock/blockchain/tree/master/indexdb/sqlite",
20
20
  "keywords": [
@@ -63,10 +63,10 @@
63
63
  "clean": "rm -rf lib esm"
64
64
  },
65
65
  "dependencies": {
66
- "@arcblock/did": "1.29.15",
67
- "@ocap/indexdb": "1.29.15",
68
- "@ocap/types": "1.29.15",
69
- "@ocap/util": "1.29.15",
66
+ "@arcblock/did": "1.29.16",
67
+ "@ocap/indexdb": "1.29.16",
68
+ "@ocap/types": "1.29.16",
69
+ "@ocap/util": "1.29.16",
70
70
  "debug": "^4.4.3",
71
71
  "better-sqlite3": "^11.8.1",
72
72
  "kysely": "^0.27.0",