如何在 Deno 中导出多个数据库?

How to export multiple databases in Deno?

我有这样的代码:

import { MongoClient } from "./deps.ts";

const client = new MongoClient();
await client.connect(Deno.env.get("MONGODB_URI") || "");

const first_db = client.database(Deno.env.get("DB1_NAME") || "");
const second_db = client.database(Deno.env.get("DB2_NAME") || "");

export default  first_db;
export  second_db;


export const firstCollection = first_db.collection("first");
export const secondCollection = second_db.collection("second");

这行代码 export second_db; 不起作用,如果我在 second_db 之前添加 const 它会给我另一个错误:

Cannot redeclare block-scoped variable 'second_db'.deno-ts(2451) 'const' declarations must be initialized.deno-ts(1155) Variable 'second_db' implicitly has an 'any' type.

不知道如何导出多个数据库?

我还想知道它具有 default 类型的数据库与其他没有它的数据库有什么区别?

这是有效 export 语法的文档。

你可以像这样将它应用到你的情况中。假设包含导出的模块称为 ./module.ts:

export const first_db = 'some value';
export const second_db = 'some value';
export default first_db;

const first_db = 'some value';
const second_db = 'some value';

export {
  first_db as default,
  first_db,
  second_db,
};

然后,您可以像这样将它们导入另一个模块:

import {first_db, second_db} from './module.ts';

// OR

import first_db, {second_db} from './module.ts';

// OR

import {default as first_db, second_db} from './module.ts';