@mongorm/orm 0.1.1-beta.3 → 0.1.1-beta.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.d.mts +36 -30
- package/dist/index.mjs +6 -3
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -127,11 +127,13 @@ export declare class ModelCursor<Shape extends SchemaShape, Result extends objec
|
|
|
127
127
|
//#endregion
|
|
128
128
|
//#region src/query/types.d.ts
|
|
129
129
|
type StoredDocument<Shape extends SchemaShape> = Infer<Schema<Shape>> & Document;
|
|
130
|
-
type ModelFilterForDocument<DocumentShape extends Document, FieldShape extends object = DocumentShape> = Partial<{ [Key in keyof FieldShape]: Condition<FieldShape[Key]>; }> & Partial<Pick<RootFilterOperators<DocumentShape>, '$comment' | '$expr' | '$jsonSchema' | '$text' | '$where'>> & {
|
|
130
|
+
type ModelFilterForDocument<DocumentShape extends Document, FieldShape extends object = DocumentShape> = Partial<{ [Key in keyof FieldShape]: Condition<FieldShape[Key]>; }> & Partial<{ [Path in NestedFilterKey<FieldShape>]: Condition<NestedFilterValue<FieldShape, Path>>; }> & Partial<Pick<RootFilterOperators<DocumentShape>, '$comment' | '$expr' | '$jsonSchema' | '$text' | '$where'>> & {
|
|
131
131
|
$and?: ModelFilterForDocument<DocumentShape, FieldShape>[];
|
|
132
132
|
$nor?: ModelFilterForDocument<DocumentShape, FieldShape>[];
|
|
133
133
|
$or?: ModelFilterForDocument<DocumentShape, FieldShape>[];
|
|
134
134
|
};
|
|
135
|
+
type NestedFilterKey<Value, Prefix extends string = ''> = Value extends object ? Value extends ObjectId$1 | Date | readonly unknown[] ? never : { [Key in Extract<keyof Value, string>]: NonNullable<Value[Key]> extends object ? `${Prefix}${Key}` | `${Prefix}${Key}.${NestedFilterKey<NonNullable<Value[Key]>>}` : `${Prefix}${Key}`; }[Extract<keyof Value, string>] : never;
|
|
136
|
+
type NestedFilterValue<Value, Path extends string> = Path extends `${infer Head}.${infer Tail}` ? Head extends keyof Value ? NestedFilterValue<NonNullable<Value[Head]>, Tail> : never : Path extends keyof Value ? Value[Path] : never;
|
|
135
137
|
type ModelFilter<Shape extends SchemaShape> = ModelFilterForDocument<StoredDocument<Shape>, Infer<Schema<Shape>>>;
|
|
136
138
|
type SortDirection = 'asc' | 'desc';
|
|
137
139
|
type ModelSort<Shape extends SchemaShape> = Partial<Record<Extract<keyof Infer<Schema<Shape>>, string>, SortDirection>>;
|
|
@@ -220,11 +222,36 @@ export declare class ModelQuery<Shape extends SchemaShape, Result extends object
|
|
|
220
222
|
then<TResult1 = Result[], TResult2 = never>(onfulfilled?: ((value: Result[]) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null): PromiseLike<TResult1 | TResult2>;
|
|
221
223
|
}
|
|
222
224
|
//#endregion
|
|
225
|
+
//#region src/schema/scalars.d.ts
|
|
226
|
+
declare module 'zod' {
|
|
227
|
+
interface ZodType {
|
|
228
|
+
hidden(): this & {
|
|
229
|
+
readonly __hidden: true;
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/** A schema field that can be marked as hidden from default query results. */
|
|
234
|
+
type HiddenCapable<T extends z.ZodType> = Omit<T, 'optional' | 'nullable' | 'nullish'> & HiddenMethods<T>;
|
|
235
|
+
/** A schema field marked as hidden from default query results. */
|
|
236
|
+
type HiddenSchema<T extends z.ZodType> = Omit<T, 'optional' | 'nullable' | 'nullish'> & HiddenMethods<T> & {
|
|
237
|
+
readonly __hidden: true;
|
|
238
|
+
};
|
|
239
|
+
type HiddenMethods<T extends z.ZodType> = {
|
|
240
|
+
hidden(): HiddenSchema<T>;
|
|
241
|
+
optional(): HiddenCapable<z.ZodOptional<T>>;
|
|
242
|
+
nullable(): HiddenCapable<z.ZodNullable<T>>;
|
|
243
|
+
nullish(): HiddenCapable<z.ZodOptional<z.ZodNullable<T>>>;
|
|
244
|
+
};
|
|
245
|
+
/** Create a MongoDB ObjectId schema. */
|
|
246
|
+
declare const objectId: () => HiddenCapable<z.ZodInstanceOf<ObjectId$1>>;
|
|
247
|
+
//#endregion
|
|
223
248
|
//#region src/schema/schema.d.ts
|
|
224
249
|
/** Built-in persistence behavior applied by a schema. */
|
|
225
250
|
interface SchemaOptions {
|
|
226
251
|
readonly timestamps?: boolean;
|
|
227
252
|
readonly softdelete?: boolean;
|
|
253
|
+
/** Hide Mongorm-managed fields from default query results. */
|
|
254
|
+
readonly hideManaged?: boolean;
|
|
228
255
|
}
|
|
229
256
|
type IndexFieldMap<Shape extends SchemaShape> = Record<Extract<keyof Shape, string>, IndexDirection>;
|
|
230
257
|
/** A non-empty, autocomplete-friendly MongoDB index key definition. */
|
|
@@ -255,14 +282,15 @@ type SchemaIndexNames<Indexes extends readonly SchemaIndex<any>[]> = Extract<Ind
|
|
|
255
282
|
readonly name?: infer Name;
|
|
256
283
|
};
|
|
257
284
|
} ? Name : never : never, string>;
|
|
258
|
-
type
|
|
259
|
-
|
|
260
|
-
|
|
285
|
+
type ManagedSchema<T extends z.ZodType, Options extends SchemaOptions> = Options['hideManaged'] extends true ? HiddenSchema<T> : T;
|
|
286
|
+
type TimestampShape<Options extends SchemaOptions> = {
|
|
287
|
+
createdAt: ManagedSchema<z.ZodDefault<z.ZodDate>, Options>;
|
|
288
|
+
updatedAt: ManagedSchema<z.ZodDefault<z.ZodDate>, Options>;
|
|
261
289
|
};
|
|
262
|
-
type SoftDeleteShape = {
|
|
263
|
-
deletedAt: z.ZodDefault<z.ZodNullable<z.ZodDate
|
|
290
|
+
type SoftDeleteShape<Options extends SchemaOptions> = {
|
|
291
|
+
deletedAt: ManagedSchema<z.ZodDefault<z.ZodNullable<z.ZodDate>>, Options>;
|
|
264
292
|
};
|
|
265
|
-
type ManagedShape<Options extends SchemaOptions> = (Options['timestamps'] extends true ? TimestampShape : {}) & (Options['softdelete'] extends true ? SoftDeleteShape : {});
|
|
293
|
+
type ManagedShape<Options extends SchemaOptions> = (Options['timestamps'] extends true ? TimestampShape<Options> : {}) & (Options['softdelete'] extends true ? SoftDeleteShape<Options> : {});
|
|
266
294
|
type ManagedField<Options extends SchemaOptions> = (Options['timestamps'] extends true ? 'createdAt' | 'updatedAt' : never) | (Options['softdelete'] extends true ? 'deletedAt' : never);
|
|
267
295
|
type SoftDeleteEnabled<Options extends SchemaOptions> = Options['softdelete'] extends true ? true : false;
|
|
268
296
|
export declare const hasSoftDelete: (options: SchemaOptions) => boolean;
|
|
@@ -292,6 +320,7 @@ export declare class Schema<Shape extends SchemaShape, Relations extends SchemaR
|
|
|
292
320
|
constructor(shape: Shape, relations?: Relations, scopeMap?: Scopes, optionsConfig?: Options, indexDefinitions?: Indexes);
|
|
293
321
|
/** Enable managed timestamps and/or soft deletion for this schema. */
|
|
294
322
|
options<const Enabled extends SchemaOptions>(options: Enabled): Schema<Shape & ManagedShape<Enabled>, Relations, Scopes, Enabled, Indexes>;
|
|
323
|
+
private managedField;
|
|
295
324
|
/** Declare MongoDB indexes for explicit synchronization with the database. */
|
|
296
325
|
indexes<const Definitions extends readonly SchemaIndex<Shape>[]>(definitions: readonly SchemaIndex<Shape>[] & ValidateIndexDefinitions<Shape, Definitions>): Schema<Shape, Relations, Scopes, Options, Definitions>;
|
|
297
326
|
/** Add one or more one-way relations without requiring circular schema declarations. */
|
|
@@ -374,29 +403,6 @@ type SchemaRegistryBuilder<Registry extends Record<string, SchemaLike>> = Regist
|
|
|
374
403
|
};
|
|
375
404
|
declare const createSchemaRegistry: <const Registry extends Record<string, SchemaLike>>(registry: Registry) => SchemaRegistryBuilder<Registry>;
|
|
376
405
|
//#endregion
|
|
377
|
-
//#region src/schema/scalars.d.ts
|
|
378
|
-
declare module 'zod' {
|
|
379
|
-
interface ZodType {
|
|
380
|
-
hidden(): this & {
|
|
381
|
-
readonly __hidden: true;
|
|
382
|
-
};
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
/** A schema field that can be marked as hidden from default query results. */
|
|
386
|
-
type HiddenCapable<T extends z.ZodType> = Omit<T, 'optional' | 'nullable' | 'nullish'> & HiddenMethods<T>;
|
|
387
|
-
/** A schema field marked as hidden from default query results. */
|
|
388
|
-
type HiddenSchema<T extends z.ZodType> = Omit<T, 'optional' | 'nullable' | 'nullish'> & HiddenMethods<T> & {
|
|
389
|
-
readonly __hidden: true;
|
|
390
|
-
};
|
|
391
|
-
type HiddenMethods<T extends z.ZodType> = {
|
|
392
|
-
hidden(): HiddenSchema<T>;
|
|
393
|
-
optional(): HiddenCapable<z.ZodOptional<T>>;
|
|
394
|
-
nullable(): HiddenCapable<z.ZodNullable<T>>;
|
|
395
|
-
nullish(): HiddenCapable<z.ZodOptional<z.ZodNullable<T>>>;
|
|
396
|
-
};
|
|
397
|
-
/** Create a MongoDB ObjectId schema. */
|
|
398
|
-
declare const objectId: () => HiddenCapable<z.ZodInstanceOf<ObjectId$1>>;
|
|
399
|
-
//#endregion
|
|
400
406
|
//#region src/api.d.ts
|
|
401
407
|
type ZodConstructorKey = { [Key in keyof typeof z]: Key extends string ? Key extends Lowercase<Key> ? (typeof z)[Key] extends ((...args: any[]) => any) ? Key : never : never : never; }[keyof typeof z];
|
|
402
408
|
type ZodConstructors = Pick<typeof z, ZodConstructorKey>;
|
package/dist/index.mjs
CHANGED
|
@@ -171,16 +171,19 @@ var Schema = class Schema {
|
|
|
171
171
|
if (options.softdelete && "deletedAt" in this.definition.shape) throw new Error("The deletedAt field is managed by Mongorm");
|
|
172
172
|
const managedShape = {
|
|
173
173
|
...options.timestamps ? {
|
|
174
|
-
createdAt: z.date().default(() => /* @__PURE__ */ new Date()),
|
|
175
|
-
updatedAt: z.date().default(() => /* @__PURE__ */ new Date())
|
|
174
|
+
createdAt: this.managedField(z.date().default(() => /* @__PURE__ */ new Date()), options),
|
|
175
|
+
updatedAt: this.managedField(z.date().default(() => /* @__PURE__ */ new Date()), options)
|
|
176
176
|
} : {},
|
|
177
|
-
...options.softdelete ? { deletedAt: z.date().nullable().default(null) } : {}
|
|
177
|
+
...options.softdelete ? { deletedAt: this.managedField(z.date().nullable().default(null), options) } : {}
|
|
178
178
|
};
|
|
179
179
|
return new Schema({
|
|
180
180
|
...this.definition.shape,
|
|
181
181
|
...managedShape
|
|
182
182
|
}, this.relationMap, this.scopeMap, options, this.indexDefinitions);
|
|
183
183
|
}
|
|
184
|
+
managedField(field, options) {
|
|
185
|
+
return options.hideManaged ? withHidden(field).hidden() : field;
|
|
186
|
+
}
|
|
184
187
|
/** Declare MongoDB indexes for explicit synchronization with the database. */
|
|
185
188
|
indexes(definitions) {
|
|
186
189
|
if (definitions.some(({ fields }) => Object.keys(fields).length === 0)) throw new Error("Index definitions must include at least one field");
|