Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting Prisma 2 schema programmatically in JavaScript (file and string) #3265

Closed
timleslie opened this issue Aug 11, 2020 · 3 comments
Closed
Labels
kind/feature A request for a new feature. topic: prisma format CLI: prisma format topic: sdk
Milestone

Comments

@timleslie
Copy link

Problem

I am programmatically generating a prisma schema. Before I write it to a file I would like to ensure it is correctly formatted.

I currently achieve this with the following code:

const execa = require('execa');
const { resolveBinary } = require('@prisma/sdk');

const prismaSchema = (
  await execa(await resolveBinary('prisma-fmt'), ['format'], {
    env: { ...process.env, RUST_BACKTRACE: '1' },
    maxBuffer: 1000 * 1000 * 1000,
    input: `unformatted schema goes here`
  })
).stdout;

it would be nice if there was a way to achieve this without needing to execute a binary. Failing that, a sanctioned way to run the binary would be an improvement.

Suggested solution

I imagine a solution that looked something like:

const { formatSchema } = require('@prisma/sdk');

const prismaSchema = formatSchema('unformatted schema goes here');

Additional context

See discussion here

@Jolg42
Copy link
Member

Jolg42 commented Aug 19, 2020

Hi @timleslie !

You can actually use it today (it is already there!) 😃

const { formatSchema } = require('@prisma/sdk');

// and then use it like
const output = await formatSchema({ schemaPath })

Under the hood this will call the prisma-fmt binary and return the output.

I will close this for now then. If you think it should be reopened let me know.

@Jolg42 Jolg42 closed this as completed Aug 19, 2020
@timleslie
Copy link
Author

This requires having the file written out to the file-system. Ideally I'd like to be able to do all this in memory before having to write to disk. In my use case I'm generating a collection of schemas and want to be able to take a hash of the (formatted) schema and look it up and only write to disk if it's a new schema. Writing to disk just to format the file so that I can check whether I need to write to disk is a bit of overkill. (I also realise that what I'm doing is perhaps outside of what could be considered regular usage of the library :-) )

ping @Jolg42

@Jolg42 Jolg42 reopened this Aug 21, 2020
Jolg42 added a commit that referenced this issue Aug 21, 2020
@Jolg42
Copy link
Member

Jolg42 commented Aug 21, 2020

I think it's a valid use-case!

So since dev version 2.6.0-dev.16 you can now use it like:

await formatSchema({ 
	schema: `
		// My Prisma Schema
	`
})

or with a path

await formatSchema({ schemaPath: '..../schema.prisma' })

It will be part of the 2.6.0 release.

Enjoy 😉

@Jolg42 Jolg42 closed this as completed Aug 21, 2020
@janpio janpio added this to the Release 2.6.0 milestone Aug 21, 2020
@janpio janpio changed the title Formatting Prisma 2 schema programmatically in JavaScript Formatting Prisma 2 schema programmatically in JavaScript (file and string) Aug 21, 2020
@janpio janpio added topic: prisma format CLI: prisma format and removed topic: prisma-format labels Feb 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. topic: prisma format CLI: prisma format topic: sdk
Projects
None yet
Development

No branches or pull requests

3 participants