prisma-flare 1.0.0 → 1.1.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.
package/dist/index.cjs CHANGED
@@ -52,7 +52,7 @@ __export(index_exports, {
52
52
  module.exports = __toCommonJS(index_exports);
53
53
 
54
54
  // src/core/extendedPrismaClient.ts
55
- var import_client = require("@prisma/client");
55
+ var import_client2 = require("@prisma/client");
56
56
 
57
57
  // src/core/modelRegistry.ts
58
58
  var ModelRegistry = class {
@@ -710,53 +710,6 @@ var FlareBuilder = class _FlareBuilder {
710
710
  }
711
711
  };
712
712
 
713
- // src/core/extendedPrismaClient.ts
714
- var FlareClient = class extends import_client.PrismaClient {
715
- constructor(options = {}) {
716
- super(options);
717
- }
718
- /**
719
- * Creates a new FlareBuilder instance for the specified model.
720
- * @param modelName - The name of the model.
721
- * @returns FlareBuilder instance
722
- */
723
- from(modelName) {
724
- const key = modelName.charAt(0).toLowerCase() + modelName.slice(1);
725
- const model = this[key];
726
- if (!model) {
727
- throw new Error(`Model ${modelName} does not exist on PrismaClient.`);
728
- }
729
- return new FlareBuilder(model);
730
- }
731
- /**
732
- * Executes a transaction with the FlareClient capabilities.
733
- * @param fn - The transaction function.
734
- * @param options - Transaction options.
735
- * @returns The result of the transaction.
736
- */
737
- async transaction(fn, options) {
738
- return super.$transaction(async (tx) => {
739
- const extendedTx = new Proxy(tx, {
740
- get: (target, prop, receiver) => {
741
- if (prop === "from") {
742
- return (modelName) => {
743
- const key = modelName.charAt(0).toLowerCase() + modelName.slice(1);
744
- const model = target[key];
745
- if (!model) {
746
- throw new Error(`Model ${modelName} does not exist on TransactionClient.`);
747
- }
748
- return new FlareBuilder(model);
749
- };
750
- }
751
- return Reflect.get(target, prop, receiver);
752
- }
753
- });
754
- return fn(extendedTx);
755
- }, options);
756
- }
757
- };
758
- var ExtendedPrismaClient = FlareClient;
759
-
760
713
  // src/core/hookRegistry.ts
761
714
  function valuesEqual(a, b) {
762
715
  if (a == null && b == null) return true;
@@ -928,74 +881,8 @@ var HookRegistry = class {
928
881
  var hookRegistry = new HookRegistry();
929
882
  var hookRegistry_default = hookRegistry;
930
883
 
931
- // src/core/hooks.ts
932
- function normalizeModelName(model) {
933
- return model.toLowerCase();
934
- }
935
- function beforeCreate(model, callback) {
936
- hookRegistry_default.addHook(normalizeModelName(model), "create", "before", callback);
937
- }
938
- function beforeDelete(model, callback) {
939
- hookRegistry_default.addHook(normalizeModelName(model), "delete", "before", callback);
940
- }
941
- function afterCreate(model, callback) {
942
- hookRegistry_default.addHook(normalizeModelName(model), "create", "after", callback);
943
- }
944
- function afterDelete(model, callback) {
945
- hookRegistry_default.addHook(normalizeModelName(model), "delete", "after", callback);
946
- }
947
- function beforeUpdate(model, callback) {
948
- hookRegistry_default.addHook(normalizeModelName(model), "update", "before", callback);
949
- }
950
- function afterUpdate(model, callback) {
951
- hookRegistry_default.addHook(normalizeModelName(model), "update", "after", callback);
952
- }
953
- function afterChange(model, column, callback) {
954
- hookRegistry_default.addColumnHook(normalizeModelName(model), column, callback);
955
- }
956
- function afterUpsert(model, callback) {
957
- hookRegistry_default.addHook(normalizeModelName(model), "upsert", "after", callback);
958
- }
959
-
960
- // src/core/hookMiddleware.ts
961
- var import_client2 = require("@prisma/client");
962
-
963
- // src/cli/config.ts
964
- var fs = __toESM(require("fs"), 1);
965
- var path = __toESM(require("path"), 1);
966
- function findProjectRoot(currentDir) {
967
- if (fs.existsSync(path.join(currentDir, "package.json"))) {
968
- return currentDir;
969
- }
970
- const parentDir = path.dirname(currentDir);
971
- if (parentDir === currentDir) {
972
- throw new Error("Could not find package.json");
973
- }
974
- return findProjectRoot(parentDir);
975
- }
976
- function loadConfig(rootDir) {
977
- const projectRoot = rootDir || findProjectRoot(process.cwd());
978
- const configPath = path.join(projectRoot, "prisma-flare.config.json");
979
- let config = {
980
- modelsPath: "prisma/models",
981
- dbPath: "prisma/db",
982
- callbacksPath: "prisma/callbacks"
983
- };
984
- if (fs.existsSync(configPath)) {
985
- try {
986
- const configFile = fs.readFileSync(configPath, "utf-8");
987
- const userConfig = JSON.parse(configFile);
988
- config = { ...config, ...userConfig };
989
- } catch {
990
- console.warn("\u26A0\uFE0F Could not read prisma-flare.config.json, using defaults.");
991
- }
992
- }
993
- return {
994
- ...config
995
- };
996
- }
997
-
998
884
  // src/core/hookMiddleware.ts
885
+ var import_client = require("@prisma/client");
999
886
  var import_fs = __toESM(require("fs"), 1);
1000
887
  var import_path = __toESM(require("path"), 1);
1001
888
  function supportsTypeScriptImports() {
@@ -1066,7 +953,7 @@ async function executeHookLogic(prisma, model, action, args, next) {
1066
953
  const isUpdateAction = action === "update" || action === "updateMany";
1067
954
  if (hasColumnHooks && isUpdateAction) {
1068
955
  fields = hookRegistry_default.getRelevantFields(modelName);
1069
- prevData = await fetchAffectedRecords(prisma, modelName, args.where, fields);
956
+ prevData = await fetchAffectedRecords(prisma, model, args.where, fields);
1070
957
  shouldRunColumnHooks = hookRegistry_default.shouldRunColumnHooks(modelName, prevData.length, { __flare: flareOptions });
1071
958
  }
1072
959
  await hookRegistry_default.runHooks("before", modelName, action, [args], prisma);
@@ -1074,7 +961,7 @@ async function executeHookLogic(prisma, model, action, args, next) {
1074
961
  if (shouldRunColumnHooks && prevData.length > 0) {
1075
962
  let newData = [];
1076
963
  const ids = prevData.map((r) => r.id);
1077
- newData = await fetchAffectedRecords(prisma, modelName, { id: { in: ids } }, fields);
964
+ newData = await fetchAffectedRecords(prisma, model, { id: { in: ids } }, fields);
1078
965
  for (let i = 0; i < prevData.length; i++) {
1079
966
  const prevRecord = prevData[i];
1080
967
  const newRecord = newData.find((record) => record.id === prevRecord.id);
@@ -1094,7 +981,7 @@ function supportsPrisma6Middleware(prisma) {
1094
981
  return typeof prisma.$use === "function";
1095
982
  }
1096
983
  function createHooksExtension(basePrisma) {
1097
- return import_client2.Prisma.defineExtension({
984
+ return import_client.Prisma.defineExtension({
1098
985
  name: "prisma-flare-hooks",
1099
986
  query: {
1100
987
  $allModels: {
@@ -1117,23 +1004,102 @@ function registerHooksLegacy(prisma) {
1117
1004
  return executeHookLogic(prisma, model, action, args, () => next(params));
1118
1005
  });
1119
1006
  }
1120
- async function registerHooks(prisma) {
1121
- let client;
1007
+ function registerHooks(prisma) {
1122
1008
  if (supportsPrisma6Middleware(prisma)) {
1123
1009
  registerHooksLegacy(prisma);
1124
- client = prisma;
1010
+ return prisma;
1125
1011
  } else {
1126
1012
  const extension = createHooksExtension(prisma);
1127
- client = prisma.$extends(extension);
1013
+ return prisma.$extends(extension);
1014
+ }
1015
+ }
1016
+
1017
+ // src/core/extendedPrismaClient.ts
1018
+ function supportsPrisma6Middleware2(prisma) {
1019
+ return typeof prisma.$use === "function";
1020
+ }
1021
+ var FlareClient = class extends import_client2.PrismaClient {
1022
+ constructor(options = {}) {
1023
+ const { callbacks = true, ...prismaOptions } = options;
1024
+ super(prismaOptions);
1025
+ if (callbacks) {
1026
+ if (supportsPrisma6Middleware2(this)) {
1027
+ registerHooksLegacy(this);
1028
+ } else {
1029
+ const extension = createHooksExtension(this);
1030
+ return this.$extends(extension);
1031
+ }
1032
+ }
1033
+ }
1034
+ /**
1035
+ * Creates a new FlareBuilder instance for the specified model.
1036
+ * @param modelName - The name of the model.
1037
+ * @returns FlareBuilder instance
1038
+ */
1039
+ from(modelName) {
1040
+ const key = modelName.charAt(0).toLowerCase() + modelName.slice(1);
1041
+ const model = this[key];
1042
+ if (!model) {
1043
+ throw new Error(`Model ${modelName} does not exist on PrismaClient.`);
1044
+ }
1045
+ return new FlareBuilder(model);
1128
1046
  }
1129
- try {
1130
- const config = loadConfig();
1131
- const projectRoot = findProjectRoot(process.cwd());
1132
- const callbacksPath = import_path.default.join(projectRoot, config.callbacksPath);
1133
- await loadCallbacks(callbacksPath);
1134
- } catch {
1047
+ /**
1048
+ * Executes a transaction with the FlareClient capabilities.
1049
+ * @param fn - The transaction function.
1050
+ * @param options - Transaction options.
1051
+ * @returns The result of the transaction.
1052
+ */
1053
+ async transaction(fn, options) {
1054
+ return super.$transaction(async (tx) => {
1055
+ const extendedTx = new Proxy(tx, {
1056
+ get: (target, prop, receiver) => {
1057
+ if (prop === "from") {
1058
+ return (modelName) => {
1059
+ const key = modelName.charAt(0).toLowerCase() + modelName.slice(1);
1060
+ const model = target[key];
1061
+ if (!model) {
1062
+ throw new Error(`Model ${modelName} does not exist on TransactionClient.`);
1063
+ }
1064
+ return new FlareBuilder(model);
1065
+ };
1066
+ }
1067
+ return Reflect.get(target, prop, receiver);
1068
+ }
1069
+ });
1070
+ return fn(extendedTx);
1071
+ }, options);
1135
1072
  }
1136
- return client;
1073
+ };
1074
+ var ExtendedPrismaClient = FlareClient;
1075
+
1076
+ // src/core/hooks.ts
1077
+ function normalizeModelName(model) {
1078
+ return model.toLowerCase();
1079
+ }
1080
+ function beforeCreate(model, callback) {
1081
+ hookRegistry_default.addHook(normalizeModelName(model), "create", "before", callback);
1082
+ }
1083
+ function beforeDelete(model, callback) {
1084
+ hookRegistry_default.addHook(normalizeModelName(model), "delete", "before", callback);
1085
+ }
1086
+ function afterCreate(model, callback) {
1087
+ hookRegistry_default.addHook(normalizeModelName(model), "create", "after", callback);
1088
+ }
1089
+ function afterDelete(model, callback) {
1090
+ hookRegistry_default.addHook(normalizeModelName(model), "delete", "after", callback);
1091
+ }
1092
+ function beforeUpdate(model, callback) {
1093
+ hookRegistry_default.addHook(normalizeModelName(model), "update", "before", callback);
1094
+ }
1095
+ function afterUpdate(model, callback) {
1096
+ hookRegistry_default.addHook(normalizeModelName(model), "update", "after", callback);
1097
+ }
1098
+ function afterChange(model, column, callback) {
1099
+ hookRegistry_default.addColumnHook(normalizeModelName(model), column, callback);
1100
+ }
1101
+ function afterUpsert(model, callback) {
1102
+ hookRegistry_default.addHook(normalizeModelName(model), "upsert", "after", callback);
1137
1103
  }
1138
1104
 
1139
1105
  // src/core/adapters/postgres.ts
@@ -1216,8 +1182,8 @@ function parseDatabaseUrl(url) {
1216
1182
  }
1217
1183
 
1218
1184
  // src/core/adapters/sqlite.ts
1219
- var fs3 = __toESM(require("fs"), 1);
1220
- var path3 = __toESM(require("path"), 1);
1185
+ var fs2 = __toESM(require("fs"), 1);
1186
+ var path2 = __toESM(require("path"), 1);
1221
1187
  var SqliteAdapter = {
1222
1188
  name: "sqlite",
1223
1189
  matches(url) {
@@ -1225,13 +1191,13 @@ var SqliteAdapter = {
1225
1191
  },
1226
1192
  async create(url) {
1227
1193
  const filePath = parseSqliteUrl(url);
1228
- const dir = path3.dirname(filePath);
1194
+ const dir = path2.dirname(filePath);
1229
1195
  try {
1230
- if (!fs3.existsSync(dir)) {
1231
- fs3.mkdirSync(dir, { recursive: true });
1196
+ if (!fs2.existsSync(dir)) {
1197
+ fs2.mkdirSync(dir, { recursive: true });
1232
1198
  }
1233
- if (!fs3.existsSync(filePath)) {
1234
- fs3.writeFileSync(filePath, "");
1199
+ if (!fs2.existsSync(filePath)) {
1200
+ fs2.writeFileSync(filePath, "");
1235
1201
  console.log(`\u2705 SQLite database created at "${filePath}"`);
1236
1202
  } else {
1237
1203
  console.log(`\u26A0\uFE0F SQLite database already exists at "${filePath}"`);
@@ -1244,20 +1210,20 @@ var SqliteAdapter = {
1244
1210
  async drop(url) {
1245
1211
  const filePath = parseSqliteUrl(url);
1246
1212
  try {
1247
- if (fs3.existsSync(filePath)) {
1248
- fs3.unlinkSync(filePath);
1213
+ if (fs2.existsSync(filePath)) {
1214
+ fs2.unlinkSync(filePath);
1249
1215
  console.log(`\u2705 SQLite database at "${filePath}" dropped successfully.`);
1250
1216
  } else {
1251
1217
  console.log(`\u26A0\uFE0F SQLite database does not exist at "${filePath}"`);
1252
1218
  }
1253
- if (fs3.existsSync(`${filePath}-journal`)) {
1254
- fs3.unlinkSync(`${filePath}-journal`);
1219
+ if (fs2.existsSync(`${filePath}-journal`)) {
1220
+ fs2.unlinkSync(`${filePath}-journal`);
1255
1221
  }
1256
- if (fs3.existsSync(`${filePath}-wal`)) {
1257
- fs3.unlinkSync(`${filePath}-wal`);
1222
+ if (fs2.existsSync(`${filePath}-wal`)) {
1223
+ fs2.unlinkSync(`${filePath}-wal`);
1258
1224
  }
1259
- if (fs3.existsSync(`${filePath}-shm`)) {
1260
- fs3.unlinkSync(`${filePath}-shm`);
1225
+ if (fs2.existsSync(`${filePath}-shm`)) {
1226
+ fs2.unlinkSync(`${filePath}-shm`);
1261
1227
  }
1262
1228
  } catch (error) {
1263
1229
  console.error("\u274C Error dropping SQLite database:", error);
@@ -1267,8 +1233,8 @@ var SqliteAdapter = {
1267
1233
  };
1268
1234
  function parseSqliteUrl(url) {
1269
1235
  let cleanPath = url.replace(/^file:/, "");
1270
- if (!path3.isAbsolute(cleanPath)) {
1271
- cleanPath = path3.resolve(process.cwd(), cleanPath);
1236
+ if (!path2.isAbsolute(cleanPath)) {
1237
+ cleanPath = path2.resolve(process.cwd(), cleanPath);
1272
1238
  }
1273
1239
  return cleanPath;
1274
1240
  }
package/dist/index.d.cts CHANGED
@@ -8,8 +8,18 @@ import { M as ModelName, l as PrismaOperation, m as HookTiming, B as BeforeHookC
8
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
9
  export { afterChange, afterCreate, afterDelete, afterUpdate, afterUpsert, beforeCreate, beforeDelete, beforeUpdate } from './core/hooks.cjs';
10
10
 
11
+ interface FlareClientOptions extends PrismaClientOptions {
12
+ /**
13
+ * Enable callbacks/hooks middleware. When true (default), the middleware
14
+ * that executes your registered callbacks (beforeCreate, afterUpdate, etc.)
15
+ * is automatically attached.
16
+ *
17
+ * @default true
18
+ */
19
+ callbacks?: boolean;
20
+ }
11
21
  declare class FlareClient extends PrismaClient {
12
- constructor(options?: PrismaClientOptions);
22
+ constructor(options?: FlareClientOptions);
13
23
  /**
14
24
  * Creates a new FlareBuilder instance for the specified model.
15
25
  * @param modelName - The name of the model.
@@ -186,22 +196,21 @@ declare function createHooksExtension(basePrisma: PrismaClient): (client: any) =
186
196
  */
187
197
  declare function registerHooksLegacy(prisma: PrismaClient): void;
188
198
  /**
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
+ * @deprecated Use `new FlareClient()` instead. FlareClient now automatically
200
+ * attaches the callbacks middleware. This function will be removed in a future version.
199
201
  *
200
202
  * @example
201
- * // In your db setup file:
202
- * export const db = await registerHooks(new FlareClient());
203
+ * // Old way (deprecated):
204
+ * import './callbacks';
205
+ * import { FlareClient, registerHooks } from 'prisma-flare';
206
+ * export const db = registerHooks(new FlareClient());
207
+ *
208
+ * // New way:
209
+ * import './callbacks';
210
+ * import { FlareClient } from 'prisma-flare';
211
+ * export const db = new FlareClient();
203
212
  */
204
- declare function registerHooks<T extends PrismaClient>(prisma: T): Promise<T>;
213
+ declare function registerHooks<T extends PrismaClient>(prisma: T): T;
205
214
 
206
215
  /**
207
216
  * Database Adapter Interface
@@ -234,4 +243,4 @@ declare class AdapterRegistry {
234
243
  }
235
244
  declare const registry: AdapterRegistry;
236
245
 
237
- export { AfterHookCallback, BeforeHookCallback, ColumnChangeCallback, type DatabaseAdapter, ExtendedPrismaClient, FlareBuilder, FlareClient, type HookConfig, HookTiming, ModelName, PrismaOperation, createHooksExtension, registry as dbAdapterRegistry, hookRegistry, loadCallbacks, modelRegistry, registerHooks, registerHooksLegacy };
246
+ export { AfterHookCallback, BeforeHookCallback, ColumnChangeCallback, type DatabaseAdapter, ExtendedPrismaClient, FlareBuilder, FlareClient, type FlareClientOptions, type HookConfig, HookTiming, ModelName, PrismaOperation, createHooksExtension, registry as dbAdapterRegistry, hookRegistry, loadCallbacks, modelRegistry, registerHooks, registerHooksLegacy };
package/dist/index.d.ts CHANGED
@@ -8,8 +8,18 @@ import { M as ModelName, l as PrismaOperation, m as HookTiming, B as BeforeHookC
8
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
9
  export { afterChange, afterCreate, afterDelete, afterUpdate, afterUpsert, beforeCreate, beforeDelete, beforeUpdate } from './core/hooks.js';
10
10
 
11
+ interface FlareClientOptions extends PrismaClientOptions {
12
+ /**
13
+ * Enable callbacks/hooks middleware. When true (default), the middleware
14
+ * that executes your registered callbacks (beforeCreate, afterUpdate, etc.)
15
+ * is automatically attached.
16
+ *
17
+ * @default true
18
+ */
19
+ callbacks?: boolean;
20
+ }
11
21
  declare class FlareClient extends PrismaClient {
12
- constructor(options?: PrismaClientOptions);
22
+ constructor(options?: FlareClientOptions);
13
23
  /**
14
24
  * Creates a new FlareBuilder instance for the specified model.
15
25
  * @param modelName - The name of the model.
@@ -186,22 +196,21 @@ declare function createHooksExtension(basePrisma: PrismaClient): (client: any) =
186
196
  */
187
197
  declare function registerHooksLegacy(prisma: PrismaClient): void;
188
198
  /**
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
+ * @deprecated Use `new FlareClient()` instead. FlareClient now automatically
200
+ * attaches the callbacks middleware. This function will be removed in a future version.
199
201
  *
200
202
  * @example
201
- * // In your db setup file:
202
- * export const db = await registerHooks(new FlareClient());
203
+ * // Old way (deprecated):
204
+ * import './callbacks';
205
+ * import { FlareClient, registerHooks } from 'prisma-flare';
206
+ * export const db = registerHooks(new FlareClient());
207
+ *
208
+ * // New way:
209
+ * import './callbacks';
210
+ * import { FlareClient } from 'prisma-flare';
211
+ * export const db = new FlareClient();
203
212
  */
204
- declare function registerHooks<T extends PrismaClient>(prisma: T): Promise<T>;
213
+ declare function registerHooks<T extends PrismaClient>(prisma: T): T;
205
214
 
206
215
  /**
207
216
  * Database Adapter Interface
@@ -234,4 +243,4 @@ declare class AdapterRegistry {
234
243
  }
235
244
  declare const registry: AdapterRegistry;
236
245
 
237
- export { AfterHookCallback, BeforeHookCallback, ColumnChangeCallback, type DatabaseAdapter, ExtendedPrismaClient, FlareBuilder, FlareClient, type HookConfig, HookTiming, ModelName, PrismaOperation, createHooksExtension, registry as dbAdapterRegistry, hookRegistry, loadCallbacks, modelRegistry, registerHooks, registerHooksLegacy };
246
+ export { AfterHookCallback, BeforeHookCallback, ColumnChangeCallback, type DatabaseAdapter, ExtendedPrismaClient, FlareBuilder, FlareClient, type FlareClientOptions, type HookConfig, HookTiming, ModelName, PrismaOperation, createHooksExtension, registry as dbAdapterRegistry, hookRegistry, loadCallbacks, modelRegistry, registerHooks, registerHooksLegacy };