Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 51 additions & 70 deletions content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,29 @@ This guide describes how to seed your database using Prisma Client and Prisma OR

## How to seed your database in Prisma ORM

Prisma ORM's integrated seeding functionality expects a command in the `"seed"` key in the `"prisma"` key of your `package.json` file. This can be any command, `prisma db seed` will just execute it. In this guide and as a default, we recommend writing a seed script inside your project's `prisma/` folder and starting it with the command.

<TabbedContent>
<TabItem value="TypeScript">
```json
"prisma": {
"seed": "ts-node prisma/seed.ts"
},
Prisma ORM's integrated seeding functionality expects a command in the `"seed"` key in the `migrations` object of your `prisma.config.ts`. This can be any command, `prisma db seed` will just execute it. In this guide and as a default, we recommend writing a seed script inside your project's `prisma/` folder and starting it with the command.

```ts
import 'dotenv/config'
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
seed: "tsx prisma/seed.ts"
},
datasource: {
url: env("DATABASE_URL"),
},
});
```
<br/>

:::info

With TypeScript,`ts-node` does transpiling and typechecking by default; typechecking can be disabled with the following flag `--transpile-only`.

Example:
`"seed": "ts-node --transpile-only prisma/seed.ts"`

This can be useful to reduce memory usage (RAM) and increase execution speed of the seed script.

:::

</TabItem>

<TabItem value="JavaScript">
```json
"prisma": {
"seed": "node prisma/seed.js"
},
```
</TabItem>

</TabbedContent>

## Integrated seeding with Prisma Migrate

Database seeding happens in two ways with Prisma ORM: manually with `prisma db seed` and automatically in `prisma migrate reset` and (in some scenarios) `prisma migrate dev`.
Database seeding happens when you run `prisma db seed`. With `prisma db seed`, _you_ decide when to invoke the seed command. It can be useful for a test setup or to prepare a new development environment, for example.

With `prisma db seed`, _you_ decide when to invoke the seed command. It can be useful for a test setup or to prepare a new development environment, for example.

### Prisma 6 Only
Prisma Migrate also integrates seamlessly with your seeds, assuming you follow the steps in the section below. Seeding is triggered automatically when Prisma Migrate resets the development database.

Prisma Migrate resets the database and triggers seeding in the following scenarios:
Expand Down Expand Up @@ -153,37 +136,28 @@ Here we suggest some specific seed scripts for different situations. You are fre
})
```

3. Add `typescript`, `ts-node` and `@types/node` development dependencies:
3. Add `typescript`, `tsx` and `@types/node` development dependencies:
```
npm install -D typescript ts-node @types/node
npm install -D typescript tsx @types/node
```

<a id="compiler-options"></a>

4. Add the `prisma.seed` field to your `package.json` file:
```json file=package.json highlight=5;normal
{
"name": "my-project",
"version": "1.0.0",
"prisma": {
"seed": "ts-node prisma/seed.ts"
},
"devDependencies": {
"@types/node": "^14.14.21",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
}
}
4. Add the `seed` field to your `prisma.config.ts` file:
```ts file=prisma.config.ts
import 'dotenv/config'
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
// add-start
seed: "tsx prisma/seed.ts"
// add-end
},
datasource: {
url: env("DATABASE_URL"),
},
});
```

Some projects may require you to add compile options. When using Next.js for example, you would setup your seed script like so:

```json file=package.json
"prisma": {
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
},
```

5. To seed the database, run the `db seed` CLI command:
```
npx prisma db seed
Expand Down Expand Up @@ -281,15 +255,22 @@ Here we suggest some specific seed scripts for different situations. You are fre
})
```

3. Add the `prisma.seed` to your `package.json` file:
```json file=package.json highlight=5;normal
{
"name": "my-project",
"version": "1.0.0",
"prisma": {
"seed": "node prisma/seed.js"
}
}
3. Add the `seed` field to your `prisma.config.ts` file:
```ts file=prisma.config.ts
import 'dotenv/config'
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
// add-start
seed: "node prisma/seed.js"
// add-end
},
datasource: {
url: env("DATABASE_URL"),
},
});
```

