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,237 @@
1
+ import * as _prisma_client from '@prisma/client';
2
+ import { PrismaClient } from '@prisma/client';
3
+ import * as _prisma_client_runtime_library from '@prisma/client/runtime/library';
4
+ import { PrismaClientOptions } from '@prisma/client/runtime/library';
5
+ import FlareBuilder from './core/flareBuilder.cjs';
6
+ export { RelationModelMap } from './core/flareBuilder.cjs';
7
+ import { M as ModelName, l as PrismaOperation, m as HookTiming, B as BeforeHookCallback, j as AfterHookCallback, k as ColumnChangeCallback } from './prisma.types-nGNe1CG8.cjs';
8
+ export { r as AggregateResult, o as CreateArgs, C as CreateData, p as CreateManyArgs, b as CreateManyData, c as DeleteArgs, n as FindFirstArgs, F as FindManyArgs, a as ModelDelegate, R as RecordType, q as UpdateArgs, f as UpsertArgs } from './prisma.types-nGNe1CG8.cjs';
9
+ export { afterChange, afterCreate, afterDelete, afterUpdate, afterUpsert, beforeCreate, beforeDelete, beforeUpdate } from './core/hooks.cjs';
10
+
11
+ declare class FlareClient extends PrismaClient {
12
+ constructor(options?: PrismaClientOptions);
13
+ /**
14
+ * Creates a new FlareBuilder instance for the specified model.
15
+ * @param modelName - The name of the model.
16
+ * @returns FlareBuilder instance
17
+ */
18
+ from<T extends ModelName>(modelName: T): FlareBuilder<T>;
19
+ /**
20
+ * Executes a transaction with the FlareClient capabilities.
21
+ * @param fn - The transaction function.
22
+ * @param options - Transaction options.
23
+ * @returns The result of the transaction.
24
+ */
25
+ transaction<R>(fn: (tx: FlareClient) => Promise<R>, options?: {
26
+ maxWait?: number;
27
+ timeout?: number;
28
+ isolationLevel?: any;
29
+ }): Promise<R>;
30
+ }
31
+ /**
32
+ * @deprecated Use `FlareClient` instead. This alias will be removed in a future version.
33
+ */
34
+ declare const ExtendedPrismaClient: typeof FlareClient;
35
+
36
+ type ModelClass<T extends ModelName = any> = new () => FlareBuilder<T, any>;
37
+ /**
38
+ * Registry for custom model classes that extend FlareBuilder.
39
+ * This allows the include() method to instantiate the correct custom model class
40
+ * for related models, preserving custom methods like `.valid()`, `.published()`, etc.
41
+ */
42
+ declare class ModelRegistry {
43
+ private models;
44
+ /**
45
+ * Register a custom model class for a given model name.
46
+ * The model name should match the Prisma model name (e.g., 'user', 'post', 'enrollment')
47
+ * @param modelName - The lowercase model name (matching Prisma delegate name)
48
+ * @param modelClass - The custom class that extends FlareBuilder
49
+ */
50
+ register<T extends ModelName>(modelName: T, modelClass: ModelClass<T>): void;
51
+ /**
52
+ * Register multiple models at once
53
+ * @param models - Object mapping model names to their classes
54
+ */
55
+ registerMany(models: Record<string, ModelClass>): void;
56
+ /**
57
+ * Get a custom model class by name
58
+ * @param modelName - The model name to look up
59
+ * @returns The model class or undefined if not registered
60
+ */
61
+ get<T extends ModelName>(modelName: string): ModelClass<T> | undefined;
62
+ /**
63
+ * Check if a model is registered
64
+ * @param modelName - The model name to check
65
+ */
66
+ has(modelName: string): boolean;
67
+ /**
68
+ * Create an instance of a registered model
69
+ * @param modelName - The model name to instantiate
70
+ * @returns A new instance of the custom model class, or undefined if not registered
71
+ */
72
+ create<T extends ModelName>(modelName: string): FlareBuilder<T, any> | undefined;
73
+ /**
74
+ * Clear all registered models (useful for testing)
75
+ */
76
+ clear(): void;
77
+ /**
78
+ * Get all registered model names
79
+ */
80
+ getRegisteredModels(): string[];
81
+ }
82
+ declare const modelRegistry: ModelRegistry;
83
+
84
+ /**
85
+ * Configuration options for the hook system.
86
+ */
87
+ interface HookConfig {
88
+ /**
89
+ * Whether column-level hooks (afterChange) are enabled.
90
+ * Set to false to disable all column hooks globally.
91
+ * @default true
92
+ */
93
+ enableColumnHooks: boolean;
94
+ /**
95
+ * Maximum number of records to re-fetch for column hooks on updateMany.
96
+ * If an updateMany affects more records than this limit, column hooks
97
+ * will be skipped and a warning logged.
98
+ * Set to 0 or Infinity to disable the limit.
99
+ * @default 1000
100
+ */
101
+ maxRefetch: number;
102
+ /**
103
+ * Whether to log warnings when hooks are skipped.
104
+ * @default true
105
+ */
106
+ warnOnSkip: boolean;
107
+ }
108
+ declare class HookRegistry {
109
+ private hooks;
110
+ private columnHooks;
111
+ private fieldCache;
112
+ private modelsWithColumnHooks;
113
+ private config;
114
+ constructor();
115
+ /**
116
+ * Configure the hook system.
117
+ * @param config - Partial configuration to merge with defaults
118
+ *
119
+ * @example
120
+ * // Disable column hooks globally for performance
121
+ * hookRegistry.configure({ enableColumnHooks: false });
122
+ *
123
+ * @example
124
+ * // Increase maxRefetch limit
125
+ * hookRegistry.configure({ maxRefetch: 5000 });
126
+ *
127
+ * @example
128
+ * // Disable limit entirely (use with caution)
129
+ * hookRegistry.configure({ maxRefetch: Infinity });
130
+ */
131
+ configure(config: Partial<HookConfig>): void;
132
+ /**
133
+ * Get current configuration.
134
+ */
135
+ getConfig(): Readonly<HookConfig>;
136
+ addHook(model: ModelName, action: PrismaOperation, timing: HookTiming, fn: BeforeHookCallback | AfterHookCallback): void;
137
+ addColumnHook(model: ModelName, column: string, fn: ColumnChangeCallback<any>): void;
138
+ runHooks(timing: HookTiming, model: ModelName, action: PrismaOperation, args: any[], prisma: any): Promise<void>;
139
+ runColumnHooks(model: ModelName, newData: any, prevData: any, prisma: any): Promise<void>;
140
+ hasColumnHooks(model: ModelName): boolean;
141
+ /**
142
+ * Check if column hooks should run for an operation.
143
+ * Takes into account global config, record count limits, and per-call options.
144
+ *
145
+ * @param model - The model name
146
+ * @param recordCount - Number of records affected (for maxRefetch check)
147
+ * @param args - The operation args (to check for __flare skip option)
148
+ * @returns Whether column hooks should execute
149
+ */
150
+ shouldRunColumnHooks(model: ModelName, recordCount: number, args?: any): boolean;
151
+ getRelevantFields(model: ModelName): Record<string, true>;
152
+ /**
153
+ * Clear all registered hooks (useful for testing)
154
+ */
155
+ clearAll(): void;
156
+ }
157
+ declare const hookRegistry: HookRegistry;
158
+
159
+ /**
160
+ * Loads callback files from a specified directory.
161
+ * Users can create a 'callbacks' directory in their project and call this function.
162
+ *
163
+ * IMPORTANT: This function is async - you MUST await it before querying to ensure
164
+ * all hooks are registered.
165
+ *
166
+ * In production Node.js environments, only .js files are loaded.
167
+ * TypeScript files (.ts) are only loaded when running under ts-node, tsx, Bun, or Vitest.
168
+ * Make sure to compile your TypeScript callbacks to JavaScript for production.
169
+ *
170
+ * @param callbacksDir - Path to the callbacks directory
171
+ * @returns Promise that resolves when all callbacks are loaded
172
+ *
173
+ * @example
174
+ * // In your app initialization:
175
+ * await loadCallbacks();
176
+ * // Now safe to query - all hooks are registered
177
+ */
178
+ declare function loadCallbacks(callbacksDir?: string): Promise<void>;
179
+ /**
180
+ * Creates a Prisma 7+ client extension for hooks
181
+ */
182
+ declare function createHooksExtension(basePrisma: PrismaClient): (client: any) => _prisma_client.PrismaClientExtends<_prisma_client_runtime_library.InternalArgs<{}, {}, {}, {}>>;
183
+ /**
184
+ * Registers hooks using the legacy $use middleware API (Prisma ≤6)
185
+ * @deprecated Use createHooksExtension for Prisma 7+
186
+ */
187
+ declare function registerHooksLegacy(prisma: PrismaClient): void;
188
+ /**
189
+ * Registers hooks on the Prisma client and automatically loads callbacks.
190
+ * Automatically detects Prisma version and uses the appropriate API:
191
+ * - Prisma ≤6: Uses $use middleware
192
+ * - Prisma 7+: Returns extended client with hooks extension
193
+ *
194
+ * Callbacks are loaded from the path specified in prisma-flare.config.json
195
+ * (defaults to 'prisma/callbacks'). Set `callbacksPath` in config to customize.
196
+ *
197
+ * @param prisma - The Prisma client instance
198
+ * @returns Promise resolving to the Prisma client (possibly extended for Prisma 7+)
199
+ *
200
+ * @example
201
+ * // In your db setup file:
202
+ * export const db = await registerHooks(new FlareClient());
203
+ */
204
+ declare function registerHooks<T extends PrismaClient>(prisma: T): Promise<T>;
205
+
206
+ /**
207
+ * Database Adapter Interface
208
+ */
209
+ interface DatabaseAdapter {
210
+ /**
211
+ * Adapter name (e.g., 'postgres', 'sqlite')
212
+ */
213
+ name: string;
214
+ /**
215
+ * Check if this adapter handles the given connection URL
216
+ */
217
+ matches(url: string): boolean;
218
+ /**
219
+ * Create the database
220
+ */
221
+ create(url: string): Promise<void>;
222
+ /**
223
+ * Drop the database
224
+ */
225
+ drop(url: string): Promise<void>;
226
+ }
227
+ /**
228
+ * Adapter Registry
229
+ */
230
+ declare class AdapterRegistry {
231
+ private adapters;
232
+ register(adapter: DatabaseAdapter): void;
233
+ getAdapter(url: string): DatabaseAdapter;
234
+ }
235
+ declare const registry: AdapterRegistry;
236
+
237
+ export { AfterHookCallback, BeforeHookCallback, ColumnChangeCallback, type DatabaseAdapter, ExtendedPrismaClient, FlareBuilder, FlareClient, type HookConfig, HookTiming, ModelName, PrismaOperation, createHooksExtension, registry as dbAdapterRegistry, hookRegistry, loadCallbacks, modelRegistry, registerHooks, registerHooksLegacy };
@@ -0,0 +1,237 @@
1
+ import * as _prisma_client from '@prisma/client';
2
+ import { PrismaClient } from '@prisma/client';
3
+ import * as _prisma_client_runtime_library from '@prisma/client/runtime/library';
4
+ import { PrismaClientOptions } from '@prisma/client/runtime/library';
5
+ import FlareBuilder from './core/flareBuilder.js';
6
+ export { RelationModelMap } from './core/flareBuilder.js';
7
+ import { M as ModelName, l as PrismaOperation, m as HookTiming, B as BeforeHookCallback, j as AfterHookCallback, k as ColumnChangeCallback } from './prisma.types-nGNe1CG8.js';
8
+ export { r as AggregateResult, o as CreateArgs, C as CreateData, p as CreateManyArgs, b as CreateManyData, c as DeleteArgs, n as FindFirstArgs, F as FindManyArgs, a as ModelDelegate, R as RecordType, q as UpdateArgs, f as UpsertArgs } from './prisma.types-nGNe1CG8.js';
9
+ export { afterChange, afterCreate, afterDelete, afterUpdate, afterUpsert, beforeCreate, beforeDelete, beforeUpdate } from './core/hooks.js';
10
+
11
+ declare class FlareClient extends PrismaClient {
12
+ constructor(options?: PrismaClientOptions);
13
+ /**
14
+ * Creates a new FlareBuilder instance for the specified model.
15
+ * @param modelName - The name of the model.
16
+ * @returns FlareBuilder instance
17
+ */
18
+ from<T extends ModelName>(modelName: T): FlareBuilder<T>;
19
+ /**
20
+ * Executes a transaction with the FlareClient capabilities.
21
+ * @param fn - The transaction function.
22
+ * @param options - Transaction options.
23
+ * @returns The result of the transaction.
24
+ */
25
+ transaction<R>(fn: (tx: FlareClient) => Promise<R>, options?: {
26
+ maxWait?: number;
27
+ timeout?: number;
28
+ isolationLevel?: any;
29
+ }): Promise<R>;
30
+ }
31
+ /**
32
+ * @deprecated Use `FlareClient` instead. This alias will be removed in a future version.
33
+ */
34
+ declare const ExtendedPrismaClient: typeof FlareClient;
35
+
36
+ type ModelClass<T extends ModelName = any> = new () => FlareBuilder<T, any>;
37
+ /**
38
+ * Registry for custom model classes that extend FlareBuilder.
39
+ * This allows the include() method to instantiate the correct custom model class
40
+ * for related models, preserving custom methods like `.valid()`, `.published()`, etc.
41
+ */
42
+ declare class ModelRegistry {
43
+ private models;
44
+ /**
45
+ * Register a custom model class for a given model name.
46
+ * The model name should match the Prisma model name (e.g., 'user', 'post', 'enrollment')
47
+ * @param modelName - The lowercase model name (matching Prisma delegate name)
48
+ * @param modelClass - The custom class that extends FlareBuilder
49
+ */
50
+ register<T extends ModelName>(modelName: T, modelClass: ModelClass<T>): void;
51
+ /**
52
+ * Register multiple models at once
53
+ * @param models - Object mapping model names to their classes
54
+ */
55
+ registerMany(models: Record<string, ModelClass>): void;
56
+ /**
57
+ * Get a custom model class by name
58
+ * @param modelName - The model name to look up
59
+ * @returns The model class or undefined if not registered
60
+ */
61
+ get<T extends ModelName>(modelName: string): ModelClass<T> | undefined;
62
+ /**
63
+ * Check if a model is registered
64
+ * @param modelName - The model name to check
65
+ */
66
+ has(modelName: string): boolean;
67
+ /**
68
+ * Create an instance of a registered model
69
+ * @param modelName - The model name to instantiate
70
+ * @returns A new instance of the custom model class, or undefined if not registered
71
+ */
72
+ create<T extends ModelName>(modelName: string): FlareBuilder<T, any> | undefined;
73
+ /**
74
+ * Clear all registered models (useful for testing)
75
+ */
76
+ clear(): void;
77
+ /**
78
+ * Get all registered model names
79
+ */
80
+ getRegisteredModels(): string[];
81
+ }
82
+ declare const modelRegistry: ModelRegistry;
83
+
84
+ /**
85
+ * Configuration options for the hook system.
86
+ */
87
+ interface HookConfig {
88
+ /**
89
+ * Whether column-level hooks (afterChange) are enabled.
90
+ * Set to false to disable all column hooks globally.
91
+ * @default true
92
+ */
93
+ enableColumnHooks: boolean;
94
+ /**
95
+ * Maximum number of records to re-fetch for column hooks on updateMany.
96
+ * If an updateMany affects more records than this limit, column hooks
97
+ * will be skipped and a warning logged.
98
+ * Set to 0 or Infinity to disable the limit.
99
+ * @default 1000
100
+ */
101
+ maxRefetch: number;
102
+ /**
103
+ * Whether to log warnings when hooks are skipped.
104
+ * @default true
105
+ */
106
+ warnOnSkip: boolean;
107
+ }
108
+ declare class HookRegistry {
109
+ private hooks;
110
+ private columnHooks;
111
+ private fieldCache;
112
+ private modelsWithColumnHooks;
113
+ private config;
114
+ constructor();
115
+ /**
116
+ * Configure the hook system.
117
+ * @param config - Partial configuration to merge with defaults
118
+ *
119
+ * @example
120
+ * // Disable column hooks globally for performance
121
+ * hookRegistry.configure({ enableColumnHooks: false });
122
+ *
123
+ * @example
124
+ * // Increase maxRefetch limit
125
+ * hookRegistry.configure({ maxRefetch: 5000 });
126
+ *
127
+ * @example
128
+ * // Disable limit entirely (use with caution)
129
+ * hookRegistry.configure({ maxRefetch: Infinity });
130
+ */
131
+ configure(config: Partial<HookConfig>): void;
132
+ /**
133
+ * Get current configuration.
134
+ */
135
+ getConfig(): Readonly<HookConfig>;
136
+ addHook(model: ModelName, action: PrismaOperation, timing: HookTiming, fn: BeforeHookCallback | AfterHookCallback): void;
137
+ addColumnHook(model: ModelName, column: string, fn: ColumnChangeCallback<any>): void;
138
+ runHooks(timing: HookTiming, model: ModelName, action: PrismaOperation, args: any[], prisma: any): Promise<void>;
139
+ runColumnHooks(model: ModelName, newData: any, prevData: any, prisma: any): Promise<void>;
140
+ hasColumnHooks(model: ModelName): boolean;
141
+ /**
142
+ * Check if column hooks should run for an operation.
143
+ * Takes into account global config, record count limits, and per-call options.
144
+ *
145
+ * @param model - The model name
146
+ * @param recordCount - Number of records affected (for maxRefetch check)
147
+ * @param args - The operation args (to check for __flare skip option)
148
+ * @returns Whether column hooks should execute
149
+ */
150
+ shouldRunColumnHooks(model: ModelName, recordCount: number, args?: any): boolean;
151
+ getRelevantFields(model: ModelName): Record<string, true>;
152
+ /**
153
+ * Clear all registered hooks (useful for testing)
154
+ */
155
+ clearAll(): void;
156
+ }
157
+ declare const hookRegistry: HookRegistry;
158
+
159
+ /**
160
+ * Loads callback files from a specified directory.
161
+ * Users can create a 'callbacks' directory in their project and call this function.
162
+ *
163
+ * IMPORTANT: This function is async - you MUST await it before querying to ensure
164
+ * all hooks are registered.
165
+ *
166
+ * In production Node.js environments, only .js files are loaded.
167
+ * TypeScript files (.ts) are only loaded when running under ts-node, tsx, Bun, or Vitest.
168
+ * Make sure to compile your TypeScript callbacks to JavaScript for production.
169
+ *
170
+ * @param callbacksDir - Path to the callbacks directory
171
+ * @returns Promise that resolves when all callbacks are loaded
172
+ *
173
+ * @example
174
+ * // In your app initialization:
175
+ * await loadCallbacks();
176
+ * // Now safe to query - all hooks are registered
177
+ */
178
+ declare function loadCallbacks(callbacksDir?: string): Promise<void>;
179
+ /**
180
+ * Creates a Prisma 7+ client extension for hooks
181
+ */
182
+ declare function createHooksExtension(basePrisma: PrismaClient): (client: any) => _prisma_client.PrismaClientExtends<_prisma_client_runtime_library.InternalArgs<{}, {}, {}, {}>>;
183
+ /**
184
+ * Registers hooks using the legacy $use middleware API (Prisma ≤6)
185
+ * @deprecated Use createHooksExtension for Prisma 7+
186
+ */
187
+ declare function registerHooksLegacy(prisma: PrismaClient): void;
188
+ /**
189
+ * Registers hooks on the Prisma client and automatically loads callbacks.
190
+ * Automatically detects Prisma version and uses the appropriate API:
191
+ * - Prisma ≤6: Uses $use middleware
192
+ * - Prisma 7+: Returns extended client with hooks extension
193
+ *
194
+ * Callbacks are loaded from the path specified in prisma-flare.config.json
195
+ * (defaults to 'prisma/callbacks'). Set `callbacksPath` in config to customize.
196
+ *
197
+ * @param prisma - The Prisma client instance
198
+ * @returns Promise resolving to the Prisma client (possibly extended for Prisma 7+)
199
+ *
200
+ * @example
201
+ * // In your db setup file:
202
+ * export const db = await registerHooks(new FlareClient());
203
+ */
204
+ declare function registerHooks<T extends PrismaClient>(prisma: T): Promise<T>;
205
+
206
+ /**
207
+ * Database Adapter Interface
208
+ */
209
+ interface DatabaseAdapter {
210
+ /**
211
+ * Adapter name (e.g., 'postgres', 'sqlite')
212
+ */
213
+ name: string;
214
+ /**
215
+ * Check if this adapter handles the given connection URL
216
+ */
217
+ matches(url: string): boolean;
218
+ /**
219
+ * Create the database
220
+ */
221
+ create(url: string): Promise<void>;
222
+ /**
223
+ * Drop the database
224
+ */
225
+ drop(url: string): Promise<void>;
226
+ }
227
+ /**
228
+ * Adapter Registry
229
+ */
230
+ declare class AdapterRegistry {
231
+ private adapters;
232
+ register(adapter: DatabaseAdapter): void;
233
+ getAdapter(url: string): DatabaseAdapter;
234
+ }
235
+ declare const registry: AdapterRegistry;
236
+
237
+ export { AfterHookCallback, BeforeHookCallback, ColumnChangeCallback, type DatabaseAdapter, ExtendedPrismaClient, FlareBuilder, FlareClient, type HookConfig, HookTiming, ModelName, PrismaOperation, createHooksExtension, registry as dbAdapterRegistry, hookRegistry, loadCallbacks, modelRegistry, registerHooks, registerHooksLegacy };