@atscript/mongo 0.0.20 → 0.0.21

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/dist/index.cjs CHANGED
@@ -738,6 +738,8 @@ var AsCollection = class {
738
738
  else {
739
739
  const weights = {};
740
740
  index = {
741
+ key,
742
+ name,
741
743
  type,
742
744
  fields: { [field]: value },
743
745
  weights
@@ -747,7 +749,10 @@ else {
747
749
  if (weight) index.weights[field] = weight;
748
750
  }
749
751
  _setSearchIndex(type, name, definition) {
750
- this._indexes.set(indexKey(type, name || DEFAULT_INDEX_NAME), {
752
+ const key = indexKey(type, name || DEFAULT_INDEX_NAME);
753
+ this._indexes.set(key, {
754
+ key,
755
+ name: name || DEFAULT_INDEX_NAME,
751
756
  type,
752
757
  definition
753
758
  });
@@ -844,6 +849,22 @@ else {
844
849
  this._finalizeIndexesForCollection();
845
850
  }
846
851
  }
852
+ getSearchIndex(name = DEFAULT_INDEX_NAME) {
853
+ if (!this._searchIndexesMap) {
854
+ this._searchIndexesMap = new Map();
855
+ let deafultIndex;
856
+ for (const index of this.indexes.values()) switch (index.type) {
857
+ case "text": if (!deafultIndex) deafultIndex = index;
858
+ case "dynamic_text": deafultIndex = index;
859
+ case "search_text":
860
+ if (!deafultIndex || deafultIndex?.type === "text") deafultIndex = index;
861
+ this._searchIndexesMap.set(index.name, index);
862
+ default:
863
+ }
864
+ if (deafultIndex && !this._searchIndexesMap.has(DEFAULT_INDEX_NAME)) this._searchIndexesMap.set(DEFAULT_INDEX_NAME, deafultIndex);
865
+ }
866
+ return this._searchIndexesMap.get(name);
867
+ }
847
868
  get flatMap() {
848
869
  this._flatten();
849
870
  return this._flatMap;
@@ -1002,6 +1023,7 @@ else if (this.idType !== "objectId") throw new Error("Missing \"_id\" field");
1002
1023
  _define_property$1(this, "_vectorFilters", void 0);
1003
1024
  _define_property$1(this, "_flatMap", void 0);
1004
1025
  _define_property$1(this, "_uniqueProps", void 0);
1026
+ _define_property$1(this, "_searchIndexesMap", void 0);
1005
1027
  this.asMongo = asMongo;
1006
1028
  this._type = _type;
1007
1029
  this.logger = logger;
package/dist/index.d.ts CHANGED
@@ -26,11 +26,15 @@ declare class AsMongo {
26
26
  }
27
27
 
28
28
  type TPlainIndex = {
29
+ key: string;
30
+ name: string;
29
31
  type: 'plain' | 'unique' | 'text';
30
32
  fields: Record<string, 1 | 'text'>;
31
33
  weights: Record<string, number>;
32
34
  };
33
35
  type TSearchIndex = {
36
+ key: string;
37
+ name: string;
34
38
  type: 'dynamic_text' | 'search_text' | 'vector';
35
39
  definition: TMongoSearchIndexDefinition;
36
40
  };
@@ -83,6 +87,8 @@ declare class AsCollection<T extends TAtscriptAnnotatedTypeConstructor> {
83
87
  protected _finalizeIndexesForCollection(): void;
84
88
  protected _prepareIndexesForField(fieldName: string, metadata: TMetadataMap<AtscriptMetadata>): void;
85
89
  protected _flatten(): void;
90
+ protected _searchIndexesMap?: Map<string, TIndex>;
91
+ getSearchIndex(name?: string): TIndex | undefined;
86
92
  get flatMap(): Map<string, TAtscriptAnnotatedType>;
87
93
  syncIndexes(): Promise<void>;
88
94
  insert(payload: (Omit<InstanceType<T>, '_id'> & {
package/dist/index.mjs CHANGED
@@ -714,6 +714,8 @@ var AsCollection = class {
714
714
  else {
715
715
  const weights = {};
716
716
  index = {
717
+ key,
718
+ name,
717
719
  type,
718
720
  fields: { [field]: value },
719
721
  weights
@@ -723,7 +725,10 @@ else {
723
725
  if (weight) index.weights[field] = weight;
724
726
  }
725
727
  _setSearchIndex(type, name, definition) {
726
- this._indexes.set(indexKey(type, name || DEFAULT_INDEX_NAME), {
728
+ const key = indexKey(type, name || DEFAULT_INDEX_NAME);
729
+ this._indexes.set(key, {
730
+ key,
731
+ name: name || DEFAULT_INDEX_NAME,
727
732
  type,
728
733
  definition
729
734
  });
@@ -820,6 +825,22 @@ else {
820
825
  this._finalizeIndexesForCollection();
821
826
  }
822
827
  }
828
+ getSearchIndex(name = DEFAULT_INDEX_NAME) {
829
+ if (!this._searchIndexesMap) {
830
+ this._searchIndexesMap = new Map();
831
+ let deafultIndex;
832
+ for (const index of this.indexes.values()) switch (index.type) {
833
+ case "text": if (!deafultIndex) deafultIndex = index;
834
+ case "dynamic_text": deafultIndex = index;
835
+ case "search_text":
836
+ if (!deafultIndex || deafultIndex?.type === "text") deafultIndex = index;
837
+ this._searchIndexesMap.set(index.name, index);
838
+ default:
839
+ }
840
+ if (deafultIndex && !this._searchIndexesMap.has(DEFAULT_INDEX_NAME)) this._searchIndexesMap.set(DEFAULT_INDEX_NAME, deafultIndex);
841
+ }
842
+ return this._searchIndexesMap.get(name);
843
+ }
823
844
  get flatMap() {
824
845
  this._flatten();
825
846
  return this._flatMap;
@@ -978,6 +999,7 @@ else if (this.idType !== "objectId") throw new Error("Missing \"_id\" field");
978
999
  _define_property$1(this, "_vectorFilters", void 0);
979
1000
  _define_property$1(this, "_flatMap", void 0);
980
1001
  _define_property$1(this, "_uniqueProps", void 0);
1002
+ _define_property$1(this, "_searchIndexesMap", void 0);
981
1003
  this.asMongo = asMongo;
982
1004
  this._type = _type;
983
1005
  this.logger = logger;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/mongo",
3
- "version": "0.0.20",
3
+ "version": "0.0.21",
4
4
  "description": "Mongodb plugin for atscript.",
5
5
  "type": "module",
6
6
  "main": "dist/index.mjs",
@@ -34,8 +34,8 @@
34
34
  "license": "ISC",
35
35
  "peerDependencies": {
36
36
  "mongodb": "^6.17.0",
37
- "@atscript/core": "^0.0.20",
38
- "@atscript/typescript": "^0.0.20"
37
+ "@atscript/core": "^0.0.21",
38
+ "@atscript/typescript": "^0.0.21"
39
39
  },
40
40
  "devDependencies": {
41
41
  "vitest": "3.2.4"