Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lemon-swans-sink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@baseplate-dev/fastify-generators': patch
---

Pass service context to create/update/delete operations in data operations
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ export interface CreateOperationConfig<
tx: PrismaTransaction;
data: InferFieldsCreateOutput<TFields> & TPrepareResult;
query: { include: NonNullable<TQueryArgs['include']> };
serviceContext: ServiceContext;
}) => Promise<
Result<
(typeof prisma)[TModelName],
Expand Down Expand Up @@ -386,11 +387,12 @@ type CreateOperationFunction<
* authorize: async (data, ctx) => {
* // Check if user has permission to create
* },
* create: ({ tx, data, query }) =>
* create: ({ tx, data, query, serviceContext }) =>
* tx.user.create({
* data: {
* name: data.name,
* email: data.email,
* createdById: serviceContext.user?.id,
* },
* ...query,
* }),
Expand Down Expand Up @@ -501,6 +503,7 @@ export function defineCreateOperation<
query: (query ?? {}) as {
include: NonNullable<TQueryArgs['include']>;
},
serviceContext: context,
});

// Run afterExecute hooks
Expand Down Expand Up @@ -585,6 +588,7 @@ export interface UpdateOperationConfig<
where: WhereUniqueInput<TModelName>;
data: InferFieldsUpdateOutput<TFields> & TPrepareResult;
query: { include: NonNullable<TQueryArgs['include']> };
serviceContext: ServiceContext;
}) => Promise<
Result<
(typeof prisma)[TModelName],
Expand Down Expand Up @@ -666,10 +670,13 @@ type UpdateOperationFunction<
* const existing = await ctx.loadExisting();
* // Check if user owns this record
* },
* update: ({ tx, where, data, query }) =>
* update: ({ tx, where, data, query, serviceContext }) =>
* tx.user.update({
* where,
* data,
* data: {
* ...data,
* updatedById: serviceContext.user?.id,
* },
* ...query,
* }),
* });
Expand Down Expand Up @@ -807,6 +814,7 @@ export function defineUpdateOperation<
query: (query ?? {}) as {
include: NonNullable<TQueryArgs['include']>;
},
serviceContext: context,
});

// Run afterExecute hooks
Expand Down Expand Up @@ -865,6 +873,7 @@ export interface DeleteOperationConfig<TModelName extends ModelPropName> {
tx: PrismaTransaction;
where: WhereUniqueInput<TModelName>;
query: { include: NonNullable<TQueryArgs['include']> };
serviceContext: ServiceContext;
}) => Promise<
Result<
(typeof prisma)[TModelName],
Expand Down Expand Up @@ -920,7 +929,7 @@ export interface DeleteOperationInput<
* const existing = await ctx.loadExisting();
* // Check if user has permission to delete
* },
* delete: ({ tx, where, query }) =>
* delete: ({ tx, where, query, serviceContext }) =>
* tx.user.delete({
* where,
* ...query,
Expand Down Expand Up @@ -1010,6 +1019,7 @@ export function defineDeleteOperation<TModelName extends ModelPropName>(
query: (query ?? {}) as {
include: NonNullable<TQueryArgs['include']>;
},
serviceContext: context,
});

// Run afterExecute hooks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ export interface CreateOperationConfig<
tx: PrismaTransaction;
data: InferFieldsCreateOutput<TFields> & TPrepareResult;
query: { include: NonNullable<TQueryArgs['include']> };
serviceContext: ServiceContext;
}) => Promise<
Result<
(typeof prisma)[TModelName],
Expand Down Expand Up @@ -386,11 +387,12 @@ type CreateOperationFunction<
* authorize: async (data, ctx) => {
* // Check if user has permission to create
* },
* create: ({ tx, data, query }) =>
* create: ({ tx, data, query, serviceContext }) =>
* tx.user.create({
* data: {
* name: data.name,
* email: data.email,
* createdById: serviceContext.user?.id,
* },
* ...query,
* }),
Expand Down Expand Up @@ -501,6 +503,7 @@ export function defineCreateOperation<
query: (query ?? {}) as {
include: NonNullable<TQueryArgs['include']>;
},
serviceContext: context,
});

