forked from prisma/prisma
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): Typed SQL (prisma#24907)
* Add types and PrismaClient generation pieces * First semi-working version * Add tests and fix things * Adapt to a new engine * Introduce preview feature * Update snapshots * Restore "loaded" module * Fix serializeRawParameters * Fix package types * Fix invalid JS identifiers * Enums * Pack "sql" files * Fix extension * Fix type error * Try to fix node16 * Better identiifer name check + snapshots * Use export that acutally exists * Add e2e test for default import * Watch support * Fix edge * Fix build * handle column nullability * add nullability tests & sqlite tests * fix nullable sqlite tests * fix bytes test * Fix node types test * Fix lint * Fix DA tests * Ensure esm import works correctly * Fix WASM runtime * Skip planetscale test * Fix sqlite * Fix linter * Throw on unsupported provider * Improve invalid SQL handling * Reuse raw results * Address review * Update snapshots * Fix ecosystem-tests * Remove force flag --------- Co-authored-by: Flavian Desverne <[email protected]>
- Loading branch information
Showing
267 changed files
with
3,918 additions
and
363 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
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
15 changes: 15 additions & 0 deletions
15
packages/cli/src/__tests__/fixtures/typed-sql-invalid-mongo/prisma/schema.prisma
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,15 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
previewFeatures = ["typedSql"] | ||
} | ||
|
||
datasource db { | ||
provider = "mongodb" | ||
url = env("TEST_MONGO_URI") | ||
} | ||
|
||
model User { | ||
id String @default(auto()) @id @map("_id") @db.ObjectId | ||
email String @unique | ||
name String? | ||
} |
1 change: 1 addition & 0 deletions
1
packages/cli/src/__tests__/fixtures/typed-sql-invalid-mongo/prisma/sql/invalidQuery.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 @@ | ||
WHAT WOULD YOU EVEN WRITE FOR MONGO SQL? |
15 changes: 15 additions & 0 deletions
15
packages/cli/src/__tests__/fixtures/typed-sql-invalid-mssql/prisma/schema.prisma
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,15 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
previewFeatures = ["typedSql"] | ||
} | ||
|
||
datasource db { | ||
provider = "sqlserver" | ||
url = env("TEST_MSSQL_JDBC_URI") | ||
} | ||
|
||
model User { | ||
id Int @default(autoincrement()) @id | ||
email String @unique | ||
name String? | ||
} |
1 change: 1 addition & 0 deletions
1
packages/cli/src/__tests__/fixtures/typed-sql-invalid-mssql/prisma/sql/invalidQuery.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 * FROM User; |
15 changes: 15 additions & 0 deletions
15
packages/cli/src/__tests__/fixtures/typed-sql-invalid/prisma/schema.prisma
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,15 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
previewFeatures = ["typedSql"] | ||
} | ||
|
||
datasource db { | ||
provider = "sqlite" | ||
url = "file:./dev.db" | ||
} | ||
|
||
model User { | ||
id Int @default(autoincrement()) @id | ||
email String @unique | ||
name String? | ||
} |
1 change: 1 addition & 0 deletions
1
packages/cli/src/__tests__/fixtures/typed-sql-invalid/prisma/sql/invalidQuery.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 * FROM Not!A!Valid!Table; |
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,57 @@ | ||
import { isValidJsIdentifier } from '@prisma/internals' | ||
import { introspectSql as migrateIntrospectSql, IntrospectSqlError, IntrospectSqlInput } from '@prisma/migrate' | ||
import fs from 'fs/promises' | ||
import { bold } from 'kleur/colors' | ||
import path from 'path' | ||
|
||
const SQL_DIR = 'sql' | ||
|
||
export async function introspectSql(schemaPath: string) { | ||
const sqlFiles = await readTypedSqlFiles(schemaPath) | ||
const introspectionResult = await migrateIntrospectSql(schemaPath, sqlFiles) | ||
if (introspectionResult.ok) { | ||
return introspectionResult.queries | ||
} | ||
throw new Error(renderErrors(introspectionResult.errors)) | ||
} | ||
|
||
export function sqlDirPath(schemaPath: string) { | ||
return path.join(path.dirname(schemaPath), SQL_DIR) | ||
} | ||
|
||
async function readTypedSqlFiles(schemaPath: string): Promise<IntrospectSqlInput[]> { | ||
const sqlPath = path.join(path.dirname(schemaPath), SQL_DIR) | ||
const files = await fs.readdir(sqlPath) | ||
const results: IntrospectSqlInput[] = [] | ||
for (const fileName of files) { | ||
const { name, ext } = path.parse(fileName) | ||
if (ext !== '.sql') { | ||
continue | ||
} | ||
const absPath = path.join(sqlPath, fileName) | ||
if (!isValidJsIdentifier(name)) { | ||
throw new Error(`${absPath} can not be used as a typed sql query: name must be a valid JS identifier`) | ||
} | ||
if (name.startsWith('$')) { | ||
throw new Error(`${absPath} can not be used as a typed sql query: name must not start with $`) | ||
} | ||
const source = await fs.readFile(path.join(sqlPath, fileName), 'utf8') | ||
results.push({ | ||
name, | ||
source, | ||
fileName: absPath, | ||
}) | ||
} | ||
|
||
return results | ||
} | ||
|
||
function renderErrors(errors: IntrospectSqlError[]) { | ||
const lines: string[] = [`Errors while reading sql files:\n`] | ||
for (const { fileName, message } of errors) { | ||
lines.push(`In ${bold(path.relative(process.cwd(), fileName))}:`) | ||
lines.push(message) | ||
lines.push('') | ||
} | ||
return lines.join('\n') | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '.prisma/client/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,4 @@ | ||
'use strict' | ||
module.exports = { | ||
...require('.prisma/client/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 @@ | ||
export * from '../../.prisma/client/sql/index.mjs' |
Oops, something went wrong.