Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur-arch authored Nov 28, 2024
2 parents 2e8b5f8 + c050161 commit 1cc285e
Show file tree
Hide file tree
Showing 11 changed files with 2,295 additions and 1,663 deletions.
4 changes: 1 addition & 3 deletions content/100-getting-started/01-quickstart-prismaPostgres.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ First, rename the existing `.env.example` file to just `.env`:
mv .env.example .env
```

Next, navigate back into the Console and click the **Generate database credentials** button.

Then, copy the resulting `DATABASE_URL` and `PULSE_API_KEY` environment variables and paste it into the `.env` file.
Then, find your database credentials in the **Set up database access** section, copy the `DATABASE_URL` and `PULSE_API_KEY` environment variables and paste them into the `.env` file.

For reference, the file should now look similar to this:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ At this point, you'll be redirected to the **Dashboard** where you will need to

Once the green **`CONNECTED`** label appears, your database is ready to use.

In the Console UI, you'll see a code snippet for a `.env` file with two environment variables defined, looking similar to this:

**TODO:** Add screenshot
In the Console UI, you'll see a code snippet for a `.env` file with two environment variables defined.

## Set environment variables in your local project

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ generator client {

Ordering by relevance can be used either separately from or together with the `search` filter: `_relevance` is used to order the list, while `search` filters the unordered list.

For example, the following query uses `_relevance` to filter by the term `developer` in the `bio` field, and then sorts the result by relevance in an ascending manner:
For example, the following query uses `_relevance` to filter by the term `developer` in the `bio` field, and then sorts the result by relevance in a _descending_ manner:

```ts
const getUsersByRelevance = await prisma.user.findMany({
Expand All @@ -410,7 +410,7 @@ const getUsersByRelevance = await prisma.user.findMany({
_relevance: {
fields: ['bio'],
search: 'developer',
sort: 'asc',
sort: 'desc',
},
},
})
Expand All @@ -427,15 +427,15 @@ Prior to Prisma ORM 5.16.0, enabling the `fullTextSearch` preview feature would

### Sort with null records first or last

<Admonition type="info">
:::info

Notes:

- This feature is generally available in version `4.16.0` and later. To use this feature in versions [`4.1.0`](https://github.com/prisma/prisma/releases/tag/4.1.0) to [`4.15.0`](https://github.com/prisma/prisma/releases/tag/4.15.0) the [Preview feature](/orm/reference/preview-features/client-preview-features#enabling-a-prisma-client-preview-feature) `orderByNulls` will need to be enabled.
- This feature is not available for MongoDB.
- You can only sort by nulls on optional [scalar](/orm/prisma-schema/data-model/models#scalar-fields) fields. If you try to sort by nulls on a required or [relation](/orm/prisma-schema/data-model/models#relation-fields) field, Prisma Client throws a [P2009 error](/orm/reference/error-reference#p2009).

</Admonition>
:::

You can sort the results so that records with `null` fields appear either first or last.

Expand Down
30 changes: 30 additions & 0 deletions content/200-orm/200-prisma-client/100-queries/058-transactions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,36 @@ To avoid transaction write conflicts and deadlocks on a transaction:
}
```

### Using `$transaction` within `Promise.all()`

If you wrap a `$transaction` inside a call to `Promise.all()`, the queries inside the transaction will be executed _serially_ (i.e. one after another):

```ts
await prisma.$transaction(async (prisma) => {
await Promise.all([
prisma.user.findMany(),
prisma.user.findMany(),
prisma.user.findMany(),
prisma.user.findMany(),
prisma.user.findMany(),
prisma.user.findMany(),
prisma.user.findMany(),
prisma.user.findMany(),
prisma.user.findMany(),
prisma.user.findMany(),
])
})
```

This may be counterintuitive because `Promise.all()` usually _parallelizes_ the calls passed into it.

The reason for this behaviour is that:
- One transaction means that all queries inside it have to be run on the same connection.
- A database connection can only ever execute one query at a time.
- As one query blocks the connection while it is doing its work, putting a transaction into `Promise.all` effectively means that queries should be ran one after another.



## Dependent writes

Writes are considered **dependent** on each other if:
Expand Down
122 changes: 1 addition & 121 deletions content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ Here we suggest some specific seed scripts for different situations. You are fre

### Seeding your database with TypeScript or JavaScript

> If you're using TypeScript with PostgreSQL, SQLite or MySQL see also: [@snaplet/seed](/orm/prisma-migrate/workflows/seeding#seeding-your-database-with-snapletseed).
<TabbedContent transparent code>

<TabItem value="TypeScript">
Expand Down Expand Up @@ -385,124 +383,6 @@ psql file.sql

</TabbedContent>

### Seeding your database with `@snaplet/seed`

[`@snaplet/seed`](https://docs.snaplet.dev/seed/getting-started/overview) offers a toolkit designed to understand your database schema, enabling you to generate production-accurate data and efficiently seed your database. It uses the full power of TypeScript to provide you with a type-safe and auto-completed experience to write your seed scripts.

`@snaplet/seed` supports PostgreSQL, SQLite and MySQL.

1. Begin by setting up a local database using `npx prisma migrate dev`. This example will use the following Prisma schema featuring a `User` and `Post` model:

```prisma file=schema.prisma
model User {
id Int @id @default(autoincrement())
email String @unique
name String
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String
published Boolean
user User @relation(fields: [userId], references: [id])
userId Int
}
```

2. Execute `npx @snaplet/seed init prisma/seed` to initialize Snaplet Seed in your Prisma project. This command generates the seed client and creates an example seed file at `prisma/seed/seed.ts`.

3. Use the generated `@snaplet/seed` client within the `prisma/seed/seed.ts` file to compose your seeds.

Here's how to create new users and posts by modifying the initially generated `prisma/seed/seed.ts` file:

```ts file=prisma/seed/seed.ts
/**
* ! Executing this script will delete all data in your database and seed it with 10 users.
* ! Make sure to adjust the script to your needs.
* Use any TypeScript runner to run this script, for example: `npx tsx seed.ts`
* Learn more about the Seed Client by following our guide: https://docs.snaplet.dev/seed/getting-started
*/
import { createSeedClient } from "@snaplet/seed";

async function main() {
const seed = await createSeedClient();

// Truncate all tables in the database
await seed.$resetDatabase();

//highlight-start
// Seed the database with 10 users
await seed.user((createMany) => createMany(10, {
// Create 10 posts for each of those users
posts: (createMany) => createMany(10),
}))
//highlight-end

console.log("Database seeded successfully!");

process.exit();
};

main();
```

4. Add `tsx` (or any other typescript runners) as a development dependency:

```terminal
npm install -D tsx
```

5. Insert the `prisma.seed` field into your `package.json` file and configure commands to seed your database:

```json file=package.json
{
"name": "my-project",
"version": "1.0.0",
"prisma": {
//add-next-line
"seed": "tsx prisma/seed/seed.ts"
},
"devDependencies": {
"@types/node": "^14.14.21",
"tsx": "^4.7.2",
"typescript": "^4.1.3"
}
}
```

6. To seed the database, execute the following CLI command:

```terminal
npx prisma db seed
```

7. To ensure the seed client remains synchronized with your schema, add a `migrate` and `postmigrate` scripts to the package.json:

```json file=package.json
{
"name": "my-project",
"version": "1.0.0",
"prisma": {
"seed": "npx tsx prisma/seed/seed.ts"
},
"scripts": {
//add-start
"migrate": "prisma migrate dev",
"postmigrate": "npx @snaplet/seed sync"
//add-end
},
"devDependencies": {
"@types/node": "^14.14.21",
"tsx": "^4.7.2",
"typescript": "^4.1.3"
}
}
```

Now, instead of `npx prisma migrate dev` you can run `npm run migrate` which will also sync the seed client after schema changes and keep both your database and seed client in sync.

### User-defined arguments

> This feature is available from version 4.15.0 and later.
Expand Down Expand Up @@ -556,5 +436,5 @@ npx prisma db seed -- --environment development

Here's a non-exhaustive list of other tools you can integrate with Prisma ORM in your development workflow to seed your database:

- [Snaplet](https://docs.snaplet.dev/recipes/prisma)
- [Supabase community project](https://github.com/supabase-community/seed)
- [Replibyte](https://www.replibyte.com/docs/introduction/)
Loading

0 comments on commit 1cc285e

Please sign in to comment.