Add migration for movies table: create a new movies table with necessary fields, migrate existing data from the old movies table, and update database schema to reflect changes.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
import { drizzle } from "drizzle-orm/libsql";
|
||||
import { movies } from "./schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { Movie } from "@/types/global";
|
||||
|
||||
const db = drizzle(process.env.DB_FILE_NAME!);
|
||||
|
||||
@@ -9,17 +10,20 @@ export const getMovies = async () => {
|
||||
return await db.select().from(movies).$withCache();
|
||||
};
|
||||
|
||||
export const addMovie = async (movie: typeof movies.$inferInsert) => {
|
||||
await db.insert(movies).values(movie).onConflictDoNothing();
|
||||
export const addMovie = async (movie: Movie) => {
|
||||
await db
|
||||
.insert(movies)
|
||||
.values({
|
||||
...movie,
|
||||
genre_ids: JSON.stringify(movie.genre_ids),
|
||||
})
|
||||
.onConflictDoNothing();
|
||||
};
|
||||
|
||||
export const deleteMovie = async (id: number) => {
|
||||
await db.delete(movies).where(eq(movies.id, id));
|
||||
};
|
||||
|
||||
export const updateMovie = async (
|
||||
id: number,
|
||||
movie: Partial<typeof movies.$inferInsert>
|
||||
) => {
|
||||
await db.update(movies).set(movie).where(eq(movies.id, id));
|
||||
export const updateMovie = async (movieId: number, movie: Partial<Movie>) => {
|
||||
await db.update(movies).set(movie).where(eq(movies.id, movieId));
|
||||
};
|
||||
|
||||
@@ -3,11 +3,18 @@ import { integer, real, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
||||
export const movies = sqliteTable("movies", {
|
||||
id: integer("id").primaryKey(),
|
||||
title: text("title").notNull(),
|
||||
adult: integer("adult", { mode: "boolean" }).notNull(),
|
||||
backdrop_path: text("backdrop_path").notNull(),
|
||||
genre_ids: text("genre_ids").notNull(),
|
||||
original_language: text("original_language").notNull(),
|
||||
original_title: text("original_title").notNull(),
|
||||
overview: text("overview").notNull(),
|
||||
popularity: real("popularity").notNull(),
|
||||
releaseDate: text("release_date").notNull(),
|
||||
posterPath: text("poster_path").notNull(),
|
||||
seen: integer("seen").default(0).notNull(),
|
||||
favorite: integer("favorite").default(0).notNull(),
|
||||
notes: text("notes").default("").notNull(),
|
||||
poster_path: text("poster_path").notNull(),
|
||||
release_date: text("release_date").notNull(),
|
||||
video: integer("video", { mode: "boolean" }).notNull(),
|
||||
vote_average: real("vote_average").notNull(),
|
||||
vote_count: integer("vote_count").notNull(),
|
||||
seen: integer("seen", { mode: "boolean" }).default(false),
|
||||
favorite: integer("favorite", { mode: "boolean" }).default(false),
|
||||
});
|
||||
|
||||
4
src/types/global.d.ts
vendored
Normal file
4
src/types/global.d.ts
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
import { movies } from "@/lib/db/schema";
|
||||
import { SearchResult } from "@/lib/tmdb/types";
|
||||
|
||||
type Movie = typeof movies.$inferSelect;
|
||||
Reference in New Issue
Block a user