@classytic/mongokit 3.16.0 → 3.16.2

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/README.md CHANGED
@@ -284,6 +284,28 @@ type UserRepo = Repository<UserDoc> &
284
284
  BatchOperationsMethods;
285
285
  ```
286
286
 
287
+ ### Type your Mongoose model — never cast `as never`
288
+
289
+ Declare the model with its doc type so `Model<UserDoc>` flows straight into
290
+ `Repository<UserDoc>` and `createMongooseAdapter` with no casts:
291
+
292
+ ```ts
293
+ // ✅ Typed — flows cleanly into Repository + the arc adapter
294
+ export type UserDoc = InferSchemaType<typeof UserSchema> & { _id: Types.ObjectId };
295
+ export const UserModel = model<UserDoc>('User', UserSchema);
296
+ export const userRepo = new Repository<UserDoc>(UserModel, [/* plugins */]);
297
+
298
+ // ❌ Untyped — the inferred doc type diverges from `UserDoc`, and
299
+ // `mongoose.Model<T>` is INVARIANT in T, so it won't unify. The
300
+ // tempting "fix" is `as never` — don't; type the model instead.
301
+ export const UserModel = model('User', UserSchema); // Model<inferred>
302
+ new Repository<UserDoc>(UserModel as never, []); // ⛔ cast smell
303
+ ```
304
+
305
+ The cast isn't a mongokit typing gap — it's a symptom of an untyped model.
306
+ One generic at the `model<Doc>(...)` call site removes it everywhere
307
+ (`Repository`, `createMongooseAdapter`, and any `RepositoryLike<Doc>` site).
308
+
287
309
  Feature-detect portable behavior with:
288
310
 
289
311
  ```ts