4. To seed the database, run the `db seed` CLI command:
Expand Down
123 changes: 122 additions & 1 deletion content/200-orm/500-reference/380-connection-urls.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ tocDepth: 3

Prisma ORM needs a connection URL to be able to connect to your database, e.g. when sending queries with [Prisma Client](/orm/prisma-client) or when changing the database schema with [Prisma Migrate](/orm/prisma-migrate).

The connection URL is provided via the `url` field of a `datasource` block in your Prisma schema. It usually consists of the following components (except for SQLite and [Prisma Postgres](/postgres)):
The connection URL is provided via the `url` field of a `datasource` block in your Prisma config (or Prisma schema if on version 6). It usually consists of the following components (except for SQLite and [Prisma Postgres](/postgres)):

- **User**: The name of your database user
- **Password**: The password for your database user
Expand Down Expand Up @@ -66,82 +66,203 @@ DATABASE_URL="postgres://2f9881cc7eef46f094ac913df34c1fb441502fe66cbe28cc48998d4

When connecting via Prisma Accelerate, the connection string doesn't require a user/password like a conventional connection string does. Instead, authentication works via an API key:

<TabbedContent code>
<TabItem value="Prisma 7">
```ts file=prisma.config.ts
export default defineConfig({
datasource: {
url: "prisma+postgres://accelerate.prisma-data.net/?api_key=API_KEY"
},
});
```
</TabItem>
<TabItem value="Prisma 6">
```prisma file=schema.prisma
datasource db {
provider = "postgresql"
url = "prisma+postgres://accelerate.prisma-data.net/?api_key=API_KEY"
}
```
</TabItem>

</TabbedContent>

