kitcn 0.33.12 → 0.34.1

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.
Files changed (28) hide show
  1. package/CHANGELOG.md +71 -0
  2. package/dist/aggregate/index.d.ts +1 -1
  3. package/dist/aggregate/index.js +2 -2
  4. package/dist/auth/generated/index.d.ts +1 -1
  5. package/dist/auth/index.d.ts +34 -17
  6. package/dist/auth/index.js +40 -3
  7. package/dist/cli.mjs +327 -143
  8. package/dist/{definitions-n-CgVbZb.js → definitions-moOcjYL-.js} +2 -2
  9. package/dist/{extensions-Bp0XHanR.js → extensions-CKfn_Qi-.js} +1 -1
  10. package/dist/{generated-contract-disabled-EEz35c4P.d.ts → generated-contract-disabled-DcgLIEHK.d.ts} +41 -41
  11. package/dist/{index-utils-DvK7P6Q1.js → index-utils-COOY08yW.js} +1 -1
  12. package/dist/{local-env-nNg6w6m5.mjs → local-env-SOUm4WE1.mjs} +286 -167
  13. package/dist/orm/aggregate-index/index.js +4 -4
  14. package/dist/orm/index.d.ts +1 -1
  15. package/dist/orm/index.js +132 -83
  16. package/dist/orm/migrations/index.d.ts +1 -1
  17. package/dist/orm/migrations/index.js +1 -1
  18. package/dist/{runtime-D60Ss1yA.js → runtime-BDgBpBg4.js} +1 -1
  19. package/dist/{schema-m04X6AL_.js → schema-CFklsQxQ.js} +5 -5
  20. package/dist/{schema-DpYdVxNn.js → schema-D0l-RIbL.js} +1 -1
  21. package/dist/{symbols-DDNAddkd.js → symbols-h1M2uuSB.js} +2 -1
  22. package/dist/{table-mQfS8B1J.js → table-Bh9i5LCc.js} +200 -108
  23. package/dist/watcher.mjs +1 -1
  24. package/dist/{where-clause-compiler-D67bItsZ.d.ts → where-clause-compiler-ChAN1JQa.d.ts} +129 -129
  25. package/package.json +2 -2
  26. package/skills/kitcn/references/features/auth.md +9 -0
  27. package/skills/kitcn/references/features/migrations.md +1 -2
  28. package/skills/kitcn/references/setup/index.md +11 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,76 @@
1
1
  # kitcn
2
2
 
