Skip to content

Commit 38febff

Browse files
authored
Support for mysql in the visualizer (#36)
* add mysql support in visualizer
1 parent ce1539c commit 38febff

File tree

13 files changed

+306
-23
lines changed

13 files changed

+306
-23
lines changed

Diff for: apps/cli/example/mysql/drizzle.config.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* This is the configuration for the server-side database.
3+
*/
4+
5+
import { defineConfig } from "drizzle-kit";
6+
7+
const base = "./example/mysql";
8+
9+
export default defineConfig({
10+
dialect: "mysql",
11+
dbCredentials: {
12+
url: process.env.ADMIN_DATABASE_URL!,
13+
},
14+
schema: `${base}/schema.ts`,
15+
out: `${base}/migrations`,
16+
verbose: false,
17+
schemaFilter: ["public"],
18+
casing: "snake_case",
19+
migrations: {
20+
prefix: "timestamp",
21+
},
22+
});

Diff for: apps/cli/example/mysql/schema.ts

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { randomUUID } from "crypto";
2+
3+
import { relations, sql, getTableColumns } from "drizzle-orm";
4+
import {
5+
mysqlTable,
6+
serial,
7+
text,
8+
int,
9+
timestamp,
10+
json,
11+
foreignKey,
12+
primaryKey,
13+
check,
14+
mysqlView,
15+
} from "drizzle-orm/mysql-core";
16+
17+
export const users = mysqlTable("users", {
18+
id: serial("id").primaryKey(),
19+
name: text("name"),
20+
});
21+
22+
export const usersRelations = relations(users, ({ many }) => ({
23+
author: many(posts, { relationName: "author" }),
24+
reviewer: many(posts, { relationName: "reviewer" }),
25+
}));
26+
27+
function generateSlug() {
28+
return randomUUID();
29+
}
30+
31+
type PostMetadata = {
32+
source: "mobile_app" | "web_app";
33+
value: {
34+
id: string;
35+
tags: string[];
36+
};
37+
};
38+
39+
export const posts = mysqlTable(
40+
"posts",
41+
{
42+
id: serial("id"),
43+
slug: text("slug")
44+
.notNull()
45+
.$default(() => generateSlug()),
46+
status: text("status", { enum: ["draft", "published"] })
47+
.default("draft")
48+
.notNull(),
49+
content: text("content"),
50+
authorId: int("author_id")
51+
.references(() => users.id)
52+
.notNull(),
53+
reviewerId: int("reviewer_id"),
54+
createdAt: timestamp("created_at", {
55+
mode: "string",
56+
}).defaultNow(),
57+
metadata: json("metadata").$type<PostMetadata>(),
58+
metadata2: json("metadata2")
59+
.$type<PostMetadata>()
60+
.default({
61+
source: "mobile_app",
62+
value: {
63+
id: "123",
64+
tags: ["tag1", "tag2"],
65+
},
66+
}),
67+
},
68+
(t) => ({
69+
p: primaryKey({ name: "my pk", columns: [t.id, t.slug] }),
70+
f: foreignKey({
71+
name: "my fk",
72+
columns: [t.authorId],
73+
foreignColumns: [users.id],
74+
}),
75+
c: check("not draft", sql`status <> 'draft'`),
76+
})
77+
);
78+
export const postsView = mysqlView("posts_view").as((qb) =>
79+
qb
80+
.select({
81+
...getTableColumns(posts),
82+
})
83+
.from(posts)
84+
);
85+
86+
export const postsViewUnsecured = mysqlView("posts_view_unsecured").as((qb) =>
87+
qb
88+
.select({
89+
...getTableColumns(posts),
90+
})
91+
.from(posts)
92+
);
93+
94+
export const postsRelations = relations(posts, ({ one }) => ({
95+
author: one(users, {
96+
fields: [posts.authorId],
97+
references: [users.id],
98+
relationName: "author",
99+
}),
100+
reviewer: one(users, {
101+
fields: [posts.reviewerId],
102+
references: [users.id],
103+
relationName: "reviewer",
104+
}),
105+
}));

Diff for: apps/cli/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "drizzle-lab",
3-
"version": "0.7.0",
3+
"version": "0.8.0",
44
"description": "Drizzle Lab CLI",
55
"sideEffects": false,
66
"type": "module",

Diff for: package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/api/src/mysql/loader/database.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export async function importFromDatabase(
195195
indexes: {},
196196
foreignKeys: {},
197197
uniqueConstraints: {},
198-
checkConstraint: {},
198+
checkConstraints: {},
199199
relations: [],
200200
schema,
201201
};
@@ -414,7 +414,7 @@ AND
414414

415415
const tableInResult = result[tableName];
416416
// if (typeof tableInResult === 'undefined') continue;
417-
tableInResult.checkConstraint[constraintName] = {
417+
tableInResult.checkConstraints[constraintName] = {
418418
name: constraintName,
419419
value: constraintValue,
420420
};

Diff for: packages/api/src/mysql/schema.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const table = object({
9595
foreignKeys: record(string(), fk),
9696
compositePrimaryKeys: record(string(), compositePK),
9797
uniqueConstraints: record(string(), uniqueConstraint).default({}),
98-
checkConstraint: record(string(), checkConstraint).default({}),
98+
checkConstraints: record(string(), checkConstraint).default({}),
9999
/* lab extension */
100100
schema: string(),
101101
relations: array(relation).default([]),
@@ -411,7 +411,7 @@ export const squashSnapshot = (json: Snapshot): SnapshotSquashed => {
411411
);
412412

413413
const squashedCheckConstraints = mapValues(
414-
it[1].checkConstraint,
414+
it[1].checkConstraints,
415415
(check) => {
416416
return MySqlSquasher.squashCheck(check);
417417
},

Diff for: packages/api/src/mysql/serializer/snapshot.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ export function drizzleObjectsToSnapshot(
412412
foreignKeys: foreignKeysObject,
413413
compositePrimaryKeys: primaryKeysObject,
414414
uniqueConstraints: uniqueConstraintObject,
415-
checkConstraint: checkConstraintObject,
415+
checkConstraints: checkConstraintObject,
416416
/* lab extension */
417417
description,
418418
relations: tableRelations,

Diff for: packages/api/src/mysql/serializer/typescript.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ export function snapshotToTypeScript(snapshot: Snapshot, casing: Casing) {
158158
const uniqueImports = Object.values(it.uniqueConstraints).map(
159159
() => "unique",
160160
);
161-
const checkImports = Object.values(it.checkConstraint).map(() => "check");
161+
const checkImports = Object.values(it.checkConstraints).map(
162+
() => "check",
163+
);
162164

163165
res.mysql.push(...idxImports);
164166
res.mysql.push(...fkImpots);
@@ -267,7 +269,7 @@ export function snapshotToTypeScript(snapshot: Snapshot, casing: Casing) {
267269
filteredFKs.length > 0 ||
268270
Object.keys(table.compositePrimaryKeys).length > 0 ||
269271
Object.keys(table.uniqueConstraints).length > 0 ||
270-
Object.keys(table.checkConstraint).length > 0
272+
Object.keys(table.checkConstraints).length > 0
271273
) {
272274
statement += ",\n";
273275
statement += "(table) => {\n";
@@ -287,7 +289,7 @@ export function snapshotToTypeScript(snapshot: Snapshot, casing: Casing) {
287289
withCasing,
288290
);
289291
statement += createTableChecks(
290-
Object.values(table.checkConstraint),
292+
Object.values(table.checkConstraints),
291293
withCasing,
292294
);
293295
statement += "\t}\n";

0 commit comments

Comments
 (0)