// Run afterExecute hooks
Expand Down Expand Up @@ -585,6 +588,7 @@ export interface UpdateOperationConfig<
where: WhereUniqueInput<TModelName>;
data: InferFieldsUpdateOutput<TFields> & TPrepareResult;
query: { include: NonNullable<TQueryArgs['include']> };
serviceContext: ServiceContext;
}) => Promise<
Result<
(typeof prisma)[TModelName],
Expand Down Expand Up @@ -666,10 +670,13 @@ type UpdateOperationFunction<
* const existing = await ctx.loadExisting();
* // Check if user owns this record
* },
* update: ({ tx, where, data, query }) =>
* update: ({ tx, where, data, query, serviceContext }) =>
* tx.user.update({
* where,
* data,
* data: {
* ...data,
* updatedById: serviceContext.user?.id,
* },
* ...query,
* }),
* });
Expand Down Expand Up @@ -807,6 +814,7 @@ export function defineUpdateOperation<
query: (query ?? {}) as {
include: NonNullable<TQueryArgs['include']>;
},
serviceContext: context,
});

// Run afterExecute hooks
Expand Down Expand Up @@ -865,6 +873,7 @@ export interface DeleteOperationConfig<TModelName extends ModelPropName> {
tx: PrismaTransaction;
where: WhereUniqueInput<TModelName>;
query: { include: NonNullable<TQueryArgs['include']> };
serviceContext: ServiceContext;
}) => Promise<
Result<
(typeof prisma)[TModelName],
Expand Down Expand Up @@ -920,7 +929,7 @@ export interface DeleteOperationInput<
* const existing = await ctx.loadExisting();
* // Check if user has permission to delete
* },
* delete: ({ tx, where, query }) =>
* delete: ({ tx, where, query, serviceContext }) =>
* tx.user.delete({
* where,
* ...query,
Expand Down Expand Up @@ -1010,6 +1019,7 @@ export function defineDeleteOperation<TModelName extends ModelPropName>(
query: (query ?? {}) as {
include: NonNullable<TQueryArgs['include']>;
},
serviceContext: context,
});

// Run afterExecute hooks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ export interface CreateOperationConfig<
tx: PrismaTransaction;
data: InferFieldsCreateOutput<TFields> & TPrepareResult;
query: { include: NonNullable<TQueryArgs['include']> };
serviceContext: ServiceContext;
}) => Promise<
Result<
(typeof prisma)[TModelName],
Expand Down Expand Up @@ -386,11 +387,12 @@ type CreateOperationFunction<
* authorize: async (data, ctx) => {
* // Check if user has permission to create
* },
* create: ({ tx, data, query }) =>
* create: ({ tx, data, query, serviceContext }) =>
* tx.user.create({
* data: {
* name: data.name,
* email: data.email,
* createdById: serviceContext.user?.id,
* },
* ...query,
* }),
Expand Down Expand Up @@ -501,6 +503,7 @@ export function defineCreateOperation<
query: (query ?? {}) as {
include: NonNullable<TQueryArgs['include']>;
},
serviceContext: context,
});

// Run afterExecute hooks
Expand Down Expand Up @@ -585,6 +588,7 @@ export interface UpdateOperationConfig<
where: WhereUniqueInput<TModelName>;
data: InferFieldsUpdateOutput<TFields> & TPrepareResult;
query: { include: NonNullable<TQueryArgs['include']> };
serviceContext: ServiceContext;
}) => Promise<
Result<
(typeof prisma)[TModelName],
Expand Down Expand Up @@ -666,10 +670,13 @@ type UpdateOperationFunction<
* const existing = await ctx.loadExisting();
* // Check if user owns this record
* },
* update: ({ tx, where, data, query }) =>
* update: ({ tx, where, data, query, serviceContext }) =>
* tx.user.update({
* where,
* data,
* data: {
* ...data,
* updatedById: serviceContext.user?.id,
* },
* ...query,
* }),
* });
Expand Down Expand Up @@ -807,6 +814,7 @@ export function defineUpdateOperation<
query: (query ?? {}) as {
include: NonNullable<TQueryArgs['include']>;
},
serviceContext: context,
});

