-
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.
Merge pull request #67 from fdarian/drizzle-custom
feat: new `drizzle.custom` directive
- Loading branch information
Showing
9 changed files
with
195 additions
and
16 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
71 changes: 71 additions & 0 deletions
71
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,71 @@ | ||
import type { DMMF } from '@prisma/generator-helper' | ||
import * as v from 'valibot' | ||
import getErrorMessage from '~/lib/error-message' | ||
|
||
const DIRECTIVE = 'drizzle.custom' | ||
|
||
export function getCustomDirective(field: DMMF.Field) { | ||
const directiveInput = field.documentation | ||
if (directiveInput == null || !directiveInput.startsWith(DIRECTIVE)) { | ||
return | ||
} | ||
|
||
const parsing = v.safeParse(DirectiveSchema, parseJson(directiveInput)) | ||
if (!parsing.success) | ||
throw new InvalidDirectiveShapeError({ | ||
input: directiveInput, | ||
issues: parsing.issues, | ||
}) | ||
return parsing.output | ||
} | ||
|
||
const NamedImportSchema = v.pipe( | ||
v.array(v.string()), | ||
v.transform((names) => ({ | ||
type: 'named-import' as const, | ||
names, | ||
})) | ||
) | ||
|
||
const DefaultImportSchema = v.pipe( | ||
v.string(), | ||
v.transform((name) => ({ | ||
type: 'default-import' as const, | ||
name, | ||
})) | ||
) | ||
|
||
const ImportSchema = v.object({ | ||
name: v.union([NamedImportSchema, DefaultImportSchema]), | ||
/** e.g. "drizzle-orm" or "../my-type" */ | ||
module: v.string(), | ||
/** Marks the import as a type import */ | ||
type: v.optional(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(args: { | ||
input: string | ||
issues: [v.BaseIssue<unknown>, ...v.BaseIssue<unknown>[]] | ||
}) { | ||
super( | ||
`Invalid ${DIRECTIVE} definition:\n\n— Error:${JSON.stringify(v.flatten(args.issues), null, 2)}\n—\n\n— Your Input\n${args.input}\n—` | ||
) | ||
} | ||
} | ||
|
||
function parseJson(directiveInput: string) { | ||
try { | ||
return JSON.parse(directiveInput.replace(DIRECTIVE, '')) | ||
} catch (err) { | ||
throw new Error( | ||
`Invalid ${DIRECTIVE} JSON shape\n\n— Error:\n${getErrorMessage(err)}\n—\n\n— Your Input\n${directiveInput}\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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Based on https://kentcdodds.com/blog/get-a-catch-block-error-message-with-typescript | ||
*/ | ||
export default function getErrorMessage(error: unknown) { | ||
return toErrorWithMessage(error).message | ||
} | ||
|
||
type ErrorWithMessage = { | ||
message: string | ||
} | ||
|
||
function isErrorWithMessage(error: unknown): error is ErrorWithMessage { | ||
return ( | ||
typeof error === 'object' && | ||
error !== null && | ||
'message' in error && | ||
typeof (error as Record<string, unknown>).message === 'string' | ||
) | ||
} | ||
|
||
function toErrorWithMessage(maybeError: unknown): ErrorWithMessage { | ||
if (isErrorWithMessage(maybeError)) return maybeError | ||
|
||
try { | ||
return new Error(JSON.stringify(maybeError)) | ||
} catch { | ||
// fallback in case there's an error stringifying the maybeError | ||
// like with circular references for example. | ||
return new Error(String(maybeError)) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import fs, { readFileSync } from 'node:fs' | ||
import type { TestContext } from 'tests/utils/types' | ||
import { beforeAll, describe, expect, test } from 'vitest' | ||
|
||
export type SomeBigInt = bigint | ||
|
||
export const OUTPUT_FILE = './prisma/drizzle/field-customizations.ts' | ||
|
||
export function testFieldCustomization({ db, schema, provider }: TestContext) { | ||
let content: string | ||
beforeAll(async () => { | ||
expect(fs.existsSync(OUTPUT_FILE)).toBe(true) | ||
content = readFileSync(OUTPUT_FILE, 'utf-8') | ||
}) | ||
|
||
describe('allFields', () => { | ||
test('should contain import', () => { | ||
expect(content).include( | ||
"import type { SomeBigInt } from '~/tests/shared/testFieldCustomization'" | ||
) | ||
}) | ||
|
||
test('should contain correct field definition', () => { | ||
expect(content).include( | ||
"allFields: bigint('allFields', { mode: 'bigint' }).$type<SomeBigInt>().$defaultFn(() => 1n)" | ||
) | ||
}) | ||
}) | ||
} |
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