Add Drizzle ORM
This commit is contained in:
parent
0291e6656e
commit
92d1716f7f
6 changed files with 1069 additions and 5 deletions
11
leaky-ships/drizzle.config.ts
Normal file
11
leaky-ships/drizzle.config.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
import "dotenv/config"
|
||||
import type { Config } from "drizzle-kit"
|
||||
|
||||
export default {
|
||||
schema: "./drizzle/schema.ts",
|
||||
out: "./drizzle/migrations",
|
||||
driver: "pg",
|
||||
dbCredentials: {
|
||||
connectionString: process.env.DATABASE_URL ?? "",
|
||||
},
|
||||
} satisfies Config
|
7
leaky-ships/drizzle/index.ts
Normal file
7
leaky-ships/drizzle/index.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { drizzle, PostgresJsDatabase } from "drizzle-orm/postgres-js"
|
||||
import postgres from "postgres"
|
||||
|
||||
const queryClient = postgres(process.env.DATABASE_URL ?? "")
|
||||
const db: PostgresJsDatabase = drizzle(queryClient)
|
||||
|
||||
export default db
|
131
leaky-ships/drizzle/schema.ts
Normal file
131
leaky-ships/drizzle/schema.ts
Normal file
|
@ -0,0 +1,131 @@
|
|||
import { createId } from "@paralleldrive/cuid2"
|
||||
import { relations } from "drizzle-orm"
|
||||
import {
|
||||
type AnyPgColumn,
|
||||
boolean,
|
||||
integer,
|
||||
pgTable,
|
||||
text,
|
||||
timestamp,
|
||||
} from "drizzle-orm/pg-core"
|
||||
|
||||
export const users = pgTable("users", {
|
||||
id: text("id").primaryKey().notNull(),
|
||||
})
|
||||
|
||||
export const games = pgTable("Game", {
|
||||
id: text("id").primaryKey().default(createId()),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
state: text("state").default("lobby"),
|
||||
allowSpectators: boolean("allow_spectators").default(true),
|
||||
allowSpecials: boolean("allow_specials").default(true),
|
||||
allowChat: boolean("allow_chat").default(true),
|
||||
allowMarkDraw: boolean("allow_mark_draw").default(true),
|
||||
gamePinId: text("game_pin_id").references((): AnyPgColumn => gamepins.id),
|
||||
})
|
||||
|
||||
export const gamepins = pgTable("Gamepin", {
|
||||
id: text("id").primaryKey().default(createId()),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
pin: text("pin"),
|
||||
gameId: text("game_id").references((): AnyPgColumn => games.id),
|
||||
})
|
||||
|
||||
export const ships = pgTable("Ship", {
|
||||
id: text("id").primaryKey().default(createId()),
|
||||
size: integer("size"),
|
||||
variant: integer("variant"),
|
||||
x: integer("x"),
|
||||
y: integer("y"),
|
||||
orientation: text("orientation"),
|
||||
user_GameId: text("user_game_id").references(() => user_games.id),
|
||||
})
|
||||
|
||||
export const hits = pgTable("Hit", {
|
||||
id: text("id").primaryKey().default(createId()),
|
||||
x: integer("x"),
|
||||
y: integer("y"),
|
||||
hit: boolean("hit"),
|
||||
user_GameId: text("user_game_id").references(() => user_games.id),
|
||||
})
|
||||
|
||||
export const user_games = pgTable("User_Game", {
|
||||
id: text("id").primaryKey().default(createId()),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
gameId: text("game_id").references(() => games.id),
|
||||
userId: text("user_id").references(() => users.id),
|
||||
index: integer("index"),
|
||||
})
|
||||
|
||||
export const moves = pgTable("Move", {
|
||||
id: text("id").primaryKey().default(createId()),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
index: integer("index"),
|
||||
type: text("type"),
|
||||
x: integer("x"),
|
||||
y: integer("y"),
|
||||
orientation: text("orientation"),
|
||||
user_game_id: text("user_game_id").references(() => user_games.id),
|
||||
})
|
||||
|
||||
export const chats = pgTable("Chat", {
|
||||
id: text("id").primaryKey().default(createId()),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
message: text("message"),
|
||||
event: text("event"),
|
||||
user_game_id: text("user_game_id").references(() => user_games.id),
|
||||
})
|
||||
|
||||
export const usersRelations = relations(users, ({ many }) => ({
|
||||
games: many(user_games),
|
||||
}))
|
||||
|
||||
export const gamesRelations = relations(games, ({ one, many }) => ({
|
||||
gamePin: one(gamepins, {
|
||||
fields: [games.gamePinId],
|
||||
references: [gamepins.id],
|
||||
}),
|
||||
users: many(user_games),
|
||||
}))
|
||||
|
||||
export const gamepinsRelations = relations(gamepins, ({ one }) => ({
|
||||
game: one(games, { fields: [gamepins.gameId], references: [games.id] }),
|
||||
}))
|
||||
|
||||
export const shipsRelations = relations(ships, ({ one }) => ({
|
||||
userGame: one(user_games, {
|
||||
fields: [ships.user_GameId],
|
||||
references: [user_games.id],
|
||||
}),
|
||||
}))
|
||||
|
||||
export const hitsRelations = relations(hits, ({ one }) => ({
|
||||
userGame: one(user_games, {
|
||||
fields: [hits.user_GameId],
|
||||
references: [user_games.id],
|
||||
}),
|
||||
}))
|
||||
|
||||
export const userGamesRelations = relations(user_games, ({ one, many }) => ({
|
||||
user: one(users, { fields: [user_games.userId], references: [users.id] }),
|
||||
game: one(games, { fields: [user_games.gameId], references: [games.id] }),
|
||||
moves: many(moves),
|
||||
ships: many(ships),
|
||||
hits: many(hits),
|
||||
chats: many(chats),
|
||||
}))
|
||||
|
||||
export const movesRelations = relations(moves, ({ one }) => ({
|
||||
userGame: one(user_games, {
|
||||
fields: [moves.user_game_id],
|
||||
references: [user_games.id],
|
||||
}),
|
||||
}))
|
||||
|
||||
export const chatsRelations = relations(chats, ({ one }) => ({
|
||||
userGame: one(user_games, {
|
||||
fields: [chats.user_game_id],
|
||||
references: [user_games.id],
|
||||
}),
|
||||
}))
|
|
@ -19,9 +19,11 @@
|
|||
"@fortawesome/sharp-solid-svg-icons": "^6.4.0",
|
||||
"@next-auth/prisma-adapter": "^1.0.7",
|
||||
"@next/font": "13.1.1",
|
||||
"@paralleldrive/cuid2": "^2.2.1",
|
||||
"@prisma/client": "^4.15.0",
|
||||
"classnames": "^2.3.2",
|
||||
"colors": "^1.4.0",
|
||||
"drizzle-orm": "^0.27.0",
|
||||
"eslint": "8.31.0",
|
||||
"eslint-config-next": "13.1.1",
|
||||
"http-status": "^1.6.2",
|
||||
|
@ -29,6 +31,7 @@
|
|||
"next": "13.1.1",
|
||||
"next-auth": "^4.22.1",
|
||||
"nodemailer": "^6.9.3",
|
||||
"postgres": "^3.3.5",
|
||||
"prisma": "^4.15.0",
|
||||
"react": "18.2.0",
|
||||
"react-colorful": "^5.6.1",
|
||||
|
@ -51,7 +54,10 @@
|
|||
"@types/react-dom": "^18.2.4",
|
||||
"@types/web-bluetooth": "^0.0.16",
|
||||
"autoprefixer": "^10.4.14",
|
||||
"dotenv": "^16.3.1",
|
||||
"drizzle-kit": "^0.19.3",
|
||||
"eslint-config-prettier": "^8.8.0",
|
||||
"pg": "^8.11.1",
|
||||
"postcss": "^8.4.24",
|
||||
"prettier": "^2.8.8",
|
||||
"prettier-plugin-tailwindcss": "^0.2.8",
|
||||
|
|
917
leaky-ships/pnpm-lock.yaml
generated
917
leaky-ships/pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load diff
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"target": "es6",
|
||||
"lib": ["dom", "dom.iterable", "esnext"],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue