diff --git a/nodejs/__test__/remote.test.ts b/nodejs/__test__/remote.test.ts index 89a9e992c..dbdd6b370 100644 --- a/nodejs/__test__/remote.test.ts +++ b/nodejs/__test__/remote.test.ts @@ -56,18 +56,17 @@ async function withMockDatabase( const server = http.createServer(listener); server.listen(8000); - const db = await connect( - "db://dev", - Object.assign( - { - apiKey: "fake", - hostOverride: "http://localhost:8000", - }, - connectionOptions, - ), - ); - try { + const db = await connect( + "db://dev", + Object.assign( + { + apiKey: "fake", + hostOverride: "http://localhost:8000", + }, + connectionOptions, + ), + ); await callback(db); } finally { server.close(); @@ -125,6 +124,33 @@ describe("remote connection", () => { ); }); + it("uses LANCEDB_API_KEY when apiKey is not provided", async () => { + const previousApiKey = process.env.LANCEDB_API_KEY; + process.env.LANCEDB_API_KEY = "env-key"; + + try { + await withMockDatabase( + (req, res) => { + expect(req.headers["x-api-key"]).toEqual("env-key"); + + const body = JSON.stringify({ tables: [] }); + res.writeHead(200, { "Content-Type": "application/json" }).end(body); + }, + async (db) => { + const tableNames = await db.tableNames(); + expect(tableNames).toEqual([]); + }, + { apiKey: undefined }, + ); + } finally { + if (previousApiKey === undefined) { + delete process.env.LANCEDB_API_KEY; + } else { + process.env.LANCEDB_API_KEY = previousApiKey; + } + } + }); + it("allows customizing user agent", async () => { await withMockDatabase( (req, res) => { diff --git a/nodejs/lancedb/index.ts b/nodejs/lancedb/index.ts index 319222421..0e27acc9a 100644 --- a/nodejs/lancedb/index.ts +++ b/nodejs/lancedb/index.ts @@ -385,6 +385,7 @@ export async function connect( } finalOptions = (finalOptions as ConnectionOptions) ?? {}; + finalOptions.apiKey ??= process.env.LANCEDB_API_KEY; (finalOptions).storageOptions = cleanseStorageOptions( (finalOptions).storageOptions, );