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

fix(client, generator): Fix TS issues with generated client and add tests #1117

Merged
merged 18 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/five-cats-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@electric-sql/prisma-generator": patch
---

Do not import `Relation` class if data model does not have any relations - fixes `unused import` TS errors.
1 change: 1 addition & 0 deletions clients/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
"build": "shx rm -rf dist && npm run build:copy-docker && concurrently \"tsup\" \"tsc -p tsconfig.build.json\" && node scripts/fix-imports.js",
"build:copy-docker": "shx mkdir -p ./dist/cli/docker-commands/docker && shx cp -r ./src/cli/docker-commands/docker ./dist/cli/docker-commands",
"test": "ava",
"generate-test-client": "npx tsx ./test/client/generateTestClient.ts",
msfstef marked this conversation as resolved.
Show resolved Hide resolved
"typecheck": "tsc -p tsconfig.json",
"posttest": "npm run typecheck",
"prepublishOnly": "pnpm run build",
Expand Down
96 changes: 59 additions & 37 deletions clients/typescript/src/cli/migrations/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,38 +255,10 @@ async function _generate(opts: Omit<GeneratorOptions, 'watch'>) {
// Introspect the created DB to update the Prisma schema
await introspectDB(prismaSchema)

// Add custom validators (such as uuid) to the Prisma schema
await addValidators(prismaSchema)

// Modify snake_case table names to PascalCase
await capitaliseTableNames(prismaSchema)

// Read the contents of the Prisma schema
const introspectedSchema = await fs.readFile(prismaSchema, 'utf8')

// Add a generator for the Electric client to the Prisma schema
await createElectricClientSchema(introspectedSchema, prismaSchema, opts)

// Generate the Electric client from the Prisma schema
await generateElectricClient(prismaSchema)

// Add a generator for the Prisma client to the Prisma schema
await createPrismaClientSchema(introspectedSchema, prismaSchema, opts)

// Generate the Prisma client from the Prisma schema
await generatePrismaClient(prismaSchema)
// Generate the Electric client from the given introspected schema
await generateClient(prismaSchema, config.CLIENT_PATH)

const relativePath = path.relative(appRoot, config.CLIENT_PATH)
// Modify the type of JSON input values in the generated Prisma client
// because we deviate from Prisma's typing for JSON values
await extendJsonType(config.CLIENT_PATH)

// Replace the type of byte array input values in the generated Prisma client
// from `Buffer` to `Uint8Array` for better cross-environment support
await replaceByteArrayType(config.CLIENT_PATH)

// Delete all files generated for the Prisma client, except the typings
await keepOnlyPrismaTypings(config.CLIENT_PATH)
console.log(`Successfully generated Electric client at: ./${relativePath}`)

// Build the migrations
Expand Down Expand Up @@ -314,6 +286,58 @@ async function _generate(opts: Omit<GeneratorOptions, 'watch'>) {
}
}

/**
* Generates the Electric client and the Prisma clients based off of the provided
* introspected Prisma schema.
* NOTE: exported for testing purposes only, not intended for external uses
* @param prismaSchema path to the introspected Prisma schema
* @param clientPath path to the directory where the client should be generated
*/
export async function generateClient(prismaSchema: string, clientPath: string) {
// Add custom validators (such as uuid) to the Prisma schema
await addValidators(prismaSchema)

// Modify snake_case table names to PascalCase
await capitaliseTableNames(prismaSchema)

// Read the contents of the Prisma schema
const introspectedSchema = await fs.readFile(prismaSchema, 'utf8')

// Add a generator for the Electric client to the Prisma schema
await createElectricClientSchema(introspectedSchema, prismaSchema, clientPath)

// Generate the Electric client from the Prisma schema
await generateElectricClient(prismaSchema)

// Add a generator for the Prisma client to the Prisma schema
await createPrismaClientSchema(introspectedSchema, prismaSchema, clientPath)

// Generate the Prisma client from the Prisma schema
await generatePrismaClient(prismaSchema)

// Perform necessary modifications to the generated client, like
// augmenting types, removing unused files, etc
await augmentGeneratedClient(clientPath)
}

