Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guide for using SafeQL and extensions to help unsupported features. #5419

Merged
merged 34 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
8a415c1
add guide for GIS support
jharrell Nov 2, 2023
2bf1da1
Apply suggestions from code review
jharrell Nov 2, 2023
f522587
Merge branch 'main' into jharrell/issue5407
jharrell Nov 3, 2023
86a5e7a
Merge branch 'main' into jharrell/issue5407
jharrell Nov 6, 2023
917378e
Updates to structure
jharrell Nov 6, 2023
32c7a3a
Update content/300-guides/500-other/900-advanced-database-tasks/06-ty…
jharrell Nov 6, 2023
7edc4f4
Apply suggestions from code review
jharrell Nov 6, 2023
c7eab15
Apply suggestions from code review
jharrell Nov 7, 2023
f66ec88
PR feedback
jharrell Nov 7, 2023
5449afb
Update content/300-guides/500-other/900-advanced-database-tasks/06-ty…
jharrell Nov 7, 2023
7dfbcbb
Merge branch 'main' into jharrell/issue5407
jharrell Nov 7, 2023
8b45930
Apply suggestions from code review
jharrell Nov 8, 2023
983fcba
expand article
jharrell Nov 8, 2023
8749586
Update spellcheck.yml
jharrell Nov 8, 2023
fea51d1
typo
jharrell Nov 8, 2023
89b1278
Merge branch 'jharrell/issue5407' of github.com:prisma/docs into jhar…
jharrell Nov 8, 2023
710e72c
Merge branch 'main' into jharrell/issue5407
ruheni Nov 8, 2023
17b85e6
Merge branch 'main' into jharrell/issue5407
jharrell Nov 8, 2023
bfd9811
Cover SafeQL usage within a client extension
jharrell Nov 8, 2023
97e0b19
Merge branch 'main' into jharrell/issue5407
ruheni Nov 9, 2023
eced3dc
Apply suggestions from code review
jharrell Nov 9, 2023
4c4f79e
Update content/300-guides/500-other/900-advanced-database-tasks/06-ty…
nikolasburk Nov 15, 2023
4b95cc8
update safeql guide
nikolasburk Nov 21, 2023
516c18f
Merge branch 'main' into jharrell/issue5407
nikolasburk Nov 21, 2023
001f178
Update content/300-guides/500-other/900-advanced-database-tasks/06-ty…
nikolasburk Nov 22, 2023
34f59df
Update content/300-guides/500-other/900-advanced-database-tasks/06-ty…
nikolasburk Nov 22, 2023
9b101cb
Update content/300-guides/500-other/900-advanced-database-tasks/06-ty…
nikolasburk Nov 22, 2023
8be143b
Update content/300-guides/500-other/900-advanced-database-tasks/06-ty…
nikolasburk Nov 22, 2023
3b539d0
Update content/300-guides/500-other/900-advanced-database-tasks/06-ty…
nikolasburk Nov 22, 2023
a99d827
Update content/300-guides/500-other/900-advanced-database-tasks/06-ty…
nikolasburk Nov 22, 2023
246a375
Update content/300-guides/500-other/900-advanced-database-tasks/06-ty…
nikolasburk Nov 22, 2023
6ec2ad2
Update content/300-guides/500-other/900-advanced-database-tasks/06-ty…
nikolasburk Nov 22, 2023
5e39403
Update content/300-guides/500-other/900-advanced-database-tasks/06-ty…
nikolasburk Nov 22, 2023
fcab560
Update content/300-guides/500-other/900-advanced-database-tasks/06-ty…
nikolasburk Nov 22, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
title: 'Using SafeQL for unsupported features'
metaTitle: 'Using SafeQL for unsupported features (Guide)'
jharrell marked this conversation as resolved.
Show resolved Hide resolved
metaDescription: 'Learn how to use SafeQL and Prisma Client extensions to work around features not natively supported by Prisma.'
---

## Overview

