drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: SQL-in-JS for Schemas

Open
#2,909 2 comments 1 reaction 0 assignees View on GitHub
drizzle/kit enhancement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Describe what you want

I am considering migrating from [Ley migrations](https://github.com/lukeed/ley) to [Drizzle Kit](https://orm.drizzle.team/kit-docs/commands) migrations using:

1. [`drizzle-kit generate`](https://orm.drizzle.team/kit-docs/commands#generate-migrations) on push to PR
2. [`drizzle-kit migrate`](https://orm.drizzle.team/kit-docs/commands#apply-migrations) on deploy

My "schemas" are currently in these Ley migrations (`sql` identifier coming from [Postgres.js](https://github.com/porsager/postgres)), in SQL-in-JS:

`migrations/084-campuses.ts`

```ts
import { Sql } from 'postgres';

export async function up(sql: Sql) {
await sql`
CREATE TABLE campuses (
id integer PRIMARY KEY,
-- The slug is a lowercased UN/LOCODE for the city
slug varchar(5) NOT NULL,
address varchar(100) NOT NULL,
city varchar(50) NOT NULL,
-- Country subdivision (province or state)
subdivision varchar(80),
country varchar(30) NOT NULL,
postal_code varchar(16) NOT NULL,
-- ISO 3166-2 code for the country subdivision (province or state)
iso31662_code varchar(6) NOT NULL,
student_access_hours varchar(75) NOT NULL,
time_zone_id integer NOT NULL REFERENCES time_zones (id),
UNIQUE (city, country),
CONSTRAINT iso31662_code_format CHECK (
iso31662_code ~ '^[A-Z]{2}-[A-Z0-9]{1,3}$'
)
)
`;
}

export async function down(sql: Sql) {
await sql`DROP TABLE campuses`;
}
```

I would like to keep writing SQL-in-JS, because of the tooling and to avoid intransparency and lock-in of non-SQL, proprietary syntax.

Would Drizzle ORM + Drizzle Kit also support schema files that export variables created using SQL-in-JS? Eg. using [Drizzle's magic `sql` operator](https://orm.drizzle.team/docs/sql)?

## Alternatives Considered

One (kind of convoluted) way that I was thinking of getting around it was this:

1. Still retain a version of the TypeScript script files which call `sql`, allowing for SQL-in-JS
2. Run [`drizzle-kit introspect`](https://orm.drizzle.team/kit-docs/commands#introspect--pull) manually on the database to generate a temporary Drizzle schema file, which then can be discarded after the `drizzle-kit generate`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.