Next js typescript conditionally export a module

In Next.js (v.15 w/ App Router) i have 2 modules of server actions where i export the same functions, each with a different database, like so :

–> databases/mongodb/server_actions

...all the necessary imports, "use server" declaration, etc

export interface IActionState {
  success: string | null
  error: string | null
}

const createWallet = async (
  previousState: IActionState,
  formData: FormData
): Promise<IActionState> => {
  try {
    const session = await auth()
    await connectDB()
    const title = formData.get("title") as string
    const user = session?.user?.email as string

    const wallet = new WalletModel({ title, user })
    await wallet.save()
    //await new Promise((resolve) => setTimeout(resolve, 1000)) // Simulate slow network
    return { success: `Wallet ${title} created successfully`, error: null }
  } catch (error: any) {
    //await new Promise((resolve) => setTimeout(resolve, 1000)) // Simulate slow network
    return { success: null, error: error?.message || "An error occurred" }
  }
}


export {
  createWallet,
  //and some others
}

the other one is databases/postgres/server_actions with the same logic

How can i create a third module, which only responsibility would be to determine which one to expose to the consumer component ?

I have tried something like :

–> dbActionsRouter.ts

const DATABASE = process.env.NEXT_PUBLIC_DATABASE

let moduleToExport: any

if (DATABASE === "MONGODB") {
  moduleToExport = await import("@/databases/mongodb/server_actions")
} else if (DATABASE === "POSTGRES") {
  moduleToExport = await import("@/databases/postgres/server_actions")
} else {
  throw new Error("DATABASE environment variable is not set or is invalid")
}

console.log(`Using ${DATABASE} database`)
console.log(`Using ${moduleToExport} as database actions`)

export default moduleToExport

But when i try to import it at a client component

import { createWallet } from "@/dbActionsRouter"

i get

module ‘”@/dbActionsRouter”‘ has no exported member ‘createWallet’

and after running it at :

...
<form action={action}>
...

Unhandled Runtime Error
TypeError: action is not a function

and these 2 console warnings :

./app/create_wallet/NewWalletForm.client.tsx
Attempted import error: ‘createWallet’ is not exported from ‘@/dbActionsRouter’ (imported as ‘createWallet’).

and

./dbActionsRouter.ts
The generated code contains ‘async/await’ because this module is using “topLevelAwait”.
However, your target environment does not appear to support ‘async/await’.
As a result, the code may not run as expected or may cause runtime errors.

Also is this the correct way to have multiple databases ready to be used ?
Im trying to make it as simple and readable as possible instead of checking the env variable at every action and run the operation in a different db every time

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật