From c2dc1f824a5cfc6e6ab1e4bb17fc387347aedc0b Mon Sep 17 00:00:00 2001 From: Nikolas Burk Date: Tue, 24 Mar 2020 15:50:19 +0100 Subject: [PATCH 1/5] continue docs --- .../03-reference/01-tools-and-interfaces/04-introspection.mdx | 1 - 1 file changed, 1 deletion(-) diff --git a/content/03-reference/01-tools-and-interfaces/04-introspection.mdx b/content/03-reference/01-tools-and-interfaces/04-introspection.mdx index 2e79c65f79..c96d5bec33 100644 --- a/content/03-reference/01-tools-and-interfaces/04-introspection.mdx +++ b/content/03-reference/01-tools-and-interfaces/04-introspection.mdx @@ -58,7 +58,6 @@ Note that as you evolve the application, this process can be repeated for an ind ![](https://imgur.com/8Tp9jRL.png) - ## Rules and conventions Prisma employs a number of conventions for translating a database schema into a Prisma data model: From 59b51519894567aebe46a2b7ffec001952890308 Mon Sep 17 00:00:00 2001 From: Nikolas Burk Date: Tue, 24 Mar 2020 15:52:08 +0100 Subject: [PATCH 2/5] continue docs --- .../05-prisma-cli/02-command-reference.mdx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/content/03-reference/01-tools-and-interfaces/05-prisma-cli/02-command-reference.mdx b/content/03-reference/01-tools-and-interfaces/05-prisma-cli/02-command-reference.mdx index bef97e1d64..c1bb991e4e 100644 --- a/content/03-reference/01-tools-and-interfaces/05-prisma-cli/02-command-reference.mdx +++ b/content/03-reference/01-tools-and-interfaces/05-prisma-cli/02-command-reference.mdx @@ -154,18 +154,18 @@ The `generate` command recognizes the following options to modify its behavior: prisma2 generate ``` - ✔ Generated Prisma Client to ./node_modules/@prisma/client in 61ms +✔ Generated Prisma Client to ./node_modules/@prisma/client in 61ms - You can now start using Prisma Client in your code: +You can now start using Prisma Client in your code: - ``` - import { PrismaClient } from '@prisma/client' - // or const { PrismaClient } = require('@prisma/client') +``` +import { PrismaClient } from '@prisma/client' +// or const { PrismaClient } = require('@prisma/client') - const prisma = new PrismaClient() - ``` +const prisma = new PrismaClient() +``` - Explore the full API: http://pris.ly/d/client +Explore the full API: http://pris.ly/d/client **Generate Prisma Client using a non-default `schema.prisma` path** From 76a958db43aa6ca2f6cc12f6c94d32bcdc1ba474 Mon Sep 17 00:00:00 2001 From: Nikolas Burk Date: Tue, 24 Mar 2020 16:36:09 +0100 Subject: [PATCH 3/5] continue docs --- .../01-rest.md | 46 +++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/content/02-understand-prisma/03-how-prisma-fits-into-your-stack/01-rest.md b/content/02-understand-prisma/03-how-prisma-fits-into-your-stack/01-rest.md index 81188de40f..6f75b96e8f 100644 --- a/content/02-understand-prisma/03-how-prisma-fits-into-your-stack/01-rest.md +++ b/content/02-understand-prisma/03-how-prisma-fits-into-your-stack/01-rest.md @@ -1,7 +1,45 @@ --- - title: "REST" - metaTitle: "" - metaDescription: "" +title: 'REST' +metaTitle: '' +metaDescription: '' --- -Coming 🔜 \ No newline at end of file +## Overview + +When building REST APIs, Prisma Client can be used inside your _route controllers_ to send databases queries. + +![](https://imgur.com/dbRvgHc.png) + +## Supported libraries + +As Prisma Client is "only" responsible for sending queries to your database, it can be combined with any HTTP server library or web framework of your choice. + +Here are a few examples: + +- [Express](https://expressjs.com/) +- [koa](https://koajs.com/) +- [hapi](https://hapi.dev/) +- [Fastify](https://www.fastify.io/) +- [Sails](https://sailsjs.com/) +- [AdonisJs](https://adonisjs.com/) +- [NestJS](https://nestjs.com/) +- [Next.js](https://nextjs.org/) +- [Foal TS](https://foalts.org/) +- [Polka](https://github.com/lukeed/polka) +- [Micro](https://github.com/zeit/micro) +- [Feathers](https://feathersjs.com/) + +## Example + +You can find several ready-to-tun examples that show how to implement a REST API with Prisma Client in the [`prisma-examples`](https://github.com/prisma/prisma-examples/) repository. + +## TypeScript + +### Fullstack + +| Example | Language | Stack | Description | +| :----------------------------------------------------------------------------------------------- | :--------- | ------------ | ----------------------------------------------------------------- | +| [`rest-nextjs`](https://github.com/prisma/prisma-examples/tree/prisma2/typescript/rest-nextjs) | TypeScript | Fullstack | Simple [Next.js](https://nextjs.org/) app (React) with a REST API | +| [`rest-express`](https://github.com/prisma/prisma-examples/tree/prisma2/typescript/rest-express) | TypeScript | Backend only | Simple REST API with Express | +| [`rest-nextjs`](https://github.com/prisma/prisma-examples/tree/prisma2/javascript/rest-nextjs) | JavaScript | Fullstack | Simple [Next.js](https://nextjs.org/) app (React) with a REST API | +| [`rest-express`](https://github.com/prisma/prisma-examples/tree/prisma2/javascript/rest-express) | JavaScript | Backend only | Simple REST API with Express | From 26e960d088b2ee930d4e464d040ca33d1436e5ae Mon Sep 17 00:00:00 2001 From: Nikolas Burk Date: Tue, 24 Mar 2020 16:46:39 +0100 Subject: [PATCH 4/5] continue docs --- .../01-rest.md | 112 +++++++++++++++++- 1 file changed, 108 insertions(+), 4 deletions(-) diff --git a/content/02-understand-prisma/03-how-prisma-fits-into-your-stack/01-rest.md b/content/02-understand-prisma/03-how-prisma-fits-into-your-stack/01-rest.md index 6f75b96e8f..c5ad93e74b 100644 --- a/content/02-understand-prisma/03-how-prisma-fits-into-your-stack/01-rest.md +++ b/content/02-understand-prisma/03-how-prisma-fits-into-your-stack/01-rest.md @@ -29,13 +29,117 @@ Here are a few examples: - [Micro](https://github.com/zeit/micro) - [Feathers](https://feathersjs.com/) -## Example +## Examples -You can find several ready-to-tun examples that show how to implement a REST API with Prisma Client in the [`prisma-examples`](https://github.com/prisma/prisma-examples/) repository. +### REST API server example + +Assume you have a Prisma schema that looks similar to this: + +```prisma +datasource db { + provider = "sqlite" + url = "file:./dev.db" +} + +generator client { + provider = "prisma-client-js" +} + +model Post { + id Int @id @default(autoincrement()) + title String + content String? + published Boolean @default(false) + author User? +} + +model User { + id Int @id @default(autoincrement()) + email String @unique + name String? + posts Post[] +} +``` + +You can now implement route controller (e.g. using Express) that use the generated [Prisma Client API]() to perform a database operation when an incoming HTTP request arrives. This page only shows few sample code snippets, if you want to run these code snippets, you can use the [REST API example](https://github.com/prisma/prisma-examples/tree/prisma2/typescript/rest-express). + +#### `GET` + +```ts +app.get('/feed', async (req, res) => { + const posts = await prisma.post.findMany({ + where: { published: true }, + include: { author: true } + }) + res.json(posts) +}) +``` -## TypeScript +Note that the `feed` endpoint in this case returns a nested JSON response of `Post` objects that _include_ an `author` object. Here's a sample response: -### Fullstack +```json +[ + { + "id": "21", + "title": "Hello World", + "content": "null", + "published": "true", + "author": { + "id": "42", + "name": "Alice", + "email": "alice@prisma.io" + } + } +] +``` + +#### `POST` + +```ts +app.post(`/post`, async (req, res) => { + const { title, content, authorEmail } = req.body + const result = await prisma.post.create({ + data: { + title, + content, + published: false, + author: { connect: { email: authorEmail } }, + }, + }) + res.json(result) +}) +``` + +#### `PUT` + +```ts +app.put('/publish/:id', async (req, res) => { + const { id } = req.params + const post = await prisma.post.update({ + where: { id: Number(id) }, + data: { published: true }, + }) + res.json(post) +}) +``` + +#### `DELETE` + +```ts +app.delete(`/post/:id`, async (req, res) => { + const { id } = req.params + const post = await prisma.post.delete({ + where: { + id: Number(id), + }, + }) + res.json(post) +}) +``` + +### Ready-to-tun example projects + +You can find several ready-to-tun examples that show how to implement a REST API with Prisma Client in the [`prisma-examples`](https://github.com/prisma/prisma-examples/) repository. | Example | Language | Stack | Description | | :----------------------------------------------------------------------------------------------- | :--------- | ------------ | ----------------------------------------------------------------- | From 580d6f4f69596c9d59a7e128680c5ac9e6c01d77 Mon Sep 17 00:00:00 2001 From: Nikolas Burk Date: Tue, 24 Mar 2020 16:58:07 +0100 Subject: [PATCH 5/5] continue docs --- .../01-prisma-schema/04-data-model.mdx | 30 +++++++++---------- .../02-prisma-client/03-crud.mdx | 6 ++-- .../02-prisma-client/12-logging.mdx | 3 +- .../20-database-polyfills.mdx | 3 +- .../01-postgresql.mdx | 3 +- .../02-mysql.mdx | 3 +- .../03-sqlite.mdx | 3 +- .../05-foreign-keys/01-postgresql.mdx | 4 +-- .../05-foreign-keys/03-sqlite.mdx | 3 +- .../06-cascading-deletes/01-postgresql.mdx | 4 +-- .../06-cascading-deletes/02-mysql.mdx | 4 +-- .../06-cascading-deletes/03-sqlite.mdx | 3 +- 12 files changed, 31 insertions(+), 38 deletions(-) diff --git a/content/03-reference/01-tools-and-interfaces/01-prisma-schema/04-data-model.mdx b/content/03-reference/01-tools-and-interfaces/01-prisma-schema/04-data-model.mdx index 2ec5722128..17654f4770 100644 --- a/content/03-reference/01-tools-and-interfaces/01-prisma-schema/04-data-model.mdx +++ b/content/03-reference/01-tools-and-interfaces/01-prisma-schema/04-data-model.mdx @@ -111,8 +111,8 @@ The _data source connector_ determines what _native database type_ each of these Expand below to see the mappings per connector and generator. - + ## Enums @@ -149,7 +149,6 @@ Enums are considered [scalar](#scalar-types) types in the Prisma data model. The Enums are defined via the `enum` block. - ## Naming enums Enum names must start with a letter. They are are typically spelled in [PascalCase](http://wiki.c2.com/?PascalCase) and use the singular form (e.g. `Role` instead of `role`, `roles` or `Roles`). @@ -201,17 +200,18 @@ Attributes modify the behavior of a [field](#fields) or block (e.g. [models](#mo Here's a quick overview of the available field attributes: -| Name | Database representation | Arguments | Description | -| :---------- | :--------------------------- | :---------------------------------------- | :------------------------------------------------------------------------------------- | -| `@id` | `PRIMARY KEY` | - | Defines a single-field ID on the model. | -| `@@id` | `PRIMARY KEY` | A list of field references | Defines a multi-field ID on the model. | -| `@default` | `DEFAULT` | An expression (e.g. `5`, `true`, `now()`) | Defines a default value for this field. `@default` takes an expression as an argument. | -| `@unique` | `UNIQUE` | - | Defines a unique constraint for this field. | -| `@@unique` | `UNIQUE` | A list of field references | Defines a unique constraint for the specified fields. | -| `@@index` | `INDEX` | A list of field references | Defines an index. | -| `@relation` | `FOREIGN KEY` / `REFERENCES` | A name and/or a list of field references | Defines meta information about the relation. [Learn more](). | -| `@map` | n/a | The name of the target database column | Maps a field name from the Prisma schema to a different column name. | -| `@@map` | n/a | The name of the target database table | Maps a model name from the Prisma schema to a differenttable name. | +| Name | Database representation | Arguments | Description | +| :----------- | :--------------------------- | :---------------------------------------- | :------------------------------------------------------------------------------------- | +| `@id` | `PRIMARY KEY` | - | Defines a single-field ID on the model. | +| `@@id` | `PRIMARY KEY` | A list of field references | Defines a multi-field ID on the model. | +| `@default` | `DEFAULT` | An expression (e.g. `5`, `true`, `now()`) | Defines a default value for this field. `@default` takes an expression as an argument. | +| `@unique` | `UNIQUE` | - | Defines a unique constraint for this field. | +| `@@unique` | `UNIQUE` | A list of field references | Defines a unique constraint for the specified fields. | +| `@@index` | `INDEX` | A list of field references | Defines an index. | +| `@relation` | `FOREIGN KEY` / `REFERENCES` | A name and/or a list of field references | Defines meta information about the relation. [Learn more](). | +| `@map` | n/a | The name of the target database column | Maps a field name from the Prisma schema to a different column name. | +| `@@map` | n/a | The name of the target database table | Maps a model name from the Prisma schema to a differenttable name. | +| `@updatedAt` | n/a | - | Automatically stores the time when a record was last updated. | Here's an overview of the extact signatures of all attributes: diff --git a/content/03-reference/01-tools-and-interfaces/02-prisma-client/03-crud.mdx b/content/03-reference/01-tools-and-interfaces/02-prisma-client/03-crud.mdx index bf1cedf149..d40ad6b972 100644 --- a/content/03-reference/01-tools-and-interfaces/02-prisma-client/03-crud.mdx +++ b/content/03-reference/01-tools-and-interfaces/02-prisma-client/03-crud.mdx @@ -86,7 +86,6 @@ export type UserWhereUniqueInput = { }; ``` - + #### Reference @@ -568,7 +567,6 @@ export type UserWhereUniqueInput = { }; ``` - + #### Reference diff --git a/content/03-reference/01-tools-and-interfaces/02-prisma-client/12-logging.mdx b/content/03-reference/01-tools-and-interfaces/02-prisma-client/12-logging.mdx index 1896c26994..30666e316c 100644 --- a/content/03-reference/01-tools-and-interfaces/02-prisma-client/12-logging.mdx +++ b/content/03-reference/01-tools-and-interfaces/02-prisma-client/12-logging.mdx @@ -140,7 +140,6 @@ export type QueryEvent = { Note that `query` contains the SQL query and `params` contains any query parameters for the SQL query. - + #### Logging `info` events diff --git a/content/03-reference/01-tools-and-interfaces/02-prisma-client/20-database-polyfills.mdx b/content/03-reference/01-tools-and-interfaces/02-prisma-client/20-database-polyfills.mdx index 7ede15a3f4..2589f73869 100644 --- a/content/03-reference/01-tools-and-interfaces/02-prisma-client/20-database-polyfills.mdx +++ b/content/03-reference/01-tools-and-interfaces/02-prisma-client/20-database-polyfills.mdx @@ -8,6 +8,7 @@ metaDescription: "" Prisma Client provides features that are typically not achievable with relational databases. These features are referred to as _polyfills_. -- Initializing [ID]() values with `cuid` and `uuid` values (requires [Prisma Migrate]()) +- Initializing [ID]() values with `cuid` and `uuid` values +- Using [`@updatedAt`]() to store the time when a record was last updated - [Making 1-1-relations required on both sides]() - [Implicit many-to-many relations]() diff --git a/content/04-guides/01-database-workflows/04-unique-constraints-and-indexes/01-postgresql.mdx b/content/04-guides/01-database-workflows/04-unique-constraints-and-indexes/01-postgresql.mdx index eda727320b..4865d6adb4 100644 --- a/content/04-guides/01-database-workflows/04-unique-constraints-and-indexes/01-postgresql.mdx +++ b/content/04-guides/01-database-workflows/04-unique-constraints-and-indexes/01-postgresql.mdx @@ -68,7 +68,6 @@ Congratulations, you just created a table called `User` in the database. The tab CREATE UNIQUE INDEX "User_email_key" ON "User"(email text_ops); ``` - + ## 3. Create a table with a multi-column unique constraint and index diff --git a/content/04-guides/01-database-workflows/04-unique-constraints-and-indexes/02-mysql.mdx b/content/04-guides/01-database-workflows/04-unique-constraints-and-indexes/02-mysql.mdx index 835fd26683..09aa6a4781 100644 --- a/content/04-guides/01-database-workflows/04-unique-constraints-and-indexes/02-mysql.mdx +++ b/content/04-guides/01-database-workflows/04-unique-constraints-and-indexes/02-mysql.mdx @@ -79,7 +79,6 @@ Congratulations, you just created a table called `User` in the database. The tab CREATE UNIQUE INDEX `email` ON `UniqueDemo`.`User`(`email`); ``` - + ## 3. Create a table with a multi-column unique constraint and index diff --git a/content/04-guides/01-database-workflows/04-unique-constraints-and-indexes/03-sqlite.mdx b/content/04-guides/01-database-workflows/04-unique-constraints-and-indexes/03-sqlite.mdx index 6bd1daac26..ba527d638b 100644 --- a/content/04-guides/01-database-workflows/04-unique-constraints-and-indexes/03-sqlite.mdx +++ b/content/04-guides/01-database-workflows/04-unique-constraints-and-indexes/03-sqlite.mdx @@ -67,7 +67,6 @@ Congratulations, you just created a table called `User` in the database. The tab CREATE UNIQUE INDEX "sqlite_autoindex_User_1" ON "User"("email"); ``` - + ## 3. Create a table with a multi-column unique constraint and index diff --git a/content/04-guides/01-database-workflows/05-foreign-keys/01-postgresql.mdx b/content/04-guides/01-database-workflows/05-foreign-keys/01-postgresql.mdx index 7edf05e6c5..e1776a6c7d 100644 --- a/content/04-guides/01-database-workflows/05-foreign-keys/01-postgresql.mdx +++ b/content/04-guides/01-database-workflows/05-foreign-keys/01-postgresql.mdx @@ -70,7 +70,7 @@ psql ForeignKeyDemo < single-column-foreign-key.sql Congratulations, you just created two tables called `User` and `Post` in the database. The `Post` table references the `User` table via the foreign key defined on the `author` column. - + ## 3. Create a table with a multi-column foreign key constraint diff --git a/content/04-guides/01-database-workflows/05-foreign-keys/03-sqlite.mdx b/content/04-guides/01-database-workflows/05-foreign-keys/03-sqlite.mdx index 1d37ea2154..f03dd3f1b5 100644 --- a/content/04-guides/01-database-workflows/05-foreign-keys/03-sqlite.mdx +++ b/content/04-guides/01-database-workflows/05-foreign-keys/03-sqlite.mdx @@ -68,7 +68,6 @@ sqlite3 ForeignKeyDemo.db < single-column-foreign-key.sql Congratulations, you just created two tables called `User` and `Post` in the database. The `Post` table references the `User` table via the foreign key defined on the `author` column. - + ## 3. Create a table with a multi-column foreign key constraint diff --git a/content/04-guides/01-database-workflows/06-cascading-deletes/01-postgresql.mdx b/content/04-guides/01-database-workflows/06-cascading-deletes/01-postgresql.mdx index a2c74e7fed..5e10d556aa 100644 --- a/content/04-guides/01-database-workflows/06-cascading-deletes/01-postgresql.mdx +++ b/content/04-guides/01-database-workflows/06-cascading-deletes/01-postgresql.mdx @@ -87,7 +87,7 @@ update or delete on table "User" violates foreign key constraint "Post_author_fk Detail: Key (id)=(1) is still referenced from table "Post". ``` - + ## 3. Create two tables with a foreign key and `CASCADE` deletion behavior diff --git a/content/04-guides/01-database-workflows/06-cascading-deletes/02-mysql.mdx b/content/04-guides/01-database-workflows/06-cascading-deletes/02-mysql.mdx index ae74f99cdf..aa992c9ecb 100644 --- a/content/04-guides/01-database-workflows/06-cascading-deletes/02-mysql.mdx +++ b/content/04-guides/01-database-workflows/06-cascading-deletes/02-mysql.mdx @@ -108,7 +108,7 @@ update or delete on table "User" violates foreign key constraint "Post_author_fk Detail: Key (id)=(1) is still referenced from table "Post". ``` - + ## 3. Create two tables with a foreign key and `CASCADE` deletion action diff --git a/content/04-guides/01-database-workflows/06-cascading-deletes/03-sqlite.mdx b/content/04-guides/01-database-workflows/06-cascading-deletes/03-sqlite.mdx index 539c4c9ac7..6859cbc528 100644 --- a/content/04-guides/01-database-workflows/06-cascading-deletes/03-sqlite.mdx +++ b/content/04-guides/01-database-workflows/06-cascading-deletes/03-sqlite.mdx @@ -85,7 +85,6 @@ update or delete on table "User" violates foreign key constraint "Post_author_fk Detail: Key (id)=(1) is still referenced from table "Post". ``` - + ## 3. Create two tables with a foreign key and `CASCADE` deletion behavior