3
+ ## 0.34.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#504](https://github.com/udecode/kitcn/pull/504) [`28b6682`](https://github.com/udecode/kitcn/commit/28b66826db4b344df64da5283e8b9e5f307016fd) Thanks [@ghandic](https://github.com/ghandic)! - ## Patches
8
+
9
+ - Fix `kitcn migrate up`, `migrate down`, `deploy` and `dev` so each migration step runs with its own `batchSize`. The CLI sends a batch size only when `--migrations-batch-size` or `batchSize` in the `kitcn.json` migrations config sets one, and a step without `batchSize` runs 128 documents per batch.
10
+
11
+ ## 0.34.0
12
+
13
+ ### Minor Changes
14
+
15
+ - [#494](https://github.com/udecode/kitcn/pull/494) [`182403a`](https://github.com/udecode/kitcn/commit/182403a7c4c6976a96b1fdbaf842bfa98904386e) Thanks [@EfficiencyCorp](https://github.com/EfficiencyCorp)! - ## Breaking changes
16
+
17
+ - Build ORM tables on first use. `convexTable(...)` no longer throws definition errors (invalid indexes, constraints, extra config). They throw when the schema is pushed, when `kitcn codegen` runs, or when `schema.export()` is called.
18
+
19
+ ```ts
20
+ // Before: throws here
21
+ const posts = convexTable("posts", { text: text() }, () => [
22
+ searchIndex("s"),
23
+ ]);
24
+
25
+ // After: throws on push and codegen, or in a test with schema.export()
26
+ const schema = defineSchema({ posts });
27
+ schema.export();
28
+ ```
29
+
30
+ - Reference columns in extra config through the table argument. A column builder variable shared across tables and used directly in an index throws an ownership error at push.
31
+
32
+ ```ts
33
+ // Before
34
+ const tenantId = text().notNull();
35
+ const a = convexTable("a", { tenantId }, () => [index("a_t").on(tenantId)]);
36
+
37
+ // After
38
+ const a = convexTable("a", { tenantId }, (t) => [
39
+ index("a_t").on(t.tenantId),
40
+ ]);
41
+ ```
42
+
43
+ - Require `rlsPolicy(...).link(table)` at module scope, in `schema.ts` or in the module that declares the table. A linked policy applies to its table as soon as it is linked.
44
+
45
+ ```ts
46
+ // Before: linked inside another table's extra config
47
+ const posts = convexTable("posts", { body: text() }, () => [
48
+ rlsPolicy("read_users", { for: "select" }).link(users),
49
+ ]);
50
+
51
+ // After: link at module scope
52
+ export const readUsers = rlsPolicy("read_users", { for: "select" }).link(
53
+ users
54
+ );
55
+ ```
56
+
57
+ ## Patches
58
+
59
+ - Preserve self-referencing foreign keys whose callbacks read table metadata through `getTableConfig`.
60
+ - Preserve foreign-key targets and required fields when column builders are reused across tables or in a discriminator.
61
+ - Improve cold Convex function start: a query or insert builds only the tables it touches, and query contexts no longer build every table's query builder and foreign-key graph. The first update or delete still reads every table's foreign keys, and `createOrm` without the aggregate capability still builds every table.
62
+ - Fix broken `references(() => ...)` callbacks surfacing on the first ORM call instead of on push, and passing on a retry.
63
+ - Improve `kitcn codegen` speed on large projects: a module that many Convex modules import evaluates once per run instead of once per importer.
64
+
65
+ ### Patch Changes
66
+
67
+ - [#495](https://github.com/udecode/kitcn/pull/495) [`a2eb3fe`](https://github.com/udecode/kitcn/commit/a2eb3fee2bfa51191715ae6b99df0980570af47c) Thanks [@EfficiencyCorp](https://github.com/EfficiencyCorp)! - ## Patches
68
+
69
+ - Support Convex 1.50.x and 1.51.x in `kitcn`. New apps install Convex 1.51.0.
70
+ - Support `--env-file` in standalone `kitcn init` and `kitcn codegen` to generate bindings against an existing deployment while keeping the project default.
71
+ - Fix `kitcn dev --env-file` using a different Convex account during initialization or environment sync than during the main dev process.
72
+ - Fix Better Auth creates that carry their own id (`forceAllowId`) failing with `` Unexpected field `_id` ``: organization invitations on Better Auth 1.7.16 with default options, and `reserveVerificationValue` (magic link and email OTP for unverified users, SIWE, SAML). The row now gets its Convex id, and Better Auth reads it back under that id. A second `reserveVerificationValue` for the same identifier is still refused while the first row exists. A forced-id `oauthClientAssertion` create (`private_key_jwt`) is refused, so a replayed assertion cannot get in.
73
+
3
74
  ## 0.33.12
4
75
 
5
76
  ### Patch Changes
@@ -1,5 +1,5 @@
1
1
  import { Vn as ConvexTextBuilderInitial, Zt as ConvexTableWithColumns } from "../capabilities-Bxzoofl4.js";
2
- import { C as ConvexNumberBuilderInitial, E as ConvexIdBuilderInitial, N as ConvexCustomBuilderInitial } from "../where-clause-compiler-D67bItsZ.js";
2
+ import { C as ConvexNumberBuilderInitial, E as ConvexIdBuilderInitial, N as ConvexCustomBuilderInitial } from "../where-clause-compiler-ChAN1JQa.js";
3
3
  import * as convex_values0 from "convex/values";
4
4
  import { GenericId, Infer, Value } from "convex/values";
5
5
  import { DocumentByName, GenericDataModel, GenericDatabaseReader, GenericDatabaseWriter, TableNamesInDataModel } from "convex/server";
@@ -1,5 +1,5 @@
1
- import { n as TableAggregate$1, t as DirectAggregate$1 } from "../runtime-D60Ss1yA.js";
2
- import { i as aggregateStorageTables } from "../schema-DpYdVxNn.js";
1
+ import { n as TableAggregate$1, t as DirectAggregate$1 } from "../runtime-BDgBpBg4.js";
2
+ import { i as aggregateStorageTables } from "../schema-D0l-RIbL.js";
3
3
 
4
4
  //#region src/aggregate/index.ts
5
5
  const wrapTriggerFactory = (methodName, factory) => ((...args) => {
@@ -1,2 +1,2 @@
1
- import { C as GenericAuthTriggerHandlers, S as GenericAuthTriggerChange, T as defineAuth, b as GenericAuthBeforeResult, i as getGeneratedAuthDisabledReason, n as GeneratedAuthDisabledReasonKind, r as createDisabledAuthRuntime, t as AuthRuntime, w as GenericAuthTriggers, x as GenericAuthDefinition, y as BetterAuthOptionsWithoutDatabase } from "../../generated-contract-disabled-EEz35c4P.js";
1
+ import { C as GenericAuthTriggerHandlers, S as GenericAuthTriggerChange, T as defineAuth, b as GenericAuthBeforeResult, i as getGeneratedAuthDisabledReason, n as GeneratedAuthDisabledReasonKind, r as createDisabledAuthRuntime, t as AuthRuntime, w as GenericAuthTriggers, x as GenericAuthDefinition, y as BetterAuthOptionsWithoutDatabase } from "../../generated-contract-disabled-DcgLIEHK.js";
2
2
  export { type AuthRuntime, BetterAuthOptionsWithoutDatabase, type GeneratedAuthDisabledReasonKind, GenericAuthBeforeResult, GenericAuthDefinition, GenericAuthTriggerChange, GenericAuthTriggerHandlers, GenericAuthTriggers, createDisabledAuthRuntime, defineAuth, getGeneratedAuthDisabledReason };
@@ -1,7 +1,7 @@
1
1
  import { a as QueryCtxWithPreferredOrmQueryTable, n as LookupByIdResultByCtx, t as DocByCtx } from "../query-context-BkNjkDCk.js";
2
2
  import { t as GetAuth } from "../types-ex0-J-SC.js";
3
3
  import { t as GenericCtx } from "../context-utils-Cbv4r0AA.js";
4
- import { C as GenericAuthTriggerHandlers, S as GenericAuthTriggerChange, T as defineAuth, _ as updateManyHandler, a as AuthFunctions, b as GenericAuthBeforeResult, c as consumeOneHandler, d as createHandler, f as deleteManyHandler, g as incrementOneHandler, h as findOneHandler, i as getGeneratedAuthDisabledReason, l as countHandler, m as findManyHandler, n as GeneratedAuthDisabledReasonKind, o as Triggers, p as deleteOneHandler, r as createDisabledAuthRuntime, s as createClient, t as AuthRuntime, u as createApi, v as updateOneHandler, w as GenericAuthTriggers, x as GenericAuthDefinition, y as BetterAuthOptionsWithoutDatabase } from "../generated-contract-disabled-EEz35c4P.js";
4
+ import { C as GenericAuthTriggerHandlers, S as GenericAuthTriggerChange, T as defineAuth, _ as updateManyHandler, a as AuthFunctions, b as GenericAuthBeforeResult, c as consumeOneHandler, d as createHandler, f as deleteManyHandler, g as incrementOneHandler, h as findOneHandler, i as getGeneratedAuthDisabledReason, l as countHandler, m as findManyHandler, n as GeneratedAuthDisabledReasonKind, o as Triggers, p as deleteOneHandler, r as createDisabledAuthRuntime, s as createClient, t as AuthRuntime, u as createApi, v as updateOneHandler, w as GenericAuthTriggers, x as GenericAuthDefinition, y as BetterAuthOptionsWithoutDatabase } from "../generated-contract-disabled-DcgLIEHK.js";
5
5
  import * as convex_values0 from "convex/values";
6
6
  import { Infer } from "convex/values";
7
7
  import { AuthConfig, DocumentByName, GenericDataModel, GenericMutationCtx, GenericQueryCtx, GenericSchema, PaginationOptions, PaginationResult, SchemaDefinition, TableNamesInDataModel } from "convex/server";
@@ -109,28 +109,28 @@ type AdapterPaginationOptions = PaginationOptions & {
109
109
  };
110
110
  declare const adapterWhereValidator: convex_values0.VObject<{
111
111
  mode?: "sensitive" | "insensitive" | undefined;
112
- operator?: "eq" | "ne" | "lt" | "lte" | "gt" | "gte" | "in" | "not_in" | "contains" | "starts_with" | "ends_with" | undefined;
113
112
  connector?: "AND" | "OR" | undefined;
114
- value: string | number | boolean | string[] | number[] | null;
113
+ operator?: "lt" | "lte" | "gt" | "gte" | "eq" | "in" | "not_in" | "ne" | "contains" | "starts_with" | "ends_with" | undefined;
115
114
  field: string;
115
+ value: string | number | boolean | string[] | number[] | null;
116
116
  }, {
117
117
  connector: convex_values0.VUnion<"AND" | "OR" | undefined, [convex_values0.VLiteral<"AND", "required">, convex_values0.VLiteral<"OR", "required">], "optional", never>;
118
118
  field: convex_values0.VString<string, "required">;
119
119
  mode: convex_values0.VUnion<"sensitive" | "insensitive" | undefined, [convex_values0.VLiteral<"sensitive", "required">, convex_values0.VLiteral<"insensitive", "required">], "optional", never>;
120
- operator: convex_values0.VUnion<"eq" | "ne" | "lt" | "lte" | "gt" | "gte" | "in" | "not_in" | "contains" | "starts_with" | "ends_with" | undefined, [convex_values0.VLiteral<"lt", "required">, convex_values0.VLiteral<"lte", "required">, convex_values0.VLiteral<"gt", "required">, convex_values0.VLiteral<"gte", "required">, convex_values0.VLiteral<"eq", "required">, convex_values0.VLiteral<"in", "required">, convex_values0.VLiteral<"not_in", "required">, convex_values0.VLiteral<"ne", "required">, convex_values0.VLiteral<"contains", "required">, convex_values0.VLiteral<"starts_with", "required">, convex_values0.VLiteral<"ends_with", "required">], "optional", never>;
120
+ operator: convex_values0.VUnion<"lt" | "lte" | "gt" | "gte" | "eq" | "in" | "not_in" | "ne" | "contains" | "starts_with" | "ends_with" | undefined, [convex_values0.VLiteral<"lt", "required">, convex_values0.VLiteral<"lte", "required">, convex_values0.VLiteral<"gt", "required">, convex_values0.VLiteral<"gte", "required">, convex_values0.VLiteral<"eq", "required">, convex_values0.VLiteral<"in", "required">, convex_values0.VLiteral<"not_in", "required">, convex_values0.VLiteral<"ne", "required">, convex_values0.VLiteral<"contains", "required">, convex_values0.VLiteral<"starts_with", "required">, convex_values0.VLiteral<"ends_with", "required">], "optional", never>;
121
121
  value: convex_values0.VUnion<string | number | boolean | string[] | number[] | null, [convex_values0.VString<string, "required">, convex_values0.VFloat64<number, "required">, convex_values0.VBoolean<boolean, "required">, convex_values0.VArray<string[], convex_values0.VString<string, "required">, "required">, convex_values0.VArray<number[], convex_values0.VFloat64<number, "required">, "required">, convex_values0.VNull<null, "required">], "required", never>;
122
- }, "required", "mode" | "operator" | "value" | "field" | "connector">;
122
+ }, "required", "mode" | "connector" | "field" | "operator" | "value">;
123
123
  declare const adapterArgsValidator: convex_values0.VObject<{
124
- limit?: number | undefined;
125
124
  where?: {
126
125
  mode?: "sensitive" | "insensitive" | undefined;
127
- operator?: "eq" | "ne" | "lt" | "lte" | "gt" | "gte" | "in" | "not_in" | "contains" | "starts_with" | "ends_with" | undefined;
128
126
  connector?: "AND" | "OR" | undefined;
129
- value: string | number | boolean | string[] | number[] | null;
127
+ operator?: "lt" | "lte" | "gt" | "gte" | "eq" | "in" | "not_in" | "ne" | "contains" | "starts_with" | "ends_with" | undefined;
130
128
  field: string;
129
+ value: string | number | boolean | string[] | number[] | null;
131
130
  }[] | undefined;
132
- select?: string[] | undefined;
131
+ limit?: number | undefined;
133
132
  offset?: number | undefined;
133
+ select?: string[] | undefined;
134
134
  sortBy?: {
135
135
  field: string;
136
136
  direction: "asc" | "desc";
@@ -150,26 +150,43 @@ declare const adapterArgsValidator: convex_values0.VObject<{
150
150
  }, "optional", "field" | "direction">;
151
151
  where: convex_values0.VArray<{
152
152
  mode?: "sensitive" | "insensitive" | undefined;
153
- operator?: "eq" | "ne" | "lt" | "lte" | "gt" | "gte" | "in" | "not_in" | "contains" | "starts_with" | "ends_with" | undefined;
154
153
  connector?: "AND" | "OR" | undefined;
155
- value: string | number | boolean | string[] | number[] | null;
154
+ operator?: "lt" | "lte" | "gt" | "gte" | "eq" | "in" | "not_in" | "ne" | "contains" | "starts_with" | "ends_with" | undefined;
156
155
  field: string;
156
+ value: string | number | boolean | string[] | number[] | null;
157
157
  }[] | undefined, convex_values0.VObject<{
158
158
  mode?: "sensitive" | "insensitive" | undefined;
159
- operator?: "eq" | "ne" | "lt" | "lte" | "gt" | "gte" | "in" | "not_in" | "contains" | "starts_with" | "ends_with" | undefined;
160
159
  connector?: "AND" | "OR" | undefined;
161
- value: string | number | boolean | string[] | number[] | null;
160
+ operator?: "lt" | "lte" | "gt" | "gte" | "eq" | "in" | "not_in" | "ne" | "contains" | "starts_with" | "ends_with" | undefined;
162
161
  field: string;
162
+ value: string | number | boolean | string[] | number[] | null;
163
163
  }, {
164
164
  connector: convex_values0.VUnion<"AND" | "OR" | undefined, [convex_values0.VLiteral<"AND", "required">, convex_values0.VLiteral<"OR", "required">], "optional", never>;
165
165
  field: convex_values0.VString<string, "required">;
166
166
  mode: convex_values0.VUnion<"sensitive" | "insensitive" | undefined, [convex_values0.VLiteral<"sensitive", "required">, convex_values0.VLiteral<"insensitive", "required">], "optional", never>;
167
- operator: convex_values0.VUnion<"eq" | "ne" | "lt" | "lte" | "gt" | "gte" | "in" | "not_in" | "contains" | "starts_with" | "ends_with" | undefined, [convex_values0.VLiteral<"lt", "required">, convex_values0.VLiteral<"lte", "required">, convex_values0.VLiteral<"gt", "required">, convex_values0.VLiteral<"gte", "required">, convex_values0.VLiteral<"eq", "required">, convex_values0.VLiteral<"in", "required">, convex_values0.VLiteral<"not_in", "required">, convex_values0.VLiteral<"ne", "required">, convex_values0.VLiteral<"contains", "required">, convex_values0.VLiteral<"starts_with", "required">, convex_values0.VLiteral<"ends_with", "required">], "optional", never>;
167
+ operator: convex_values0.VUnion<"lt" | "lte" | "gt" | "gte" | "eq" | "in" | "not_in" | "ne" | "contains" | "starts_with" | "ends_with" | undefined, [convex_values0.VLiteral<"lt", "required">, convex_values0.VLiteral<"lte", "required">, convex_values0.VLiteral<"gt", "required">, convex_values0.VLiteral<"gte", "required">, convex_values0.VLiteral<"eq", "required">, convex_values0.VLiteral<"in", "required">, convex_values0.VLiteral<"not_in", "required">, convex_values0.VLiteral<"ne", "required">, convex_values0.VLiteral<"contains", "required">, convex_values0.VLiteral<"starts_with", "required">, convex_values0.VLiteral<"ends_with", "required">], "optional", never>;
168
168
  value: convex_values0.VUnion<string | number | boolean | string[] | number[] | null, [convex_values0.VString<string, "required">, convex_values0.VFloat64<number, "required">, convex_values0.VBoolean<boolean, "required">, convex_values0.VArray<string[], convex_values0.VString<string, "required">, "required">, convex_values0.VArray<number[], convex_values0.VFloat64<number, "required">, "required">, convex_values0.VNull<null, "required">], "required", never>;
169
- }, "required", "mode" | "operator" | "value" | "field" | "connector">, "optional">;
170
- }, "required", "limit" | "model" | "where" | "select" | "offset" | "sortBy" | "sortBy.field" | "sortBy.direction">;
169
+ }, "required", "mode" | "connector" | "field" | "operator" | "value">, "optional">;
170
+ }, "required", "model" | "where" | "limit" | "offset" | "select" | "sortBy" | "sortBy.field" | "sortBy.direction">;
171
171
  declare const hasUniqueFields: (betterAuthSchema: BetterAuthDBSchema, model: string, input: Record<string, any>) => boolean;
172
172
  declare const checkUniqueFields: <Schema extends SchemaDefinition<any, any>>(ctx: GenericQueryCtx<GenericDataModel>, schema: Schema, betterAuthSchema: BetterAuthDBSchema, table: string, input: Record<string, any>, doc?: Record<string, any>) => Promise<void>;
173
+ /**
174
+ * Better Auth gates single-use work on a forced id: it derives the id from a
175
+ * one-time key, so the first insert wins and every later one fails on the
176
+ * duplicate key. `reserveVerificationValue` does this on the verification
177
+ * model (magic link and email OTP cleanup locks, SIWE email claims, DPoP
178
+ * proofs, SAML assertion ids), and `private_key_jwt` does it on
179
+ * `oauthClientAssertion` with each assertion's `jti`.
180
+ *
181
+ * Convex picks its own ids and cannot store the forced one. On the
182
+ * verification model the identifier stands in for it: a forced-id create is
183
+ * refused while a row with the same identifier exists, as the duplicate key
184
+ * would refuse it. The read and the insert run in one mutation, so concurrent
185
+ * reservations serialize on the identifier index. A schema without that index
186
+ * fails closed. A client assertion row holds nothing but its id, so its
187
+ * forced-id create is refused outright rather than letting every replay in.
188
+ */
189
+ declare const checkForcedId: <Schema extends SchemaDefinition<any, any>>(ctx: GenericQueryCtx<GenericDataModel>, schema: Schema, betterAuthSchema: BetterAuthDBSchema, table: string, input: Record<string, any>) => Promise<void>;
173
190
  declare const selectFields: <T extends TableNamesInDataModel<GenericDataModel>, D extends DocumentByName<GenericDataModel, T>>(doc: D | null, select?: string[]) => D | null;
174
191
  declare const paginate: <Doc extends DocumentByName<GenericDataModel, T>, T extends TableNamesInDataModel<GenericDataModel>>(ctx: GenericQueryCtx<GenericDataModel>, schema: SchemaDefinition<any, any>, betterAuthSchema: BetterAuthDBSchema, args: Infer<typeof adapterArgsValidator> & {
175
192
  paginationOpts: AdapterPaginationOptions;
@@ -531,4 +548,4 @@ declare const convex: (opts: {
531
548
  };
532
549
  };
533
550
  //#endregion
534
- export { type AuthFunctions, type AuthRuntime, BetterAuthOptionsWithoutDatabase, ConvexCleanedWhere, type GeneratedAuthDisabledReasonKind, GenericAuthBeforeResult, GenericAuthDefinition, GenericAuthTriggerChange, GenericAuthTriggerHandlers, GenericAuthTriggers, GetAuth, SessionClientSignals, type Triggers, adapterArgsValidator, adapterConfig, adapterWhereValidator, checkUniqueFields, consumeOneHandler, convex, countHandler, createApi, createAuthRuntime, createClient, createDisabledAuthRuntime, createHandler, dbAdapter, defineAuth, deleteManyHandler, deleteOneHandler, findManyHandler, findOneHandler, getAuthUserId, getAuthUserIdentity, getGeneratedAuthDisabledReason, getHeaders, getInvalidAuthDefinitionExportReason, getSession, getSessionNetworkSignals, handlePagination, hasUniqueFields, httpAdapter, incrementOneHandler, listOne, paginate, resolveGeneratedAuthDefinition, selectFields, updateManyHandler, updateOneHandler };
551
+ export { type AuthFunctions, type AuthRuntime, BetterAuthOptionsWithoutDatabase, ConvexCleanedWhere, type GeneratedAuthDisabledReasonKind, GenericAuthBeforeResult, GenericAuthDefinition, GenericAuthTriggerChange, GenericAuthTriggerHandlers, GenericAuthTriggers, GetAuth, SessionClientSignals, type Triggers, adapterArgsValidator, adapterConfig, adapterWhereValidator, checkForcedId, checkUniqueFields, consumeOneHandler, convex, countHandler, createApi, createAuthRuntime, createClient, createDisabledAuthRuntime, createHandler, dbAdapter, defineAuth, deleteManyHandler, deleteOneHandler, findManyHandler, findOneHandler, getAuthUserId, getAuthUserIdentity, getGeneratedAuthDisabledReason, getHeaders, getInvalidAuthDefinitionExportReason, getSession, getSessionNetworkSignals, handlePagination, hasUniqueFields, httpAdapter, incrementOneHandler, listOne, paginate, resolveGeneratedAuthDefinition, selectFields, updateManyHandler, updateOneHandler };
@@ -3,7 +3,7 @@ import { i as partial } from "../validators-cPSuYAHV.js";
3
3
  import { i as defineAuth, n as createDisabledAuthRuntime, r as getGeneratedAuthDisabledReason, t as DEFAULT_AUTH_DEFINITION_PATH } from "../generated-contract-disabled-RyPYlwq0.js";
4
4
  import { n as createGeneratedFunctionReference, o as isQueryCtx, s as isRunMutationCtx } from "../api-entry-Buvjl9hn.js";
5
5
  import { i as customQuery, n as customCtx, r as customMutation } from "../customFunctions-CWs3rEjV.js";
6
- import { C as eq, o as getAggregateIndexes } from "../index-utils-DvK7P6Q1.js";
6
+ import { C as eq, o as getAggregateIndexes } from "../index-utils-COOY08yW.js";
7
7
  import { c as stream, f as unsetToken, s as mergedStream, t as getByIdWithOrmQueryFallback } from "../query-context-DQLv58LH.js";
8
8
  import { n as convex } from "../convex-plugin-UGuLZPyQ.js";
9
9
  import { v } from "convex/values";
@@ -168,6 +168,41 @@ const checkUniqueFields = async (ctx, schema, betterAuthSchema, table, input, do
168
168
  if (existingDoc && existingDoc._id !== doc?._id) throw new Error(`${table} ${uniqueIndex.join(", ")} already exists`);
169
169
  }
170
170
  };
171
+ /**
172
+ * Better Auth gates single-use work on a forced id: it derives the id from a
173
+ * one-time key, so the first insert wins and every later one fails on the
174
+ * duplicate key. `reserveVerificationValue` does this on the verification
175
+ * model (magic link and email OTP cleanup locks, SIWE email claims, DPoP
176
+ * proofs, SAML assertion ids), and `private_key_jwt` does it on
177
+ * `oauthClientAssertion` with each assertion's `jti`.
178
+ *
179
+ * Convex picks its own ids and cannot store the forced one. On the
180
+ * verification model the identifier stands in for it: a forced-id create is
181
+ * refused while a row with the same identifier exists, as the duplicate key
182
+ * would refuse it. The read and the insert run in one mutation, so concurrent
183
+ * reservations serialize on the identifier index. A schema without that index
184
+ * fails closed. A client assertion row holds nothing but its id, so its
185
+ * forced-id create is refused outright rather than letting every replay in.
186
+ */
187
+ const checkForcedId = async (ctx, schema, betterAuthSchema, table, input) => {
188
+ const { modelNameToKey } = getSchemaViews(betterAuthSchema);
189
+ const model = modelNameToKey.get(table) ?? table;
190
+ if (model === "oauthClientAssertion") throw new Error(`${table} needs its forced id to block replays, which Convex cannot store`);
191
+ if (model !== "verification") return;
192
+ const field = betterAuthSchema?.verification?.fields?.identifier?.fieldName ?? "identifier";
193
+ const value = input[field];
194
+ if (typeof value !== "string") throw new Error(`${table} ${field} is required with a forced id`);
195
+ const { index } = findIndex(schema, {
196
+ model: table,
197
+ where: [{
198
+ field,
199
+ operator: "eq",
200
+ value
201
+ }]
202
+ }) || {};
203
+ if (!index) throw new Error(`No index found for ${table} ${field}`);
204
+ if (await ctx.db.query(table).withIndex(index.indexDescriptor, (q) => q.eq(field, value)).first()) throw new Error(`${table} ${field} already exists`);
205
+ };
171
206
  const selectFields = (doc, select) => {
172
207
  if (!doc) return null;
173
208
  if (!select?.length) return doc;
@@ -581,8 +616,10 @@ const withAuthTimestamps = (data, schema, betterAuthSchema, model) => {
581
616
  const createHandler = async (ctx, args, schema, betterAuthSchema) => {
582
617
  const triggerCtx = args.triggerCtx ?? ctx;
583
618
  const tableTriggers = args.tableTriggers;
584
- const data = serializeDatesForConvex(withAuthTimestamps(await applyBeforeHook(args.input.model, "create", args.input.data, tableTriggers?.create?.before, triggerCtx), schema, betterAuthSchema, args.input.model));
619
+ const { _id, ...input } = args.input.data;
620
+ const data = serializeDatesForConvex(withAuthTimestamps(await applyBeforeHook(args.input.model, "create", input, tableTriggers?.create?.before, triggerCtx), schema, betterAuthSchema, args.input.model));
585
621
  await checkUniqueFields(ctx, schema, betterAuthSchema, args.input.model, data);
622
+ if (_id !== void 0) await checkForcedId(ctx, schema, betterAuthSchema, args.input.model, data);
586
623
  const ormTable = resolveOrmTable(ctx, schema, betterAuthSchema, args.input.model);
587
624
  const doc = ormTable ? await ormInsert(ctx, ormTable.table, data) : await (async () => {
588
625
  const id = await ctx.db.insert(args.input.model, data);
@@ -1750,4 +1787,4 @@ const getHeaders = async (ctx, session) => {
1750
1787
  };
1751
1788
 
1752
1789
  //#endregion
1753
- export { adapterArgsValidator, adapterConfig, adapterWhereValidator, checkUniqueFields, consumeOneHandler, convex, countHandler, createApi, createAuthRuntime, createClient, createDisabledAuthRuntime, createHandler, dbAdapter, defineAuth, deleteManyHandler, deleteOneHandler, findManyHandler, findOneHandler, getAuthUserId, getAuthUserIdentity, getGeneratedAuthDisabledReason, getHeaders, getInvalidAuthDefinitionExportReason, getSession, getSessionNetworkSignals, handlePagination, hasUniqueFields, httpAdapter, incrementOneHandler, listOne, paginate, resolveGeneratedAuthDefinition, selectFields, updateManyHandler, updateOneHandler };
1790
+ export { adapterArgsValidator, adapterConfig, adapterWhereValidator, checkForcedId, checkUniqueFields, consumeOneHandler, convex, countHandler, createApi, createAuthRuntime, createClient, createDisabledAuthRuntime, createHandler, dbAdapter, defineAuth, deleteManyHandler, deleteOneHandler, findManyHandler, findOneHandler, getAuthUserId, getAuthUserIdentity, getGeneratedAuthDisabledReason, getHeaders, getInvalidAuthDefinitionExportReason, getSession, getSessionNetworkSignals, handlePagination, hasUniqueFields, httpAdapter, incrementOneHandler, listOne, paginate, resolveGeneratedAuthDefinition, selectFields, updateManyHandler, updateOneHandler };