Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 22 additions & 12 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export interface ServerOptions {
logLevel?: string[]
}

type EnhancementKind = 'password' | 'omit' | 'policy' | 'validation' | 'delegate' | 'encryption'
// enable all enhancements except policy
const Enhancements: EnhancementKind[] = ['password', 'omit', 'validation', 'delegate', 'encryption']

/**
* Resolve the absolute path to the Prisma schema directory
*/
Expand Down Expand Up @@ -136,6 +140,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 +179,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` +
`Please run \`zenstack generate\` first or specify the correct output directory of ZenStack generated modules using the \`-z\` option.`
)
}
Expand All @@ -195,7 +200,7 @@ async function loadZenStackModules(

const zenstackVersion = getZenStackVersion()

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

/**
Expand All @@ -204,11 +209,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 +240,15 @@ export async function startServer(options: ServerOptions) {
app.use(
'/api/model',
ZenStackMiddleware({
getPrisma: () => prisma,
getPrisma: () => {
return enhanceFunc(
prisma,
{},
{
kinds: Enhancements,
}
)
},
})
)

Expand Down