@zenstackhq/runtime 0.5.0 → 0.6.0-pre.2
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.
- package/client/index.d.ts +0 -1
- package/client/index.js +0 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/policy.d.ts +11 -0
- package/lib/policy.js +10 -0
- package/lib/policy.js.map +1 -0
- package/lib/proxy/handler.d.ts +37 -0
- package/lib/proxy/handler.js +333 -0
- package/lib/proxy/handler.js.map +1 -0
- package/lib/proxy/logger.d.ts +18 -0
- package/lib/proxy/logger.js +40 -0
- package/lib/proxy/logger.js.map +1 -0
- package/lib/proxy/nested-write-vistor.d.ts +30 -0
- package/lib/proxy/nested-write-vistor.js +69 -0
- package/lib/proxy/nested-write-vistor.js.map +1 -0
- package/lib/proxy/policy-utils.d.ts +78 -0
- package/lib/proxy/policy-utils.js +508 -0
- package/lib/proxy/policy-utils.js.map +1 -0
- package/lib/types.d.ts +7 -0
- package/lib/types.js +13 -1
- package/lib/types.js.map +1 -1
- package/lib/version.d.ts +1 -0
- package/lib/version.js +9 -0
- package/lib/version.js.map +1 -0
- package/package.json +4 -2
- package/server/index.d.ts +1 -3
- package/server/index.js +1 -2
- package/server/auth.d.ts +0 -1
- package/server/auth.js +0 -3
package/client/index.d.ts
CHANGED
package/client/index.js
CHANGED
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -19,4 +19,5 @@ __exportStar(require("./config"), exports);
|
|
|
19
19
|
__exportStar(require("./service"), exports);
|
|
20
20
|
__exportStar(require("./request-handler"), exports);
|
|
21
21
|
__exportStar(require("./validation"), exports);
|
|
22
|
+
__exportStar(require("./policy"), exports);
|
|
22
23
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,2CAAyB;AACzB,4CAA0B;AAC1B,oDAAkC;AAClC,+CAA6B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,2CAAyB;AACzB,4CAA0B;AAC1B,oDAAkC;AAClC,+CAA6B;AAC7B,2CAAyB"}
|
package/lib/policy.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AuthUser, FieldInfo, PolicyOperationKind, QueryContext } from './types';
|
|
2
|
+
export type WithPolicyContext = {
|
|
3
|
+
user?: AuthUser;
|
|
4
|
+
};
|
|
5
|
+
type PolicyFunc = (context: QueryContext) => any;
|
|
6
|
+
export type PolicyDef = {
|
|
7
|
+
guard: Record<string, Record<PolicyOperationKind, PolicyFunc>>;
|
|
8
|
+
fieldMapping: Record<string, Record<string, FieldInfo>>;
|
|
9
|
+
};
|
|
10
|
+
export declare function withPolicy<DbClient = any>(prisma: DbClient, policy: PolicyDef, context: WithPolicyContext): DbClient;
|
|
11
|
+
export {};
|
package/lib/policy.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.withPolicy = void 0;
|
|
5
|
+
const handler_1 = require("./proxy/handler");
|
|
6
|
+
function withPolicy(prisma, policy, context) {
|
|
7
|
+
return new Proxy(prisma, (0, handler_1.prismaClientProxyHandler)(prisma, policy, context.user));
|
|
8
|
+
}
|
|
9
|
+
exports.withPolicy = withPolicy;
|
|
10
|
+
//# sourceMappingURL=policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy.js","sourceRoot":"","sources":["../../src/policy.ts"],"names":[],"mappings":";AAAA,uDAAuD;;;AAEvD,6CAA2D;AAmB3D,SAAgB,UAAU,CACtB,MAAgB,EAChB,MAAiB,EACjB,OAA0B;IAE1B,OAAO,IAAI,KAAK,CACZ,MAAM,EACN,IAAA,kCAAwB,EAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,CACzD,CAAC;AACN,CAAC;AATD,gCASC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { DbClientContract, DbOperations } from '../types';
|
|
2
|
+
import { AuthUser } from '../types';
|
|
3
|
+
import { PolicyDef } from '../policy';
|
|
4
|
+
type PrismaClientOperations = 'findUnique' | 'findUniqueOrThrow' | 'findFirst' | 'findFirstOrThrow' | 'findMany' | 'create' | 'createMany' | 'delete' | 'update' | 'deleteMany' | 'updateMany' | 'upsert' | 'aggregate' | 'groupBy' | 'count';
|
|
5
|
+
export declare function prismaClientProxyHandler(prisma: any, policy: PolicyDef, user?: AuthUser): {
|
|
6
|
+
get: (target: any, prop: string | symbol, receiver: any) => any;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Request handler for /data endpoint which processes data CRUD requests.
|
|
10
|
+
*/
|
|
11
|
+
export declare class PrismaModelHandler<DbClient extends DbClientContract> implements Record<PrismaClientOperations, (args: any) => Promise<unknown>> {
|
|
12
|
+
private readonly prisma;
|
|
13
|
+
private readonly policy;
|
|
14
|
+
private readonly model;
|
|
15
|
+
private readonly user?;
|
|
16
|
+
private readonly logger;
|
|
17
|
+
constructor(prisma: DbClient, policy: PolicyDef, model: string, user?: AuthUser | undefined);
|
|
18
|
+
findUnique(args: any): Promise<unknown>;
|
|
19
|
+
findUniqueOrThrow(args: any): Promise<{}>;
|
|
20
|
+
findFirst(args: any): Promise<unknown>;
|
|
21
|
+
findFirstOrThrow(args: any): Promise<{}>;
|
|
22
|
+
findMany(args: any): Promise<unknown[]>;
|
|
23
|
+
private checkPolicyForCreatedModels;
|
|
24
|
+
private checkReadback;
|
|
25
|
+
create(args: any): Promise<unknown>;
|
|
26
|
+
createMany(args: any, skipDuplicates?: boolean): Promise<unknown>;
|
|
27
|
+
doUpdate(args: any, updateAction: (db: DbOperations) => Promise<unknown>, operation: string): Promise<unknown>;
|
|
28
|
+
update(args: any): Promise<unknown>;
|
|
29
|
+
updateMany(args: any): Promise<unknown>;
|
|
30
|
+
upsert(args: any): Promise<unknown>;
|
|
31
|
+
delete(args: any): Promise<any>;
|
|
32
|
+
deleteMany(args: any): Promise<unknown>;
|
|
33
|
+
aggregate(args: any): Promise<unknown>;
|
|
34
|
+
groupBy(args: any): Promise<unknown>;
|
|
35
|
+
count(args: any): Promise<number>;
|
|
36
|
+
}
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.PrismaModelHandler = exports.prismaClientProxyHandler = void 0;
|
|
16
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
17
|
+
const cuid_1 = __importDefault(require("cuid"));
|
|
18
|
+
const superjson_1 = __importDefault(require("superjson"));
|
|
19
|
+
const constants_1 = require("../constants");
|
|
20
|
+
const policy_utils_1 = require("./policy-utils");
|
|
21
|
+
const runtime_1 = require("@prisma/client/runtime");
|
|
22
|
+
const logger_1 = require("./logger");
|
|
23
|
+
function prismaClientProxyHandler(prisma, policy, user) {
|
|
24
|
+
return {
|
|
25
|
+
get: (target, prop, receiver) => {
|
|
26
|
+
const propVal = Reflect.get(target, prop, receiver);
|
|
27
|
+
if (!propVal || typeof prop !== 'string' || prop.startsWith('$')) {
|
|
28
|
+
return Reflect.get(target, prop, receiver);
|
|
29
|
+
}
|
|
30
|
+
return new PrismaModelHandler(prisma, policy, prop, user);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
exports.prismaClientProxyHandler = prismaClientProxyHandler;
|
|
35
|
+
/**
|
|
36
|
+
* Request handler for /data endpoint which processes data CRUD requests.
|
|
37
|
+
*/
|
|
38
|
+
class PrismaModelHandler {
|
|
39
|
+
constructor(prisma, policy, model, user) {
|
|
40
|
+
this.prisma = prisma;
|
|
41
|
+
this.policy = policy;
|
|
42
|
+
this.model = model;
|
|
43
|
+
this.user = user;
|
|
44
|
+
this.logger = new logger_1.Logger(prisma);
|
|
45
|
+
}
|
|
46
|
+
findUnique(args) {
|
|
47
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
const entities = yield (0, policy_utils_1.readWithCheck)(this.model, args, this.user, this.prisma, this.policy, this.logger);
|
|
49
|
+
return entities[0];
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
findUniqueOrThrow(args) {
|
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
const entity = yield this.findUnique(args);
|
|
55
|
+
if (!entity) {
|
|
56
|
+
throw new runtime_1.NotFoundError('entity not found');
|
|
57
|
+
}
|
|
58
|
+
return entity;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
findFirst(args) {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
const entities = yield (0, policy_utils_1.readWithCheck)(this.model, args, this.user, this.prisma, this.policy, this.logger);
|
|
64
|
+
return entities[0];
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
findFirstOrThrow(args) {
|
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
const entity = yield this.findFirst(args);
|
|
70
|
+
if (!entity) {
|
|
71
|
+
throw new runtime_1.NotFoundError('entity not found');
|
|
72
|
+
}
|
|
73
|
+
return entity;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
findMany(args) {
|
|
77
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
return (0, policy_utils_1.readWithCheck)(this.model, args, this.user, this.prisma, this.policy, this.logger);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
checkPolicyForCreatedModels(createdModels, transactionId, tx) {
|
|
82
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
83
|
+
if (createdModels.length === 0) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
yield Promise.all(createdModels.map((model) => __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
yield (0, policy_utils_1.checkPolicyForFilter)(model, {
|
|
88
|
+
[constants_1.TRANSACTION_FIELD_NAME]: `${transactionId}:create`,
|
|
89
|
+
}, 'create', this.user, tx, this.policy, this.logger);
|
|
90
|
+
})));
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
checkReadback(readArgs, operation) {
|
|
94
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
+
try {
|
|
96
|
+
const result = yield (0, policy_utils_1.readWithCheck)(this.model, readArgs, this.user, this.prisma, this.policy, this.logger);
|
|
97
|
+
if (result.length === 0) {
|
|
98
|
+
this.logger.warn(`${operation} result cannot be read back`);
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
return result[0];
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
this.logger.warn(`${operation} result cannot be read back: ${err}`);
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
create(args) {
|
|
110
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
111
|
+
if (!args) {
|
|
112
|
+
throw new runtime_1.PrismaClientValidationError('query argument is required');
|
|
113
|
+
}
|
|
114
|
+
if (!args.data) {
|
|
115
|
+
throw new runtime_1.PrismaClientValidationError('data field is required in query argument');
|
|
116
|
+
}
|
|
117
|
+
yield (0, policy_utils_1.validateModelPayload)(this.model, 'create', args.data);
|
|
118
|
+
// preprocess payload to modify fields as required by attribute like @password
|
|
119
|
+
yield (0, policy_utils_1.preprocessWritePayload)(this.policy, this.model, args.data);
|
|
120
|
+
const transactionId = (0, cuid_1.default)();
|
|
121
|
+
const { createdModels } = yield (0, policy_utils_1.injectTransactionId)(this.model, args.data, 'create', transactionId, this.policy);
|
|
122
|
+
// start an interactive transaction
|
|
123
|
+
const createResult = yield this.prisma.$transaction((tx) => __awaiter(this, void 0, void 0, function* () {
|
|
124
|
+
// inject transaction id into update/create payload (direct and nested)
|
|
125
|
+
// conduct the create
|
|
126
|
+
this.logger.info(`Conducting create: ${this.model}:\n${superjson_1.default.stringify(args)}`);
|
|
127
|
+
const createResult = (yield tx[this.model].create(args));
|
|
128
|
+
// verify that nested creates pass policy check
|
|
129
|
+
this.logger.info(`Checking all created models: [${createdModels.join(',')}]`);
|
|
130
|
+
yield this.checkPolicyForCreatedModels(createdModels, transactionId, tx);
|
|
131
|
+
return createResult;
|
|
132
|
+
}));
|
|
133
|
+
// verify that return data requested by query args pass policy check
|
|
134
|
+
const readArgs = Object.assign(Object.assign({}, args), { where: { id: createResult.id } });
|
|
135
|
+
delete readArgs.data;
|
|
136
|
+
return this.checkReadback(readArgs, 'create');
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
createMany(args, skipDuplicates) {
|
|
140
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
141
|
+
if (!args) {
|
|
142
|
+
throw new runtime_1.PrismaClientValidationError('query argument is required');
|
|
143
|
+
}
|
|
144
|
+
if (!args.data) {
|
|
145
|
+
throw new runtime_1.PrismaClientValidationError('data field is required and must be an array');
|
|
146
|
+
}
|
|
147
|
+
const transactionId = (0, cuid_1.default)();
|
|
148
|
+
let createdModels = [];
|
|
149
|
+
for (const data of (0, policy_utils_1.ensureArray)(args.data)) {
|
|
150
|
+
yield (0, policy_utils_1.validateModelPayload)(this.model, 'create', data);
|
|
151
|
+
// preprocess payload to modify fields as required by attribute like @password
|
|
152
|
+
yield (0, policy_utils_1.preprocessWritePayload)(this.policy, this.model, data);
|
|
153
|
+
// inject transaction id into update/create payload (direct and nested)
|
|
154
|
+
const { createdModels: created } = yield (0, policy_utils_1.injectTransactionId)(this.model, data, 'create', transactionId, this.policy);
|
|
155
|
+
createdModels.push(...created);
|
|
156
|
+
}
|
|
157
|
+
createdModels = [...new Set(createdModels)];
|
|
158
|
+
// start an interactive transaction
|
|
159
|
+
const createResult = yield this.prisma.$transaction((tx) => __awaiter(this, void 0, void 0, function* () {
|
|
160
|
+
// conduct the create
|
|
161
|
+
this.logger.info(`Conducting createMany: ${this.model}:\n${superjson_1.default.stringify(args)}`);
|
|
162
|
+
const createResult = yield tx[this.model].createMany(args, skipDuplicates);
|
|
163
|
+
// verify that nested creates pass policy check
|
|
164
|
+
this.logger.info(`Checking all created models: [${createdModels.join(',')}]`);
|
|
165
|
+
yield this.checkPolicyForCreatedModels(createdModels, transactionId, tx);
|
|
166
|
+
return createResult;
|
|
167
|
+
}));
|
|
168
|
+
return createResult;
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
doUpdate(args, updateAction, operation) {
|
|
172
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
173
|
+
yield (0, policy_utils_1.validateModelPayload)(this.model, 'update', args.data);
|
|
174
|
+
// preprocess payload to modify fields as required by attribute like @password
|
|
175
|
+
yield (0, policy_utils_1.preprocessWritePayload)(this.policy, this.model, args.data);
|
|
176
|
+
const transactionId = (0, cuid_1.default)();
|
|
177
|
+
yield this.prisma.$transaction((tx) => __awaiter(this, void 0, void 0, function* () {
|
|
178
|
+
// make sure the entity (including ones involved in nested write) pass policy check
|
|
179
|
+
yield (0, policy_utils_1.preUpdateCheck)(this.model, args, this.user, tx, this.policy, this.logger);
|
|
180
|
+
// inject transaction id into update/create payload (direct and nested)
|
|
181
|
+
const { createdModels } = yield (0, policy_utils_1.injectTransactionId)(this.model, args.data, 'update', transactionId, this.policy);
|
|
182
|
+
// conduct the update
|
|
183
|
+
this.logger.info(`Conducting update: ${this.model}:\n${superjson_1.default.stringify(args)}`);
|
|
184
|
+
yield updateAction(tx[this.model]);
|
|
185
|
+
// verify that nested creates pass policy check
|
|
186
|
+
if (createdModels.length > 0) {
|
|
187
|
+
this.logger.info(`Checking all created models: [${createdModels.join(',')}]`);
|
|
188
|
+
yield this.checkPolicyForCreatedModels(createdModels, transactionId, tx);
|
|
189
|
+
}
|
|
190
|
+
}));
|
|
191
|
+
// verify that return data requested by query args pass policy check
|
|
192
|
+
const readArgs = Object.assign({}, args);
|
|
193
|
+
delete readArgs.data;
|
|
194
|
+
return this.checkReadback(readArgs, operation);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
update(args) {
|
|
198
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
+
if (!args) {
|
|
200
|
+
throw new runtime_1.PrismaClientValidationError('query argument is required');
|
|
201
|
+
}
|
|
202
|
+
if (!args.where) {
|
|
203
|
+
throw new runtime_1.PrismaClientValidationError('where field is required in query argument');
|
|
204
|
+
}
|
|
205
|
+
if (!args.data) {
|
|
206
|
+
throw new runtime_1.PrismaClientValidationError('data field is required in query argument');
|
|
207
|
+
}
|
|
208
|
+
return this.doUpdate(args, (db) => db.update(args), 'update');
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
updateMany(args) {
|
|
212
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
213
|
+
if (!args) {
|
|
214
|
+
throw new runtime_1.PrismaClientValidationError('query argument is required');
|
|
215
|
+
}
|
|
216
|
+
if (!args.data) {
|
|
217
|
+
throw new runtime_1.PrismaClientValidationError('data field is required in query argument');
|
|
218
|
+
}
|
|
219
|
+
return this.doUpdate(args, (db) => db.updateMany(args), 'updateMany');
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
upsert(args) {
|
|
223
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
224
|
+
if (!args) {
|
|
225
|
+
throw new runtime_1.PrismaClientValidationError('query argument is required');
|
|
226
|
+
}
|
|
227
|
+
if (!args.where) {
|
|
228
|
+
throw new runtime_1.PrismaClientValidationError('where field is required in query argument');
|
|
229
|
+
}
|
|
230
|
+
if (!args.create) {
|
|
231
|
+
throw new runtime_1.PrismaClientValidationError('create field is required in query argument');
|
|
232
|
+
}
|
|
233
|
+
if (!args.update) {
|
|
234
|
+
throw new runtime_1.PrismaClientValidationError('update field is required in query argument');
|
|
235
|
+
}
|
|
236
|
+
yield (0, policy_utils_1.validateModelPayload)(this.model, 'create', args.create);
|
|
237
|
+
yield (0, policy_utils_1.validateModelPayload)(this.model, 'update', args.update);
|
|
238
|
+
// preprocess payload to modify fields as required by attribute like @password
|
|
239
|
+
yield (0, policy_utils_1.preprocessWritePayload)(this.policy, this.model, args.create);
|
|
240
|
+
yield (0, policy_utils_1.preprocessWritePayload)(this.policy, this.model, args.update);
|
|
241
|
+
const transactionId = (0, cuid_1.default)();
|
|
242
|
+
yield this.prisma.$transaction((tx) => __awaiter(this, void 0, void 0, function* () {
|
|
243
|
+
// make sure the entity (including ones involved in nested write) pass policy check
|
|
244
|
+
yield (0, policy_utils_1.preUpdateCheck)(this.model, args, this.user, tx, this.policy, this.logger);
|
|
245
|
+
// inject transaction id into update/create payload (direct and nested)
|
|
246
|
+
const { createdModels: createdFromCreate } = yield (0, policy_utils_1.injectTransactionId)(this.model, args.create, 'create', transactionId, this.policy);
|
|
247
|
+
const { createdModels: createdFromUpdate } = yield (0, policy_utils_1.injectTransactionId)(this.model, args.update, 'update', transactionId, this.policy);
|
|
248
|
+
const createdModels = [
|
|
249
|
+
...new Set([...createdFromCreate, ...createdFromUpdate]),
|
|
250
|
+
];
|
|
251
|
+
// conduct the update
|
|
252
|
+
this.logger.info(`Conducting update: ${this.model}:\n${superjson_1.default.stringify(args)}`);
|
|
253
|
+
yield tx[this.model].upsert(args);
|
|
254
|
+
// verify that nested creates pass policy check
|
|
255
|
+
yield this.checkPolicyForCreatedModels(createdModels, transactionId, tx);
|
|
256
|
+
}));
|
|
257
|
+
// verify that return data requested by query args pass policy check
|
|
258
|
+
const readArgs = Object.assign({}, args);
|
|
259
|
+
delete readArgs.create;
|
|
260
|
+
delete readArgs.update;
|
|
261
|
+
return this.checkReadback(readArgs, 'upsert');
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
delete(args) {
|
|
265
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
266
|
+
if (!args) {
|
|
267
|
+
throw new runtime_1.PrismaClientValidationError('query argument is required');
|
|
268
|
+
}
|
|
269
|
+
if (!args.where) {
|
|
270
|
+
throw new runtime_1.PrismaClientValidationError('where field is required in query argument');
|
|
271
|
+
}
|
|
272
|
+
// ensures the item under deletion passes policy check
|
|
273
|
+
yield (0, policy_utils_1.checkPolicyForFilter)(this.model, args.where, 'delete', this.user, this.prisma, this.policy, this.logger);
|
|
274
|
+
let readResult;
|
|
275
|
+
try {
|
|
276
|
+
const items = yield (0, policy_utils_1.readWithCheck)(this.model, args, this.user, this.prisma, this.policy, this.logger);
|
|
277
|
+
readResult = items[0];
|
|
278
|
+
}
|
|
279
|
+
catch (err) {
|
|
280
|
+
// not readable
|
|
281
|
+
readResult = undefined;
|
|
282
|
+
}
|
|
283
|
+
// conduct the deletion
|
|
284
|
+
this.logger.info(`Conducting delete ${this.model}:\n${superjson_1.default.stringify(args)}`);
|
|
285
|
+
yield this.prisma[this.model].delete(args);
|
|
286
|
+
return readResult;
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
deleteMany(args) {
|
|
290
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
291
|
+
if (!args) {
|
|
292
|
+
throw new runtime_1.PrismaClientValidationError('query argument is required');
|
|
293
|
+
}
|
|
294
|
+
if (!args.where) {
|
|
295
|
+
throw new runtime_1.PrismaClientValidationError('where field is required in query argument');
|
|
296
|
+
}
|
|
297
|
+
// ensures the item under deletion passes policy check
|
|
298
|
+
yield (0, policy_utils_1.checkPolicyForFilter)(this.model, args.where, 'delete', this.user, this.prisma, this.policy, this.logger);
|
|
299
|
+
// conduct the deletion
|
|
300
|
+
this.logger.info(`Conducting delete ${this.model}:\n${superjson_1.default.stringify(args)}`);
|
|
301
|
+
return this.prisma[this.model].deleteMany(args);
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
aggregate(args) {
|
|
305
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
306
|
+
if (!args) {
|
|
307
|
+
throw new runtime_1.PrismaClientValidationError('query argument is required');
|
|
308
|
+
}
|
|
309
|
+
const aggArgs = yield (0, policy_utils_1.ensureAuthGuard)(args, this.model, 'read', this.user, this.policy);
|
|
310
|
+
return this.prisma[this.model].aggregate(aggArgs);
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
groupBy(args) {
|
|
314
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
315
|
+
if (!args) {
|
|
316
|
+
throw new runtime_1.PrismaClientValidationError('query argument is required');
|
|
317
|
+
}
|
|
318
|
+
const aggArgs = yield (0, policy_utils_1.ensureAuthGuard)(args, this.model, 'read', this.user, this.policy);
|
|
319
|
+
return this.prisma[this.model].groupBy(aggArgs);
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
count(args) {
|
|
323
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
324
|
+
if (!args) {
|
|
325
|
+
throw new runtime_1.PrismaClientValidationError('query argument is required');
|
|
326
|
+
}
|
|
327
|
+
const aggArgs = yield (0, policy_utils_1.ensureAuthGuard)(args, this.model, 'read', this.user, this.policy);
|
|
328
|
+
return this.prisma[this.model].count(aggArgs);
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
exports.PrismaModelHandler = PrismaModelHandler;
|
|
333
|
+
//# sourceMappingURL=handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handler.js","sourceRoot":"","sources":["../../../src/proxy/handler.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uDAAuD;AACvD,gDAAwB;AACxB,0DAAkC;AAClC,4CAAsD;AAGtD,iDASwB;AACxB,oDAGgC;AAChC,qCAAkC;AAoBlC,SAAgB,wBAAwB,CACpC,MAAW,EACX,MAAiB,EACjB,IAAe;IAEf,OAAO;QACH,GAAG,EAAE,CAAC,MAAW,EAAE,IAAqB,EAAE,QAAa,EAAE,EAAE;YACvD,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;YACpD,IAAI,CAAC,OAAO,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;gBAC9D,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;aAC9C;YACD,OAAO,IAAI,kBAAkB,CACzB,MAA0B,EAC1B,MAAM,EACN,IAAI,EACJ,IAAI,CACP,CAAC;QACN,CAAC;KACJ,CAAC;AACN,CAAC;AAnBD,4DAmBC;AAED;;GAEG;AACH,MAAa,kBAAkB;IAI3B,YACqB,MAAgB,EAChB,MAAiB,EACjB,KAAa,EACb,IAAe;QAHf,WAAM,GAAN,MAAM,CAAU;QAChB,WAAM,GAAN,MAAM,CAAW;QACjB,UAAK,GAAL,KAAK,CAAQ;QACb,SAAI,GAAJ,IAAI,CAAW;QAEhC,IAAI,CAAC,MAAM,GAAG,IAAI,eAAM,CAAC,MAAM,CAAC,CAAC;IACrC,CAAC;IAEK,UAA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D,CAAC;KAAA;IAEK,KAAK,CAAC,IAAS;;YACjB,IAAI,CAAC,IAAI,EAAE;gBACP,MAAM,IAAI,qCAA2B,CAAC,4BAA4B,CAAC,CAAC;aACvE;YAED,MAAM,OAAO,GAAG,MAAM,IAAA,8BAAe,EACjC,IAAI,EACJ,IAAI,CAAC,KAAK,EACV,MAAM,EACN,IAAI,CAAC,IAAI,EACT,IAAI,CAAC,MAAM,CACd,CAAC;YAEF,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAClD,CAAC;KAAA;CACJ;AAhiBD,gDAgiBC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare class Logger {
|
|
2
|
+
private readonly prisma;
|
|
3
|
+
constructor(prisma: any);
|
|
4
|
+
private get emitter();
|
|
5
|
+
log(level: 'info' | 'warn' | 'error', message: string): void;
|
|
6
|
+
/**
|
|
7
|
+
* Generates a log message with info level.
|
|
8
|
+
*/
|
|
9
|
+
info(message: string): void;
|
|
10
|
+
/**
|
|
11
|
+
* Generates a log message with warn level.
|
|
12
|
+
*/
|
|
13
|
+
warn(message: string): void;
|
|
14
|
+
/**
|
|
15
|
+
* Generates a log message with error level.
|
|
16
|
+
*/
|
|
17
|
+
error(message: string): void;
|
|
18
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Logger = void 0;
|
|
4
|
+
class Logger {
|
|
5
|
+
constructor(prisma) {
|
|
6
|
+
this.prisma = prisma;
|
|
7
|
+
}
|
|
8
|
+
get emitter() {
|
|
9
|
+
const engine = this.prisma.getEngine();
|
|
10
|
+
return engine ? engine.logEmitter : undefined;
|
|
11
|
+
}
|
|
12
|
+
log(level, message) {
|
|
13
|
+
var _a;
|
|
14
|
+
(_a = this.emitter) === null || _a === void 0 ? void 0 : _a.emit(level, {
|
|
15
|
+
timestamp: new Date(),
|
|
16
|
+
message,
|
|
17
|
+
target: 'zenstack',
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Generates a log message with info level.
|
|
22
|
+
*/
|
|
23
|
+
info(message) {
|
|
24
|
+
this.log('info', message);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Generates a log message with warn level.
|
|
28
|
+
*/
|
|
29
|
+
warn(message) {
|
|
30
|
+
this.log('warn', message);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Generates a log message with error level.
|
|
34
|
+
*/
|
|
35
|
+
error(message) {
|
|
36
|
+
this.log('error', message);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.Logger = Logger;
|
|
40
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../../src/proxy/logger.ts"],"names":[],"mappings":";;;AAGA,MAAa,MAAM;IACf,YAA6B,MAAW;QAAX,WAAM,GAAN,MAAM,CAAK;IAAG,CAAC;IAE5C,IAAY,OAAO;QACf,MAAM,MAAM,GAAI,IAAI,CAAC,MAAc,CAAC,SAAS,EAAE,CAAC;QAChD,OAAO,MAAM,CAAC,CAAC,CAAE,MAAM,CAAC,UAA2B,CAAC,CAAC,CAAC,SAAS,CAAC;IACpE,CAAC;IAEM,GAAG,CAAC,KAAgC,EAAE,OAAe;;QACxD,MAAA,IAAI,CAAC,OAAO,0CAAE,IAAI,CAAC,KAAK,EAAE;YACtB,SAAS,EAAE,IAAI,IAAI,EAAE;YACrB,OAAO;YACP,MAAM,EAAE,UAAU;SACrB,CAAC,CAAC;IACP,CAAC;IAED;;OAEG;IACI,IAAI,CAAC,OAAe;QACvB,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9B,CAAC;IAED;;OAEG;IACI,IAAI,CAAC,OAAe;QACvB,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9B,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,OAAe;QACxB,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC/B,CAAC;CACJ;AApCD,wBAoCC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { PrismaWriteActionType, FieldInfo } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Visitor callback function type
|
|
4
|
+
*
|
|
5
|
+
* @fieldInfo current visiting field
|
|
6
|
+
* @action prisma action for this field, e.g., update, create, etc.
|
|
7
|
+
* @fieldData data attached to the field, a scalar type for simple field
|
|
8
|
+
* and nested structure for model field
|
|
9
|
+
* @parentData parent data of @see fieldData, can be used to replace current field data
|
|
10
|
+
* @state a custom state
|
|
11
|
+
*
|
|
12
|
+
* @return if a truethy value is returned, recursive visiting will continue and the return
|
|
13
|
+
* value will be used as the new state passed to visiting of the direct child level; otherwise
|
|
14
|
+
* visiting is stopped at this level
|
|
15
|
+
*/
|
|
16
|
+
export type NestedWriterVisitorCallback<State = unknown> = (fieldInfo: FieldInfo, action: PrismaWriteActionType, fieldData: any, parentData: any, state: State) => Promise<State | undefined>;
|
|
17
|
+
/**
|
|
18
|
+
* Recursive visitor for nested write (create/update) payload
|
|
19
|
+
*/
|
|
20
|
+
export declare class NestedWriteVisitor<State> {
|
|
21
|
+
private readonly resolveField;
|
|
22
|
+
constructor(resolveField: (model: string, field: string) => Promise<FieldInfo | undefined>);
|
|
23
|
+
private isPrismaWriteAction;
|
|
24
|
+
/**
|
|
25
|
+
* Start visiting
|
|
26
|
+
*
|
|
27
|
+
* @see NestedWriterVisitorCallback
|
|
28
|
+
*/
|
|
29
|
+
visit(model: string, fieldData: any, parentData: any, state: State, callback: NestedWriterVisitorCallback<State>): Promise<void>;
|
|
30
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
4
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
5
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
6
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
7
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
8
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
9
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
10
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.NestedWriteVisitor = void 0;
|
|
15
|
+
const types_1 = require("../types");
|
|
16
|
+
/**
|
|
17
|
+
* Recursive visitor for nested write (create/update) payload
|
|
18
|
+
*/
|
|
19
|
+
class NestedWriteVisitor {
|
|
20
|
+
constructor(resolveField) {
|
|
21
|
+
this.resolveField = resolveField;
|
|
22
|
+
}
|
|
23
|
+
isPrismaWriteAction(value) {
|
|
24
|
+
return types_1.PrismaWriteActions.includes(value);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Start visiting
|
|
28
|
+
*
|
|
29
|
+
* @see NestedWriterVisitorCallback
|
|
30
|
+
*/
|
|
31
|
+
visit(model, fieldData, parentData, state, callback) {
|
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
if (!fieldData) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
for (const [field, payload] of Object.entries(fieldData)) {
|
|
37
|
+
if (!payload) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const fieldInfo = yield this.resolveField(model, field);
|
|
41
|
+
if (!fieldInfo) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (!fieldInfo.isDataModel) {
|
|
45
|
+
// simple field, just call action
|
|
46
|
+
yield callback(fieldInfo, 'none', payload, fieldData, state);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
// deal with nested write of other model, here payload is a
|
|
50
|
+
// potentially nested structure like:
|
|
51
|
+
//
|
|
52
|
+
// { update: { field: {...} } }
|
|
53
|
+
//
|
|
54
|
+
for (const [subKey, subPayload] of Object.entries(payload)) {
|
|
55
|
+
if (this.isPrismaWriteAction(subKey) && subPayload) {
|
|
56
|
+
const newState = yield callback(fieldInfo, subKey, subPayload, payload, state);
|
|
57
|
+
if (newState) {
|
|
58
|
+
// recurse into content
|
|
59
|
+
yield this.visit(fieldInfo.type, subPayload, payload, newState, callback);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.NestedWriteVisitor = NestedWriteVisitor;
|
|
69
|
+
//# sourceMappingURL=nested-write-vistor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nested-write-vistor.js","sourceRoot":"","sources":["../../../src/proxy/nested-write-vistor.ts"],"names":[],"mappings":";AAAA,sEAAsE;AACtE,uDAAuD;;;;;;;;;;;;AAEvD,oCAAgF;AAwBhF;;GAEG;AACH,MAAa,kBAAkB;IAC3B,YACqB,YAGkB;QAHlB,iBAAY,GAAZ,YAAY,CAGM;IACpC,CAAC;IAEI,mBAAmB,CAAC,KAAa;QACrC,OAAO,0BAAkB,CAAC,QAAQ,CAAC,KAA8B,CAAC,CAAC;IACvE,CAAC;IAED;;;;OAIG;IACG,KAAK,CACP,KAAa,EACb,SAAc,EACd,UAAe,EACf,KAAY,EACZ,QAA4C;;YAE5C,IAAI,CAAC,SAAS,EAAE;gBACZ,OAAO;aACV;YAED,KAAK,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC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