prisma-flare 1.0.0

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 (45) hide show
  1. package/dist/cli/db-create.cjs +240 -0
  2. package/dist/cli/db-create.d.cts +1 -0
  3. package/dist/cli/db-create.d.ts +1 -0
  4. package/dist/cli/db-create.js +217 -0
  5. package/dist/cli/db-drop.cjs +263 -0
  6. package/dist/cli/db-drop.d.cts +1 -0
  7. package/dist/cli/db-drop.d.ts +1 -0
  8. package/dist/cli/db-drop.js +240 -0
  9. package/dist/cli/db-migrate.cjs +318 -0
  10. package/dist/cli/db-migrate.d.cts +1 -0
  11. package/dist/cli/db-migrate.d.ts +1 -0
  12. package/dist/cli/db-migrate.js +295 -0
  13. package/dist/cli/db-reset.cjs +110 -0
  14. package/dist/cli/db-reset.d.cts +1 -0
  15. package/dist/cli/db-reset.d.ts +1 -0
  16. package/dist/cli/db-reset.js +87 -0
  17. package/dist/cli/db-seed.cjs +87 -0
  18. package/dist/cli/db-seed.d.cts +1 -0
  19. package/dist/cli/db-seed.d.ts +1 -0
  20. package/dist/cli/db-seed.js +64 -0
  21. package/dist/cli/index.cjs +352 -0
  22. package/dist/cli/index.d.cts +1 -0
  23. package/dist/cli/index.d.ts +1 -0
  24. package/dist/cli/index.js +328 -0
  25. package/dist/core/flareBuilder.cjs +681 -0
  26. package/dist/core/flareBuilder.d.cts +402 -0
  27. package/dist/core/flareBuilder.d.ts +402 -0
  28. package/dist/core/flareBuilder.js +658 -0
  29. package/dist/core/hooks.cjs +243 -0
  30. package/dist/core/hooks.d.cts +13 -0
  31. package/dist/core/hooks.d.ts +13 -0
  32. package/dist/core/hooks.js +209 -0
  33. package/dist/generated.cjs +31 -0
  34. package/dist/generated.d.cts +4 -0
  35. package/dist/generated.d.ts +4 -0
  36. package/dist/generated.js +6 -0
  37. package/dist/index.cjs +1315 -0
  38. package/dist/index.d.cts +237 -0
  39. package/dist/index.d.ts +237 -0
  40. package/dist/index.js +1261 -0
  41. package/dist/prisma.types-nGNe1CG8.d.cts +201 -0
  42. package/dist/prisma.types-nGNe1CG8.d.ts +201 -0
  43. package/license.md +21 -0
  44. package/package.json +115 -0
  45. package/readme.md +957 -0
