Skip to content

Commit

Permalink
feat(fts): engine integration (#8849)
Browse files Browse the repository at this point in the history
Co-authored-by: Joël Galeran <Jolg42@users.noreply.github.com>
Co-authored-by: Joël <joel.galeran@gmail.com>
  • Loading branch information
3 people committed Aug 24, 2021
1 parent c0c551f commit 120f04c
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 85 deletions.
6 changes: 3 additions & 3 deletions packages/cli/package.json
Expand Up @@ -63,9 +63,9 @@
"devDependencies": {
"@prisma/client": "workspace:*",
"@prisma/debug": "workspace:*",
"@prisma/fetch-engine": "2.30.0-22.6ee261c643b2c461a20371f48ffa5090ed578aaf",
"@prisma/fetch-engine": "2.30.0-28.60b19f4a1de4fe95741da371b4c44a92f4d1adcb",
"@prisma/generator-helper": "workspace:*",
"@prisma/get-platform": "2.30.0-22.6ee261c643b2c461a20371f48ffa5090ed578aaf",
"@prisma/get-platform": "2.30.0-28.60b19f4a1de4fe95741da371b4c44a92f4d1adcb",
"@prisma/migrate": "workspace:*",
"@prisma/sdk": "workspace:*",
"@prisma/studio-server": "0.422.0",
Expand Down Expand Up @@ -124,7 +124,7 @@
"precommit": "lint-staged"
},
"dependencies": {
"@prisma/engines": "2.30.0-22.6ee261c643b2c461a20371f48ffa5090ed578aaf"
"@prisma/engines": "2.30.0-28.60b19f4a1de4fe95741da371b4c44a92f4d1adcb"
},
"lint-staged": {
"*.ts": [
Expand Down
8 changes: 4 additions & 4 deletions packages/client/package.json
Expand Up @@ -61,10 +61,10 @@
"devDependencies": {
"@prisma/debug": "workspace:*",
"@prisma/engine-core": "workspace:*",
"@prisma/engines": "2.30.0-22.6ee261c643b2c461a20371f48ffa5090ed578aaf",
"@prisma/fetch-engine": "2.30.0-22.6ee261c643b2c461a20371f48ffa5090ed578aaf",
"@prisma/engines": "2.30.0-28.60b19f4a1de4fe95741da371b4c44a92f4d1adcb",
"@prisma/fetch-engine": "2.30.0-28.60b19f4a1de4fe95741da371b4c44a92f4d1adcb",
"@prisma/generator-helper": "workspace:*",
"@prisma/get-platform": "2.30.0-22.6ee261c643b2c461a20371f48ffa5090ed578aaf",
"@prisma/get-platform": "2.30.0-28.60b19f4a1de4fe95741da371b4c44a92f4d1adcb",
"@prisma/migrate": "workspace:*",
"@prisma/sdk": "workspace:*",
"@timsuchanek/copy": "1.4.5",
Expand Down Expand Up @@ -128,7 +128,7 @@
}
},
"dependencies": {
"@prisma/engines-version": "2.30.0-22.6ee261c643b2c461a20371f48ffa5090ed578aaf"
"@prisma/engines-version": "2.30.0-28.60b19f4a1de4fe95741da371b4c44a92f4d1adcb"
},
"lint-staged": {
"*.ts": [
Expand Down
@@ -0,0 +1,23 @@
datasource db {
provider = "postgresql"
url = env("TEST_POSTGRES_URI")
}

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

model User {
id Int @id @default(autoincrement())
email String @unique
name String
posts Post[]
}

model Post {
id Int @id @default(autoincrement())
title String
user User @relation(fields: [userId], references: [id])
userId Int
}
@@ -0,0 +1,223 @@
import path from 'path'
import { generateTestClient } from '../../../../utils/getTestClient'
import { tearDownPostgres } from '../../../../utils/setupPostgres'
import { migrateDb } from '../../__helpers__/migrateDb'

// @ts-ignore trick to get typings at dev time
import type { PrismaClient } from './node_modules/@prisma/client'

let prisma: PrismaClient
const baseUri = process.env.TEST_POSTGRES_URI
describe('full-text-search (postgres)', () => {
beforeAll(async () => {
process.env.TEST_POSTGRES_URI += '-full-text-search'
await tearDownPostgres(process.env.TEST_POSTGRES_URI!)
await migrateDb({
connectionString: process.env.TEST_POSTGRES_URI!,
schemaPath: path.join(__dirname, 'schema.prisma'),
})
await generateTestClient(__dirname)
const { PrismaClient } = await require('./node_modules/@prisma/client')

prisma = new PrismaClient()

await prisma.user.create({
data: {
email: 'email1@email.io',
name: '0 1 2 3 4 5 6 7 8',
},
})

await prisma.user.create({
data: {
email: 'email2@email.io',
name: '0 2 4 6 8',
},
})

await prisma.user.create({
data: {
email: 'email3@email.io',
name: '1 3 5 7 9',
},
})
})

afterAll(async () => {
await prisma.user.deleteMany()
await prisma.$disconnect()
process.env.TEST_POSTGRES_URI = baseUri
})

/**
* Test search with the `&` operator
*/
test('findMany with &', async () => {
const result = await prisma.user.findMany({
where: {
name: {
search: '1 & 2',
},
},
})

expect(result).toMatchInlineSnapshot(`
Array [
Object {
email: email1@email.io,
id: 1,
name: 0 1 2 3 4 5 6 7 8,
},
]
`)
})

/**
* Test search with the `|` operator
*/
test('findMany with |', async () => {
const result = await prisma.user.findMany({
where: {
name: {
search: '1 | 2',
},
},
})

expect(result).toMatchInlineSnapshot(`
Array [
Object {
email: email1@email.io,
id: 1,
name: 0 1 2 3 4 5 6 7 8,
},
Object {
email: email2@email.io,
id: 2,
name: 0 2 4 6 8,
},
Object {
email: email3@email.io,
id: 3,
name: 1 3 5 7 9,
},
]
`)
})

/**
* Test search with the `<->` operator
*/
test('findMany with <->', async () => {
const result = await prisma.user.findMany({
where: {
name: {
search: '4 <-> 5',
},
},
})

expect(result).toMatchInlineSnapshot(`
Array [
Object {
email: email1@email.io,
id: 1,
name: 0 1 2 3 4 5 6 7 8,
},
]
`)
})

/**
* Test search with the `|` + `&` operators
*/
test('findMany with & and |', async () => {
const result = await prisma.user.findMany({
where: {
name: {
search: '(0 | 1) & (2 | 3)',
},
},
})

expect(result).toMatchInlineSnapshot(`
Array [
Object {
email: email1@email.io,
id: 1,
name: 0 1 2 3 4 5 6 7 8,
},
Object {
email: email2@email.io,
id: 2,
name: 0 2 4 6 8,
},
Object {
email: email3@email.io,
id: 3,
name: 1 3 5 7 9,
},
]
`)
})

/**
* Test search with the `!` operator
*/
test('findMany with !', async () => {
const result = await prisma.user.findMany({
where: {
name: {
search: '0 & !1',
},
},
})

expect(result).toMatchInlineSnapshot(`
Array [
Object {
email: email2@email.io,
id: 2,
name: 0 2 4 6 8,
},
]
`)
})

/**
* Get an array of empty results
*/
test('no results', async () => {
const result = await prisma.user.findMany({
where: {
name: {
search: '!0 & !1',
},
},
})

expect(result).toMatchInlineSnapshot(`Array []`)
})

/**
* Use an invalid operator
*/
test('bad operator', async () => {
const result = prisma.user.findMany({
where: {
name: {
search: '0 1',
},
},
})

await expect(result).rejects.toThrowErrorMatchingInlineSnapshot(`
Invalid \`prisma.user.findMany()\` invocation:
Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(Error { kind: Db, cause: Some(DbError { severity: "ERROR", parsed_severity: Some(Error), code: SqlState("42601"), message: "syntax error in tsquery: \\"0 1\\"", detail: None, hint: None, position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("tsquery.c"), line: Some(514), routine: Some("makepol") }) }) })
`)
})
})
4 changes: 2 additions & 2 deletions packages/engine-core/package.json
Expand Up @@ -45,9 +45,9 @@
},
"dependencies": {
"@prisma/debug": "workspace:*",
"@prisma/engines": "2.30.0-22.6ee261c643b2c461a20371f48ffa5090ed578aaf",
"@prisma/engines": "2.30.0-28.60b19f4a1de4fe95741da371b4c44a92f4d1adcb",
"@prisma/generator-helper": "workspace:*",
"@prisma/get-platform": "2.30.0-22.6ee261c643b2c461a20371f48ffa5090ed578aaf",
"@prisma/get-platform": "2.30.0-28.60b19f4a1de4fe95741da371b4c44a92f4d1adcb",
"chalk": "4.1.2",
"execa": "5.1.1",
"get-stream": "6.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/integration-tests/package.json
Expand Up @@ -9,7 +9,7 @@
"repository": "git@github.com:prisma/prisma.git",
"devDependencies": {
"@prisma/client": "workspace:*",
"@prisma/get-platform": "2.30.0-22.6ee261c643b2c461a20371f48ffa5090ed578aaf",
"@prisma/get-platform": "2.30.0-28.60b19f4a1de4fe95741da371b4c44a92f4d1adcb",
"@prisma/migrate": "workspace:*",
"@prisma/sdk": "workspace:*",
"@sindresorhus/slugify": "1.1.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/migrate/package.json
Expand Up @@ -16,7 +16,7 @@
"version": "latest"
},
"devDependencies": {
"@prisma/engines-version": "2.30.0-22.6ee261c643b2c461a20371f48ffa5090ed578aaf",
"@prisma/engines-version": "2.30.0-28.60b19f4a1de4fe95741da371b4c44a92f4d1adcb",
"@prisma/generator-helper": "workspace:*",
"@prisma/sdk": "workspace:*",
"@types/jest": "27.0.1",
Expand Down Expand Up @@ -55,7 +55,7 @@
},
"dependencies": {
"@prisma/debug": "workspace:*",
"@prisma/get-platform": "2.30.0-22.6ee261c643b2c461a20371f48ffa5090ed578aaf",
"@prisma/get-platform": "2.30.0-28.60b19f4a1de4fe95741da371b4c44a92f4d1adcb",
"@sindresorhus/slugify": "1.1.2",
"execa": "5.1.1",
"global-dirs": "3.0.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/package.json
Expand Up @@ -53,10 +53,10 @@
"dependencies": {
"@prisma/debug": "workspace:*",
"@prisma/engine-core": "workspace:*",
"@prisma/engines": "2.30.0-22.6ee261c643b2c461a20371f48ffa5090ed578aaf",
"@prisma/fetch-engine": "2.30.0-22.6ee261c643b2c461a20371f48ffa5090ed578aaf",
"@prisma/engines": "2.30.0-28.60b19f4a1de4fe95741da371b4c44a92f4d1adcb",
"@prisma/fetch-engine": "2.30.0-28.60b19f4a1de4fe95741da371b4c44a92f4d1adcb",
"@prisma/generator-helper": "workspace:*",
"@prisma/get-platform": "2.30.0-22.6ee261c643b2c461a20371f48ffa5090ed578aaf",
"@prisma/get-platform": "2.30.0-28.60b19f4a1de4fe95741da371b4c44a92f4d1adcb",
"@timsuchanek/copy": "1.4.5",
"archiver": "4.0.2",
"arg": "5.0.1",
Expand Down

0 comments on commit 120f04c

Please sign in to comment.