This page explains how to use Prisma Client extensions and [SafeQL](https://safeql.dev) in order to add support for features not natively supported by Prisma.

Our example will be using [PostGIS](https://postgis.net/) and PostgreSQL, but is applicable to other features and database engines.

## Prerequisites

To follow this guide, we recommend:

- an existing [PostgreSQL](https://www.postgresql.org/) database
- an existing Prisma project

## 1. Set up Prisma for use with PostGIS

If you haven't already, update your Prisma schema to add the `postgis` PostgreSQL extension

```prisma
generator client {
provider = "prisma-client-js"
previewFeatures = ["postgresqlExtensions"]
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
extensions = [postgis]
}
```

Then, create a migration using `npx prisma migrate dev` in order to enable the extension. The migration file should look like the following:

```sql

-- CreateExtension
CREATE EXTENSION IF NOT EXISTS "postgis";

```

Make sure that the migration is applied!

## 2. Create a new model that uses a GIS column

After the migration is applied, you can now add a new model that has a GIS column. For our example we'll be using the following `PointOfInterest` model.

```prisma
model PointOfInterest {
id Int @id @default(autoincrement())
name String
location Unsupported("geography(Point, 4326)")
}
```

You'll notice that `location` is marked as `Unsupported`. This means that we lose a lot of the benefits of Prisma when working with `PointOfInterest`. We'll be using [SafeQL](https://safeql.dev/) to fix this.

Create a new migration with `npx prisma migrate dev` and apply it to add the `PointOfInterest` table to our database.

## 3. Integrate SafeQL

SafeQL is easily integrated with Prisma. Follow [SafeQL's integration guide](https://safeql.dev/compatibility/prisma.html) in order to lint `$queryRaw` and `$executeRaw` Prisma calls.
jharrell marked this conversation as resolved.
Show resolved Hide resolved
jharrell marked this conversation as resolved.
Show resolved Hide resolved

<Admonition>

Note that for SafeQL to run it will need to have access to your database. We recommend passing the same environment variable or connection string that is used by Prisma.

</Admonition>

## 4. Creating a GIS extension
jharrell marked this conversation as resolved.
Show resolved Hide resolved

Now that we have a model that uses GIS, let's make a Prisma Client extension in order to query this table. We will be making an extension that finds the closest points of interest to a given longitude and latitude.
jharrell marked this conversation as resolved.
Show resolved Hide resolved
nikolasburk marked this conversation as resolved.
Show resolved Hide resolved

```ts file=lib/db.ts
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient().$extends((client) => {
return client.$extends({
model: {
pointOfInterest: {
async findClosestPoints(
latitude: Number,
longitude: Number,
limit: Number
) {
return client.$queryRaw<
{ id: number; name: string; location: string }[]
>`SELECT * FROM "PointOfInterest" ORDER BY ST_DistanceSphere(location::geometry, ST_MakePoint(${longitude}, ${latitude})) LIMIT ${limit}`
},
},
},
})
})
jharrell marked this conversation as resolved.
Show resolved Hide resolved

export { prisma }
```

Now we can use our Prisma Client as normal in order to find close points of interest to a given longitude and latitude!

```ts file=app.ts
const closestPointOfInterest = await prisma.pointOfInterest.findClosestPoints(
myLatitude,
myLongitude,
1
)
```

We also have the benefit of SafeQL to add extra type safety to our raw queries. For example, if we removed the cast to `geometry` for `location`, we would get the linting error:

```terminal
error Invalid Query: function st_distancesphere(geography, geometry) does not exist @ts-safeql/check-sql
```

Which would let us know to check our function signature again.
jharrell marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: Advanced features using raw SQL
metaTitle: Advanced features using raw SQL (Guides)
metaDescription: Learn how to use raw SQL and Prisma Client extensions to cover areas not natively supported by Prisma.
toc: false
---

<TopBlock>

There are certain database features that are not natively supported by Prisma. In these cases, it's possible to use [Prisma Client extensions](/concepts/components/prisma-client/client-extensions) and tools like [SafeQL](https://safeql.dev/) in order to keep the great developer experience of Prisma when working with raw SQL.

</TopBlock>

## In this section

<Subsections />
Loading