@explita/prisma-audit-log 0.2.0 → 0.2.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.
@@ -1,2 +1,2 @@
1
- import type { HandlerArgs } from "../types.js";
2
- export declare function handleCreate(opts: HandlerArgs): Promise<any>;
1
+ import type { AuditLog, HandlerArgs } from "../types.js";
2
+ export declare function handleCreate(opts: HandlerArgs, action?: AuditLog["action"]): Promise<any>;
@@ -2,11 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.handleCreate = handleCreate;
4
4
  const process_js_1 = require("./process.js");
5
- async function handleCreate(opts) {
5
+ async function handleCreate(opts, action = "CREATE") {
6
6
  const { args, prisma, query, options, modelName } = opts;
7
7
  const result = await query(args);
8
8
  const auditLog = {
9
- action: "CREATE",
9
+ action,
10
10
  model: modelName,
11
11
  recordId: result.id,
12
12
  newData: result,
@@ -16,7 +16,7 @@ async function handleUpdateMany(opts) {
16
16
  const ids = currentRecords.map((row) => row.id);
17
17
  const result = await query(args);
18
18
  //updated records
19
- const updatedRecords = operation == "createManyAndReturn"
19
+ const updatedRecords = operation == "updateManyAndReturn"
20
20
  ? result
21
21
  : await prisma[modelName].findMany({
22
22
  where: { id: { in: ids } },
@@ -28,6 +28,8 @@ async function handleUpdateMany(opts) {
28
28
  if (!updated)
29
29
  continue;
30
30
  const changedFields = (0, utils_js_1.getChangedFields)(current, updated);
31
+ if (changedFields.length === 0)
32
+ continue;
31
33
  if (changedFields.length === 1 &&
32
34
  (changedFields[0] === "updatedAt" || changedFields[0] === "updated_at")) {
33
35
  continue;
@@ -1,2 +1,2 @@
1
- import type { HandlerArgs } from "../types.js";
2
- export declare function handleUpdate(opts: HandlerArgs): Promise<any>;
1
+ import type { AuditLog, HandlerArgs } from "../types.js";
2
+ export declare function handleUpdate(opts: HandlerArgs, action?: AuditLog["action"]): Promise<any>;
@@ -3,29 +3,34 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.handleUpdate = handleUpdate;
4
4
  const utils_js_1 = require("../lib/utils.js");
5
5
  const process_js_1 = require("./process.js");
6
- async function handleUpdate(opts) {
7
- const { args, prisma, query, options, modelName } = opts;
8
- // Get the current state before update
9
- const current = await prisma[modelName].findUnique({
10
- where: args["where"],
11
- });
6
+ async function handleUpdate(opts, action = "UPDATE") {
7
+ const { args, prisma, query, options, modelName, existingRecord } = opts;
8
+ // Use provided existingRecord or fetch it if not provided
9
+ const current = existingRecord ||
10
+ (await prisma[modelName].findUnique({
11
+ where: args["where"],
12
+ }));
13
+ if (!current) {
14
+ return query(args);
15
+ }
12
16
  const result = await query(args);
13
- if (current) {
14
- const changedFields = (0, utils_js_1.getChangedFields)(current, result);
15
- if (changedFields.length === 1 &&
16
- (changedFields[0] === "updatedAt" || changedFields[0] === "updated_at")) {
17
- return result;
18
- }
19
- const { oldData, newData } = (0, utils_js_1.buildSnapshot)(changedFields, current, result);
20
- const auditLog = {
21
- action: "UPDATE",
22
- model: modelName,
23
- recordId: result.id,
24
- oldData,
25
- newData,
26
- changedFields,
27
- };
28
- await (0, process_js_1.saveAuditLogs)(prisma, [auditLog], options);
17
+ const changedFields = (0, utils_js_1.getChangedFields)(current, result);
18
+ if (changedFields.length === 0)
19
+ return result;
20
+ // Skip if only timestamps were updated
21
+ if (changedFields.length === 1 &&
22
+ (changedFields[0] === "updatedAt" || changedFields[0] === "updated_at")) {
23
+ return result;
29
24
  }
25
+ const { oldData, newData } = (0, utils_js_1.buildSnapshot)(changedFields, current, result);
26
+ const auditLog = {
27
+ action,
28
+ model: modelName,
29
+ recordId: result.id,
30
+ oldData,
31
+ newData,
32
+ changedFields,
33
+ };
34
+ await (0, process_js_1.saveAuditLogs)(prisma, [auditLog], options);
30
35
  return result;
31
36
  }
@@ -0,0 +1,2 @@
1
+ import type { HandlerArgs } from "../types.js";
2
+ export declare function handleUpsert(opts: HandlerArgs): Promise<any>;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handleUpsert = handleUpsert;
4
+ const create_js_1 = require("./create.js");
5
+ const update_js_1 = require("./update.js");
6
+ async function handleUpsert(opts) {
7
+ const { args, prisma, modelName } = opts;
8
+ // Check if the record exists before performing the upsert
9
+ const existingRecord = await prisma[modelName].findUnique({
10
+ where: args["where"],
11
+ });
12
+ if (existingRecord) {
13
+ // Handle update case with the existing record to avoid extra query
14
+ // Pass a custom action to handleUpdate to indicate this is from an upsert
15
+ return await (0, update_js_1.handleUpdate)({
16
+ ...opts,
17
+ existingRecord,
18
+ }, "UPDATE_upsert");
19
+ }
20
+ else {
21
+ // Handle create case with custom action
22
+ return await (0, create_js_1.handleCreate)({
23
+ ...opts,
24
+ }, "CREATE_upsert");
25
+ }
26
+ }
@@ -9,6 +9,7 @@ const update_many_js_1 = require("../core/update-many.js");
9
9
  const delete_js_1 = require("../core/delete.js");
10
10
  const delete_many_js_1 = require("../core/delete-many.js");
11
11
  const update_js_1 = require("../core/update.js");
12
+ const upsert_js_1 = require("../core/upsert.js");
12
13
  /**
13
14
  * Creates a Prisma extension that adds audit logging
14
15
  */
@@ -63,6 +64,8 @@ function auditLogExtension(options = {}) {
63
64
  case "updateMany":
64
65
  case "updateManyAndReturn":
65
66
  return (0, update_many_js_1.handleUpdateMany)(queryArgs);
67
+ case "upsert":
68
+ return (0, upsert_js_1.handleUpsert)(queryArgs);
66
69
  default:
67
70
  return query(args);
68
71
  }
package/dist/types.d.ts CHANGED
@@ -57,7 +57,7 @@ export interface AuditLogOptions {
57
57
  }
58
58
  export interface AuditLog {
59
59
  id: string;
60
- action: "CREATE" | "UPDATE" | "DELETE";
60
+ action: "CREATE" | "UPDATE" | "DELETE" | "CREATE_upsert" | "UPDATE_upsert";
61
61
  model: string;
62
62
  recordId: string;
63
63
  oldData?: Record<string, any>;
@@ -83,7 +83,8 @@ export type HandlerArgs = {
83
83
  args: JsArgs;
84
84
  query: (args: JsArgs) => Promise<any>;
85
85
  options: AuditLogOptions;
86
- operation?: string;
86
+ operation: string;
87
+ existingRecord?: any;
87
88
  };
88
89
  export type FieldFilterConfig = {
89
90
  include?: string[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@explita/prisma-audit-log",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "A Prisma extension for automatic audit logging",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",