Skip to content

Conversation

@Tenrys
Copy link

@Tenrys Tenrys commented Mar 24, 2023

This allows to do something like this:

import { Prisma } from "@prisma/client";
import { NestedParams } from "prisma-nested-middleware";

const archivedField = "archivedAt" as const;
type Model = Omit<Prisma.DMMF.Model, "fields"> & {
	fields: Record<string, Prisma.DMMF.Field>;
};
const models = Prisma.dmmf.datamodel.models.reduce((acc, model) => {
	acc[model.name as Prisma.ModelName] = {
		...model,
		fields: model.fields.reduce((acc, field) => {
			acc[field.name] = field;
			return acc;
		}),
	};
	return acc;
}, {} as Record<Prisma.ModelName, Model>);
const modelsWithArchivedAt = Object.fromEntries(
	Object.entries(models).filter(([, model]) => model.fields[archivedField])
);

export default (params: NestedParams) => {
	if (!params.model) return;

	if (["count", "findFirst", "findMany", "update", "updateMany", "select"].includes(params.action)) {
		if (modelsWithArchivedAt[params.model]) {
			if (params.action == "update") params.action = "updateMany";
			if (params.action == "select" && !params.relation?.isList) return;
			if (!params.args.where) params.args.where = {};
			if (params.args.where[archivedField] == undefined) {
				params.args.where[archivedField] = null;
			}
		}
	}
};

This line in particular:

if (params.action == "select" && !params.relation?.isList) return;

@olivierwilkinson
Copy link
Owner

olivierwilkinson commented Mar 24, 2023

Hi there 👋 I actually have a new major release in the works that includes relations in the params along with some other new features. I'm doing some final cleanup of it so it should be out this weekend, I'll leave this open for the meantime so that we can chat on here about if the new release works for your use case 😄

@olivierwilkinson
Copy link
Owner

olivierwilkinson commented Mar 27, 2023

Heya,

The major version I was hoping to release needs some more work before it's ready to go out so I've released a minor version that includes the relation info in params 😄, it is available in v2.3.0.

I'm still hoping to get the major version out in the next few days as long as there are no other wrinkles. Also just to give you a heads up, in tha version I've restructured the params so that the scope object includes the relation info so if you don't have any pressing need to upgrade it might be worth waiting for that to land.

@Tenrys
Copy link
Author

Tenrys commented Mar 27, 2023

So, with 2.3.0 the relation is alongside scope in the params, but in the next major version it'll be within scope, yes?

@olivierwilkinson
Copy link
Owner

olivierwilkinson commented Mar 27, 2023

So, with 2.3.0 the relation is alongside scope in the params, but in the next major version it'll be within scope, yes?

Exactly right 👍

The next major version adds quite a few new things to NestedParams so it made sense to update the scope object to accomodate them, roughly it will look like this:

type Scope = {
  parentParams: NestedParams,
  relation: Prisma.DMMF.Field,
  ...[other fields]
}

@olivierwilkinson
Copy link
Owner

olivierwilkinson commented Mar 28, 2023

Heya 👋

I've released v3, it ended up slightly differently to what I described here: I realised that in one of my middleware it would be useful to know the relation from the current model back to it's parent, so I've included that in the scope object as well.

The new scope object has the following structure:

type Scope = {
  parentParams: NestedParams;
  relations: { to: Prisma.DMMF.Field; from: Prisma.DMMF.Field };
  ...[where action fields]
};

In order to do what you outlined above you would use scope.relations.to since that is the relation from the parent to the current model. The scope.relations.from field is the relation from the model back to it's parent.

I hope you find the new version useful, there are quite a few new features so check out the readme. Please let me know if you find any issues! 😄

@Tenrys Tenrys closed this Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants