@@ -40,15 +40,16 @@ import type {
4040} from './crud-types' ;
4141import type { Diagnostics } from './diagnostics' ;
4242import type { ClientOptions , QueryOptions } from './options' ;
43- import type { ExtClientMembersBase , ExtQueryArgsBase , ExtResultBase , RuntimePlugin } from './plugin' ;
43+ import type {
44+ ExtClientMembersBase ,
45+ ExtQueryArgsBase ,
46+ ExtResultBase ,
47+ ExtResultInferenceArgs ,
48+ RuntimePlugin ,
49+ } from './plugin' ;
4450import type { ZenStackPromise } from './promise' ;
4551import type { ToKysely } from './query-builder' ;
46- import type {
47- GetSlicedModels ,
48- GetSlicedOperations ,
49- GetSlicedProcedures ,
50- ModelAllowsCreate ,
51- } from './type-utils' ;
52+ import type { GetSlicedModels , GetSlicedOperations , GetSlicedProcedures , ModelAllowsCreate } from './type-utils' ;
5253import type { ZodSchemaFactory } from './zod/factory' ;
5354
5455type TransactionUnsupportedMethods = ( typeof TRANSACTION_UNSUPPORTED_METHODS ) [ number ] ;
@@ -128,12 +129,20 @@ export type ClientContract<
128129 get $auth ( ) : AuthType < Schema > | undefined ;
129130
130131 /**
131- * Sets the current user identity.
132+ * Returns a new client bound to the specified user identity. The original client remains unchanged.
133+ *
134+ * @example
135+ * ```
136+ * const userClient = db.$setAuth({ id: 'user-id' });
137+ * ```
132138 */
133- $setAuth ( auth : AuthType < Schema > | undefined ) : ClientContract < Schema , Options , ExtQueryArgs , ExtClientMembers , ExtResult > ;
139+ $setAuth (
140+ auth : AuthType < Schema > | undefined ,
141+ ) : ClientContract < Schema , Options , ExtQueryArgs , ExtClientMembers , ExtResult > ;
134142
135143 /**
136- * Returns a new client with new options applied.
144+ * Returns a new client with new options applied. The original client remains unchanged.
145+ *
137146 * @example
138147 * ```
139148 * const dbNoValidation = db.$setOptions({ ...db.$options, validateInput: false });
@@ -144,14 +153,19 @@ export type ClientContract<
144153 ) : ClientContract < Schema , NewOptions , ExtQueryArgs , ExtClientMembers , ExtResult > ;
145154
146155 /**
147- * Returns a new client enabling/disabling query args validation.
156+ * Returns a new client enabling/disabling query args validation. The original client remains unchanged.
148157 *
149158 * @deprecated Use {@link $setOptions} instead.
150159 */
151160 $setInputValidation ( enable : boolean ) : ClientContract < Schema , Options , ExtQueryArgs , ExtClientMembers , ExtResult > ;
152161
153162 /**
154163 * The Kysely query builder instance.
164+ *
165+ * @example
166+ * ```
167+ * db.$qb.selectFrom('User').selectAll().where('id', '=', 1).execute();
168+ * ```
155169 */
156170 readonly $qb : ToKysely < Schema > ;
157171
@@ -162,39 +176,68 @@ export type ClientContract<
162176
163177 /**
164178 * Starts an interactive transaction.
179+ *
180+ * @example
181+ * ```
182+ * await db.$transaction(async (tx) => {
183+ * const user = await tx.user.update({ where: { id: 1 }, data: { name: 'Alice' } });
184+ * const post = await tx.post.create({ data: { title: 'Hello World', authorId: user.id } });
185+ * return { user, posts: [post] };
186+ * ```
165187 */
166188 $transaction < T > (
167- callback : ( tx : TransactionClientContract < Schema , Options , ExtQueryArgs , ExtClientMembers , ExtResult > ) => Promise < T > ,
189+ callback : (
190+ tx : TransactionClientContract < Schema , Options , ExtQueryArgs , ExtClientMembers , ExtResult > ,
191+ ) => Promise < T > ,
168192 options ?: { isolationLevel ?: TransactionIsolationLevel } ,
169193 ) : Promise < T > ;
170194
171195 /**
172- * Starts a sequential transaction.
196+ * Starts a sequential transaction that runs the provided operations in order.
197+ *
198+ * @example
199+ * ```
200+ * await db.$transaction([
201+ * db.user.update({ where: { id: 1 }, data: { name: 'Alice' } }),
202+ * db.post.create({ data: { title: 'Hello World', authorId: 1 } }),
203+ * ]);
173204 */
174205 $transaction < P extends ZenStackPromise < Schema , any > [ ] > (
175206 arg : [ ...P ] ,
176207 options ?: { isolationLevel ?: TransactionIsolationLevel } ,
177208 ) : Promise < UnwrapTuplePromises < P > > ;
178209
179210 /**
180- * Returns a new client with the specified plugin installed.
211+ * Returns a new client with the specified plugin installed. The original client remains unchanged.
212+ *
213+ * @see {@link https://zenstack.dev/docs/orm/plugins/|Plugin Documentation }
181214 */
182215 $use <
183216 PluginSchema extends SchemaDef = Schema ,
184217 PluginExtQueryArgs extends ExtQueryArgsBase = { } ,
185218 PluginExtClientMembers extends ExtClientMembersBase = { } ,
186219 PluginExtResult extends ExtResultBase < PluginSchema > = { } ,
220+ _R = { } , // auxiliary type for inferring precise typing for `PluginExtResult`
187221 > (
188- plugin : RuntimePlugin < PluginSchema , PluginExtQueryArgs , PluginExtClientMembers , PluginExtResult > ,
189- ) : ClientContract < Schema , Options , ExtQueryArgs & PluginExtQueryArgs , ExtClientMembers & PluginExtClientMembers , ExtResult & PluginExtResult > ;
222+ plugin : RuntimePlugin < PluginSchema , PluginExtQueryArgs , PluginExtClientMembers , PluginExtResult > & {
223+ // intersect with the `result` extension field for precise typing
224+ result ?: ExtResultInferenceArgs < Schema , _R > ;
225+ } ,
226+ ) : ClientContract <
227+ Schema ,
228+ Options ,
229+ ExtQueryArgs & PluginExtQueryArgs ,
230+ ExtClientMembers & PluginExtClientMembers ,
231+ ExtResult & PluginExtResult
232+ > ;
190233
191234 /**
192- * Returns a new client with the specified plugin removed.
235+ * Returns a new client with the specified plugin removed. The original client remains unchanged.
193236 */
194237 $unuse ( pluginId : string ) : ClientContract < Schema , Options , ExtQueryArgs , ExtClientMembers , ExtResult > ;
195238
196239 /**
197- * Returns a new client with all plugins removed.
240+ * Returns a new client with all plugins removed. The original client remains unchanged.
198241 */
199242 $unuseAll ( ) : ClientContract < Schema , Options > ;
200243
@@ -224,7 +267,13 @@ export type ClientContract<
224267 */
225268 get $diagnostics ( ) : Promise < Diagnostics > ;
226269} & {
227- [ Key in GetSlicedModels < Schema , Options > as Uncapitalize < Key > ] : ModelOperations < Schema , Key , Options , ExtQueryArgs , ExtResult > ;
270+ [ Key in GetSlicedModels < Schema , Options > as Uncapitalize < Key > ] : ModelOperations <
271+ Schema ,
272+ Key ,
273+ Options ,
274+ ExtQueryArgs ,
275+ ExtResult
276+ > ;
228277} & ProcedureOperations < Schema , Options > &
229278 ExtClientMembers ;
230279
@@ -297,7 +346,7 @@ type SliceOperations<
297346 [ Key in keyof T as Key extends GetSlicedOperations < Schema , Model , Options > ? Key : never ] : T [ Key ] ;
298347 } ,
299348 // exclude create operations for models that don't allow create (delegate models, required Unsupported fields)
300- | ( ModelAllowsCreate < Schema , Model > extends true ? never : OperationsRequiringCreate )
349+ ModelAllowsCreate < Schema , Model > extends true ? never : OperationsRequiringCreate
301350> ;
302351
303352export type AllModelOperations <
0 commit comments