Avoiding Direct Database Import in `functions.ts`

When you manage your project without using layers or a content management system in "effect," and you import a database instance directly into functions.ts like this: import db from 'drizzle, it establishes a database connection during the import process itself. Consequently, if you reference functions.ts in another file, such as test.ts, to log something or use a function, it inadvertently triggers a connection to the database because the database instance is already imported in functions.ts. This happens even if the purpose of using functions.ts in test.ts is merely to access non-database-related functionalities.

The database instance becomes a dependency as soon as you run the app because of this import in functions.ts. However, there's an issue with managing the TypeScript typings for the database. The database has various functionalities, and without importing these typings, you can't easily determine the data types it handles. So the question is, how can we include all the necessary TypeScript typings for the database as a dependency?
// Importing the database instance
import db from "./db/db";

// Example function that queries the database
export const getUserData = async (userId: string) => {
  const userData = await db.query(`SELECT * FROM users WHERE id = ${userId}`);
  return userData;
};

// Another function that doesn't interact with the database
export const simpleCalculation = (a: number, b: number) => {
  return a + b;
};


i wish there will be something to import type of db insted of db itself

oh i think we should do like this
import type db from "./db/db";
dependency: typeof db;
Was this page helpful?