-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
farreldarian
committed
Jun 13, 2024
1 parent
3178920
commit b5382a5
Showing
2 changed files
with
80 additions
and
2 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
60 changes: 60 additions & 0 deletions
60
packages/generator/src/lib/adapter/fields/directives/custom.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,60 @@ | ||
import type { DMMF } from '@prisma/generator-helper' | ||
import * as v from 'valibot' | ||
|
||
const DIRECTIVE = 'drizzle.custom' | ||
|
||
export function getCustomDirective(field: DMMF.Field) { | ||
const directiveInput = field.documentation | ||
if (directiveInput == null || !directiveInput.startsWith(DIRECTIVE)) { | ||
return | ||
} | ||
|
||
const jsonParsing = safe(() => | ||
JSON.parse(directiveInput.replace(DIRECTIVE, '')) | ||
) | ||
if (!jsonParsing.ok) | ||
throw new Error(`Invalid ${DIRECTIVE} JSON shape\n\n${jsonParsing.error}`) | ||
|
||
const schemaParsing = v.safeParse(DirectiveSchema, jsonParsing.value) | ||
if (!schemaParsing.success) | ||
throw new InvalidDirectiveShapeError(schemaParsing.issues) | ||
|
||
return schemaParsing.output | ||
} | ||
|
||
const ImportSchema = v.object({ | ||
name: v.union([v.array(v.string()), v.string()]), | ||
module: v.string(), | ||
type: v.boolean(), | ||
}) | ||
|
||
const DirectiveSchema = v.object({ | ||
imports: v.optional(v.array(ImportSchema)), | ||
type: v.optional(v.string()), | ||
default: v.optional(v.string()), | ||
}) | ||
|
||
class InvalidDirectiveShapeError extends Error { | ||
constructor(issues: [v.BaseIssue<unknown>, ...v.BaseIssue<unknown>[]]) { | ||
super( | ||
`Invalid ${DIRECTIVE} definition:\n\n${JSON.stringify(v.flatten(issues), null, 2)}` | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* Executes a function safely and returns the result or any error that occurred. | ||
* | ||
* @template T - The type of the value returned by the function. | ||
* @param {() => T} func - The function to execute safely. | ||
* @returns {{ ok: true; value: T } | { ok: false; error: Error }} - An object containing either the result of the function or an error. | ||
*/ | ||
function safe<T>( | ||
func: () => T | ||
): { ok: true; value: T } | { ok: false; error: Error } { | ||
try { | ||
return { ok: true, value: func() } as const | ||
} catch (error) { | ||
return { ok: false, error: error as Error } as const | ||
} | ||
} |