/**
* Performs any necessary modifications to the generated client such
* as augmenting types, removing unused files, etc.
* @param clientPath Path to the generated client directory
*/
async function augmentGeneratedClient(clientPath: string) {
// Modify the type of JSON input values in the generated Prisma client
// because we deviate from Prisma's typing for JSON values
await extendJsonType(clientPath)

// Replace the type of byte array input values in the generated Prisma client
// from `Buffer` to `Uint8Array` for better cross-environment support
await replaceByteArrayType(clientPath)

// Delete all files generated for the Prisma client, except the typings
await keepOnlyPrismaTypings(clientPath)
}

/**
* Escapes file path for use in strings.
* On Windows, replaces backslashes with double backslashes for string escaping.
Expand Down Expand Up @@ -369,10 +393,9 @@ async function createIntrospectionSchema(
async function createElectricClientSchema(
introspectedSchema: string,
prismaSchemaFile: string,
opts: GeneratorOptions
clientPath: string
) {
const config = opts.config
const output = path.resolve(config.CLIENT_PATH)
const output = path.resolve(clientPath)

const schema = dedent`
generator electric {
Expand All @@ -397,10 +420,9 @@ async function createElectricClientSchema(
async function createPrismaClientSchema(
introspectedSchema: string,
prismaSchemaFile: string,
opts: GeneratorOptions
clientPath: string
) {
const config = opts.config
const output = path.resolve(config.CLIENT_PATH)
const output = path.resolve(clientPath)

const schema = dedent`
generator client {
Expand Down Expand Up @@ -763,7 +785,7 @@ function replaceByteArrayType(prismaDir: string): Promise<void> {
return fs.appendFile(
prismaTypings,
// omit 'set' property as it conflicts with the DAL setter prop name
"\n\ntype Buffer = Omit<Uint8Array | 'set'>\n"
"\n\ntype Buffer = Omit<Uint8Array, 'set'>\n"
)
}

Expand Down
203 changes: 203 additions & 0 deletions clients/typescript/test/cli/migrations/migrate.generation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
import test from 'ava'
import fs from 'fs'
import ts from 'typescript'
import { generateClient } from '../../../src/cli/migrations/migrate'
import path from 'path'

const tempDir = `.tmp`
const generatedFilePrefix = '_generation_test'

const defaultTsCompilerOptions: ts.CompilerOptions = {
target: ts.ScriptTarget.ES2020,
useDefineForClassFields: true,
module: ts.ModuleKind.ESNext,
skipLibCheck: true,

/* Bundler mode */
moduleResolution: ts.ModuleResolutionKind.Bundler,
allowImportingTsExtensions: true,
resolveJsonModule: true,
isolatedModules: true,
noEmit: true,

/* Linting */
strict: true,
noUnusedLocals: true,
noUnusedParameters: true,
noFallthroughCasesInSwitch: true,
}

const dbSnippet = `
datasource db {
provider = "postgresql"
url = env("PRISMA_DB_URL")
}
`

const simpleSchema = `
${dbSnippet}
model Items {
value String @id
}
`

const relationalSchema = `
${dbSnippet}

model Items {
value String @id
nbr Int?
}

model User {
id Int @id
name String?
posts Post[]
profile Profile?
}

model Post {
id Int @id
title String @unique
contents String
nbr Int?
authorId Int
author User? @relation(fields: [authorId], references: [id])
}

model Profile {
id Int @id
bio String
userId Int @unique
user User? @relation(fields: [userId], references: [id])
}
`

const dataTypesSchema = `
${dbSnippet}

model DataTypes {
id Int @id
date DateTime? @db.Date
time DateTime? @db.Time(3)
timetz DateTime? @db.Timetz(3)
timestamp DateTime? @unique @db.Timestamp(3)
timestamptz DateTime? @db.Timestamptz(3)
bool Boolean?
uuid String? @db.Uuid
int2 Int? @db.SmallInt
int4 Int?
int8 BigInt?
float4 Float? @db.Real
float8 Float? @db.DoublePrecision
json Json?
bytea Bytes?
enum KindOfCategory?
relatedId Int?
related Dummy? @relation(fields: [relatedId], references: [id])
}

model Dummy {
id Int @id
timestamp DateTime? @db.Timestamp(3)
datatype DataTypes[]
}

enum KindOfCategory {
FIRST
SECOND
RANDOM
}
`

/**
* Checks if the generated client from the Prisma schema can
* be compiled using TypeScript without emitting any errors.
* @param options compiler options to use
* @returns whether the generated client compiles successfully
*/
function checkGeneratedClientCompiles(
clientPath: string,
options: ts.CompilerOptions = defaultTsCompilerOptions
) {
const sourceFiles = fs
.readdirSync(clientPath, { withFileTypes: true })
.filter((entry) => entry.isFile() && entry.name.endsWith('.ts'))
.map((file) => path.join(clientPath, file.name))
const program = ts.createProgram({
rootNames: sourceFiles,
options,
})
// Check if the program compiles successfully
const diagnostics = ts.getPreEmitDiagnostics(program)
return diagnostics.length === 0
}

/**
* Generates the type-safe TS client for the specified Prisma schema,
* following all steps performed by the CLI generation process.
* @param inlinePrismaSchema the inline Prisma schema to generate the client for
* @param token unique token to use for the generated schema and client dirs
* @returns the path to the generated client
*/
const generateClientFromPrismaSchema = async (
inlinePrismaSchema: string,
token: string
): Promise<string> => {
const schemaFilePath = path.join(
tempDir,
`${generatedFilePrefix}_schema_${token}.prisma`
)
const generatedClientPath = path.join(
tempDir,
`${generatedFilePrefix}_client_${token}`
)
const migrationsPath = path.join(generatedClientPath, 'migrations.ts')
fs.writeFileSync(schemaFilePath, inlinePrismaSchema)
// clean up the generated client if present
fs.rmSync(generatedClientPath, { recursive: true, force: true })
await generateClient(schemaFilePath, generatedClientPath)
await fs.writeFileSync(migrationsPath, 'export default []')
return generatedClientPath
}

test.before(() => {
msfstef marked this conversation as resolved.
Show resolved Hide resolved
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir)
}
})

test.after(() => {
// avoid deleting whole temp directory as it might be used by
// other tests as well
const files = fs.readdirSync(tempDir)
for (const file of files) {
if (file.startsWith(generatedFilePrefix)) {
fs.rmSync(path.join(tempDir, file), { recursive: true, force: true })
}
}
})

test('should generate valid TS client for simple schema', async (t) => {
const clientPath = await generateClientFromPrismaSchema(
simpleSchema,
'simple'
)
t.true(checkGeneratedClientCompiles(clientPath))
})

test('should generate valid TS client for relational schema', async (t) => {
const clientPath = await generateClientFromPrismaSchema(
relationalSchema,
'relational'
)
t.true(checkGeneratedClientCompiles(clientPath))
})

test('should generate valid TS client for schema with all data types', async (t) => {
const clientPath = await generateClientFromPrismaSchema(
dataTypesSchema,
'datatypes'
)
t.true(checkGeneratedClientCompiles(clientPath))
})
3 changes: 1 addition & 2 deletions clients/typescript/test/client/conversions/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
_NOT_UNIQUE_,
_RECORD_NOT_FOUND_,
} from '../../../src/client/validation/errors/messages'
import { JsonNull, schema } from '../generated'
import { DataTypes, Dummy } from '../generated/client'
import { JsonNull, schema, DataTypes, Dummy } from '../generated'

const db = new Database(':memory:')
const electric = await electrify(
Expand Down
Loading
Loading