Skip to content

Commit

Permalink
Add runtime loader to rewrite .ts paths
Browse files Browse the repository at this point in the history
  • Loading branch information
junlarsen committed Mar 4, 2025
1 parent 44e96a9 commit 8225c02
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
22 changes: 22 additions & 0 deletions apps/rpc/runtime.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { URL } from "node:url"
import { existsSync } from "node:fs"
import path from "node:path"

export async function resolve(specifier, context, nextResolve) {
// Only handle relative or absolute paths without extensions
if (
(specifier.startsWith("./") || specifier.startsWith("../") || specifier.startsWith("/")) &&
!path.extname(specifier)
) {
// Try with .ts extension first
const tsFile = `${specifier}.ts`
const resolvedTsPath = new URL(tsFile, context.parentURL).href
return {
url: resolvedTsPath,
shortCircuit: true,
}
}

// Let Node.js handle it if no match
return nextResolve(specifier)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type Extras,
ExtrasSchema,
} from "@dotkomonline/types"
import { Prisma } from "@prisma/client"
import { Prisma } from "@dotkomonline/db"
import type { JsonValue } from "@prisma/client/runtime/library"

export interface AttendanceRepository {
Expand Down
2 changes: 1 addition & 1 deletion packages/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ generator zod {
provider = "zod-prisma-types"
output = "../src/schemas"
useMultipleFiles = true
useMultipleFiles = false
createInputTypes = false
addIncludeType = false
addSelectType = false
Expand Down
2 changes: 1 addition & 1 deletion packages/db/src/fixtures/attendance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Prisma } from "@prisma/client"
import { Prisma } from "../index"

export const getAttendanceFixtures = (): Prisma.AttendanceCreateManyInput[] => [
{
Expand Down
12 changes: 9 additions & 3 deletions packages/db/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { type Prisma, PrismaClient } from "@prisma/client"
import { createRequire } from "node:module"
import type { DefaultArgs } from "@prisma/client/runtime/library"

export * as schemas from "./schemas"
const require = createRequire(import.meta.url)
const { Prisma, PrismaClient } = require("@prisma/client")
export * as schemas from "./schemas/index"
export type * from "@prisma/client"
export { Prisma, PrismaClient }

export type DBClient = PrismaClient<Prisma.PrismaClientOptions, never, DefaultArgs>
import type { Prisma as _Prisma, PrismaClient as _PrismaClient } from "@prisma/client"

export type DBClient = _PrismaClient<_Prisma.PrismaClientOptions, never, DefaultArgs>
export const createPrisma = (databaseUrl: string): DBClient =>
new PrismaClient({
datasourceUrl: databaseUrl,
Expand Down

0 comments on commit 8225c02

Please sign in to comment.