-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(typed-sql): Support enum names that are not valid JS identifiers (…
…#25262) For enum types, we are using DB names for types and values. Problem: enum can be mapped to arbitrary name in the database that would not necessary be a valid JS identifier. In that case, Prisma 5.19.0 generates syntactically invalid TS declaration. This PR fixes this. Rather than introduce sanitization function (and risk naming conflicts when sanitizer produces identical name for two different originals), we are continuing to rely on DB names, we just use them differently depending on their validity as an identifier. For valid identifiers, we continue to use namespaced name: `$DbEnum.MyEnum`. For invalid ones, we access them as a string literal property: `$DbEnums["MyEnum"]`. Fix #25163
- Loading branch information
Serhii Tatarintsev
authored
Sep 24, 2024
1 parent
ce11a90
commit 100c926
Showing
8 changed files
with
131 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/client/tests/functional/issues/25163-typed-sql-enum/_matrix.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { defineMatrix } from '../../_utils/defineMatrix' | ||
import { Providers } from '../../_utils/providers' | ||
|
||
export default defineMatrix(() => [[{ provider: Providers.POSTGRESQL }, { provider: Providers.COCKROACHDB }]]) |
35 changes: 35 additions & 0 deletions
35
packages/client/tests/functional/issues/25163-typed-sql-enum/prisma/_schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { idForProvider } from '../../../_utils/idForProvider' | ||
import testMatrix from '../_matrix' | ||
|
||
export default testMatrix.setupSchema(({ provider }) => { | ||
return /* Prisma */ ` | ||
generator client { | ||
provider = "prisma-client-js" | ||
previewFeatures = ["typedSql"] | ||
} | ||
datasource db { | ||
provider = "${provider}" | ||
url = env("DATABASE_URI_${provider}") | ||
} | ||
model User { | ||
id ${idForProvider(provider)} | ||
role UserRole | ||
favoriteAnimal Animal | ||
} | ||
enum UserRole { | ||
ADMIN | ||
USER | ||
@@map("user-role") | ||
} | ||
enum Animal { | ||
CAT | ||
DOG | ||
STEVE | ||
} | ||
` | ||
}) |
1 change: 1 addition & 0 deletions
1
packages/client/tests/functional/issues/25163-typed-sql-enum/prisma/sql/getUser.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SELECT "role", "favoriteAnimal" FROM "User" |
47 changes: 47 additions & 0 deletions
47
packages/client/tests/functional/issues/25163-typed-sql-enum/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { expectTypeOf } from 'expect-type' | ||
|
||
import testMatrix from './_matrix' | ||
// @ts-ignore | ||
import type { PrismaClient } from './node_modules/@prisma/client' | ||
// @ts-ignore | ||
import * as Sql from './node_modules/@prisma/client/sql' | ||
|
||
declare let prisma: PrismaClient | ||
declare let sql: typeof Sql | ||
|
||
testMatrix.setupTestSuite( | ||
() => { | ||
beforeAll(async () => { | ||
await prisma.user.create({ | ||
data: { | ||
role: 'ADMIN', | ||
favoriteAnimal: 'STEVE', | ||
}, | ||
}) | ||
}) | ||
|
||
test('returns enums that are mapped to invalid JS identifier correctly', async () => { | ||
const result = await prisma.$queryRawTyped(sql.getUser()) | ||
expect(result).toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"favoriteAnimal": "STEVE", | ||
"role": "ADMIN", | ||
}, | ||
] | ||
`) | ||
|
||
expectTypeOf(result[0].favoriteAnimal).toEqualTypeOf<'CAT' | 'DOG' | 'STEVE'>() | ||
expectTypeOf(result[0].favoriteAnimal).toEqualTypeOf<Sql.$DbEnums.Animal>() | ||
|
||
expectTypeOf(result[0].role).toEqualTypeOf<'ADMIN' | 'USER'>() | ||
expectTypeOf(result[0].role).toEqualTypeOf<Sql.$DbEnums['user-role']>() | ||
}) | ||
}, | ||
{ | ||
optOut: { | ||
from: ['sqlite', 'mysql', 'mongodb', 'sqlserver'], | ||
reason: 'Test need both enums and typed-sql support', | ||
}, | ||
}, | ||
) |