Skip to content

Commit

Permalink
Merge pull request #65 from jansedlon/main
Browse files Browse the repository at this point in the history
  • Loading branch information
fdarian authored Jun 6, 2024
2 parents 449e4c1 + bf3a541 commit 429beb0
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 40 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"fp-ts": "^2.16.2",
"lodash": "^4.17.21",
"pluralize": "^8.0.0",
"valibot": "^0.30.0"
"valibot": "^v0.31.0-rc.12"
},
"devDependencies": {
"@types/lodash": "^4.14.202",
Expand Down
25 changes: 16 additions & 9 deletions packages/generator/src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { GeneratorOptions } from '@prisma/generator-helper'
import {
type Output,
type SchemaIssues,
type InferIssue,
type InferOutput,
flatten,
object,
optional,
Expand All @@ -10,17 +10,22 @@ import {
} from 'valibot'
import { DateMode } from '~/shared/date-mode'
import { ModuleResolution } from '~/shared/generator-context/module-resolution'
import { BooleanInStr, withDefault } from './valibot-schema'
import { BooleanInStr } from './valibot-schema'

const Config = object({
relationalQuery: withDefault(optional(BooleanInStr), true),
relationalQuery: optional(BooleanInStr, true),
moduleResolution: optional(ModuleResolution),
verbose: optional(BooleanInStr),
formatter: optional(string()),
abortOnFailedFormatting: withDefault(optional(BooleanInStr), true),
abortOnFailedFormatting: optional(BooleanInStr, true),
dateMode: optional(DateMode),
})
export type Config = Output<typeof Config>
export type Config = InferOutput<typeof Config>

type ConfigSchemaIssues = [
InferIssue<typeof Config>,
...InferIssue<typeof Config>[],
]

export function parseConfig(config: GeneratorOptions['generator']['config']) {
const parsing = safeParse(Config, config)
Expand All @@ -29,21 +34,23 @@ export function parseConfig(config: GeneratorOptions['generator']['config']) {
}

class ConfigError extends Error {
constructor(issues: SchemaIssues) {
constructor(
issues: [InferIssue<typeof Config>, ...InferIssue<typeof Config>[]]
) {
super(`[prisma-generator-drizzle] Invalid Config:\n${formatError(issues)}`)
this.name = 'ConfigError'
}
}

function formatError(issues: SchemaIssues) {
function formatError(issues: ConfigSchemaIssues) {
let message = ''

const flattened = flatten(issues)
if (flattened.root) {
message += `\n- ${flattened.root}`
}

for (const [key, issues] of Object.entries(flattened.nested)) {
for (const [key, issues] of Object.entries(flattened.nested ?? {})) {
message += `\n- ${key}: ${issues}`
}

Expand Down
27 changes: 8 additions & 19 deletions packages/generator/src/lib/valibot-schema.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import { coerce, picklist } from 'valibot'
import {
type BaseSchema,
type OptionalSchema,
type Output,
transform,
} from 'valibot'
import { boolean, pipe, string, union } from 'valibot'
import { transform } from 'valibot'

export function withDefault<Schema extends OptionalSchema<BaseSchema>>(
schema: Schema,
value: Output<Schema['wrapped']>
) {
return transform(schema, (val) => val ?? value)
}
export const BooleanInStr = pipe(
union([string(), boolean()]),
transform((value) => {
if (typeof value === 'string') return value.toLowerCase() === 'true'

export const BooleanInStr = transform(
coerce(picklist(['true', 'false']), (value) => {
if (typeof value !== 'string') return value
return value.toLowerCase()
}),
(value) => value === 'true'
return value
})
)
4 changes: 2 additions & 2 deletions packages/generator/src/shared/date-mode.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { literal, parse, union } from 'valibot'
import { parse, picklist } from 'valibot'
import type { ParsableField } from '~/lib/adapter/adapter'
import { getDirective } from '~/lib/directive'
import { getGenerator } from './generator-context'

export const DateMode = union([literal('string'), literal('date')])
export const DateMode = picklist(['string', 'date'])

export function getDateMode(field: ParsableField) {
const directive = getDirective(field, 'drizzle.dateMode')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import fs from 'node:fs'
import path from 'node:path'
import { brand, object, parse, string, transform } from 'valibot'
import { brand, object, parse, pipe, string, toLowerCase } from 'valibot'
import stripJsonComments from '~/lib/strip-json-comments'

export const ModuleResolution = brand(
transform(string(), (value) => value.toLowerCase()),
'ModuleResolution'
export const ModuleResolution = pipe(
string(),
toLowerCase(),
brand('ModuleResolution')
)

export function resolveModuleResolution() {
Expand Down
2 changes: 1 addition & 1 deletion packages/usage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"pg": "^8.11.3",
"postgres": "^3.4.3",
"uuid": "^9.0.1",
"valibot": "^0.30.0"
"valibot": "0.31.0-rc.12"
},
"devDependencies": {
"@types/better-sqlite3": "^7.6.9",
Expand Down
4 changes: 2 additions & 2 deletions packages/usage/src/lib/mysql.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { drizzle } from 'drizzle-orm/mysql2'
import mysql from 'mysql2/promise'
import { schema } from 'prisma/mysql/drizzle/schema'
import { url, object, parse, string } from 'valibot'
import { url, object, parse, string, pipe } from 'valibot'

const env = parse(
object({
MYSQL_DATABASE_URL: string([url()]),
MYSQL_DATABASE_URL: pipe(string(), url()),
}),
process.env
)
Expand Down
4 changes: 2 additions & 2 deletions packages/usage/src/lib/postgres.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { drizzle } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'
import { schema } from 'prisma/drizzle/schema'
import { url, object, parse, string } from 'valibot'
import { url, object, parse, string, pipe } from 'valibot'

const env = parse(
object({
PG_DATABASE_URL: string([url()]),
PG_DATABASE_URL: pipe(string(), url()),
}),
process.env
)
Expand Down

0 comments on commit 429beb0

Please sign in to comment.