Skip to content

Commit 8934679

Browse files
authored
merge dev to main (v2.12.1) (#2026)
2 parents 0107e1c + 41157ff commit 8934679

File tree

20 files changed

+235
-39
lines changed

20 files changed

+235
-39
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "zenstack-monorepo",
3-
"version": "2.12.0",
3+
"version": "2.12.1",
44
"description": "",
55
"scripts": {
66
"build": "pnpm -r --filter=\"!./packages/ide/*\" build",

packages/ide/jetbrains/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ plugins {
99
}
1010

1111
group = "dev.zenstack"
12-
version = "2.12.0"
12+
version = "2.12.1"
1313

1414
repositories {
1515
mavenCentral()

packages/ide/jetbrains/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jetbrains",
3-
"version": "2.12.0",
3+
"version": "2.12.1",
44
"displayName": "ZenStack JetBrains IDE Plugin",
55
"description": "ZenStack JetBrains IDE plugin",
66
"homepage": "https://zenstack.dev",

packages/language/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/language",
3-
"version": "2.12.0",
3+
"version": "2.12.1",
44
"displayName": "ZenStack modeling language compiler",
55
"description": "ZenStack modeling language compiler",
66
"homepage": "https://zenstack.dev",

packages/misc/redwood/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/redwood",
33
"displayName": "ZenStack RedwoodJS Integration",
4-
"version": "2.12.0",
4+
"version": "2.12.1",
55
"description": "CLI and runtime for integrating ZenStack with RedwoodJS projects.",
66
"repository": {
77
"type": "git",

packages/plugins/openapi/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/openapi",
33
"displayName": "ZenStack Plugin and Runtime for OpenAPI",
4-
"version": "2.12.0",
4+
"version": "2.12.1",
55
"description": "ZenStack plugin and runtime supporting OpenAPI",
66
"main": "index.js",
77
"repository": {

packages/plugins/swr/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/swr",
33
"displayName": "ZenStack plugin for generating SWR hooks",
4-
"version": "2.12.0",
4+
"version": "2.12.1",
55
"description": "ZenStack plugin for generating SWR hooks",
66
"main": "index.js",
77
"repository": {

packages/plugins/tanstack-query/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/tanstack-query",
33
"displayName": "ZenStack plugin for generating tanstack-query hooks",
4-
"version": "2.12.0",
4+
"version": "2.12.1",
55
"description": "ZenStack plugin for generating tanstack-query hooks",
66
"main": "index.js",
77
"exports": {

packages/plugins/trpc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/trpc",
33
"displayName": "ZenStack plugin for tRPC",
4-
"version": "2.12.0",
4+
"version": "2.12.1",
55
"description": "ZenStack plugin for tRPC",
66
"main": "index.js",
77
"repository": {

packages/runtime/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/runtime",
33
"displayName": "ZenStack Runtime Library",
4-
"version": "2.12.0",
4+
"version": "2.12.1",
55
"description": "Runtime of ZenStack for both client-side and server-side environments.",
66
"repository": {
77
"type": "git",

packages/runtime/src/cross/model-meta.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ export type FieldInfo = {
8686
relationField?: string;
8787

8888
/**
89-
* Mapping from foreign key field names to relation field names.
90-
* Only available on relation fields.
89+
* Mapping from relation's pk to fk. Only available on relation fields.
9190
*/
9291
foreignKeyMapping?: Record<string, string>;
9392

packages/runtime/src/enhancements/node/default-auth.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,31 +126,24 @@ class DefaultAuthHandler extends DefaultPrismaProxyHandler {
126126
return;
127127
}
128128

129-
if (context.field?.backLink && context.nestingPath.length > 1) {
129+
if (context.field?.backLink) {
130130
// if the fk field is in a creation context where its implied by the parent,
131131
// we should not set the default value, e.g.:
132132
//
133133
// ```
134134
// parent.create({ data: { child: { create: {} } } })
135135
// ```
136136
//
137-
// event if child's fk to parent has a default value, we should not set default
137+
// even if child's fk to parent has a default value, we should not set default
138138
// value here
139139

140-
// fetch parent model from the parent context
141-
const parentModel = getModelInfo(
142-
this.options.modelMeta,
143-
context.nestingPath[context.nestingPath.length - 2].model
144-
);
145-
146-
if (parentModel) {
147-
// get the opposite side of the relation for the current create context
148-
const oppositeRelationField = requireField(this.options.modelMeta, model, context.field.backLink);
149-
if (parentModel.name === oppositeRelationField.type) {
150-
// if the opposite side matches the parent model, it means we currently in a creation context
151-
// that implicitly sets this fk field
152-
return;
153-
}
140+
// get the opposite side of the relation for the current create context
141+
const oppositeRelationField = requireField(this.options.modelMeta, model, context.field.backLink);
142+
if (
143+
oppositeRelationField.foreignKeyMapping &&
144+
Object.values(oppositeRelationField.foreignKeyMapping).includes(fieldInfo.name)
145+
) {
146+
return;
154147
}
155148
}
156149

packages/runtime/src/enhancements/node/proxy.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,7 @@ export function makeProxy<T extends PrismaProxyHandler>(
267267
if ($extends && typeof $extends === 'function') {
268268
return (...args: any[]) => {
269269
const result = $extends.bind(target)(...args);
270-
if (!result[PRISMA_PROXY_ENHANCER]) {
271-
return makeProxy(result, modelMeta, makeHandler, name + '$ext', errorTransformer);
272-
} else {
273-
// avoid double wrapping
274-
return result;
275-
}
270+
return makeProxy(result, modelMeta, makeHandler, name + '$ext', errorTransformer);
276271
};
277272
} else {
278273
return $extends;
@@ -289,7 +284,7 @@ export function makeProxy<T extends PrismaProxyHandler>(
289284
return propVal;
290285
}
291286

292-
return createHandlerProxy(makeHandler(target, prop), propVal, prop, errorTransformer);
287+
return createHandlerProxy(makeHandler(target, prop), propVal, prop, proxy, errorTransformer);
293288
},
294289
});
295290

@@ -303,10 +298,15 @@ function createHandlerProxy<T extends PrismaProxyHandler>(
303298
handler: T,
304299
origTarget: any,
305300
model: string,
301+
dbOrTx: any,
306302
errorTransformer?: ErrorTransformer
307303
): T {
308304
return new Proxy(handler, {
309305
get(target, propKey) {
306+
if (propKey === '$parent') {
307+
return dbOrTx;
308+
}
309+
310310
const prop = target[propKey as keyof T];
311311
if (typeof prop !== 'function') {
312312
// the proxy handler doesn't have this method, fall back to the original target

packages/schema/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"publisher": "zenstack",
44
"displayName": "ZenStack Language Tools",
55
"description": "FullStack enhancement for Prisma ORM: seamless integration from database to UI",
6-
"version": "2.12.0",
6+
"version": "2.12.1",
77
"author": {
88
"name": "ZenStack Team"
99
},

packages/sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/sdk",
3-
"version": "2.12.0",
3+
"version": "2.12.1",
44
"description": "ZenStack plugin development SDK",
55
"main": "index.js",
66
"scripts": {

packages/server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/server",
3-
"version": "2.12.0",
3+
"version": "2.12.1",
44
"displayName": "ZenStack Server-side Adapters",
55
"description": "ZenStack server-side adapters",
66
"homepage": "https://zenstack.dev",

packages/testtools/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/testtools",
3-
"version": "2.12.0",
3+
"version": "2.12.1",
44
"description": "ZenStack Test Tools",
55
"main": "index.js",
66
"private": true,
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
3+
describe('Proxy Extension Context', () => {
4+
it('works', async () => {
5+
const { enhance } = await loadSchema(
6+
`
7+
model Counter {
8+
model String @unique
9+
value Int
10+
11+
@@allow('all', true)
12+
}
13+
14+
model Address {
15+
id String @id @default(cuid())
16+
city String
17+
18+
@@allow('all', true)
19+
}
20+
`
21+
);
22+
23+
const db = enhance();
24+
const dbExtended = db.$extends({
25+
client: {
26+
$one() {
27+
return 1;
28+
}
29+
},
30+
model: {
31+
$allModels: {
32+
async createWithCounter(this: any, args: any) {
33+
const modelName = this.$name;
34+
const dbOrTx = this.$parent;
35+
36+
// prisma exposes some internal properties, makes sure these are still preserved
37+
expect(dbOrTx._engine).toBeDefined();
38+
39+
const fn = async (tx: any) => {
40+
const counter = await tx.counter.findUnique({
41+
where: { model: modelName },
42+
});
43+
44+
await tx.counter.upsert({
45+
where: { model: modelName },
46+
update: { value: (counter?.value ?? 0) + tx.$one() },
47+
create: { model: modelName, value: tx.$one() },
48+
});
49+
50+
return tx[modelName].create(args);
51+
};
52+
53+
if (dbOrTx['$transaction']) {
54+
// not running in a transaction, so we need to create a new transaction
55+
return dbOrTx.$transaction(fn);
56+
}
57+
58+
return fn(dbOrTx);
59+
},
60+
},
61+
},
62+
});
63+
64+
const cities = ['Vienna', 'New York', 'Delhi'];
65+
66+
await Promise.all([
67+
...cities.map((city) => dbExtended.address.createWithCounter({ data: { city } })),
68+
...cities.map((city) =>
69+
dbExtended.$transaction((tx: any) => tx.address.createWithCounter({ data: { city: `${city}$tx` } }))
70+
),
71+
]);
72+
73+
await expect(dbExtended.counter.findUniqueOrThrow({ where: { model: 'Address' } })).resolves.toMatchObject({
74+
model: 'Address',
75+
value: cities.length * 2,
76+
});
77+
});
78+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
3+
describe('issue 2014', () => {
4+
it('regression', async () => {
5+
const { prisma, enhance } = await loadSchema(
6+
`
7+
model Tenant {
8+
id Int @id @default(autoincrement())
9+
10+
users User[]
11+
}
12+
13+
model User {
14+
id Int @id @default(autoincrement())
15+
tenantId Int @default(auth().tenantId)
16+
tenant Tenant @relation(fields: [tenantId], references: [id])
17+
18+
@@allow('all', true)
19+
}
20+
`,
21+
{ logPrismaQuery: true }
22+
);
23+
24+
const tenant = await prisma.tenant.create({ data: {} });
25+
const user = await prisma.user.create({ data: { tenantId: tenant.id } });
26+
27+
const db = enhance(user);
28+
const extendedDb = db.$extends({});
29+
30+
await expect(
31+
extendedDb.user.create({
32+
data: {},
33+
})
34+
).resolves.toEqual({
35+
id: 2,
36+
tenantId: tenant.id,
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)