Skip to content

Commit

Permalink
Merge branch 'main' into janpio-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
jharrell authored Jun 17, 2024
2 parents 7197cba + ae57130 commit db44330
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,11 @@ const getActiveUsers = await prisma.user.findMany({

> **Note**: It is not currently possible to [return the count of a relation](https://github.com/prisma/prisma/issues/5079).
### Sort by relevance (PostgreSQL)
### Sort by relevance (PostgreSQL and MySQL)

In [3.5.0](https://github.com/prisma/prisma/releases/3.5.0) and later, when using PostgreSQL you can sort records by relevance to the query using the `_relevance` keyword. This uses the relevance ranking functions from PostgreSQL's full text search feature, which are explained further in [the PostgreSQL documentation](https://www.postgresql.org/docs/12/textsearch-controls.html).
In [3.5.0+](https://github.com/prisma/prisma/releases/3.5.0) for PostgreSQL and [3.8.0+](https://github.com/prisma/prisma/releases/3.8.0) for MySQL, you can sort records by relevance to the query using the `_relevance` keyword. This uses the relevance ranking functions from full text search features.

This feature is further explain in [the PostgreSQL documentation](https://www.postgresql.org/docs/12/textsearch-controls.html) and [the MySQL documentation](https://dev.mysql.com/doc/refman/8.0/en/fulltext-search.html).

Enable order by relevance with the `fullTextSearch` [preview feature](/orm/prisma-client/queries/full-text-search):

Expand All @@ -416,6 +418,13 @@ const getUsersByRelevance = await prisma.user.findMany({
})
```

:::info

Enabling the `fullTextSearch` preview feature breaks the imports of the `<Model>OrderByWithRelationInput` TypeScript types. It is is renamed to `<Model>OrderByWithRelationAndSearchRelevanceInput`.

:::


### Sort with null records first or last

<Admonition type="info">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ The availability of advanced `Json` filtering depends on your Prisma version:

</Admonition>

### <inlinecode>path</inlinecode> syntax depending on database
### `path` syntax depending on database

The filters below use a `path` option to select specific parts of the `Json` value to filter on. The implementation of that filtering differs between connectors:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ metaDescription: 'Learn to configure your build process on Netlify to avoid cach

## Problem

If you deploy an application using Prisma ORM to [Netlify](https://netlify.com/), you may run into the following error message on deployment:
If you deploy an application using Prisma ORM to [Netlify](https://www.netlify.com/), you may run into the following error message on deployment:

```
Prisma has detected that this project was built on Netlify, which caches dependencies.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
---

title: 'Using the Nuxt Prisma Module'
metaTitle: 'Add Prisma ORM Easily to Your Nuxt Apps'
metaDescription: 'Learn how to easily add Prisma ORM to your Nuxt apps, use its features, and understand its limitations.'

---

The Nuxt Prisma module simplifies the integration of Prisma ORM into your Nuxt applications. This module provides several features to streamline the setup and usage of Prisma, making it easier to manage your database interactions.

### Features

- **Project initialization**: Automatically sets up a Prisma ORM project with a SQLite database within your Nuxt project.
- **Composable**: Provides an auto-imported `usePrismaClient()` composable for use in your Vue files.
- **API Route Integration**: Automatically imports an instance of `prisma` for use in your API routes.
- **Prisma Studio access**: Enables access to Prisma Studio through Nuxt Devtools for database management.

## Getting Started

1. Create a [new Nuxt Project](https://nuxt.com/docs/getting-started/installation#new-project):
```sh
npx nuxi@latest init test-nuxt-app
```

2. Navigate to project directory and install `@prisma/nuxt`:
```sh
cd test-nuxt-app
npm install @prisma/nuxt
```

3. Add `@prisma/nuxt` to your `nuxt.config.ts`:
```typescript
export default defineNuxtConfig({
modules: ["@prisma/nuxt"],
devtools: { enabled: true },
});
```

4. Start the development server:
```sh
npm run dev
```

Starting the development server will:

- Automatically install the [Prisma CLI](/orm/reference/prisma-cli-reference)
- Initialize a Prisma project with SQLite
- Create an `User` and `Post` example model in the Prisma Schema
- Prompt you to run a migration to create database tables with [Prisma Migrate](/orm/prisma-migrate/understanding-prisma-migrate/overview)
- Install and generate a [Prisma Client](/orm/reference/prisma-client-reference)
- Prompt you to start the [Prisma Studio](/orm/tools/prisma-studio)

5. You can now use Prisma ORM in your project. If you accepted the prompt to add Prisma Studio, you can access Prisma Studio through the Nuxt Devtools. See the [usage section](#usage) to learn how to use Prisma Client in your app.

## Usage

### Option A: `usePrismaClient` composable

#### Using the composable in your Nuxt server component

If you're using [Nuxt server components](https://nuxt.com/docs/guide/directory-structure/components#server-components), you can use the global instance of the Prisma Client in your `.server.vue` files:
```html
<script setup>
const prisma = usePrismaClient()
const user = await prisma.user.findFirst()
</script>
<template>
<p>{{ user.name }}</p>
</template>
```
#### Using the auto-imported Prisma Client instance in your API route
You can use the auto-imported Prisma Client instance, prisma, in your Nuxt API route as follows:
```typescript
export default defineEventHandler(async (event) => {
return {
user: await prisma.user.findFirst(),
};
});
```
### Option B: `lib/prisma.ts`
After running through the initial setup prompts, this module creates the `lib/prisma.ts` file which contains a global instance of Prisma Client.
```typescript
import { PrismaClient } from '@prisma/client'
const prismaClientSingleton = () => {
return new PrismaClient()
}
declare const globalThis: {
prismaGlobal: ReturnType<typeof prismaClientSingleton>;
} & typeof global;
const prisma = globalThis.prismaGlobal ?? prismaClientSingleton()
export default prisma
if (process.env.NODE_ENV !== 'production') globalThis.prismaGlobal = prisma
```
You can customize Prisma Client's capabilities by using client extensions in your `lib/prisma.ts` file. Here is an example using the [Pulse client extension](https://www.npmjs.com/package/@prisma/extension-pulse):
```typescript
import { PrismaClient } from '@prisma/client'
import { withPulse } from '@prisma/extension-pulse'
const prismaClientSingleton = () => {
return new PrismaClient().$extends(withPulse({
apiKey: process.env.PULSE_API_KEY
}))
}
declare const globalThis: {
prismaGlobal: ReturnType<typeof prismaClientSingleton>;
} & typeof global;
const prisma = globalThis.prismaGlobal ?? prismaClientSingleton()
export default prisma
if (process.env.NODE_ENV !== 'production') globalThis.prismaGlobal = prisma
```
#### Using the global Prisma Client instance in your API route
You can use the global instance of the Prisma Client in your Nuxt API route as follows:
```typescript
import prisma from "~/lib/prisma";
export default defineEventHandler(async (event) => {
return {
user: await prisma.user.findFirst(),
};
});
```
#### Using the global Prisma Client instance in your Nuxt server component
If you're using [Nuxt server components](https://nuxt.com/docs/guide/directory-structure/components#server-components), you can use the global instance of the Prisma Client `.server.vue` files:
```html
<script setup>
import prisma from '~/lib/prisma';
const user = await prisma.user.findFirst()
</script>
<template>
<p>{{ user.name }}</p>
</template>
```
80 changes: 16 additions & 64 deletions content/400-pulse/400-api-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,8 @@ You can pass an object with configuration options to `stream()`. The object has

When called with no filter arguments, the `stream()` method returns the following type:

<TabbedContent code>
<TabItem value="Type">

```ts
PulseSubscription<
```ts no-copy
const stream: PulseSubscription<
| PulseCreateEvent<{
id: number;
name: string | null;
Expand All @@ -73,44 +70,23 @@ PulseSubscription<
name: string | null;
email: string;
}>
>;
> = await prisma.user.stream();
```

</TabItem>
<TabItem value="No filter arguments">
```ts
const stream = await prisma.user.stream()
```

</TabItem>
</TabbedContent>

Depending on the arguments you provide, the return type may change. For example, if you filter for only `create` events, the type will get adjusted:

<TabbedContent code>
<TabItem value="Type">

```ts
PulseSubscription<
```ts no-copy
const stream: PulseSubscription<
PulseCreateEvent<{
id: number;
email: string;
name: string | null;
}>
>;
```

</TabItem>
<TabItem value="Filter for `create` events">
```ts
const stream = await prisma.user.stream({
create: {}
})
> = await prisma.user.stream({
create: {},
});
```

</TabItem>
</TabbedContent>

### Examples

#### Use a `name` to be able to "resume" the stream
Expand Down Expand Up @@ -190,11 +166,8 @@ You can pass an object with configuration options to `subscribe()`. The object h

When called with no filter arguments, the `subscribe()` method returns the following type:

<TabbedContent code>
<TabItem value="Type">

```ts
PulseSubscription<
```ts no-copy
const subscription: PulseSubscription<
| PulseCreateEvent<{
id: number;
name: string | null;
Expand All @@ -210,44 +183,23 @@ PulseSubscription<
name: string | null;
email: string;
}>
>;
> = await prisma.user.subscribe();
```

</TabItem>
<TabItem value="No filter arguments">
```ts
const subscription = await prisma.user.subscribe()
```

</TabItem>
</TabbedContent>

Depending on the arguments you provide, the return type may change. For example, if you filter for only `create` events, the type will get adjusted:

<TabbedContent code>
<TabItem value="Type">

```ts
PulseSubscription<
```ts no-copy
const subscription: PulseSubscription<
PulseCreateEvent<{
id: number;
email: string;
name: string | null;
}>
>;
```

</TabItem>
<TabItem value="Filter for `create` events">
```ts
const subscription = await prisma.user.subscribe({
create: {}
})
> = await prisma.user.subscribe({
create: {},
});
```

</TabItem>
</TabbedContent>

### Examples

#### Filter for new `User` records with a non-null value for `name`
Expand Down
Loading

0 comments on commit db44330

Please sign in to comment.