-
Notifications
You must be signed in to change notification settings - Fork 16
WIP New Prisma Client JS API draft #356
Comments
@schickling This draft is insanely amazing. Infact, you can use this itself as docs once its implemented. Great work 👍 Just hoping that you will get there soon! Btw, is this planned before GA? |
Glad you like it @tvvignesh. We will most likely not be able to implement all of the suggested API changes above but this will be the direction we're going to. Would be great if you could add more thoughts in a further comment what exactly you like about the changes above. Any kind of feedback is helpful! |
@schickling - wow, lots of good stuff here. some great API improvements that will for sure provide a more intuitive mechanism by which to construct queries/mutations. QQ: Would this deprecate the current API or rather be an alternative/extension? |
@schickling Sorry for the late reply. More than any feature in specific, its the documentation which I really liked. Also the fact that you have thought out lots of possible use cases. The main thing more than documentation in any project is a sample implementation of the same which I see a lot here. Typically people just say this is the function, this is the parameter and leave it there. This takes it to the next level. |
There's some good stuff in here! Will any of this make it into v1? |
@flybayer we've started breaking up the API above into separate smaller feature requests which we'll start to ship one by one for now. |
Prisma Schema
The example code below assumes the following Prisma schema:
Types
Terminology
Constructor
Logging & Debugging
Logging
All engine related logs can be configured with the
log
property.These are examples how to specify different log levels:
Just providing the log levels, stdout as default
Changing on a per log level, where the logs end up: As an event or in stdout.
Log level names get mapped to the event name for the event emitter.
Debugging
The
debug
property includes debugging logs of the TypeScript implementation; whereas, thelog
property addresses the logs coming from the query engine.As the TypeScript debugging logs are mostly used by maintainers and not users, it was moved to
__internal
to not confuse users of the PrismaClient.js library.Basic Queries
Field-level Primary Key constraint
Different unique constraints will change Prisma Client's
where
blocks:Field-level unique constraint
Model-level composite constraint (unnamed)
Naming the composite constraint
Writing Data
Write operations
Nested Write API
link
unlink
resetAndLink
linkOrCreate
create
update
replace
delete
Fluent Write API
create
orCreate
update
replace
delete
Many operations
Nested writes
Load: Select / Include API
Default selection set
Advanced Fluent API
Null narrowing
find
queries returnnull
by defaultupdate
anddelete
as well as field traversal queries will fail if record isnull
Expressing the same query using fluent API syntax and nested writes
Mental model: Graph traversal
Working with types
typeof
?)Expressions
where
)Criteria Filters
Order By
The query
findMany
allows the user to supply theorderBy
argument which controls the sorting of the returned array of records:If a query uses pagination parameters but no
orderBy
parameter is supplied an implicitorderBy
for the id field of the model is added. The following two queries are semantically equivalent:Write Operations (Update/Atomic)
Set:
Type specific:
See:
Top level callback
Aggregations
Group by
Distinct
Experimental: Meta response
Note: This is a early draft for this feature and won't be implemented in the near future
Meta
Strategies
Can be applied to every paginable list and stream
Optimistic Concurrency Control / Optimistic Offline Lock
Supported operations
update
replace
delete
update
delete
Batching
Note: Combined with OCC (i.e.
if
) also known as "unit of work"Criteria API
Design decisions
Constructor
Connection management
Error Handling
Error Character Encoding
Prisma Client generates pretty error messages with ANSI characters for features like color coding errors and warnings in queries and newlines that are very useful for
development as they usually pin point the issue.
However, when this data is serialized it contains a lot of unicode characters.
Serialized Prisma Client error
There are two prominent use cases amongst others for disabling/better structuring the error logs:
In Production logs, one might want to read the error messages thrown by Prisma Client.
In tools like Studio, it currently strips the ANSI characters (like this) and displays the output as
the error message.
To solve these two use case, Prisma Client can do the following:
Use
NODE_ENV
. WhenNODE_ENV
is set todevelopment
or is unset, Prisma Client can emit logs with ANSI characters. However, whenNODE_ENV
is set toproduction
Prisma Client can omit ANSI characters and any additional newline characters from the logs.PrismaClient can additionally offer
prettyLogs
as a constructor argument (defaults totrue
) to switch off the pretty error logging.Unresolved questions
Figured out but needs spec
$
pre-fixingBigger todos
DateTime
in Prisma schema toDate
in JS) and how to overwrite default type-mapping behavior (e.g. using Moment.js).replace()
vs.update({}, { replace: true })
(alternative:overwrite: true
,reset: true
)Small & self-contained
connect
API from Prisma Client instance (solves: https://github.com/prisma/prismajs/issues/153).first()
type narrowing forinclude
/select
find
vsfindUnique
vsget
...PrismaClient
constructor APIwhere
to Criteria (filter/unique criteria)Ugly parts
.select()
vs nested{ select: { } }
APIRelated
Future topics
The text was updated successfully, but these errors were encountered: