Skip to content

Commit

Permalink
Merge pull request #75 from fdarian/pgd-47-use-custom-generator
Browse files Browse the repository at this point in the history
feat: decouple with Prisma's DMMF and add ast definition
  • Loading branch information
fdarian authored Jul 14, 2024
2 parents bdc70f3 + 7cfb8ac commit be534c2
Show file tree
Hide file tree
Showing 24 changed files with 334 additions and 123 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ jobs:
steps:
- name: ⬇️ Checkout repo
uses: actions/checkout@v4

- name: ⎔ Setup node
uses: actions/setup-node@v4
with:
node-version: 22

- name: ⎔ Setup bun
uses: oven-sh/setup-bun@v1
Expand Down
11 changes: 11 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Testing Notes

> Quick notes on the findings when building the test, not a comprehensive guide yet.
### On using Vitest

- Although this project uses bun for the package manager, Vitest is used for testing solely because of its [typechecking capabilities](https://vitest.dev/guide/testing-types).

- When running Vitest using Bun (`bun vitest run`), there are still `node` calls occurring in the background. Therefore, ensure that you specify `bunx prisma-generator-drizzle` when building the Prisma schema.

- We're considering a full migration to `pnpm` + `vitest` in the future for the sake of consistency.
Binary file modified bun.lockb
Binary file not shown.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "prisma-generator-drizzle-root",
"workspaces": ["packages/*", "examples/*"],
"devDependencies": {
"@biomejs/biome": "^1.8.2",
"lefthook": "^1.6.16"
"lefthook": "^1.6.16",
"turbo": "^2.0.6"
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"workspaces": ["packages/*", "examples/*"]
}
3 changes: 2 additions & 1 deletion packages/generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"test": "bun test"
},
"dependencies": {
"@mrleebo/prisma-ast": "^0.12.0",
"@prisma/client": "5.10.2",
"@prisma/generator-helper": "5.10.2",
"@prisma/sdk": "4.0.0",
Expand All @@ -35,7 +36,7 @@
"@types/prettier": "3.0.0",
"prisma": "5.10.2",
"tsup": "^8.0.2",
"typescript": "5.4.2"
"typescript": "5.5.3"
},
"repository": {
"type": "git",
Expand Down
13 changes: 10 additions & 3 deletions packages/generator/src/generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { execSync } from 'node:child_process'
import fs from 'node:fs'
import path from 'node:path'
import { createPrismaSchemaBuilder } from '@mrleebo/prisma-ast'
import {
type DMMF,
type GeneratorOptions,
Expand All @@ -23,6 +24,7 @@ import type { RelationalModuleSet } from './lib/adapter/modules/relational'
import { generateSchemaModules as generateSchemaModule } from './lib/adapter/modules/relational'
import type { BaseGeneratedModules } from './lib/adapter/modules/sets/base-generated-modules'
import { logger } from './lib/logger'
import { createSchema } from './lib/prisma-helpers/schema/schema'
import {
type ImportValue,
type NamedImport,
Expand All @@ -46,6 +48,11 @@ generatorHandler({
}
},
onGenerate: async (options: GeneratorOptions) => {
const schema = createSchema({
astSchema: createPrismaSchemaBuilder(options.datamodel).getSchema(),
dmmf: options.dmmf,
})

initializeGenerator(options)

logger.log('Generating drizzle schema...')
Expand All @@ -55,14 +62,14 @@ generatorHandler({
const modules: GeneratedModules = {
extras: adapter.extraModules,
enums: generateEnumModules(adapter),
models: generateModelModules(adapter),
models: generateModelModules(adapter, schema),
}

if (isRelationalQueryEnabled()) {
const relational = generateRelationalModules(modules.models)
const relational = generateRelationalModules(schema, modules.models)
modules.relational = relational

const implicit = generateImplicitModules(adapter, relational)
const implicit = generateImplicitModules(adapter, schema, relational)
modules.implicitModels = implicit.models
modules.implicitRelational = implicit.relational

Expand Down
14 changes: 3 additions & 11 deletions packages/generator/src/lib/adapter/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import type {
PrismaEnumField,
PrismaScalarField,
} from '../prisma-helpers/field'
import type { SchemaField } from '../prisma-helpers/schema/schema-field'
import type { ImportValue } from '../syntaxes/imports'
import type { Module } from '../syntaxes/module'
import type { FieldFunc } from './fields/createField'

export type ParsableField = PrismaScalarField | PrismaEnumField

type DeclarationFunc = { imports: ImportValue[]; func: string }

export function createAdapter<TName extends string>(impl: {
Expand All @@ -17,16 +12,13 @@ export function createAdapter<TName extends string>(impl: {
table: (name: string, fields: FieldFunc[]) => DeclarationFunc
}
fields: Partial<
Record<
PrismaScalarField['type'] | 'enum',
(field: ParsableField) => FieldFunc
>
Record<SchemaField['type'] | 'enum', (field: SchemaField) => FieldFunc>
>
extraModules?: Module[]
}) {
return {
...impl,
parseField(field: ParsableField) {
parseField(field: SchemaField) {
const fieldType = field.kind === 'enum' ? 'enum' : field.type
const fieldFunc =
fieldType in impl.fields ? impl.fields[fieldType] : undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import type { DMMF } from '@prisma/generator-helper'
import { or } from 'fp-ts/lib/Refinement'
import { pipe } from 'fp-ts/lib/function'
import { isKind } from '~/lib/prisma-helpers/field'
import { getDbName } from '~/lib/prisma-helpers/getDbName'
import { getModelVarName } from '~/lib/prisma-helpers/model'
import {
type SchemaModel,
getModelFields,
} from '~/lib/prisma-helpers/schema/schema-model'
import type { Adapter } from '../types'

export function generateTableDeclaration(adapter: Adapter, model: DMMF.Model) {
const fields = model.fields
export function generateTableDeclaration(adapter: Adapter, model: SchemaModel) {
const fields = getModelFields(model)
.filter(pipe(isKind('scalar'), or(isKind('enum'))))
.map(adapter.parseField)
const name = getModelVarName(model)
const name = model.getVarName()

const tableDeclaration = adapter.getDeclarationFunc.table(
getDbName(model),
model.getDbName(),
fields
)

Expand Down
Loading

0 comments on commit be534c2

Please sign in to comment.