In this snippet, `API_KEY` is a placeholder for the API key you are receiving when setting up a new Prismas Postgres instance via the [Prisma Console](https://console.prisma.io). Here is an example for what a real connection URL to Prisma Postgres may look like:

<TabbedContent code>
<TabItem value="Prisma 7">
```ts file=prisma.config.ts
export default defineConfig({
datasource: {
url: "prisma+postgres://accelerate.prisma-data.net/?api_key=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGlfa2V5IjoiMGNkZTFlMjQtNzhiYi00NTY4LTkyM2EtNWUwOTEzZWUyNjU1IiwidGVuYW50X2lkIjoiNzEyZWRlZTc1Y2U2MDk2ZjI4NDg3YjE4NWMyYzA2OTNhNGMxNzJkMjhhOWFlNGUwZTYxNWE4NWIxZWY1YjBkMCIsImludGVybmFsX3NlY3JldCI6IjA4MzQ2Y2RlLWI5ZjktNDQ4Yy04NThmLTMxNjg4ODEzNmEzZCJ9.N1Za6q6NfInzHvRkud6Ojt_-RFg18a0601vdYWGKOrk"
},
});
```
</TabItem>
<TabItem value="Prisma 6">
```prisma file=schema.prisma
datasource db {
provider = "postgresql"
url = "prisma+postgres://accelerate.prisma-data.net/?api_key=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcGlfa2V5IjoiMGNkZTFlMjQtNzhiYi00NTY4LTkyM2EtNWUwOTEzZWUyNjU1IiwidGVuYW50X2lkIjoiNzEyZWRlZTc1Y2U2MDk2ZjI4NDg3YjE4NWMyYzA2OTNhNGMxNzJkMjhhOWFlNGUwZTYxNWE4NWIxZWY1YjBkMCIsImludGVybmFsX3NlY3JldCI6IjA4MzQ2Y2RlLWI5ZjktNDQ4Yy04NThmLTMxNjg4ODEzNmEzZCJ9.N1Za6q6NfInzHvRkud6Ojt_-RFg18a0601vdYWGKOrk"
}
```
</TabItem>

</TabbedContent>

#### Local Prisma Postgres

The connection string for connecting to a [local Prisma Postgres](/postgres/database/local-development) instance mirrors the structure of a remote instance via Accelerate:


<TabbedContent code>
<TabItem value="Prisma 7">
```ts file=prisma.config.ts
export default defineConfig({
datasource: {
url: "prisma+postgres://accelerate.prisma-data.net/?api_key=API_KEY"
},
});
```
</TabItem>
<TabItem value="Prisma 6">
```prisma file=schema.prisma
datasource db {
provider = "postgresql"
url = "prisma+postgres://accelerate.prisma-data.net/?api_key=API_KEY"
}
```
</TabItem>

</TabbedContent>

However, in this case the `API_KEY` doesn't provide authentication details. Instead, it encodes information about the local Prisma Postgres instance. You can obtain a local connection string via the [`prisma dev`](/orm/reference/prisma-cli-reference#dev) command.

### PostgreSQL


<TabbedContent code>
<TabItem value="Prisma 7">
```ts file=prisma.config.ts
export default defineConfig({
datasource: {
url: "postgresql://janedoe:mypassword@localhost:5432/mydb?schema=sample"
},
});
```
</TabItem>

<TabItem value="Prisma 6">
```prisma file=schema.prisma
datasource db {
provider = "postgresql"
url = "postgresql://janedoe:mypassword@localhost:5432/mydb?schema=sample"
}
```
</TabItem>

</TabbedContent>

### MySQL


<TabbedContent code>
<TabItem value="Prisma 7">
```ts file=prisma.config.ts
export default defineConfig({
datasource: {
url: "mysql://janedoe:mypassword@localhost:3306/mydb"
},
});
```
</TabItem>
<TabItem value="Prisma 6">
```prisma file=schema.prisma
datasource db {
provider = "mysql"
url = "mysql://janedoe:mypassword@localhost:3306/mydb"
}
```
</TabItem>

</TabbedContent>

### Microsoft SQL Server


<TabbedContent code>
<TabItem value="Prisma 7">
```ts file=prisma.config.ts
export default defineConfig({
datasource: {
url: "sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;"
},
});
```
</TabItem>
<TabItem value="Prisma 6">
```prisma file=schema.prisma
datasource db {
provider = "sqlserver"
url = "sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;"
}
```
</TabItem>

</TabbedContent>

### SQLite


<TabbedContent code>
<TabItem value="Prisma 7">
```ts file=prisma.config.ts
export default defineConfig({
datasource: {
url: "file:./dev.db"
},
});
```
</TabItem>
<TabItem value="Prisma 6">
```prisma file=schema.prisma
datasource db {
provider = "sqlite"
url = "file:./dev.db"
}
```
</TabItem>

</TabbedContent>

### CockroachDB


<TabbedContent code>
<TabItem value="Prisma 7">
```ts file=prisma.config.ts
export default defineConfig({
datasource: {
url: "postgresql://janedoe:mypassword@localhost:26257/mydb?schema=public"
},
});
```
</TabItem>
<TabItem value="Prisma 6">
```prisma file=schema.prisma
datasource db {
provider = "cockroachdb"
url = "postgresql://janedoe:mypassword@localhost:26257/mydb?schema=public"
}
```
</TabItem>

</TabbedContent>

### MongoDB

_Support for MongoDB is limited to Prisma 6. We're working on support for MongoDB in Prisma 7_

```prisma file=schema.prisma
datasource db {
provider = "mongodb"
Expand Down
4 changes: 4 additions & 0 deletions content/800-guides/070-cloudflare-d1.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,15 @@ export default {

const users = await prisma.user.findMany();
const result = JSON.stringify(users);
ctx.waitUntil(prisma.$disconnect()); // or just await prisma.$disconnect()
return new Response(result);
},
};
```

We explicitly call `prisma.$disconnect()` here to guarantee timely release of resources or else the
worker might run out of memory.

## 7. Run the Worker locally

With the database query in place and Prisma Client generated, you can run the Worker locally.
Expand Down
Loading