Skip to content

Commit 23a06cc

Browse files
committed
feat: add encrypted kind
1 parent b41fd93 commit 23a06cc

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

packages/runtime/src/enhancements/node/create-enhancement.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ import { withJsonProcessor } from './json-processor';
1414
import { Logger } from './logger';
1515
import { withOmit } from './omit';
1616
import { withPassword } from './password';
17+
import { withEncrypted } from './encrypted';
1718
import { policyProcessIncludeRelationPayload, withPolicy } from './policy';
1819
import type { PolicyDef } from './types';
1920

2021
/**
2122
* All enhancement kinds
2223
*/
23-
const ALL_ENHANCEMENTS: EnhancementKind[] = ['password', 'omit', 'policy', 'validation', 'delegate'];
24+
const ALL_ENHANCEMENTS: EnhancementKind[] = ['password', 'omit', 'policy', 'validation', 'delegate', 'encrypted'];
2425

2526
/**
2627
* Options for {@link createEnhancement}
@@ -100,6 +101,7 @@ export function createEnhancement<DbClient extends object>(
100101
}
101102

102103
const hasPassword = allFields.some((field) => field.attributes?.some((attr) => attr.name === '@password'));
104+
const hasEncrypted = allFields.some((field) => field.attributes?.some((attr) => attr.name === '@encrypted'));
103105
const hasOmit = allFields.some((field) => field.attributes?.some((attr) => attr.name === '@omit'));
104106
const hasDefaultAuth = allFields.some((field) => field.defaultValueProvider);
105107
const hasTypeDefField = allFields.some((field) => field.isTypeDef);
@@ -120,13 +122,18 @@ export function createEnhancement<DbClient extends object>(
120122
}
121123
}
122124

123-
// password enhancement must be applied prior to policy because it changes then length of the field
125+
// password and encrypted enhancement must be applied prior to policy because it changes then length of the field
124126
// and can break validation rules like `@length`
125127
if (hasPassword && kinds.includes('password')) {
126128
// @password proxy
127129
result = withPassword(result, options);
128130
}
129131

132+
if (hasEncrypted && kinds.includes('encrypted')) {
133+
// @encrypted proxy
134+
result = withEncrypted(result, options);
135+
}
136+
130137
// 'policy' and 'validation' enhancements are both enabled by `withPolicy`
131138
if (kinds.includes('policy') || kinds.includes('validation')) {
132139
result = withPolicy(result, options, context);

packages/runtime/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export type EnhancementContext<User extends AuthUser = AuthUser> = {
145145
/**
146146
* Kinds of enhancements to `PrismaClient`
147147
*/
148-
export type EnhancementKind = 'password' | 'omit' | 'policy' | 'validation' | 'delegate';
148+
export type EnhancementKind = 'password' | 'omit' | 'policy' | 'validation' | 'delegate' | 'encrypted';
149149

150150
/**
151151
* Function for transforming errors.

packages/schema/src/res/stdlib.zmodel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,14 @@ attribute @@auth() @@@supportTypeDef
552552
*/
553553
attribute @password(saltLength: Int?, salt: String?) @@@targetField([StringField])
554554

555+
556+
/**
557+
* Indicates that the field is encrypted when storing in the DB and should be decrypted when read
558+
*
559+
* ZenStack uses the Web Crypto API to encrypt and decrypt the field.
560+
*/
561+
attribute @encrypted(secret: String) @@@targetField([StringField])
562+
555563
/**
556564
* Indicates that the field should be omitted when read from the generated services.
557565
*/

0 commit comments

Comments
 (0)