generated from cp-20/nextjs-with-mantine-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from cp-20/replace-prisma-with-drizzle-orm
PrismaをDrizzle ORMでリプレイス
- Loading branch information
Showing
15 changed files
with
724 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { drizzle } from 'drizzle-orm/postgres-js'; | ||
import postgres from 'postgres'; | ||
import * as models from './models'; | ||
|
||
const connectionString = process.env.DATABASE_URL; | ||
if (connectionString === undefined) { | ||
throw new Error('DATABASE_URL is not defined'); | ||
} | ||
|
||
const client = postgres(connectionString); | ||
|
||
export const db = drizzle(client, { schema: models }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// 全体的にテーブル設計を見直したい | ||
// 特にDB側はスネークケースで統一したい | ||
|
||
import { relations } from 'drizzle-orm'; | ||
import { | ||
pgTable, | ||
serial, | ||
text, | ||
varchar, | ||
integer, | ||
timestamp, | ||
unique, | ||
primaryKey, | ||
} from 'drizzle-orm/pg-core'; | ||
|
||
export const users = pgTable('users', { | ||
id: varchar('id').primaryKey(), | ||
email: text('email').notNull(), | ||
name: varchar('name', { length: 1024 }).notNull(), | ||
displayName: varchar('displayName', { length: 1024 }), | ||
avatarUrl: text('avatarUrl'), | ||
createdAt: timestamp('createdAt', { mode: 'date' }).notNull().defaultNow(), | ||
updatedAt: timestamp('updatedAt', { mode: 'date' }).notNull(), | ||
}); | ||
|
||
export const clips = pgTable( | ||
'clips', | ||
{ | ||
id: serial('id').primaryKey(), | ||
status: integer('status').notNull(), | ||
progress: integer('progress').notNull(), | ||
comment: text('comment'), | ||
articleId: integer('articleId') | ||
.notNull() | ||
.references(() => articles.id), | ||
// authorIdにしたい | ||
authorId: varchar('userId') | ||
.notNull() | ||
.references(() => users.id), | ||
createdAt: timestamp('createdAt', { mode: 'date' }).notNull().defaultNow(), | ||
updatedAt: timestamp('updatedAt', { mode: 'date' }).notNull(), | ||
}, | ||
(t) => ({ | ||
unique: unique('article_and_author').on(t.articleId, t.authorId), | ||
}), | ||
); | ||
|
||
export const clipRelations = relations(clips, ({ one }) => ({ | ||
article: one(articles, { | ||
fields: [clips.articleId], | ||
references: [articles.id], | ||
}), | ||
})); | ||
|
||
export const articles = pgTable('article', { | ||
id: serial('id').primaryKey(), | ||
url: text('url').notNull().unique(), | ||
title: text('title').notNull(), | ||
body: text('body').notNull(), | ||
ogImageUrl: text('ogImageUrl'), | ||
summary: text('summary'), | ||
createdAt: timestamp('createdAt', { mode: 'date' }).notNull().defaultNow(), | ||
// updatedAt: timestamp('updatedAt', { mode: 'date' }).notNull(), | ||
}); | ||
|
||
export const articleRefs = pgTable( | ||
'articleRef', | ||
{ | ||
referFrom: integer('referFrom') | ||
.notNull() | ||
.references(() => articles.id), | ||
referTo: integer('referTo') | ||
.notNull() | ||
.references(() => articles.id), | ||
createdAt: timestamp('createdAt', { mode: 'date' }).notNull().defaultNow(), | ||
updatedAt: timestamp('updatedAt', { mode: 'date' }).notNull(), | ||
}, | ||
(t) => ({ | ||
unique: primaryKey(t.referFrom, t.referTo), | ||
}), | ||
); | ||
|
||
export const tags = pgTable('tags', { | ||
id: serial('id').primaryKey(), | ||
name: text('name').unique(), | ||
articleId: integer('articleId') | ||
.notNull() | ||
.references(() => articles.id), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.