@@ -65,8 +65,11 @@ interface RegisterBetterAuthStubsOptions {
65
65
  * Mongoose-based resources can `.populate()` references to BA-owned
66
66
  * documents. Idempotent — safe to call multiple times.
67
67
  *
68
- * Schemas are `strict: false` and `_id: false` so BA's writes flow through
69
- * unchanged and Mongoose hydrates whatever BA wrote. Use this when you have
68
+ * Schemas are `strict: false` (so BA's writes flow through unchanged and
69
+ * Mongoose hydrates whatever BA wrote) and keep the DEFAULT ObjectId `_id`
70
+ * SchemaType — BA's mongo adapter stores ObjectId `_id`s, so the caster is
71
+ * required for `findById`, `_id` filters, and `populate`/`ref` to resolve a
72
+ * hex-string id (see the NOTE in the loop below). Use this when you have
70
73
  * resources that *reference* BA collections (`createdBy: { ref: 'user' }`)
71
74
  * but don't expose CRUD on the BA collection itself.
72
75
  *
@@ -7,8 +7,11 @@ import { pluralizeBetterAuthCollection, resolveBetterAuthCollections } from "@cl
7
7
  * Mongoose-based resources can `.populate()` references to BA-owned
8
8
  * documents. Idempotent — safe to call multiple times.
9
9
  *
10
- * Schemas are `strict: false` and `_id: false` so BA's writes flow through
11
- * unchanged and Mongoose hydrates whatever BA wrote. Use this when you have
10
+ * Schemas are `strict: false` (so BA's writes flow through unchanged and
11
+ * Mongoose hydrates whatever BA wrote) and keep the DEFAULT ObjectId `_id`
12
+ * SchemaType — BA's mongo adapter stores ObjectId `_id`s, so the caster is
13
+ * required for `findById`, `_id` filters, and `populate`/`ref` to resolve a
14
+ * hex-string id (see the NOTE in the loop below). Use this when you have
12
15
  * resources that *reference* BA collections (`createdBy: { ref: 'user' }`)
13
16
  * but don't expose CRUD on the BA collection itself.
14
17
  *
@@ -26,7 +29,6 @@ function registerBetterAuthStubs(mongoose, options = {}) {
26
29
  const schema = new mongoose.Schema({}, {
27
30
  strict: false,
28
31
  collection: finalName,
29
- _id: false,
30
32
  timestamps: false
31
33
  });
32
34
  mongoose.model(finalName, schema);
package/dist/index.mjs CHANGED
@@ -9,7 +9,7 @@ import { isGeoOperator, parseGeoFilter } from "./query/primitives/geo.mjs";
9
9
  import { i as getMongooseProjection, n as filterResponseData, r as getFieldsForUser, t as createFieldPreset } from "./field-selection-CQW3WBiK.mjs";
10
10
  import { buildFieldTypeMap, coerceFieldValue, coerceHeuristic } from "./query/primitives/coercion.mjs";
11
11
  import { extractSchemaIndexes } from "./query/primitives/indexes.mjs";
12
- import { n as buildCrudSchemasFromMongooseSchema, r as createMemoryCache, t as buildCrudSchemasFromModel } from "./mongooseToJsonSchema-Bi0q0pYW.mjs";
12
+ import { n as buildCrudSchemasFromMongooseSchema, r as createMemoryCache, t as buildCrudSchemasFromModel } from "./mongooseToJsonSchema-CMM2QHzd.mjs";
13
13
  import { isControlParam } from "@classytic/repo-core/query-parser";
14
14
  //#region src/create-repository.ts
15
15
  function isLoggerAudit(value) {
@@ -505,10 +505,37 @@ function convertTreeToJsonSchema(tree, options, seen = /* @__PURE__ */ new WeakS
505
505
  if (required.length) schema.required = required;
506
506
  return schema;
507
507
  }
508
+ /**
509
+ * Recursively delete every `default` keyword from a JSON-schema subtree
510
+ * (properties, array items, nested subdocuments).
511
+ *
512
+ * A `default` has no place in an UPDATE (PATCH) validator. Hosts that run
513
+ * Fastify/AJV with `useDefaults: true` (arc's `createApp` does) would
514
+ * otherwise have the default INJECTED into any PATCH body that omits the
515
+ * field — and the repository then `$set`s it, silently overwriting stored
516
+ * data on a partial update. The canonical bite: a nullable field carrying
517
+ * `default: null` (e.g. `publishedAt`) gets nulled out on every
518
+ * content-only save, unpublishing the doc. Partial updates must touch only
519
+ * the fields the client actually sent; DB-layer defaults still apply on
520
+ * create via Mongoose. The nullable type widening (`type: [..., 'null']`)
521
+ * stays, so a client may still explicitly send `null` — it just won't be
522
+ * auto-injected.
523
+ */
524
+ function stripDefaultsDeep(node) {
525
+ if (Array.isArray(node)) {
526
+ for (const item of node) stripDefaultsDeep(item);
527
+ return;
528
+ }
529
+ if (!node || typeof node !== "object") return;
530
+ const obj = node;
531
+ delete obj.default;
532
+ for (const value of Object.values(obj)) stripDefaultsDeep(value);
533
+ }
508
534
  function buildJsonSchemaForUpdate(createJson, options) {
509
535
  const clone = JSON.parse(JSON.stringify(createJson));
510
536
  delete clone.required;
511
537
  applyFieldRules(clone, collectFieldsToOmit(options, "update"), options);
538
+ stripDefaultsDeep(clone);
512
539
  if (options?.strictAdditionalProperties === true) clone.additionalProperties = false;
513
540
  if (options?.update?.requireAtLeastOne === true) clone.minProperties = 1;
514
541
  return clone;
@@ -2,5 +2,5 @@ import { t as configureLogger } from "../logger-DprijP5h.mjs";
2
2
  import { t as createError } from "../error-B00pI7dV.mjs";
3
3
  import { n as isObjectIdInstance, r as isValidIdForType, t as getSchemaIdType } from "../id-resolution-DduoO4LI.mjs";
4
4
  import { i as getMongooseProjection, n as filterResponseData, r as getFieldsForUser, t as createFieldPreset } from "../field-selection-CQW3WBiK.mjs";
5
- import { n as buildCrudSchemasFromMongooseSchema, r as createMemoryCache, t as buildCrudSchemasFromModel } from "../mongooseToJsonSchema-Bi0q0pYW.mjs";
5
+ import { n as buildCrudSchemasFromMongooseSchema, r as createMemoryCache, t as buildCrudSchemasFromModel } from "../mongooseToJsonSchema-CMM2QHzd.mjs";
6
6
  export { buildCrudSchemasFromModel, buildCrudSchemasFromMongooseSchema, configureLogger, createError, createFieldPreset, createMemoryCache, filterResponseData, getFieldsForUser, getMongooseProjection, getSchemaIdType, isObjectIdInstance, isValidIdForType };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classytic/mongokit",
3
- "version": "3.16.0",
3
+ "version": "3.16.2",
4
4
  "description": "Production-grade MongoDB repositories - smart pagination, events, and plugins. Built on @classytic/repo-core.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -145,8 +145,8 @@
145
145
  },
146
146
  "devDependencies": {
147
147
  "@arethetypeswrong/cli": "^0.18.2",
148
- "@better-auth/api-key": "^1.6.9",
149
- "@better-auth/mongo-adapter": "^1.6.9",
148
+ "@better-auth/api-key": "^1.6.22",
149
+ "@better-auth/mongo-adapter": "^1.6.22",
150
150
  "@biomejs/biome": "^2.4.10",
151
151
  "@classytic/dev-tools": "^0.2.0",
152
152
  "@classytic/repo-core": "0.6.0",
@@ -154,7 +154,7 @@
154
154
  "@vitest/coverage-v8": "^4.1.4",
155
155
  "ajv": "^8.18.0",
156
156
  "ajv-formats": "^3.0.1",
157
- "better-auth": "^1.6.9",
157
+ "better-auth": "^1.6.22",
158
158
  "dotenv": "^17.4.2",
159
159
  "knip": "^6.3.0",
160
160
  "mongodb-memory-server": "^10.4.3",
@@ -163,5 +163,9 @@
163
163
  "tsdown": "^0.21.8",
164
164
  "typescript": "^6.0.3",
165
165
  "vitest": "^4.1.4"
166
+ },
167
+ "overrides": {
168
+ "mongodb": "7.2.0",
169
+ "bson": "7.2.0"
166
170
  }
167
171
  }