@@ -0,0 +1,402 @@
1
+ import { Prisma } from '@prisma/client';
2
+ import { M as ModelName, a as ModelDelegate, Q as QueryArgs, W as WhereInput, O as OrderByInput, R as RecordType, D as DistinctInput, S as SelectInput, I as IncludeKey, G as GroupByInput, H as HavingInput, P as PaginatedResult, C as CreateData, b as CreateManyData, c as DeleteArgs, d as DeleteManyArgs, U as UpdateData, e as UpdateManyData, f as UpsertArgs, g as SumFields, A as AvgFields, h as MinFields, i as MaxFields } from '../prisma.types-nGNe1CG8.js';
3
+
4
+ /**
5
+ * Global interface for relation-to-model mapping.
6
+ * This is augmented by prisma-flare/generated to provide type-safe includes.
7
+ *
8
+ * @example
9
+ * // In your project, prisma-flare generate creates:
10
+ * declare module 'prisma-flare' {
11
+ * interface RelationModelMap {
12
+ * posts: Post;
13
+ * author: User;
14
+ * }
15
+ * }
16
+ */
17
+ interface RelationModelMap {
18
+ [key: string]: FlareBuilder<any, any>;
19
+ }
20
+ /**
21
+ * Helper type to get the model class for a relation name
22
+ */
23
+ type GetRelationModel<K extends string> = K extends keyof RelationModelMap ? RelationModelMap[K] : FlareBuilder<any, {}>;
24
+ /**
25
+ * FlareBuilder for chainable Prisma queries with full type safety
26
+ * The type safety is enforced through the ModelDelegate parameter
27
+ */
28
+ declare class FlareBuilder<T extends ModelName, Args extends Record<string, any> = {}> {
29
+ protected model: ModelDelegate<T>;
30
+ protected query: QueryArgs;
31
+ constructor(model: ModelDelegate<T>, query?: QueryArgs);
32
+ /**
33
+ * Adds a where condition to the query with type safety from Prisma.
34
+ * Multiple where() calls are composed using AND logic to avoid silent overwrites.
35
+ * @param condition - Where filter matching your Prisma model
36
+ *
37
+ * @example
38
+ * // These conditions are AND-ed together:
39
+ * DB.posts
40
+ * .where({ published: true })
41
+ * .where({ authorId: 1 })
42
+ * .findMany()
43
+ * // Equivalent to: { AND: [{ published: true }, { authorId: 1 }] }
44
+ */
45
+ where(condition: WhereInput<T>): FlareBuilder<T, Args & {
46
+ where: WhereInput<T>;
47
+ }>;
48
+ /**
49
+ * Adds a where condition using AND logic (explicit alias for where())
50
+ * @param condition - Where filter matching your Prisma model
51
+ *
52
+ * @example
53
+ * DB.posts
54
+ * .where({ published: true })
55
+ * .andWhere({ createdAt: { gte: new Date('2024-01-01') } })
56
+ * .findMany()
57
+ */
58
+ andWhere(condition: WhereInput<T>): FlareBuilder<T, Args & {
59
+ where: WhereInput<T>;
60
+ }>;
61
+ /**
62
+ * Adds a where condition using OR logic.
63
+ *
64
+ * ⚠️ **IMPORTANT**: `orWhere()` wraps the *entire* accumulated where clause:
65
+ * `OR([prevWhere, condition])`. This means:
66
+ *
67
+ * ```ts
68
+ * .where(A).orWhere(B).where(C) // becomes: (A OR B) AND C
69
+ * ```
70
+ *
71
+ * For complex boolean logic, prefer `whereGroup()` / `orWhereGroup()` for explicit control.
72
+ *
73
+ * @param condition - Where filter matching your Prisma model
74
+ *
75
+ * @example
76
+ * // Simple case - OK:
77
+ * DB.posts
78
+ * .where({ published: true })
79
+ * .orWhere({ featured: true })
80
+ * .findMany()
81
+ * // Result: published OR featured
82
+ *
83
+ * @example
84
+ * // For complex logic, use whereGroup instead:
85
+ * DB.posts
86
+ * .where({ published: true })
87
+ * .whereGroup(qb => qb
88
+ * .where({ category: 'news' })
89
+ * .orWhere({ category: 'tech' })
90
+ * )
91
+ * .findMany()
92
+ * // Result: published AND (category='news' OR category='tech')
93
+ */
94
+ orWhere(condition: WhereInput<T>): FlareBuilder<T, Args & {
95
+ where: WhereInput<T>;
96
+ }>;
97
+ /**
98
+ * Creates a grouped where condition using a callback.
99
+ * Use this for explicit control over boolean logic grouping.
100
+ * The callback receives a fresh builder - its accumulated where becomes a single group.
101
+ *
102
+ * @param callback - Function that builds the grouped condition
103
+ * @param mode - How to combine with existing where: 'AND' (default) or 'OR'
104
+ *
105
+ * @example
106
+ * // (status = 'active') AND (name LIKE 'A%' OR name LIKE 'B%')
107
+ * DB.users
108
+ * .where({ status: 'active' })
109
+ * .whereGroup(qb => qb
110
+ * .where({ name: { startsWith: 'A' } })
111
+ * .orWhere({ name: { startsWith: 'B' } })
112
+ * )
113
+ * .findMany()
114
+ *
115
+ * @example
116
+ * // (status = 'active') OR (role = 'admin' AND verified = true)
117
+ * DB.users
118
+ * .where({ status: 'active' })
119
+ * .whereGroup(qb => qb
120
+ * .where({ role: 'admin' })
121
+ * .where({ verified: true })
122
+ * , 'OR')
123
+ * .findMany()
124
+ */
125
+ whereGroup(callback: (builder: FlareBuilder<T, {}>) => FlareBuilder<T, any>, mode?: 'AND' | 'OR'): FlareBuilder<T, Args & {
126
+ where: WhereInput<T>;
127
+ }>;
128
+ /**
129
+ * Alias for whereGroup with OR mode.
130
+ * Creates a grouped condition that's OR-ed with existing where.
131
+ *
132
+ * @param callback - Function that builds the grouped condition
133
+ *
134
+ * @example
135
+ * // (published = true) OR (authorId = 1 AND draft = true)
136
+ * DB.posts
137
+ * .where({ published: true })
138
+ * .orWhereGroup(qb => qb
139
+ * .where({ authorId: 1 })
140
+ * .where({ draft: true })
141
+ * )
142
+ * .findMany()
143
+ */
144
+ orWhereGroup(callback: (builder: FlareBuilder<T, {}>) => FlareBuilder<T, any>): FlareBuilder<T, Args & {
145
+ where: WhereInput<T>;
146
+ }>;
147
+ /**
148
+ * Adds a where condition to the query for the specified id.
149
+ * Uses the same AND composition as where() for consistency.
150
+ * @param id - The id to search for
151
+ */
152
+ withId(id: number | string): FlareBuilder<T, Args & {
153
+ where: {
154
+ id: number | string;
155
+ };
156
+ }>;
157
+ /**
158
+ * Adds an order by condition to the query
159
+ * @param orderBy - OrderBy object matching your Prisma model
160
+ */
161
+ order(orderBy: OrderByInput<T>): FlareBuilder<T, Args & {
162
+ orderBy: OrderByInput<T>;
163
+ }>;
164
+ /**
165
+ * Gets the last record sorted by the specified field
166
+ * @param key - Field to sort by (defaults to 'createdAt')
167
+ */
168
+ last(key?: keyof RecordType<T> | string): FlareBuilder<T, Args & {
169
+ orderBy: any;
170
+ take: number;
171
+ }>;
172
+ /**
173
+ * Gets the first record sorted by the specified field
174
+ * @param key - Field to sort by (defaults to 'createdAt')
175
+ */
176
+ first(key?: keyof RecordType<T> | string): FlareBuilder<T, Args & {
177
+ orderBy: any;
178
+ take: number;
179
+ }>;
180
+ /**
181
+ * Sets a limit on the number of records to retrieve
182
+ * @param limit - Maximum number of records
183
+ */
184
+ limit(limit: number): FlareBuilder<T, Args & {
185
+ take: number;
186
+ }>;
187
+ /**
188
+ * Sets distinct fields for the query
189
+ * @param distinct - Fields to be distinct
190
+ */
191
+ distinct(distinct: DistinctInput<T>): FlareBuilder<T, Args & {
192
+ distinct: DistinctInput<T>;
193
+ }>;
194
+ /**
195
+ * Selects specific fields to retrieve
196
+ * @param fields - Select object matching your Prisma model
197
+ */
198
+ select<S extends SelectInput<T>>(fields: S): FlareBuilder<T, Args & {
199
+ select: S;
200
+ }>;
201
+ /**
202
+ * Selects only the specified field and returns its value
203
+ * @param field - Field name to retrieve
204
+ */
205
+ only<K extends keyof RecordType<T>>(field: K): Promise<RecordType<T>[K] | null>;
206
+ /**
207
+ * Returns the current query object
208
+ */
209
+ getQuery(): QueryArgs;
210
+ /**
211
+ * Includes a relation (typed from Prisma's `include` args)
212
+ */
213
+ include<K extends IncludeKey<T>>(relation: K): FlareBuilder<T, Args & {
214
+ include: {
215
+ [P in K]: true;
216
+ };
217
+ }>;
218
+ /**
219
+ * Includes a relation with query customization using a builder.
220
+ * The callback receives the custom model class if registered via prisma-flare generate.
221
+ * Type inference is automatic when RelationModelMap is properly augmented.
222
+ *
223
+ * @example
224
+ * // Type-safe includes with custom methods (automatic after prisma-flare generate):
225
+ * .include("posts", (posts) => posts.published().recent(5))
226
+ * .include("author", (author) => author.withEmail("test@example.com"))
227
+ */
228
+ include<K extends IncludeKey<T>, R extends FlareBuilder<any, any>>(relation: K, callback: (builder: GetRelationModel<K & string>) => R): FlareBuilder<T, Args & {
229
+ include: {
230
+ [P in K]: R extends FlareBuilder<any, infer RA> ? RA : true;
231
+ };
232
+ }>;
233
+ /**
234
+ * Groups results by specified fields
235
+ * @param groupBy - Fields to group by
236
+ */
237
+ groupBy(groupBy: GroupByInput<T>): FlareBuilder<T, Args & {
238
+ by: GroupByInput<T>;
239
+ }>;
240
+ /**
241
+ * Adds a having condition to the query
242
+ * @param condition - Having condition
243
+ */
244
+ having(condition: HavingInput<T>): FlareBuilder<T, Args & {
245
+ having: HavingInput<T>;
246
+ }>;
247
+ /**
248
+ * Skips the specified number of records
249
+ * @param offset - Number of records to skip
250
+ */
251
+ skip(offset: number): FlareBuilder<T, Args & {
252
+ skip: number;
253
+ }>;
254
+ /**
255
+ * Checks if any record exists matching the current query
256
+ * @param existenceKey - Key to check for existence (defaults to 'id')
257
+ */
258
+ exists(existenceKey?: string): Promise<boolean>;
259
+ /**
260
+ * Paginates the results
261
+ * @param page - Page number (1-based)
262
+ * @param perPage - Number of records per page
263
+ */
264
+ paginate(page?: number, perPage?: number): Promise<PaginatedResult<RecordType<T>>>;
265
+ /**
266
+ * Conditionally executes a callback on the query builder
267
+ * @param condition - Boolean or function returning boolean
268
+ * @param callback - Function to execute if condition is true
269
+ */
270
+ when(condition: boolean | (() => boolean), callback: (qb: this) => void): this;
271
+ /**
272
+ * Processes results in chunks to avoid memory issues
273
+ * @param size - Size of each chunk
274
+ * @param callback - Function to process each chunk
275
+ */
276
+ chunk(size: number, callback: (results: RecordType<T>[]) => Promise<void> | void): Promise<void>;
277
+ /**
278
+ * Clones the current query builder instance.
279
+ * Uses structuredClone for proper handling of Date, BigInt, etc.
280
+ */
281
+ clone(): FlareBuilder<T, Args>;
282
+ /**
283
+ * Finds the first record matching the query or throws an error if none found
284
+ * Throws a Prisma NotFoundError if no record matches the query
285
+ * @throws {Prisma.NotFoundError} When no record matches the query
286
+ * @returns Promise resolving to the found record
287
+ */
288
+ findFirstOrThrow(): Promise<NonNullable<RecordType<T>>>;
289
+ /**
290
+ * Finds a unique record by primary key or throws an error if not found
291
+ * Requires a unique constraint (typically the id field)
292
+ * Throws a Prisma NotFoundError if no record matches
293
+ * @throws {Prisma.NotFoundError} When no record is found
294
+ * @returns Promise resolving to the found record
295
+ */
296
+ findUniqueOrThrow(): Promise<NonNullable<RecordType<T>>>;
297
+ /**
298
+ * Finds all records matching the query
299
+ * Respects all previously set query conditions (where, orderBy, take, skip, include, select, distinct)
300
+ * @returns Promise resolving to an array of records matching the query
301
+ */
302
+ findMany(): Promise<Prisma.Result<ModelDelegate<T>, Args, 'findMany'>>;
303
+ /**
304
+ * Finds the first record matching the query
305
+ * Returns null if no record matches. To throw an error instead, use findFirstOrThrow()
306
+ * @returns Promise resolving to the first matching record or null
307
+ */
308
+ findFirst(): Promise<Prisma.Result<ModelDelegate<T>, Args, 'findFirst'>>;
309
+ /**
310
+ * Finds a unique record by primary key
311
+ * Returns null if no record is found. To throw an error instead, use findUniqueOrThrow()
312
+ * Requires a unique constraint in the where condition (typically the id field)
313
+ * @returns Promise resolving to the found record or null
314
+ */
315
+ findUnique(): Promise<Prisma.Result<ModelDelegate<T>, Args, 'findUnique'>>;
316
+ /**
317
+ * Creates a new record with the provided data
318
+ * Any hooks registered for 'create' operations will be triggered
319
+ * @param data - Data matching your Prisma model's create input
320
+ * @returns Promise resolving to the newly created record
321
+ */
322
+ create(data: CreateData<T>): Promise<Prisma.Result<ModelDelegate<T>, Args, 'create'>>;
323
+ /**
324
+ * Creates multiple records in a single operation
325
+ * More efficient than creating records individually
326
+ * Any hooks registered for 'create' operations will be triggered for each record
327
+ * @param data - Array of data objects matching your Prisma model's create input
328
+ * @returns Promise resolving to the count of created records
329
+ */
330
+ createMany(data: CreateManyData<T>): Promise<Prisma.Result<ModelDelegate<T>, Args, 'createMany'>>;
331
+ /**
332
+ * Deletes a single record matching the current query conditions
333
+ * Requires at least one unique constraint in the where condition (typically id)
334
+ * Any hooks registered for 'delete' operations will be triggered
335
+ * @param args - Optional additional delete arguments to override query conditions
336
+ * @returns Promise resolving to the deleted record
337
+ */
338
+ delete(args?: DeleteArgs<T>): Promise<Prisma.Result<ModelDelegate<T>, Args, 'delete'>>;
339
+ /**
340
+ * Deletes multiple records matching the current query conditions
341
+ * More efficient than deleting records individually
342
+ * Any hooks registered for 'delete' operations will be triggered for each record
343
+ * @param args - Optional additional delete arguments to override query conditions
344
+ * @returns Promise resolving to the count of deleted records
345
+ */
346
+ deleteMany(args?: DeleteManyArgs<T>): Promise<Prisma.Result<ModelDelegate<T>, Args, 'deleteMany'>>;
347
+ /**
348
+ * Updates a single record matching the current query conditions
349
+ * Requires at least one unique constraint in the where condition (typically id)
350
+ * Any hooks registered for 'update' operations will be triggered
351
+ * @param data - Data to update, matching your Prisma model's update input
352
+ * @returns Promise resolving to the updated record
353
+ */
354
+ update(data: UpdateData<T>): Promise<Prisma.Result<ModelDelegate<T>, Args, 'update'>>;
355
+ /**
356
+ * Updates multiple records matching the current query conditions
357
+ * More efficient than updating records individually
358
+ * Any hooks registered for 'update' operations will be triggered for each record
359
+ * @param data - Data to update, matching your Prisma model's update input
360
+ * @returns Promise resolving to the count of updated records
361
+ */
362
+ updateMany(data: UpdateManyData<T>): Promise<Prisma.Result<ModelDelegate<T>, Args, 'updateMany'>>;
363
+ /**
364
+ * Updates a record if it exists, otherwise creates a new record
365
+ * The record is uniquely identified by the where condition (typically id)
366
+ * Any hooks registered for 'update' or 'create' operations will be triggered accordingly
367
+ * @param args - Optional upsert arguments including where, update, and create data
368
+ * @returns Promise resolving to the upserted record
369
+ */
370
+ upsert(args?: UpsertArgs<T>): Promise<Prisma.Result<ModelDelegate<T>, Args, 'upsert'>>;
371
+ /**
372
+ * Counts the number of records matching the query
373
+ */
374
+ count(): Promise<number>;
375
+ /**
376
+ * Sums the specified numeric field
377
+ * @param field - Field name to sum
378
+ */
379
+ sum(field: SumFields<T> & string): Promise<number | null>;
380
+ /**
381
+ * Calculates the average of the specified numeric field
382
+ * @param field - Field name to average
383
+ */
384
+ avg(field: AvgFields<T> & string): Promise<number | null>;
385
+ /**
386
+ * Finds the minimum value of the specified field
387
+ * @param field - Field name to find minimum
388
+ */
389
+ min(field: MinFields<T> & string): Promise<any>;
390
+ /**
391
+ * Finds the maximum value of the specified field
392
+ * @param field - Field name to find maximum
393
+ */
394
+ max(field: MaxFields<T> & string): Promise<any>;
395
+ /**
396
+ * Plucks the specified field from all results
397
+ * @param field - Field name to pluck
398
+ */
399
+ pluck<K extends keyof RecordType<T>>(field: K): Promise<RecordType<T>[K][]>;
400
+ }
401
+
402
+ export { type RelationModelMap, FlareBuilder as default };