Home > Software engineering >  How to export multiple databases in Deno?
How to export multiple databases in Deno?

Time:02-27

I have a code like this:

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");

This line of the code export second_db; doesn't work and if I add const before second_db it gives me another following error:

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.

I don't know how to export multiple databases?

Also I want to know what is the difference between a database it has default type and the other databases they don't have it?

CodePudding user response:

Here's the documentation for valid export syntax.

You can apply it to your situation like this. Let's say your module that includes the exports is called ./module.ts:

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

or

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

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

Then, you can import them in another module like this:

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';
  • Related