@atscript/mongo 0.1.3 → 0.1.4

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
@@ -400,7 +400,7 @@ var CollectionPatcher = class CollectionPatcher {
400
400
  * @param collection Target collection wrapper
401
401
  * @returns Atscript Validator
402
402
  */ static prepareValidator(collection) {
403
- return collection.type.validator({
403
+ return collection.createValidator({
404
404
  plugins: [validateMongoIdPlugin, validateMongoUniqueArrayItemsPlugin],
405
405
  replace: (def, path) => {
406
406
  if (path === "" && def.type.kind === "object") {
@@ -656,6 +656,9 @@ const DEFAULT_INDEX_NAME = "DEFAULT";
656
656
  return `${INDEX_PREFIX}${type}__${cleanName}`;
657
657
  }
658
658
  var AsCollection = class {
659
+ createValidator(opts) {
660
+ return this._type.validator(opts);
661
+ }
659
662
  async exists() {
660
663
  return this.asMongo.collectionExists(this.name);
661
664
  }
@@ -697,7 +700,7 @@ var AsCollection = class {
697
700
  */ getValidator(purpose) {
698
701
  if (!this.validators.has(purpose)) switch (purpose) {
699
702
  case "insert": {
700
- this.validators.set(purpose, this.type.validator({
703
+ this.validators.set(purpose, this.createValidator({
701
704
  plugins: [validateMongoIdPlugin, validateMongoUniqueArrayItemsPlugin],
702
705
  replace(type, path) {
703
706
  if (path === "_id" && type.type.tags.has("objectId")) return {
@@ -710,7 +713,7 @@ var AsCollection = class {
710
713
  break;
711
714
  }
712
715
  case "update": {
713
- this.validators.set(purpose, this.type.validator({ plugins: [validateMongoIdPlugin] }));
716
+ this.validators.set(purpose, this.createValidator({ plugins: [validateMongoIdPlugin] }));
714
717
  break;
715
718
  }
716
719
  case "patch": {
@@ -789,7 +792,10 @@ else flatUnion.item(existing);
789
792
  case "object":
790
793
  this._addFieldToFlatMap(prefix || "", def);
791
794
  const items = Array.from(def.type.props.entries());
792
- for (const [key, value] of items) this._flattenType(value, prefix ? `${prefix}.${key}` : key, inComplexTypeOrArray);
795
+ for (const [key, value] of items) {
796
+ if ((0, __atscript_typescript_utils.isPhantomType)(value)) continue;
797
+ this._flattenType(value, prefix ? `${prefix}.${key}` : key, inComplexTypeOrArray);
798
+ }
793
799
  break;
794
800
  case "array":
795
801
  let typeArray = def;
@@ -813,7 +819,10 @@ else flatUnion.item(existing);
813
819
  switch (def.type.kind) {
814
820
  case "object":
815
821
  const items = Array.from(def.type.props.entries());
816
- for (const [key, value] of items) this._flattenType(value, name ? `${name}.${key}` : key, true);
822
+ for (const [key, value] of items) {
823
+ if ((0, __atscript_typescript_utils.isPhantomType)(value)) continue;
824
+ this._flattenType(value, name ? `${name}.${key}` : key, true);
825
+ }
817
826
  break;
818
827
  case "union":
819
828
  case "intersection":
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { TAtscriptPlugin } from '@atscript/core';
2
2
  import * as mongodb from 'mongodb';
3
- import { MongoClient, Collection, ObjectId, InsertOneOptions, ReplaceOptions, UpdateOptions, OptionalUnlessRequiredId, Filter, WithoutId } from 'mongodb';
3
+ import { MongoClient, Collection, ObjectId, InsertOneOptions, ReplaceOptions, UpdateOptions, Filter } from 'mongodb';
4
4
  import * as _atscript_typescript_utils from '@atscript/typescript/utils';
5
- import { TAtscriptAnnotatedTypeConstructor, Validator, TAtscriptAnnotatedType, TAtscriptTypeObject, TMetadataMap } from '@atscript/typescript/utils';
5
+ import { TAtscriptAnnotatedType, Validator, TValidatorOptions, TAtscriptTypeObject, TMetadataMap } from '@atscript/typescript/utils';
6
6
 
7
7
  declare const MongoPlugin: () => TAtscriptPlugin;
8
8
 
@@ -22,7 +22,7 @@ declare class AsMongo {
22
22
  protected collectionsList?: Promise<Set<string>>;
23
23
  protected getCollectionsList(): Promise<Set<string>>;
24
24
  collectionExists(name: string): Promise<boolean>;
25
- getCollection<T extends TAtscriptAnnotatedTypeConstructor>(type: T, logger?: TGenericLogger): AsCollection<T>;
25
+ getCollection<T extends TAtscriptAnnotatedType>(type: T, logger?: TGenericLogger): AsCollection<T>;
26
26
  private _collections;
27
27
  }
28
28
 
@@ -41,17 +41,18 @@ type TSearchIndex = {
41
41
  };
42
42
  type TIndex = TPlainIndex | TSearchIndex;
43
43
  type TValidatorPurpose = 'insert' | 'update' | 'patch';
44
- declare class AsCollection<T extends TAtscriptAnnotatedTypeConstructor> {
44
+ declare class AsCollection<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedType, DataType = T extends {
45
+ type: {
46
+ __dataType?: infer D;
47
+ };
48
+ } ? unknown extends D ? T extends new (...args: any[]) => infer I ? I : unknown : D : unknown> {
45
49
  protected readonly asMongo: AsMongo;
46
50
  protected readonly _type: T;
47
51
  protected readonly logger: TGenericLogger;
48
52
  readonly name: string;
49
- readonly collection: Collection<InstanceType<T>>;
50
- protected readonly validators: Map<TValidatorPurpose, Validator<T, T extends {
51
- type: {
52
- __dataType?: infer D;
53
- };
54
- } ? unknown extends D ? T extends new (...args: any[]) => infer I ? I : D : D : unknown>>;
53
+ readonly collection: Collection<any>;
54
+ protected readonly validators: Map<TValidatorPurpose, Validator<T, DataType>>;
55
+ createValidator(opts?: Partial<TValidatorOptions>): Validator<T, DataType>;
55
56
  protected _indexes: Map<string, TIndex>;
56
57
  protected _vectorFilters: Map<string, string>;
57
58
  protected _flatMap?: Map<string, TAtscriptAnnotatedType>;
@@ -79,11 +80,7 @@ declare class AsCollection<T extends TAtscriptAnnotatedTypeConstructor> {
79
80
  * @returns {Validator} The corresponding validator instance.
80
81
  * @throws {Error} If an unknown purpose is provided.
81
82
  */
82
- getValidator(purpose: TValidatorPurpose): Validator<T, T extends {
83
- type: {
84
- __dataType?: infer D;
85
- };
86
- } ? unknown extends D ? T extends new (...args: any[]) => infer I ? I : D : D : unknown> | undefined;
83
+ getValidator(purpose: TValidatorPurpose): Validator<T, DataType> | undefined;
87
84
  get type(): TAtscriptAnnotatedType<TAtscriptTypeObject>;
88
85
  get indexes(): Map<string, TIndex>;
89
86
  protected _addIndexField(type: TPlainIndex['type'], name: string, field: string, weight?: number): void;
@@ -103,35 +100,35 @@ declare class AsCollection<T extends TAtscriptAnnotatedTypeConstructor> {
103
100
  getSearchIndex(name?: string): TIndex | undefined;
104
101
  get flatMap(): Map<string, TAtscriptAnnotatedType<_atscript_typescript_utils.TAtscriptTypeDef<unknown>, unknown>>;
105
102
  syncIndexes(): Promise<void>;
106
- insert(payload: (Omit<InstanceType<T>, '_id'> & {
107
- _id?: InstanceType<T>['_id'] | ObjectId;
108
- }) | (Omit<InstanceType<T>, '_id'> & {
109
- _id?: InstanceType<T>['_id'] | ObjectId;
110
- })[], options?: InsertOneOptions): Promise<mongodb.InsertManyResult<InstanceType<T>>> | Promise<mongodb.InsertOneResult<InstanceType<T>>>;
111
- replace(payload: Omit<InstanceType<T>, '_id'> & {
112
- _id: InstanceType<T>['_id'] | ObjectId;
113
- }, options?: ReplaceOptions): Promise<mongodb.UpdateResult<InstanceType<T>>>;
114
- update(payload: AsMongoPatch<Omit<InstanceType<T>, '_id'>> & {
115
- _id: InstanceType<T>['_id'] | ObjectId;
116
- }, options?: UpdateOptions): Promise<mongodb.UpdateResult<InstanceType<T>>>;
117
- prepareInsert(payload: (Omit<InstanceType<T>, '_id'> & {
118
- _id?: InstanceType<T>['_id'] | ObjectId;
119
- }) | (Omit<InstanceType<T>, '_id'> & {
120
- _id?: InstanceType<T>['_id'] | ObjectId;
121
- })[]): OptionalUnlessRequiredId<InstanceType<T>> | OptionalUnlessRequiredId<InstanceType<T>>[];
122
- prepareReplace(payload: Omit<InstanceType<T>, '_id'> & {
123
- _id: InstanceType<T>['_id'] | ObjectId;
103
+ insert(payload: (Omit<DataType, '_id'> & {
104
+ _id?: string | number | ObjectId;
105
+ }) | (Omit<DataType, '_id'> & {
106
+ _id?: string | number | ObjectId;
107
+ })[], options?: InsertOneOptions): Promise<mongodb.InsertManyResult<any>> | Promise<mongodb.InsertOneResult<any>>;
108
+ replace(payload: Omit<DataType, '_id'> & {
109
+ _id: string | number | ObjectId;
110
+ }, options?: ReplaceOptions): Promise<mongodb.UpdateResult<any>>;
111
+ update(payload: AsMongoPatch<Omit<DataType, '_id'>> & {
112
+ _id: string | number | ObjectId;
113
+ }, options?: UpdateOptions): Promise<mongodb.UpdateResult<any>>;
114
+ prepareInsert(payload: (Omit<DataType, '_id'> & {
115
+ _id?: string | number | ObjectId;
116
+ }) | (Omit<DataType, '_id'> & {
117
+ _id?: string | number | ObjectId;
118
+ })[]): DataType | DataType[];
119
+ prepareReplace(payload: Omit<DataType, '_id'> & {
120
+ _id: string | number | ObjectId;
124
121
  }): {
125
- toArgs: () => [Filter<InstanceType<T>>, WithoutId<InstanceType<T>>, ReplaceOptions];
126
- filter: Filter<InstanceType<T>>;
127
- updateFilter: WithoutId<InstanceType<T>>;
122
+ toArgs: () => [Filter<any>, any, ReplaceOptions];
123
+ filter: Filter<any>;
124
+ updateFilter: any;
128
125
  updateOptions: ReplaceOptions;
129
126
  };
130
- prepareUpdate(payload: AsMongoPatch<Omit<InstanceType<T>, '_id'>> & {
131
- _id: InstanceType<T>['_id'] | ObjectId;
127
+ prepareUpdate(payload: AsMongoPatch<Omit<DataType, '_id'>> & {
128
+ _id: string | number | ObjectId;
132
129
  }): {
133
- toArgs: () => [Filter<InstanceType<T>>, mongodb.Document[] | mongodb.UpdateFilter<InstanceType<T>>, UpdateOptions];
134
- filter: Filter<InstanceType<T>>;
130
+ toArgs: () => [Filter<any>, mongodb.UpdateFilter<any> | mongodb.Document[], UpdateOptions];
131
+ filter: Filter<any>;
135
132
  updateFilter: mongodb.Document[];
136
133
  updateOptions: UpdateOptions;
137
134
  };
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AnnotationSpec, isArray, isInterface, isPrimitive, isRef, isStructure } from "@atscript/core";
2
- import { defineAnnotatedType, isAnnotatedType, isAnnotatedTypeOfPrimitive } from "@atscript/typescript/utils";
2
+ import { defineAnnotatedType, isAnnotatedType, isAnnotatedTypeOfPrimitive, isPhantomType } from "@atscript/typescript/utils";
3
3
  import { MongoClient, ObjectId } from "mongodb";
4
4
 
5
5
  //#region packages/mongo/src/plugin/primitives.ts
@@ -376,7 +376,7 @@ var CollectionPatcher = class CollectionPatcher {
376
376
  * @param collection Target collection wrapper
377
377
  * @returns Atscript Validator
378
378
  */ static prepareValidator(collection) {
379
- return collection.type.validator({
379
+ return collection.createValidator({
380
380
  plugins: [validateMongoIdPlugin, validateMongoUniqueArrayItemsPlugin],
381
381
  replace: (def, path) => {
382
382
  if (path === "" && def.type.kind === "object") {
@@ -632,6 +632,9 @@ const DEFAULT_INDEX_NAME = "DEFAULT";
632
632
  return `${INDEX_PREFIX}${type}__${cleanName}`;
633
633
  }
634
634
  var AsCollection = class {
635
+ createValidator(opts) {
636
+ return this._type.validator(opts);
637
+ }
635
638
  async exists() {
636
639
  return this.asMongo.collectionExists(this.name);
637
640
  }
@@ -673,7 +676,7 @@ var AsCollection = class {
673
676
  */ getValidator(purpose) {
674
677
  if (!this.validators.has(purpose)) switch (purpose) {
675
678
  case "insert": {
676
- this.validators.set(purpose, this.type.validator({
679
+ this.validators.set(purpose, this.createValidator({
677
680
  plugins: [validateMongoIdPlugin, validateMongoUniqueArrayItemsPlugin],
678
681
  replace(type, path) {
679
682
  if (path === "_id" && type.type.tags.has("objectId")) return {
@@ -686,7 +689,7 @@ var AsCollection = class {
686
689
  break;
687
690
  }
688
691
  case "update": {
689
- this.validators.set(purpose, this.type.validator({ plugins: [validateMongoIdPlugin] }));
692
+ this.validators.set(purpose, this.createValidator({ plugins: [validateMongoIdPlugin] }));
690
693
  break;
691
694
  }
692
695
  case "patch": {
@@ -765,7 +768,10 @@ else flatUnion.item(existing);
765
768
  case "object":
766
769
  this._addFieldToFlatMap(prefix || "", def);
767
770
  const items = Array.from(def.type.props.entries());
768
- for (const [key, value] of items) this._flattenType(value, prefix ? `${prefix}.${key}` : key, inComplexTypeOrArray);
771
+ for (const [key, value] of items) {
772
+ if (isPhantomType(value)) continue;
773
+ this._flattenType(value, prefix ? `${prefix}.${key}` : key, inComplexTypeOrArray);
774
+ }
769
775
  break;
770
776
  case "array":
771
777
  let typeArray = def;
@@ -789,7 +795,10 @@ else flatUnion.item(existing);
789
795
  switch (def.type.kind) {
790
796
  case "object":
791
797
  const items = Array.from(def.type.props.entries());
792
- for (const [key, value] of items) this._flattenType(value, name ? `${name}.${key}` : key, true);
798
+ for (const [key, value] of items) {
799
+ if (isPhantomType(value)) continue;
800
+ this._flattenType(value, name ? `${name}.${key}` : key, true);
801
+ }
793
802
  break;
794
803
  case "union":
795
804
  case "intersection":
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/mongo",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
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.1.3",
38
- "@atscript/typescript": "^0.1.3"
37
+ "@atscript/core": "^0.1.4",
38
+ "@atscript/typescript": "^0.1.4"
39
39
  },
40
40
  "devDependencies": {
41
41
  "vitest": "3.2.4"