Skip to content

Commit 37f9bb7

Browse files
authored
Merge pull request #29 from zenstackhq/dev
feat: use enhanced prisma to support Polymorphism (#28)
2 parents f435c31 + de8e516 commit 37f9bb7

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
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": "@zenstackhq/proxy",
3-
"version": "0.2.7",
3+
"version": "0.3.0",
44
"description": "A CLI tool to run an Express server that proxies CRUD requests to a ZenStack backend",
55
"main": "index.js",
66
"publishConfig": {

src/server.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export interface ServerOptions {
1616
logLevel?: string[]
1717
}
1818

19+
type EnhancementKind = 'password' | 'omit' | 'policy' | 'validation' | 'delegate' | 'encryption'
20+
// enable all enhancements except policy
21+
const Enhancements: EnhancementKind[] = ['password', 'omit', 'validation', 'delegate', 'encryption']
22+
1923
/**
2024
* Resolve the absolute path to the Prisma schema directory
2125
*/
@@ -136,6 +140,7 @@ async function loadZenStackModules(
136140
let enums: any
137141
// Load Prisma Client - either from custom output or default @prisma/client
138142
let PrismaClient: any
143+
let enhanceFunc: any
139144

140145
const generator = zmodelConfig.generator
141146
if (generator.output) {
@@ -174,17 +179,17 @@ async function loadZenStackModules(
174179
: path.join(process.cwd(), zenstackPath)
175180
: undefined
176181

177-
let modelMetaPath: string | undefined
178182
try {
179183
if (zenstackAbsPath) {
180-
modelMetaPath = path.join(zenstackAbsPath, 'model-meta')
184+
modelMeta = require(path.join(zenstackAbsPath, 'model-meta')).default
185+
enhanceFunc = require(path.join(zenstackAbsPath, 'enhance')).enhance
181186
} else {
182-
modelMetaPath = '@zenstackhq/runtime/model-meta'
187+
modelMeta = require('@zenstackhq/runtime/model-meta').default
188+
enhanceFunc = require('@zenstackhq/runtime').enhance
183189
}
184-
modelMeta = require(modelMetaPath).default
185190
} catch {
186191
throw new CliError(
187-
`Failed to load ZenStack generated model meta from: ${modelMetaPath}\n` +
192+
`Failed to load ZenStack generated model meta from: ${zenstackAbsPath || '@zenstackhq/runtime'}\n` +
188193
`Please run \`zenstack generate\` first or specify the correct output directory of ZenStack generated modules using the \`-z\` option.`
189194
)
190195
}
@@ -195,7 +200,7 @@ async function loadZenStackModules(
195200

196201
const zenstackVersion = getZenStackVersion()
197202

198-
return { PrismaClient, modelMeta, enums, zenstackVersion }
203+
return { PrismaClient, modelMeta, enums, zenstackVersion, enhanceFunc }
199204
}
200205

201206
/**
@@ -204,11 +209,8 @@ async function loadZenStackModules(
204209
export async function startServer(options: ServerOptions) {
205210
const { zenstackPath, port, zmodelConfig, zmodelSchemaDir } = options
206211

207-
const { PrismaClient, modelMeta, enums, zenstackVersion } = await loadZenStackModules(
208-
zmodelConfig,
209-
zmodelSchemaDir,
210-
zenstackPath
211-
)
212+
const { PrismaClient, modelMeta, enums, zenstackVersion, enhanceFunc } =
213+
await loadZenStackModules(zmodelConfig, zmodelSchemaDir, zenstackPath)
212214

213215
const prismaVersion = getPrismaVersion()
214216

@@ -238,7 +240,15 @@ export async function startServer(options: ServerOptions) {
238240
app.use(
239241
'/api/model',
240242
ZenStackMiddleware({
241-
getPrisma: () => prisma,
243+
getPrisma: () => {
244+
return enhanceFunc(
245+
prisma,
246+
{},
247+
{
248+
kinds: Enhancements,
249+
}
250+
)
251+
},
242252
})
243253
)
244254

0 commit comments

Comments
 (0)