```const response = require('cfn-response') const ...
# orm-help
a
Copy code
const response = require('cfn-response')
const path = require('path')

const schema = path.join(__dirname, '..', 'prisma', 'schema.prisma')

async function applyMigration() {
  const execaCommand = (await import('execa')).execaCommand
  const subprocess = execaCommand(`npx prisma migrate deploy --schema=${schema}`)
  subprocess.stdout.pipe(process.stdout)
  subprocess.stderr.pipe(process.stderr)
  await subprocess
    .catch((e) => {
      console.error(e)
      throw new Error('Migration failed!')
    })
}

async function handler(event, context) {
  console.log('Received event: ', JSON.stringify(event, null, 2))
  if (event.RequestType === 'Delete') return
  await applyMigration()
    .then(() => response.send(event, context, response.SUCCESS, 'ok'))
    .catch((error) => response.send(event, context, response.ERROR, error.message))
}

exports.handler = handler
j
Can you execute
npx prisma
and get output? If not, what about just
npx
?
a
From what I can tell it seems to be an issue with building the image in codebuild? When I build the image locally it works fine but with the CI pipeline it fails
it is indeed
npx
that causes this problem
I see on github people have solutions for how to get around using npx so will give that a go
j
Oh interesting. If you have a project which includes Prisma, you can also put it into an Npm script and then run
npm run scriptname
instead - that does not use
npx
under he hood.
a
Hey @janpio ended up with the solution below which is working!
Copy code
// @ts-check
const response = require('cfn-response')
const prismaPath = require.resolve('prisma')

async function prisma(...arguments) {
  console.log(`Running \`prisma ${arguments.join(' ')}\``)
  const execaCommand = (await import('execa')).execaCommand
  const subprocess = execaCommand(`${prismaPath} ${arguments.join(' ')}`)
  subprocess.stdout?.pipe(process.stdout)
  subprocess.stderr?.pipe(process.stderr)
  await subprocess
}

async function handler(event, context) {
  console.log('Received event: ', JSON.stringify(event, null, 2))
  if (event.RequestType === 'Delete') return
  try {
    await prisma('migrate', 'deploy')
    response.send(event, context, response.SUCCESS, { message: 'ok' })
  } catch (error) {
    console.error(error)
    response.send(event, context, response.FAILED, { message: error.message })
  }
}

exports.handler = handler
j
That is smart, you are getting the path of the installation in node_modules and then running it via that, correct?
a
Yeah that's right! Seemed like the most robust solution
j
I fully agree - we actually do something similar internally when we run
prisma generate
internally.
I think we have an issue about programmatic execution of the CLI. Can you post your solution there? (Or maybe even write a blog post or something like that?) I think this would be nice to have for our users!
a
This issue right? If so will add a comment about our implementation
j
Yes exactly 👍