// Run afterExecute hooks
Expand Down Expand Up @@ -865,6 +873,7 @@ export interface DeleteOperationConfig<TModelName extends ModelPropName> {
tx: PrismaTransaction;
where: WhereUniqueInput<TModelName>;
query: { include: NonNullable<TQueryArgs['include']> };
serviceContext: ServiceContext;
}) => Promise<
Result<
(typeof prisma)[TModelName],
Expand Down Expand Up @@ -920,7 +929,7 @@ export interface DeleteOperationInput<
* const existing = await ctx.loadExisting();
* // Check if user has permission to delete
* },
* delete: ({ tx, where, query }) =>
* delete: ({ tx, where, query, serviceContext }) =>
* tx.user.delete({
* where,
* ...query,
Expand Down Expand Up @@ -1010,6 +1019,7 @@ export function defineDeleteOperation<TModelName extends ModelPropName>(
query: (query ?? {}) as {
include: NonNullable<TQueryArgs['include']>;
},
serviceContext: context,
});

// Run afterExecute hooks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ export interface CreateOperationConfig<
tx: PrismaTransaction;
data: InferFieldsCreateOutput<TFields> & TPrepareResult;
query: { include: NonNullable<TQueryArgs['include']> };
serviceContext: ServiceContext;
}) => Promise<
Result<
(typeof prisma)[TModelName],
Expand Down Expand Up @@ -386,11 +387,12 @@ type CreateOperationFunction<
* authorize: async (data, ctx) => {
* // Check if user has permission to create
* },
* create: ({ tx, data, query }) =>
* create: ({ tx, data, query, serviceContext }) =>
* tx.user.create({
* data: {
* name: data.name,
* email: data.email,
* createdById: serviceContext.user?.id,
* },
* ...query,
* }),
Expand Down Expand Up @@ -501,6 +503,7 @@ export function defineCreateOperation<
query: (query ?? {}) as {
include: NonNullable<TQueryArgs['include']>;
},
serviceContext: context,
});

// Run afterExecute hooks
Expand Down Expand Up @@ -585,6 +588,7 @@ export interface UpdateOperationConfig<
where: WhereUniqueInput<TModelName>;
data: InferFieldsUpdateOutput<TFields> & TPrepareResult;
query: { include: NonNullable<TQueryArgs['include']> };
serviceContext: ServiceContext;
}) => Promise<
Result<
(typeof prisma)[TModelName],
Expand Down Expand Up @@ -666,10 +670,13 @@ type UpdateOperationFunction<
* const existing = await ctx.loadExisting();
* // Check if user owns this record
* },
* update: ({ tx, where, data, query }) =>
* update: ({ tx, where, data, query, serviceContext }) =>
* tx.user.update({
* where,
* data,
* data: {
* ...data,
* updatedById: serviceContext.user?.id,
* },
* ...query,
* }),
* });
Expand Down Expand Up @@ -807,6 +814,7 @@ export function defineUpdateOperation<
query: (query ?? {}) as {
include: NonNullable<TQueryArgs['include']>;
},
serviceContext: context,
});

// Run afterExecute hooks
Expand Down Expand Up @@ -865,6 +873,7 @@ export interface DeleteOperationConfig<TModelName extends ModelPropName> {
tx: PrismaTransaction;
where: WhereUniqueInput<TModelName>;
query: { include: NonNullable<TQueryArgs['include']> };
serviceContext: ServiceContext;
}) => Promise<
Result<
(typeof prisma)[TModelName],
Expand Down Expand Up @@ -920,7 +929,7 @@ export interface DeleteOperationInput<
* const existing = await ctx.loadExisting();
* // Check if user has permission to delete
* },
* delete: ({ tx, where, query }) =>
* delete: ({ tx, where, query, serviceContext }) =>
* tx.user.delete({
* where,
* ...query,
Expand Down Expand Up @@ -1010,6 +1019,7 @@ export function defineDeleteOperation<TModelName extends ModelPropName>(
query: (query ?? {}) as {
include: NonNullable<TQueryArgs['include']>;
},
serviceContext: context,
});

// Run afterExecute hooks
Expand Down
Loading