← Files RivetARCHIVED FILE

node_modules/drizzle-orm/sqlite-core/foreign-keys.cjs

2.16 KB · Nov 5, 2026 · 12:09 UTC

↓ Download file

Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
let __entity_ts = require("../entity.cjs");
let __table_utils_ts = require("../table.utils.cjs");

//#region src/sqlite-core/foreign-keys.ts
var ForeignKeyBuilder = class {
	static [__entity_ts.entityKind] = "SQLiteForeignKeyBuilder";
	/** @internal */
	reference;
	/** @internal */
	_onUpdate;
	/** @internal */
	_onDelete;
	constructor(config, actions) {
		this.reference = () => {
			const { name, columns, foreignColumns } = config();
			return {
				name,
				columns,
				foreignTable: foreignColumns[0].table,
				foreignColumns
			};
		};
		if (actions) {
			this._onUpdate = actions.onUpdate;
			this._onDelete = actions.onDelete;
		}
	}
	onUpdate(action) {
		this._onUpdate = action;
		return this;
	}
	onDelete(action) {
		this._onDelete = action;
		return this;
	}
	/** @internal */
	build(table) {
		return new ForeignKey(table, this);
	}
};
var ForeignKey = class {
	static [__entity_ts.entityKind] = "SQLiteForeignKey";
	reference;
	onUpdate;
	onDelete;
	constructor(table, builder) {
		this.table = table;
		this.reference = builder.reference;
		this.onUpdate = builder._onUpdate;
		this.onDelete = builder._onDelete;
	}
	getName() {
		const { name, columns, foreignColumns } = this.reference();
		const columnNames = columns.map((column) => column.name);
		const foreignColumnNames = foreignColumns.map((column) => column.name);
		const chunks = [
			this.table[__table_utils_ts.TableName],
			...columnNames,
			foreignColumns[0].table[__table_utils_ts.TableName],
			...foreignColumnNames
		];
		return name ?? `${chunks.join("_")}_fk`;
	}
	isNameExplicit() {
		return !!this.reference().name;
	}
};
function foreignKey(config) {
	function mappedConfig() {
		if (typeof config === "function") {
			const { name, columns, foreignColumns } = config();
			return {
				name,
				columns,
				foreignColumns
			};
		}
		return config;
	}
	return new ForeignKeyBuilder(mappedConfig);
}

//#endregion
exports.ForeignKey = ForeignKey;
exports.ForeignKeyBuilder = ForeignKeyBuilder;
exports.foreignKey = foreignKey;
//# sourceMappingURL=foreign-keys.cjs.map

SHA-256: 37f691e2ef18e467c5dd6a489512646aa6803547eb1d93f9ceb3a5556780a59f