How to use @prisma/cli - 10 common examples

To help you get started, we’ve selected a few @prisma/cli examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github prisma / lift / src / bin.ts View on Github external
async function main(): Promise {
  // create a new CLI with our subcommands
  const cli = LiftCommand.new({
    save: LiftSave.new(),
    up: LiftUp.new(),
    down: LiftDown.new(),
    dev: LiftWatch.new(providerAliases),
    ['tmp-prepare']: LiftTmpPrepare.new(),
    studio: StudioCommand.new(providerAliases),
  })
  // parse the arguments
  const result = await cli.parse(process.argv.slice(2))
  if (result instanceof HelpError) {
    console.error(result)
    return 1
  } else if (isError(result)) {
    console.error(result)
    return 1
  }
  console.log(result)

  return 0
}
process.on('SIGINT', () => {
github prisma / prisma2 / cli / prisma2 / src / bin.ts View on Github external
'tmp-prepare': LiftTmpPrepare.new(),
      introspect: Introspect.new(),
      dev: LiftWatch.new(aliases),
      studio: StudioCommand.new(aliases),
      generate: Generate.new(),
      version: Version.new(),
      validate: Validate.new(),
    },
    ['init', 'lift', 'tmp-prepare', 'introspect', 'dev', 'studio', 'generate', 'validate'],
  )
  // parse the arguments
  const result = await cli.parse(process.argv.slice(2))
  if (result instanceof HelpError) {
    console.error(result.message)
    return 1
  } else if (isError(result)) {
    console.error(result)
    return 1
  }
  console.log(result)

  return 0
}
github prisma / prisma2 / cli / introspection / src / bin.ts View on Github external
async function main(): Promise {
  process.env.NODE_ENV = 'production'
  // create a new CLI with our subcommands
  const args = arg(process.argv.slice(2), {})

  if (isError(args)) {
    console.error(args.message)
    return 1
  }

  const commands = {
    init: Init.new(),
    introspect: Introspect.new(),
  }

  if (commands[args._[0]]) {
    const result = await commands[args._[0]].parse(process.argv.slice(3))
    console.log(result)
  } else {
    console.error(`Command not found: ${args._[0]}. Available commands: ${Object.keys(commands).join(', ')}`)
    return 1
  }
github prisma / prisma2 / cli / introspection / src / bin.ts View on Github external
async function main(): Promise {
  process.env.NODE_ENV = 'production'
  // create a new CLI with our subcommands
  const args = arg(process.argv.slice(2), {})

  if (isError(args)) {
    console.error(args.message)
    return 1
  }

  const commands = {
    init: Init.new(),
    introspect: Introspect.new(),
  }

  if (commands[args._[0]]) {
    const result = await commands[args._[0]].parse(process.argv.slice(3))
    console.log(result)
  } else {
    console.error(`Command not found: ${args._[0]}. Available commands: ${Object.keys(commands).join(', ')}`)
github prisma / lift / src / cli / commands / LiftSave.ts View on Github external
if (migration.warnings && migration.warnings.length > 0) {
      console.log(chalk.bold(`\n\n⚠️  There might be data loss when applying the migration:\n`))
      for (const warning of migration.warnings) {
        console.log(chalk(`  • ${warning.description}`))
      }
      console.log()
    }

    if (preview) {
      lift.stop()
      return `\nRun ${chalk.greenBright('prisma lift save --name MIGRATION_NAME')} to create the migration\n`
    }

    await getSchema() // just to leverage on its error handling
    const schemaDir = (await getSchemaDir())! // TODO: Probably getSchemaDir() should return Promise instead of Promise

    const migrationsDir = path.join(schemaDir, 'migrations', migrationId)
    await serializeFileMap(files, migrationsDir)
    const lockFilePath = path.join(schemaDir, 'migrations', 'lift.lock')
    await writeFile(lockFilePath, newLockFile)

    lift.stop()

    return `\nLift just created your migration ${printMigrationId(migrationId)} in\n\n${chalk.dim(
      printFiles(`migrations/${migrationId}`, files),
    )}\n\nRun ${chalk.greenBright('prisma2 lift up')} to apply the migration\n`
  }
github prisma / prisma2 / cli / prisma2 / src / Generate.ts View on Github external
public async parse(argv: string[], minimalOutput = false): Promise {
    const datamodelPath = await getSchemaPath()
    if (!datamodelPath) {
      throw new Error(`Can't find schema.prisma`) // TODO: Add this into a central place in getSchemaPath() as an arg
    }
    const generators = await getGenerators({
      schemaPath: datamodelPath,
      providerAliases: this.aliases,
      printDownloadProgress: true,
      version: pkg.prisma.version,
    })

    if (generators.length === 0) {
      console.log(missingGeneratorMessage)
    }

    // CONTINUE HERE
github prisma / prisma2 / cli / introspection / src / introspectionConnector.ts View on Github external
export async function getCredentialsFromExistingDatamodel(): Promise {
  const schemaPath = await getSchemaPath()
  if (schemaPath) {
    const datamodel = readFileSync(schemaPath, 'utf-8')
    const { datasources } = await getConfig({ datamodel })
    // For now just take the first data source
    if (datasources && datasources.length > 1) {
      console.error(
        `There are more than 1 datasources listed in the datamodel ${datasources.map(d => d.name).join(', ')}, taking ${
          datasources[0].name
        }`,
      )
    }
    if (datasources && datasources.length > 0) {
      const uri = datasources[0].url.value
      return uriToCredentials(uri)
    }
  }
github prisma / lift / src / cli / commands / LiftSave.ts View on Github external
const { files, newLockFile, migrationId } = await lift.save(migration, name, preview)

    if (migration.warnings && migration.warnings.length > 0) {
      console.log(chalk.bold(`\n\n⚠️  There might be data loss when applying the migration:\n`))
      for (const warning of migration.warnings) {
        console.log(chalk(`  • ${warning.description}`))
      }
      console.log()
    }

    if (preview) {
      lift.stop()
      return `\nRun ${chalk.greenBright('prisma lift save --name MIGRATION_NAME')} to create the migration\n`
    }

    await getSchema() // just to leverage on its error handling
    const schemaDir = (await getSchemaDir())! // TODO: Probably getSchemaDir() should return Promise instead of Promise

    const migrationsDir = path.join(schemaDir, 'migrations', migrationId)
    await serializeFileMap(files, migrationsDir)
    const lockFilePath = path.join(schemaDir, 'migrations', 'lift.lock')
    await writeFile(lockFilePath, newLockFile)

    lift.stop()

    return `\nLift just created your migration ${printMigrationId(migrationId)} in\n\n${chalk.dim(
      printFiles(`migrations/${migrationId}`, files),
    )}\n\nRun ${chalk.greenBright('prisma2 lift up')} to apply the migration\n`
  }
github prisma / lift / src / cli / commands / LiftCommand.ts View on Github external
public async parse(argv: string[]): Promise {
    // parse the arguments according to the spec
    const args = arg(argv, {
      '--help': Boolean,
      '-h': '--help',
    })
    if (isError(args)) {
      return this.help(args.message)
    }
    // display help for help flag or no subcommand
    if (args._.length === 0 || args['--help']) {
      return this.help()
    }
    // check if we have that subcommand
    const cmd = this.cmds[args._[0]]
    if (cmd) {
      const nextFreePort = await getNextFreePort(process.cwd())
      if (typeof nextFreePort !== 'number') {
        const command = `prisma2 lift ${argv.join(' ')}`
        throw new Error(`Cannot run ${chalk.bold(command)} because there is a ${chalk.bold(
          'prisma2 dev',
        )} command running in this directory.
Please ${gamboge(`stop ${chalk.bold('prisma2 dev')} first`)}, then try ${chalk.greenBright.bold(command)} again`)
github prisma / lift / src / cli / commands / Converter.ts View on Github external
public async parse(argv: string[]): Promise {
    // parse the arguments according to the spec
    const args = arg(argv, {
      '--help': Boolean,
      '-h': '--help',
    })

    if (isError(args)) {
      return this.help(args.message)
    } else if (args['--help']) {
      return this.help()
    }

    const datamodel = await this.readStdin()
    const engine = new LiftEngine({ projectDir: process.cwd() })
    const parser = DefaultParser.create(DatabaseType.postgres)
    const isdl = parser.parseFromSchemaString(datamodel)
    return isdlToDatamodel2(isdl, [])
  }

@prisma/cli

Prisma is an open-source database toolkit. It includes a JavaScript/TypeScript ORM for Node.js, migrations and a modern GUI to view and edit the data in your database. You can use Prisma in new projects or add it to an existing one.

Apache-2.0
Latest version published 3 years ago

Package Health Score

65 / 100
Full package analysis