Home / Function/ construct() — drizzle-orm Function Reference

construct() — drizzle-orm Function Reference

Architecture documentation for the construct() function in driver.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  b1cceb78_bbf5_9c3a_fc8b_674d4b3d47a6["construct()"]
  9bd9488e_a0fa_b4b6_7790_4361efcb51f2["driver.ts"]
  b1cceb78_bbf5_9c3a_fc8b_674d4b3d47a6 -->|defined in| 9bd9488e_a0fa_b4b6_7790_4361efcb51f2
  2f6e8052_53b2_cccd_aa69_e0fc6c4eb606["drizzle()"]
  2f6e8052_53b2_cccd_aa69_e0fc6c4eb606 -->|calls| b1cceb78_bbf5_9c3a_fc8b_674d4b3d47a6
  412c0f72_ff84_8028_1616_d90ddd770c3f["mock()"]
  412c0f72_ff84_8028_1616_d90ddd770c3f -->|calls| b1cceb78_bbf5_9c3a_fc8b_674d4b3d47a6
  93acaa91_7d68_0b4b_6f50_d63ec4520df2["isCallbackClient()"]
  b1cceb78_bbf5_9c3a_fc8b_674d4b3d47a6 -->|calls| 93acaa91_7d68_0b4b_6f50_d63ec4520df2
  ad296a8a_046e_6969_b0e3_fe4b5e197410["createSession()"]
  b1cceb78_bbf5_9c3a_fc8b_674d4b3d47a6 -->|calls| ad296a8a_046e_6969_b0e3_fe4b5e197410
  style b1cceb78_bbf5_9c3a_fc8b_674d4b3d47a6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

drizzle-orm/src/mysql2/driver.ts lines 60–111

function construct<
	TSchema extends Record<string, unknown> = Record<string, never>,
	TClient extends Pool | Connection | CallbackPool | CallbackConnection = CallbackPool,
>(
	client: TClient,
	config: MySql2DrizzleConfig<TSchema> = {},
): MySql2Database<TSchema> & {
	$client: AnyMySql2Connection extends TClient ? CallbackPool : TClient;
} {
	const dialect = new MySqlDialect({ casing: config.casing });
	let logger;
	if (config.logger === true) {
		logger = new DefaultLogger();
	} else if (config.logger !== false) {
		logger = config.logger;
	}

	const clientForInstance = isCallbackClient(client) ? client.promise() : client;

	let schema: RelationalSchemaConfig<TablesRelationalConfig> | undefined;
	if (config.schema) {
		if (config.mode === undefined) {
			throw new DrizzleError({
				message:
					'You need to specify "mode": "planetscale" or "default" when providing a schema. Read more: https://orm.drizzle.team/docs/rqb#modes',
			});
		}

		const tablesConfig = extractTablesRelationalConfig(
			config.schema,
			createTableRelationsHelpers,
		);
		schema = {
			fullSchema: config.schema,
			schema: tablesConfig.tables,
			tableNamesMap: tablesConfig.tableNamesMap,
		};
	}

	const mode = config.mode ?? 'default';

	const driver = new MySql2Driver(clientForInstance as MySql2Client, dialect, { logger, cache: config.cache });
	const session = driver.createSession(schema, mode);
	const db = new MySql2Database(dialect, session, schema as any, mode) as MySql2Database<TSchema>;
	(<any> db).$client = client;
	(<any> db).$cache = config.cache;
	if ((<any> db).$cache) {
		(<any> db).$cache['invalidate'] = config.cache?.onMutate;
	}

	return db as any;
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does construct() do?
construct() is a function in the drizzle-orm codebase, defined in drizzle-orm/src/mysql2/driver.ts.
Where is construct() defined?
construct() is defined in drizzle-orm/src/mysql2/driver.ts at line 60.
What does construct() call?
construct() calls 2 function(s): createSession, isCallbackClient.
What calls construct()?
construct() is called by 2 function(s): drizzle, mock.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free