Home / Class/ SingleStoreDialect Class — drizzle-orm Architecture

SingleStoreDialect Class — drizzle-orm Architecture

Architecture documentation for the SingleStoreDialect class in dialect.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  1da3f399_3da2_55ff_342e_fb92de5e010c["SingleStoreDialect"]
  52aacec5_24ec_0169_7fbd_21dd56996746["dialect.ts"]
  1da3f399_3da2_55ff_342e_fb92de5e010c -->|defined in| 52aacec5_24ec_0169_7fbd_21dd56996746
  a966cae1_83e3_097d_f142_1835fe00928c["constructor()"]
  1da3f399_3da2_55ff_342e_fb92de5e010c -->|method| a966cae1_83e3_097d_f142_1835fe00928c
  858ed298_39e8_cc51_0441_6e5b1d8c2025["migrate()"]
  1da3f399_3da2_55ff_342e_fb92de5e010c -->|method| 858ed298_39e8_cc51_0441_6e5b1d8c2025
  10c0e32c_8214_ee3f_190d_19b8c845ead4["escapeName()"]
  1da3f399_3da2_55ff_342e_fb92de5e010c -->|method| 10c0e32c_8214_ee3f_190d_19b8c845ead4
  d33607ca_d449_7fc5_9fe6_b9de544fe865["escapeParam()"]
  1da3f399_3da2_55ff_342e_fb92de5e010c -->|method| d33607ca_d449_7fc5_9fe6_b9de544fe865
  c2401330_81cb_8373_59ce_c352fb2dc8ce["escapeString()"]
  1da3f399_3da2_55ff_342e_fb92de5e010c -->|method| c2401330_81cb_8373_59ce_c352fb2dc8ce
  4a664942_d2a4_176d_a746_ecda030d3d74["buildWithCTE()"]
  1da3f399_3da2_55ff_342e_fb92de5e010c -->|method| 4a664942_d2a4_176d_a746_ecda030d3d74
  4c2e54df_cf4a_0fab_693f_3d09bedc5a55["buildDeleteQuery()"]
  1da3f399_3da2_55ff_342e_fb92de5e010c -->|method| 4c2e54df_cf4a_0fab_693f_3d09bedc5a55
  ee86c4e3_48eb_8ec8_d0c7_76b611607d8f["buildUpdateSet()"]
  1da3f399_3da2_55ff_342e_fb92de5e010c -->|method| ee86c4e3_48eb_8ec8_d0c7_76b611607d8f
  675693c4_8831_5655_52c6_ac204a0b0346["buildUpdateQuery()"]
  1da3f399_3da2_55ff_342e_fb92de5e010c -->|method| 675693c4_8831_5655_52c6_ac204a0b0346
  625dcfbf_0612_f909_2143_34d4e88bb50c["buildSelection()"]
  1da3f399_3da2_55ff_342e_fb92de5e010c -->|method| 625dcfbf_0612_f909_2143_34d4e88bb50c
  27bb2127_ce03_aaf8_abe2_0189af2b27e5["buildLimit()"]
  1da3f399_3da2_55ff_342e_fb92de5e010c -->|method| 27bb2127_ce03_aaf8_abe2_0189af2b27e5
  20a80e8b_02c4_b4c7_180f_3bc7c4c5fc21["buildOrderBy()"]
  1da3f399_3da2_55ff_342e_fb92de5e010c -->|method| 20a80e8b_02c4_b4c7_180f_3bc7c4c5fc21
  3f459852_55f4_d544_4624_42cc8b2004a6["buildSelectQuery()"]
  1da3f399_3da2_55ff_342e_fb92de5e010c -->|method| 3f459852_55f4_d544_4624_42cc8b2004a6

Relationship Graph

Source Code

drizzle-orm/src/singlestore-core/dialect.ts lines 43–834

export class SingleStoreDialect {
	static readonly [entityKind]: string = 'SingleStoreDialect';

	/** @internal */
	readonly casing: CasingCache;

	constructor(config?: SingleStoreDialectConfig) {
		this.casing = new CasingCache(config?.casing);
	}

	async migrate(
		migrations: MigrationMeta[],
		session: SingleStoreSession,
		config: Omit<MigrationConfig, 'migrationsSchema'>,
	): Promise<void> {
		const migrationsTable = config.migrationsTable ?? '__drizzle_migrations';
		const migrationTableCreate = sql`
			create table if not exists ${sql.identifier(migrationsTable)} (
				id serial primary key,
				hash text not null,
				created_at bigint
			)
		`;
		await session.execute(migrationTableCreate);

		const dbMigrations = await session.all<{ id: number; hash: string; created_at: string }>(
			sql`select id, hash, created_at from ${sql.identifier(migrationsTable)} order by created_at desc limit 1`,
		);

		const lastDbMigration = dbMigrations[0];

		await session.transaction(async (tx) => {
			for (const migration of migrations) {
				if (
					!lastDbMigration
					|| Number(lastDbMigration.created_at) < migration.folderMillis
				) {
					for (const stmt of migration.sql) {
						await tx.execute(sql.raw(stmt));
					}
					await tx.execute(
						sql`insert into ${
							sql.identifier(migrationsTable)
						} (\`hash\`, \`created_at\`) values(${migration.hash}, ${migration.folderMillis})`,
					);
				}
			}
		});
	}

	escapeName(name: string): string {
		return `\`${name}\``;
	}

	escapeParam(_num: number): string {
		return `?`;
	}

	escapeString(str: string): string {
		return `'${str.replace(/'/g, "''")}'`;
	}

	private buildWithCTE(queries: Subquery[] | undefined): SQL | undefined {
		if (!queries?.length) return undefined;

		const withSqlChunks = [sql`with `];
		for (const [i, w] of queries.entries()) {
			withSqlChunks.push(sql`${sql.identifier(w._.alias)} as (${w._.sql})`);
			if (i < queries.length - 1) {
				withSqlChunks.push(sql`, `);
			}
		}
		withSqlChunks.push(sql` `);
		return sql.join(withSqlChunks);
	}

	buildDeleteQuery({ table, where, returning, withList, limit, orderBy }: SingleStoreDeleteConfig): SQL {
		const withSql = this.buildWithCTE(withList);

		const returningSql = returning
			? sql` returning ${this.buildSelection(returning, { isSingleTable: true })}`

Domain

Frequently Asked Questions

What is the SingleStoreDialect class?
SingleStoreDialect is a class in the drizzle-orm codebase, defined in drizzle-orm/src/singlestore-core/dialect.ts.
Where is SingleStoreDialect defined?
SingleStoreDialect is defined in drizzle-orm/src/singlestore-core/dialect.ts at line 43.

Analyze Your Own Codebase

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

Try Supermodel Free