MCPcopy
hub / github.com/drizzle-team/drizzle-orm / connectToMySQL

Function connectToMySQL

drizzle-kit/src/cli/connections.ts:741–879  ·  view source on GitHub ↗
(
	it: MysqlCredentials,
)

Source from the content-addressed store, hash-verified

739};
740
741export const connectToMySQL = async (
742 it: MysqlCredentials,
743): Promise<{
744 db: DB;
745 packageName: 'mysql2' | '@planetscale/database';
746 proxy: Proxy;
747 transactionProxy: TransactionProxy;
748 database: string;
749 migrate: (config: MigrationConfig) => Promise<void>;
750}> => {
751 const result = parseMysqlCredentials(it);
752
753 if (await checkPackage('mysql2')) {
754 const { createConnection } = await import('mysql2/promise');
755 const { drizzle } = await import('drizzle-orm/mysql2');
756 const { migrate } = await import('drizzle-orm/mysql2/migrator');
757
758 const connection = result.url
759 ? await createConnection(result.url)
760 : await createConnection(result.credentials!); // needed for some reason!
761
762 const db = drizzle(connection);
763 const migrateFn = async (config: MigrationConfig) => {
764 return migrate(db, config);
765 };
766
767 const typeCast = (field: any, next: any) => {
768 if (field.type === 'TIMESTAMP' || field.type === 'DATETIME' || field.type === 'DATE') {
769 return field.string();
770 }
771 return next();
772 };
773
774 await connection.connect();
775 const query: DB['query'] = async <T>(
776 sql: string,
777 params?: any[],
778 ): Promise<T[]> => {
779 const res = await connection.execute({
780 sql,
781 values: params,
782 typeCast,
783 });
784 return res[0] as any;
785 };
786
787 const proxy: Proxy = async (params: ProxyParams) => {
788 const result = await connection.query({
789 sql: params.sql,
790 values: params.params,
791 rowsAsArray: params.mode === 'array',
792 typeCast,
793 });
794 return result[0] as any[];
795 };
796
797 const transactionProxy: TransactionProxy = async (queries) => {
798 const results: any[] = [];

Callers 4

drizzleForMySQLFunction · 0.85
schema.tsFile · 0.85
mysqlPushFunction · 0.85
introspectMysqlFunction · 0.85

Calls 3

checkPackageFunction · 0.90
parseMysqlCredentialsFunction · 0.85
drizzleFunction · 0.50

Tested by

no test coverage detected