Skip to content

Commit 8157e00

Browse files
committed
fix(storage, database): import connector libs from their real specifier
1 parent a44368a commit 8157e00

7 files changed

Lines changed: 51 additions & 16 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"dependencies": {
7272
"consola": "^3.4.2",
7373
"crossws": "^0.4.12",
74-
"db0": "^0.4.0",
74+
"db0": "^0.4.1",
7575
"env-runner": "^0.2.1",
7676
"h3": "^2.0.1-rc.30",
7777
"hookable": "^6.1.1",
@@ -81,7 +81,7 @@
8181
"rou3": "^0.9.2",
8282
"srvx": "^1.0.2",
8383
"unenv": "^2.0.0-rc.24",
84-
"unstorage": "^2.0.0-alpha.9"
84+
"unstorage": "^2.0.0-alpha.10"
8585
},
8686
"devDependencies": {
8787
"@apphosting/common": "^0.0.9",

pnpm-lock.yaml

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/build/virtual/database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function genConnectorOptions(nitro: Nitro, connection: DatabaseConnection) {
4848
connection.options[dep.option] === undefined &&
4949
isDepInstalled(dep.name, nitro.options.rootDir)
5050
)
51-
.map((dep) => `${dep.option}: () => import(${JSON.stringify(dep.name)})`);
51+
.map((dep) => `${dep.option}: () => import(${JSON.stringify(dep.import || dep.name)})`);
5252

5353
const options = JSON.stringify(connection.options);
5454
return libs.length > 0 ? `{ ...${options}, ${libs.join(", ")} }` : options;

src/build/virtual/storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function genDriverOptions(nitro: Nitro, mount: ReturnType<typeof resolveStorageM
5050
mount.options[dep.option] === undefined &&
5151
isDepInstalled(dep.name, nitro.options.rootDir)
5252
)
53-
.map((dep) => `${dep.option}: () => import(${JSON.stringify(dep.name)})`);
53+
.map((dep) => `${dep.option}: () => import(${JSON.stringify(dep.import || dep.name)})`);
5454

5555
const options = JSON.stringify(mount.options);
5656
return libs.length > 0 ? `{ ...${options}, ${libs.join(", ")} }` : options;

src/utils/dep.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ export interface LibDep {
8686
option: string;
8787
/** Package name. */
8888
name: string;
89+
/** Import specifier, if it differs from the package name (e.g. `mysql2/promise`). */
90+
import?: string;
8991
/** Supported version range. */
9092
version?: string;
9193
/** Only required for some of the features. */

test/unit/virtual-database.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ describe("virtual/database template", () => {
5454
expect(template).toContain(`options: { ...{"url":"postgres://"}, lib: () => import("pg") }`);
5555
});
5656

57+
it("uses the connector import specifier when it differs from the package name", () => {
58+
installedDeps.add("mysql2");
59+
const template = database(
60+
createNitroStub({ default: { connector: "mysql2", options: { host: "localhost" } } })
61+
).template();
62+
expect(template).toContain(
63+
`options: { ...{"host":"localhost"}, lib: () => import("mysql2/promise") }`
64+
);
65+
});
66+
5767
it("does not provide `lib` for dependencies that are not installed", () => {
5868
const template = database(
5969
createNitroStub({ default: { connector: "postgresql", options: { url: "postgres://" } } })

test/unit/virtual-storage.test.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
import { describe, expect, it } from "vitest";
1+
import { describe, expect, it, vi } from "vitest";
22
import type { Nitro } from "nitro/types";
33

4-
import storage from "../../src/build/virtual/storage.ts";
4+
const mockedDeps = new Set<string>();
5+
6+
vi.mock("../../src/utils/dep.ts", async (importOriginal) => {
7+
const original = await importOriginal<typeof import("../../src/utils/dep.ts")>();
8+
return {
9+
...original,
10+
isDepInstalled: (id: string, dir: string) =>
11+
mockedDeps.has(id) || original.isDepInstalled(id, dir),
12+
};
13+
});
14+
15+
const { default: storage } = await import("../../src/build/virtual/storage.ts");
516

617
function createNitroStub(
718
tracingChannel: Nitro["options"]["tracingChannel"],
@@ -49,6 +60,18 @@ describe("virtual/storage template", () => {
4960
);
5061
});
5162

63+
it("uses the driver import specifier when it differs from the package name", () => {
64+
mockedDeps.add("uploadthing");
65+
try {
66+
const template = storage(
67+
createNitroStub(undefined, { "/files": { driver: "uploadthing", token: "x" } })
68+
).template();
69+
expect(template).toContain(`lib: () => import("uploadthing/server")`);
70+
} finally {
71+
mockedDeps.clear();
72+
}
73+
});
74+
5275
it("does not provide `lib` for dependencies that are not installed", () => {
5376
const template = storage(
5477
createNitroStub(undefined, { "/cache": { driver: "redis", base: "cache" } })

0 commit comments

Comments
 (0)