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

Data type JSON #186

Closed
Errorname opened this issue Jul 19, 2019 · 30 comments
Closed

Data type JSON #186

Errorname opened this issue Jul 19, 2019 · 30 comments
Assignees
Labels
kind/feature A request for a new feature. tech/engines Issue for tech Engines. topic: native database types
Milestone

Comments

@Errorname
Copy link
Contributor

When converting the following datamodel:

type A {
  id: ID! @unique

  text: String!

  meta: JSON!
}

Using the following method:

cat datamodel.graphql | prisma2 convert > schema.prisma

I get the following schema:

model A {
  id   String @default(cuid()) @id @unique
  text String
}

As we can see, the meta: JSON attribute is missing

@pantharshit00
Copy link
Contributor

pantharshit00 commented Jul 19, 2019

JSON type is currently not supported in the datamodel. We may support it via native database types. Marking this as a feature request.

@nhuesmann
Copy link

@pantharshit00 Any update on whether you will be supporting the JSON type? I'm using it in a Prisma1 project and definitely want to continue to use it in Prisma2. The only alternative I see currently is storing it as a string, however I'd like to utilize Postgres's underlying json type for additional safety.

@janpio janpio changed the title Datamodel converting forget JSON attributes Date type JSON Aug 27, 2019
@robmurtagh
Copy link

Would love to see this feature!

@pantharshit00
Copy link
Contributor

We surely have an intent to support this via native database types we have introduced in the schema: https://github.com/prisma/specs/tree/master/schema#type-specifications

So the JSON type will be just another database-specific type:

model User {
  id   String @id @default(uuid())
  misc String @pg.JSON
}

@nhuesmann
Copy link

@pantharshit00 Sounds promising! If you do implement this, could you guys make sure to provide examples of how it will work with Nexus?

@divyenduz divyenduz changed the title Date type JSON Data type JSON Sep 3, 2019
@thimovss
Copy link

thimovss commented Oct 1, 2019

Any updates on if/when this feature will be implemented?

@chrisdrackett
Copy link

just ran into this while migrating from prisma 1 to 2. This has effectively stopped the migration process for us.

@nhuesmann
Copy link

@chrisdrackett I'm just storing the stringified JSON for now until they implement the JSON type.

@chrisdrackett
Copy link

yep, I'm doing the same. I have a bit more JSON.parse and JSON.stringify than I'd like, but its working :)

@aberbob
Copy link

aberbob commented Nov 12, 2019

Commenting to know when anything changes. I am trying to move from Sequelize and need the JSON data type in MySQL. Migration was a breeze (30 mins for 46 tables and relations in about every table) except for the JSON type. :)

@pantharshit00
Copy link
Contributor

The spec work is still ongoing. Latest work can be tracked in this PR in the specs repository: prisma/specs#291

@heymartinadams
Copy link

@chrisdrackett not that it matters much, but your approach might appear to have some advantages :) https://www.youtube.com/watch?v=ff4fgQxPaO0

@janpio
Copy link
Member

janpio commented Jan 7, 2020

Internal Note: Dropping from current sprint, and picking up later.

@namesphill
Copy link

Hey! Do you guys happen to have an ETA on this feature?

@janpio janpio added this to the Beta 3 milestone Apr 11, 2020
@janpio janpio added tech/engines Issue for tech Engines. team/product labels Apr 11, 2020
@homerjam
Copy link

Great to see this in the Beta 3 milestone. Does anyone have any suggestions for a quick and dirty workaround for upgrading a prisma1 project in the meantime?

@martinnirtl
Copy link

martinnirtl commented Apr 20, 2020

Hey!

You could use the string type with JSON.stringify for the time 😃

@homerjam
Copy link

Thanks - that's what I'm planning but hoped there was something "more smarter" 🤓

@airhorns
Copy link

Is this closed because this is a wontfix now? JSONB support would be really awesome to get in!

@martinnirtl
Copy link

martinnirtl commented Apr 30, 2020

its now supported in beta4 😃 (i can only speak for postgres users)

@robmurtagh
Copy link

Ah, great. Where do you see documentation of JSON support for beta4?

I can't see anything in the release notes:
https://github.com/prisma/prisma/releases/tag/2.0.0-beta.4

@martinnirtl
Copy link

i just saw that the issue was closed and gave Json-type a shot with prisma migrate.

everything worked out and i got jsonb fields in postgres db 😬

@pantharshit00
Copy link
Contributor

pantharshit00 commented May 1, 2020

Please follow https://github.com/prisma/prisma/issues/2322 for documentation update for JSON support. Please note there is still ongoing work there like filtering support.

@hugohammarstrom

This comment has been minimized.

@pantharshit00

This comment has been minimized.

@mavilein
Copy link
Member

mavilein commented May 4, 2020

Here is a summary of what we implemented so far:

Prisma Schema
The data type for JSON is called Json in the schema. It can be used like this:

model Blog {
  id   Int @id
  json Json 
}

It is supported by the MySQL and Postgres connectors. It is not supported by SQLite.

Introspection
The prisma introspect command maps the following column types to Json:

  • Postgres: json and jsonb
  • MySQL: JSON

Migrations
prisma migrate uses the following data type when creating a column for a field of type Json:

  • Postgres: jsonb
  • MySQL: JSON

Prisma Client
A field of type Json are exposed as proper objects in Javascript.

Important Details
JSON support for MySQL is only supported for versions greater than 5.6. As we don't have version information available during schema validation no error is shown in this case. However we have version information available in prisma migrate. Therefore we error when a migration against MySQL 5.6 uses the Json data type.

@thesunny
Copy link

thesunny commented May 14, 2020

@mavilein

Thank you for the summary!

Should the field type be returned as a JSON object as opposed to a JavaScript object. This would be more accurate.

For example, my API type checks that the return values for an API method are valid JSON objects and returning an object would fail that test. This is because API responses are JSON encoded and this prevents values like Date objects from being returned which can't be encoded.

This is maybe not 100% a proper JSON value type (I'm unsure about undefined) but as a starting point:

export type JSONValue =
  | string
  | number
  | boolean
  | null
  | undefined
  | JSONArray
  | JSONObject

export type JSONArray = Array<JSONValue>

export type JSONObject = {
  [key: string]: JSONValue
}

If you think this is worthwhile, I will open a separate issue for it.

@pantharshit00
Copy link
Contributor

@thesunny Can you please open an issue about this on the client repository(https://github.com/prisma/prisma-client-js)?

The typescript typing is unrelated to the engines implementation so it is better to track this separately. Also, better typing suggestions are always welcomed :)

@gustawdaniel
Copy link

How about JSON support in prisma v2?

@janpio
Copy link
Member

janpio commented Jul 4, 2020

@gustawdaniel Was added back in beta.4 already: https://github.com/prisma/prisma/releases/tag/2.0.0-beta.4 So can be used now of course!

@gustawdaniel
Copy link

Thanks @janpio

So I have rather problem with nexus graphql framework than with prisma, because of my version is 2.1.3.

https://stackoverflow.com/questions/62730081/how-to-use-json-type-in-prisma-graphql-nexus-framework

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. tech/engines Issue for tech Engines. topic: native database types
Projects
None yet
Development

No branches or pull requests