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
2 changes: 0 additions & 2 deletions packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@
"safe-json-stringify": "^1.2.0",
"semver": "^7.5.2",
"superjson": "^1.13.0",
"traverse": "^0.6.10",
"ts-pattern": "^4.3.0",
"tslib": "^2.4.1",
"uuid": "^9.0.0",
Expand All @@ -127,7 +126,6 @@
"@types/pluralize": "^0.0.29",
"@types/safe-json-stringify": "^1.1.5",
"@types/semver": "^7.3.13",
"@types/traverse": "^0.6.37",
"@types/uuid": "^8.3.4",
"decimal.js-light": "^2.5.1",
"superjson": "^1.13.0",
Expand Down
9 changes: 4 additions & 5 deletions packages/runtime/src/enhancements/node/delegate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import deepmerge, { type ArrayMergeOptions } from 'deepmerge';
import traverse from 'traverse';
import { DELEGATE_AUX_RELATION_PREFIX } from '../../constants';
import {
FieldInfo,
Expand All @@ -14,7 +13,7 @@ import {
isDelegateModel,
resolveField,
} from '../../cross';
import { isPlainObject, lowerCaseFirst } from '../../local-helpers';
import { isPlainObject, simpleTraverse, lowerCaseFirst } from '../../local-helpers';
import type { CrudContract, DbClientContract, EnhancementContext } from '../../types';
import type { InternalEnhancementOptions } from './create-enhancement';
import { Logger } from './logger';
Expand Down Expand Up @@ -487,12 +486,12 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {

const prisma = this.prisma;
const prismaModule = this.options.prismaModule;
traverse(data).forEach(function () {
if (this.key?.startsWith(DELEGATE_AUX_RELATION_PREFIX)) {
simpleTraverse(data, ({ key }) => {
if (key?.startsWith(DELEGATE_AUX_RELATION_PREFIX)) {
throw prismaClientValidationError(
prisma,
prismaModule,
`Auxiliary relation field "${this.key}" cannot be set directly`
`Auxiliary relation field "${key}" cannot be set directly`
);
}
});
Expand Down
18 changes: 7 additions & 11 deletions packages/runtime/src/enhancements/node/policy/policy-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import deepmerge from 'deepmerge';
import traverse from 'traverse';
import { z, type ZodError, type ZodObject, type ZodSchema } from 'zod';
import { fromZodError } from 'zod-validation-error';
import { CrudFailureReason, PrismaErrorCode } from '../../../constants';
Expand All @@ -15,7 +14,7 @@ import {
type FieldInfo,
type ModelMeta,
} from '../../../cross';
import { isPlainObject, lowerCaseFirst, upperCaseFirst } from '../../../local-helpers';
import { isPlainObject, simpleTraverse, lowerCaseFirst, upperCaseFirst } from '../../../local-helpers';
import {
AuthUser,
CrudContract,
Expand Down Expand Up @@ -691,27 +690,24 @@ export class PolicyUtil extends QueryUtils {
// here we prefix the constraint variables coming from delegated checkers
// with the relation field name to avoid conflicts
const prefixConstraintVariables = (constraint: unknown, prefix: string) => {
return traverse(constraint).map(function (value) {
return simpleTraverse(constraint, ({ value, update }) => {
if (isVariableConstraint(value)) {
this.update(
update(
{
...value,
name: `${prefix}${value.name}`,
},
true
}
);
}
});
};

// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this;
result = traverse(result).forEach(function (value) {
result = simpleTraverse(result, ({ value, update }) => {
if (isDelegateConstraint(value)) {
const { model: delegateModel, relation, operation: delegateOp } = value;
let newValue = that.getCheckerConstraint(delegateModel, delegateOp ?? operation);
let newValue = this.getCheckerConstraint(delegateModel, delegateOp ?? operation);
newValue = prefixConstraintVariables(newValue, `${relation}.`);
this.update(newValue, true);
update(newValue);
}
});

Expand Down
1 change: 1 addition & 0 deletions packages/runtime/src/local-helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './simple-traverse';
export * from './sleep';
export * from './is-plain-object';
export * from './lower-case-first';
Expand Down
61 changes: 61 additions & 0 deletions packages/runtime/src/local-helpers/simple-traverse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

type Cb = (
data: {
path: readonly string[];
key: string | undefined;
value: any;
update: (nextValue: any) => void;
}
) => void;

export function simpleTraverse<T>(root: T, cb: Cb) {
const path: string[] = [];
const parents: any[] = [];

function walker(node: any) {
const isObject = typeof node === 'object' && node !== null;
const isCircular = isObject && parents.some((p) => p === node);

let keepGoing = true;

function update(nextValue: any) {
if (path.length) {
const parent = parents[parents.length - 1];
const key = path[path.length - 1];
parent[key] = nextValue;
}

node = nextValue;

keepGoing = false;
}

cb({
path: [...path],
key: path[path.length - 1],
value: node,
update,
});

if (!keepGoing) return node;

if (isObject && !isCircular) {
parents.push(node);

Object.keys(node).forEach((key) => {
path.push(key);

walker(node[key]);

path.pop();
});

parents.pop();
}

return node;
}

return walker(root);
}
Loading
Loading