Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/proxy",
"version": "0.2.7",
"version": "0.3.0",
"description": "A CLI tool to run an Express server that proxies CRUD requests to a ZenStack backend",
"main": "index.js",
"publishConfig": {
Expand Down
40 changes: 28 additions & 12 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface ServerOptions {
logLevel?: string[]
}

type EnhancementKind = 'password' | 'omit' | 'policy' | 'validation' | 'delegate' | 'encryption'

/**
* Resolve the absolute path to the Prisma schema directory
*/
Expand Down Expand Up @@ -136,6 +138,7 @@ async function loadZenStackModules(
let enums: any
// Load Prisma Client - either from custom output or default @prisma/client
let PrismaClient: any
let enhanceFunc: any

const generator = zmodelConfig.generator
if (generator.output) {
Expand Down Expand Up @@ -174,17 +177,17 @@ async function loadZenStackModules(
: path.join(process.cwd(), zenstackPath)
: undefined

let modelMetaPath: string | undefined
try {
if (zenstackAbsPath) {
modelMetaPath = path.join(zenstackAbsPath, 'model-meta')
modelMeta = require(path.join(zenstackAbsPath, 'model-meta')).default
enhanceFunc = require(path.join(zenstackAbsPath, 'enhance')).enhance
} else {
modelMetaPath = '@zenstackhq/runtime/model-meta'
modelMeta = require('@zenstackhq/runtime/model-meta').default
enhanceFunc = require('@zenstackhq/runtime').enhance
}
modelMeta = require(modelMetaPath).default
} catch {
throw new CliError(
`Failed to load ZenStack generated model meta from: ${modelMetaPath}\n` +
`Failed to load ZenStack generated model meta from: ${zenstackAbsPath || '@zenstackhq/runtime'}\n` +
Comment thread
jiashengguo marked this conversation as resolved.
`Please run \`zenstack generate\` first or specify the correct output directory of ZenStack generated modules using the \`-z\` option.`
)
}
Comment thread
jiashengguo marked this conversation as resolved.
Expand All @@ -195,7 +198,7 @@ async function loadZenStackModules(

Comment thread
jiashengguo marked this conversation as resolved.
const zenstackVersion = getZenStackVersion()

return { PrismaClient, modelMeta, enums, zenstackVersion }
return { PrismaClient, modelMeta, enums, zenstackVersion, enhanceFunc }
}

/**
Expand All @@ -204,11 +207,8 @@ async function loadZenStackModules(
export async function startServer(options: ServerOptions) {
const { zenstackPath, port, zmodelConfig, zmodelSchemaDir } = options

const { PrismaClient, modelMeta, enums, zenstackVersion } = await loadZenStackModules(
zmodelConfig,
zmodelSchemaDir,
zenstackPath
)
const { PrismaClient, modelMeta, enums, zenstackVersion, enhanceFunc } =
await loadZenStackModules(zmodelConfig, zmodelSchemaDir, zenstackPath)

const prismaVersion = getPrismaVersion()

Expand Down Expand Up @@ -238,7 +238,23 @@ export async function startServer(options: ServerOptions) {
app.use(
'/api/model',
ZenStackMiddleware({
getPrisma: () => prisma,
getPrisma: () => {
// enable all enhancements except policy
const Enhancements: EnhancementKind[] = [
Comment thread
jiashengguo marked this conversation as resolved.
Outdated
'password',
'omit',
'validation',
'delegate',
'encryption',
]
Comment thread
jiashengguo marked this conversation as resolved.
Outdated
return enhanceFunc(
prisma,
{},
{
kinds: Enhancements,
}
)
},
Comment thread
jiashengguo marked this conversation as resolved.
})
)

Expand Down
Loading