Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default defineConfig({
});
```

Prisma's `prisma-client` generator writes TypeScript. Node.js 22.18 and newer can load it directly if the generator sets `importFileExtension = "ts"` and `clientPath` points to the generated `client.ts`. Otherwise, compile the client to JavaScript first, or create the client yourself as described in [Use your own Prisma client and Vitest environment](#use-your-own-prisma-client-and-vitest-environment).
Prisma's `prisma-client` generator writes TypeScript. Node.js 22.18 and newer can load it directly if the generator sets `importFileExtension = "ts"`, `clientPath` points to the generated `client.ts`, and the nearest `package.json` sets `"type": "module"`. Node.js loads `.ts` files in other packages as CommonJS, and the generated client's `import` statements fail there. Otherwise, compile the client to JavaScript first, or create the client yourself as described in [Use your own Prisma client and Vitest environment](#use-your-own-prisma-client-and-vitest-environment).

#### Step 4: Provide a `DATABASE_URL`

Expand Down
18 changes: 18 additions & 0 deletions src/contest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,24 @@ describe('toImportSpecifier', () => {
expect(toImportSpecifier('/abs/client.ts')).toBe('file:///abs/client.ts');
});

it('resolves directories and paths without an extension to their entry file', () => {
expect(toImportSpecifier('./test/client-dir')).toBe(
pathToFileURL(resolve('test/client-dir/index.js')).href,
);
expect(toImportSpecifier('./test/prisma-client-stub')).toBe(
pathToFileURL(resolve('test/prisma-client-stub.js')).href,
);
});

it('loads a client from a directory', async () => {
const before = prismaClientStubInstances.length;
const ctx = await createContext({ clientPath: './test/client-dir' });

expect(prismaClientStubInstances).toHaveLength(before + 1);

await ctx.teardown();
});

it('passes package names through unchanged', () => {
expect(toImportSpecifier('@prisma/client')).toBe('@prisma/client');
});
Expand Down
20 changes: 16 additions & 4 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createRequire } from 'node:module';
import { isAbsolute, resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
import { PrismaPg } from '@prisma/adapter-pg';
Expand Down Expand Up @@ -38,15 +39,26 @@ export async function createContext(options: PrismaPostgresEnvironmentOptions) {
);
}

const require = createRequire(import.meta.url);

/**
* Resolves relative and absolute file paths against the working directory and
* turns them into file URLs. Package names, such as `@prisma/client`, are
* turns them into file URLs. Directories and paths without an extension
* resolve to their entry file, as they did with `require()`, because
* `import()` needs a file. Package names, such as `@prisma/client`, are
* passed to `import()` unchanged.
*/
export function toImportSpecifier(clientPath: string) {
return clientPath.startsWith('.') || isAbsolute(clientPath)
? pathToFileURL(resolve(clientPath)).href
: clientPath;
if (!clientPath.startsWith('.') && !isAbsolute(clientPath)) return clientPath;

const path = resolve(clientPath);
let file = path;
try {
file = require.resolve(path);
} catch {
// Not resolvable by require(), e.g. a .ts entry: let import() try it.
}
return pathToFileURL(file).href;
}

export function createTransactionContext(
Expand Down
1 change: 1 addition & 0 deletions test/client-dir/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { PrismaClient } from '../prisma-client-stub.js';
Loading