Skip to content

Commit

Permalink
update and migrate drizzle
Browse files Browse the repository at this point in the history
  • Loading branch information
ahkhanjani committed Nov 6, 2024
1 parent 867d30a commit 318b4bf
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 309 deletions.
4 changes: 2 additions & 2 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@
"@trpc/server": "11.0.0-rc.608",
"@types/fs-extra": "^11.0.4",
"@types/node": "^20.17.6",
"drizzle-kit": "^0.24.2",
"drizzle-orm": "^0.33.0",
"drizzle-kit": "^0.28.0",
"drizzle-orm": "^0.36.1",
"mysql2": "^3.11.3",
"next": "^15.0.2",
"next-auth": "5.0.0-beta.25",
Expand Down
1 change: 1 addition & 0 deletions cli/template/extras/config/drizzle-config-mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export default {
dbCredentials: {
url: env.DATABASE_URL,
},
casing: "snake_case",
tablesFilter: ["project1_*"],
} satisfies Config;
1 change: 1 addition & 0 deletions cli/template/extras/config/drizzle-config-postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export default {
dbCredentials: {
url: env.DATABASE_URL,
},
casing: "snake_case",
tablesFilter: ["project1_*"],
} satisfies Config;
1 change: 1 addition & 0 deletions cli/template/extras/config/drizzle-config-sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export default {
dbCredentials: {
url: env.DATABASE_URL,
},
casing: "snake_case",
tablesFilter: ["project1_*"],
} satisfies Config;
21 changes: 8 additions & 13 deletions cli/template/extras/src/server/db/schema-drizzle/base-mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
// https://orm.drizzle.team/docs/sql-schema-declaration

import { sql } from "drizzle-orm";
import {
bigint,
index,
mysqlTableCreator,
timestamp,
varchar,
} from "drizzle-orm/mysql-core";
import { index, mysqlTableCreator } from "drizzle-orm/mysql-core";

/**
* This is an example of how to use the multi-project schema feature of Drizzle ORM. Use the same
Expand All @@ -20,14 +14,15 @@ export const createTable = mysqlTableCreator((name) => `project1_${name}`);

export const posts = createTable(
"post",
{
id: bigint("id", { mode: "number" }).primaryKey().autoincrement(),
name: varchar("name", { length: 256 }),
createdAt: timestamp("created_at")
(t) => ({
id: t.bigint({ mode: "number" }).primaryKey().autoincrement(),
name: t.varchar({ length: 256 }),
createdAt: t
.timestamp()
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp("updated_at").onUpdateNow(),
},
updatedAt: t.timestamp().onUpdateNow(),
}),
(example) => ({
nameIndex: index("name_idx").on(example.name),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
// https://orm.drizzle.team/docs/sql-schema-declaration

import { sql } from "drizzle-orm";
import {
bigint,
index,
mysqlTableCreator,
timestamp,
varchar,
} from "drizzle-orm/mysql-core";
import { index, mysqlTableCreator } from "drizzle-orm/mysql-core";

/**
* This is an example of how to use the multi-project schema feature of Drizzle ORM. Use the same
Expand All @@ -20,14 +14,15 @@ export const createTable = mysqlTableCreator((name) => `project1_${name}`);

export const posts = createTable(
"post",
{
id: bigint("id", { mode: "number" }).primaryKey().autoincrement(),
name: varchar("name", { length: 256 }),
createdAt: timestamp("created_at")
(t) => ({
id: t.bigint({ mode: "number" }).primaryKey().autoincrement(),
name: t.varchar({ length: 256 }),
createdAt: t
.timestamp()
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp("updated_at").onUpdateNow(),
},
updatedAt: t.timestamp().onUpdateNow(),
}),
(example) => ({
nameIndex: index("name_idx").on(example.name),
})
Expand Down
27 changes: 9 additions & 18 deletions cli/template/extras/src/server/db/schema-drizzle/base-postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
// https://orm.drizzle.team/docs/sql-schema-declaration

import { sql } from "drizzle-orm";
import {
index,
integer,
pgTableCreator,
timestamp,
varchar,
} from "drizzle-orm/pg-core";
import { index, pgTableCreator } from "drizzle-orm/pg-core";

/**
* This is an example of how to use the multi-project schema feature of Drizzle ORM. Use the same
Expand All @@ -20,17 +14,14 @@ export const createTable = pgTableCreator((name) => `project1_${name}`);

export const posts = createTable(
"post",
{
id: integer("id").primaryKey().generatedByDefaultAsIdentity(),
name: varchar("name", { length: 256 }),
createdAt: timestamp("created_at", { withTimezone: true })
(t) => ({
id: t.integer().primaryKey().generatedByDefaultAsIdentity(),
name: t.varchar({ length: 256 }),
createdAt: t
.timestamp({ withTimezone: true })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true }).$onUpdate(
() => new Date()
),
},
(example) => ({
nameIndex: index("name_idx").on(example.name),
})
updatedAt: t.timestamp({ withTimezone: true }).$onUpdate(() => new Date()),
}),
(example) => [index("name_idx").on(example.name)]
);
17 changes: 8 additions & 9 deletions cli/template/extras/src/server/db/schema-drizzle/base-sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// https://orm.drizzle.team/docs/sql-schema-declaration

import { sql } from "drizzle-orm";
import { index, int, sqliteTableCreator, text } from "drizzle-orm/sqlite-core";
import { index, sqliteTableCreator } from "drizzle-orm/sqlite-core";

/**
* This is an example of how to use the multi-project schema feature of Drizzle ORM. Use the same
Expand All @@ -14,16 +14,15 @@ export const createTable = sqliteTableCreator((name) => `project1_${name}`);

export const posts = createTable(
"post",
{
id: int("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
name: text("name", { length: 256 }),
createdAt: int("created_at", { mode: "timestamp" })
(t) => ({
id: t.integer({ mode: "number" }).primaryKey({ autoIncrement: true }),
name: t.text({ length: 256 }),
createdAt: t
.integer({ mode: "timestamp" })
.default(sql`(unixepoch())`)
.notNull(),
updatedAt: int("updated_at", { mode: "timestamp" }).$onUpdate(
() => new Date()
),
},
updatedAt: t.integer({ mode: "timestamp" }).$onUpdate(() => new Date()),
}),
(example) => ({
nameIndex: index("name_idx").on(example.name),
})
Expand Down
104 changes: 48 additions & 56 deletions cli/template/extras/src/server/db/schema-drizzle/with-auth-mysql.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { relations, sql } from "drizzle-orm";
import {
bigint,
index,
int,
mysqlTableCreator,
primaryKey,
text,
timestamp,
varchar,
} from "drizzle-orm/mysql-core";
import { index, mysqlTableCreator, primaryKey } from "drizzle-orm/mysql-core";
import { type AdapterAccount } from "next-auth/adapters";

/**
Expand All @@ -21,36 +12,41 @@ export const createTable = mysqlTableCreator((name) => `project1_${name}`);

export const posts = createTable(
"post",
{
id: bigint("id", { mode: "number" }).primaryKey().autoincrement(),
name: varchar("name", { length: 256 }),
createdById: varchar("created_by", { length: 255 })
(t) => ({
id: t.bigint({ mode: "number" }).primaryKey().autoincrement(),
name: t.varchar({ length: 256 }),
createdById: t
.varchar("created_by", { length: 255 })
.notNull()
.references(() => users.id),
createdAt: timestamp("created_at")
createdAt: t
.timestamp()
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp("updated_at").onUpdateNow(),
},
updatedAt: t.timestamp().onUpdateNow(),
}),
(example) => ({
createdByIdIdx: index("created_by_idx").on(example.createdById),
nameIndex: index("name_idx").on(example.name),
})
);

export const users = createTable("user", {
id: varchar("id", { length: 255 })
export const users = createTable("user", (t) => ({
id: t
.varchar({ length: 255 })
.notNull()
.primaryKey()
.$defaultFn(() => crypto.randomUUID()),
name: varchar("name", { length: 255 }),
email: varchar("email", { length: 255 }).notNull(),
emailVerified: timestamp("email_verified", {
mode: "date",
fsp: 3,
}).default(sql`CURRENT_TIMESTAMP(3)`),
image: varchar("image", { length: 255 }),
});
name: t.varchar({ length: 255 }),
email: t.varchar({ length: 255 }).notNull(),
emailVerified: t
.timestamp({
mode: "date",
fsp: 3,
})
.default(sql`CURRENT_TIMESTAMP(3)`),
image: t.varchar({ length: 255 }),
}));

export const usersRelations = relations(users, ({ many }) => ({
accounts: many(accounts),
Expand All @@ -59,25 +55,22 @@ export const usersRelations = relations(users, ({ many }) => ({

export const accounts = createTable(
"account",
{
userId: varchar("user_id", { length: 255 })
(t) => ({
userId: t
.varchar({ length: 255 })
.notNull()
.references(() => users.id),
type: varchar("type", { length: 255 })
.$type<AdapterAccount["type"]>()
.notNull(),
provider: varchar("provider", { length: 255 }).notNull(),
providerAccountId: varchar("provider_account_id", {
length: 255,
}).notNull(),
refresh_token: text("refresh_token"),
access_token: text("access_token"),
expires_at: int("expires_at"),
token_type: varchar("token_type", { length: 255 }),
scope: varchar("scope", { length: 255 }),
id_token: text("id_token"),
session_state: varchar("session_state", { length: 255 }),
},
type: t.varchar({ length: 255 }).$type<AdapterAccount["type"]>().notNull(),
provider: t.varchar({ length: 255 }).notNull(),
providerAccountId: t.varchar({ length: 255 }).notNull(),
refresh_token: t.text(),
access_token: t.text(),
expires_at: t.int(),
token_type: t.varchar({ length: 255 }),
scope: t.varchar({ length: 255 }),
id_token: t.text(),
session_state: t.varchar({ length: 255 }),
}),
(account) => ({
compoundKey: primaryKey({
columns: [account.provider, account.providerAccountId],
Expand All @@ -92,15 +85,14 @@ export const accountsRelations = relations(accounts, ({ one }) => ({

export const sessions = createTable(
"session",
{
sessionToken: varchar("session_token", { length: 255 })
.notNull()
.primaryKey(),
userId: varchar("user_id", { length: 255 })
(t) => ({
sessionToken: t.varchar({ length: 255 }).notNull().primaryKey(),
userId: t
.varchar({ length: 255 })
.notNull()
.references(() => users.id),
expires: timestamp("expires", { mode: "date" }).notNull(),
},
expires: t.timestamp({ mode: "date" }).notNull(),
}),
(session) => ({
userIdIdx: index("session_user_id_idx").on(session.userId),
})
Expand All @@ -112,11 +104,11 @@ export const sessionsRelations = relations(sessions, ({ one }) => ({

export const verificationTokens = createTable(
"verification_token",
{
identifier: varchar("identifier", { length: 255 }).notNull(),
token: varchar("token", { length: 255 }).notNull(),
expires: timestamp("expires", { mode: "date" }).notNull(),
},
(t) => ({
identifier: t.varchar({ length: 255 }).notNull(),
token: t.varchar({ length: 255 }).notNull(),
expires: t.timestamp({ mode: "date" }).notNull(),
}),
(vt) => ({
compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
})
Expand Down
Loading

0 comments on commit 318b4bf

Please sign in to comment.