From eebd24a2d88c9da451f8e30af3b798be72bd87eb Mon Sep 17 00:00:00 2001 From: Huilensolis Date: Wed, 11 Sep 2024 15:12:32 -0300 Subject: [PATCH 1/2] fix(api): updated_at column missing $onUpdate function --- apps/api/src/config/database/config.ts | 23 +- .../api/src/features/entry/providers/index.ts | 216 +++++++++--------- .../src/features/entry/schema/entry.schema.ts | 61 ++--- 3 files changed, 151 insertions(+), 149 deletions(-) diff --git a/apps/api/src/config/database/config.ts b/apps/api/src/config/database/config.ts index 5c0ccd3..058a2a1 100644 --- a/apps/api/src/config/database/config.ts +++ b/apps/api/src/config/database/config.ts @@ -2,7 +2,6 @@ import { drizzle } from "drizzle-orm/postgres-js"; import postgres from "postgres"; -import { sql } from "drizzle-orm"; import { Environment } from "../environment"; import * as schema from "./schema"; @@ -13,23 +12,23 @@ import * as schema from "./schema"; * */ async function getDb() { - if (Environment.NODE_ENV === "production") { - // this set up is for supabase datbase, so we disable prepare, since transaction mode doesnt support it - const client = postgres(Environment.DATABASE_URL, { prepare: false }); + if (Environment.NODE_ENV === "production") { + // this set up is for supabase datbase, so we disable prepare, since transaction mode doesnt support it + const client = postgres(Environment.DATABASE_URL, { prepare: false }); - const db = drizzle(client, { schema }); + const db = drizzle(client, { schema }); - return db; - } + return db; + } - const { POSTGRES_PASSWORD, POSTGRES_USER, POSTGRES_DATABASE } = Environment; + const { POSTGRES_PASSWORD, POSTGRES_USER, POSTGRES_DATABASE } = Environment; - const connectionString = `postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@127.0.0.1:5432/${POSTGRES_DATABASE}`; - const client = postgres(connectionString, { prepare: false }); + const connectionString = `postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@127.0.0.1:5432/${POSTGRES_DATABASE}`; + const client = postgres(connectionString, { prepare: false }); - const db = drizzle(client, { schema }); + const db = drizzle(client, { schema }); - return db; + return db; } const db = await getDb(); diff --git a/apps/api/src/features/entry/providers/index.ts b/apps/api/src/features/entry/providers/index.ts index b39f8d2..c5644a3 100644 --- a/apps/api/src/features/entry/providers/index.ts +++ b/apps/api/src/features/entry/providers/index.ts @@ -2,130 +2,130 @@ import { db } from "@/config/database"; import type { TReturnHanler } from "@/shared/models/promises"; import { and, eq, ilike, isNull } from "drizzle-orm"; import type { - TInsertEntry, - TNewEntry, - TReadEntry, + TInsertEntry, + TNewEntry, + TReadEntry, } from "../models/entry.models"; import { Entry } from "../schema"; export class EntryProvider { - static async getPrivateEntryById({ - entryId, - userId, - }: { - entryId: string; - userId: string; - }): Promise { - const [entry] = await db - .select() - .from(Entry) - .where(and(eq(Entry.id, entryId), eq(Entry.user_id, userId))); + static async getPrivateEntryById({ + entryId, + userId, + }: { + entryId: string; + userId: string; + }): Promise { + const [entry] = await db + .select() + .from(Entry) + .where(and(eq(Entry.id, entryId), eq(Entry.user_id, userId))); - return entry; - } + return entry; + } - static async getPublicEntryById({ - entryId, - }: { - entryId: string; - }): Promise { - const [entry] = await db.select().from(Entry).where(eq(Entry.id, entryId)); + static async getPublicEntryById({ + entryId, + }: { + entryId: string; + }): Promise { + const [entry] = await db.select().from(Entry).where(eq(Entry.id, entryId)); - return entry; - } + return entry; + } - static async getEntriesListByUserId(userId: string): Promise { - const entries = await db - .select() - .from(Entry) - .where(eq(Entry.user_id, userId)); + static async getEntriesListByUserId(userId: string): Promise { + const entries = await db + .select() + .from(Entry) + .where(eq(Entry.user_id, userId)); - return entries; - } + return entries; + } - static async getPrivateEntryListByTitle({ - title, - userId, - }: { - title: string; - userId: string; - }): Promise { - const entries = await db - .select() - .from(Entry) - .where(and(ilike(Entry.title, title), eq(Entry.user_id, userId))); + static async getPrivateEntryListByTitle({ + title, + userId, + }: { + title: string; + userId: string; + }): Promise { + const entries = await db + .select() + .from(Entry) + .where(and(ilike(Entry.title, title), eq(Entry.user_id, userId))); - return entries; - } + return entries; + } - static async createEntry({ - entry, - userId, - }: { - entry: TInsertEntry; - userId: TReadEntry["user_id"]; - }): Promise> { - const newEntryValues: TNewEntry = { - content: { - type: "doc", - content: [ - { - type: "paragraph", - }, - ], - }, - ...entry, - user_id: userId, - }; + static async createEntry({ + entry, + userId, + }: { + entry: TInsertEntry; + userId: TReadEntry["user_id"]; + }): Promise> { + const newEntryValues: TNewEntry = { + content: { + type: "doc", + content: [ + { + type: "paragraph", + }, + ], + }, + ...entry, + user_id: userId, + }; - try { - const [newEntry] = await db - .insert(Entry) - .values(newEntryValues) - .returning(); + try { + const [newEntry] = await db + .insert(Entry) + .values(newEntryValues) + .returning(); - if (!newEntry) throw new Error("Failed to create entry"); + if (!newEntry) throw new Error("Failed to create entry"); - return { error: null, data: newEntry }; - } catch (error) { - return { error: "error creating new entry", data: null }; - } - } + return { error: null, data: newEntry }; + } catch (error) { + return { error: "error creating new entry", data: null }; + } + } - static async deleteEntry({ - entryId, - userId, - }: { - entryId: string; - userId: string; - }): Promise<{ error: string | null }> { - try { - await db - .update(Entry) - .set({ end_date: new Date() }) - .where(and(eq(Entry.id, entryId), eq(Entry.user_id, userId))); - return { error: null }; - } catch (error) { - return { error: "unknown" }; - } - } + static async deleteEntry({ + entryId, + userId, + }: { + entryId: string; + userId: string; + }): Promise<{ error: string | null }> { + try { + await db + .update(Entry) + .set({ end_date: new Date() }) + .where(and(eq(Entry.id, entryId), eq(Entry.user_id, userId))); + return { error: null }; + } catch (error) { + return { error: "unknown" }; + } + } - static async updateEntry({ - values, - entryId, - }: { - entryId: TReadEntry["id"]; - values: TInsertEntry; - }): Promise<{ error: string | null }> { - try { - await db - .update(Entry) - .set(values) - .where(and(eq(Entry.id, entryId), isNull(Entry.end_date))); + static async updateEntry({ + values, + entryId, + }: { + entryId: TReadEntry["id"]; + values: TInsertEntry; + }): Promise<{ error: string | null }> { + try { + await db + .update(Entry) + .set(values) + .where(and(eq(Entry.id, entryId), isNull(Entry.end_date))); - return { error: null }; - } catch (error) { - return { error: "error updating entry" }; - } - } + return { error: null }; + } catch (error) { + return { error: "error updating entry" }; + } + } } diff --git a/apps/api/src/features/entry/schema/entry.schema.ts b/apps/api/src/features/entry/schema/entry.schema.ts index 1730256..b5079cc 100644 --- a/apps/api/src/features/entry/schema/entry.schema.ts +++ b/apps/api/src/features/entry/schema/entry.schema.ts @@ -1,38 +1,41 @@ import { Users } from "@/features/user/schema"; import { - boolean, - integer, - json, - pgTable, - timestamp, - uuid, - varchar, + boolean, + integer, + json, + pgTable, + timestamp, + uuid, + varchar, } from "drizzle-orm/pg-core"; type TDocumentContent = { - type: "doc" | string; - content: Record[]; + type: "doc" | string; + content: Record[]; }; export const Entry = pgTable("entry", { - id: uuid("id").primaryKey().defaultRandom(), - user_id: uuid("user_id") - .references(() => Users.id, { onDelete: "cascade", onUpdate: "cascade" }) - .notNull(), - title: varchar("title", { length: 80 }).default("Untintled").notNull(), - created_at: timestamp("created_at").defaultNow().notNull(), - updated_at: timestamp("updated_at").defaultNow().notNull(), - word_count: integer("word_count").default(0).notNull(), - content: json("content") - .default({ - type: "doc", - content: [ - { - type: "paragraph", - }, - ], - }) - .$type(), - is_private: boolean("is_private").default(false).notNull(), - end_date: timestamp("end_date"), + id: uuid("id").primaryKey().defaultRandom(), + user_id: uuid("user_id") + .references(() => Users.id, { onDelete: "cascade", onUpdate: "cascade" }) + .notNull(), + title: varchar("title", { length: 80 }).default("Untintled").notNull(), + created_at: timestamp("created_at").defaultNow().notNull(), + updated_at: timestamp("updated_at") + .defaultNow() + .notNull() + .$onUpdateFn(() => new Date()), + word_count: integer("word_count").default(0).notNull(), + content: json("content") + .default({ + type: "doc", + content: [ + { + type: "paragraph", + }, + ], + }) + .$type(), + is_private: boolean("is_private").default(false).notNull(), + end_date: timestamp("end_date"), }); From 596709607aedb44074fafe6e39c2d7414a8a2d46 Mon Sep 17 00:00:00 2001 From: Github Action Date: Wed, 11 Sep 2024 18:13:52 +0000 Subject: [PATCH 2/2] format(api): formatt code --- apps/api/src/config/database/config.ts | 22 +- .../api/src/features/entry/providers/index.ts | 216 +++++++++--------- .../src/features/entry/schema/entry.schema.ts | 64 +++--- 3 files changed, 151 insertions(+), 151 deletions(-) diff --git a/apps/api/src/config/database/config.ts b/apps/api/src/config/database/config.ts index 058a2a1..99929aa 100644 --- a/apps/api/src/config/database/config.ts +++ b/apps/api/src/config/database/config.ts @@ -12,23 +12,23 @@ import * as schema from "./schema"; * */ async function getDb() { - if (Environment.NODE_ENV === "production") { - // this set up is for supabase datbase, so we disable prepare, since transaction mode doesnt support it - const client = postgres(Environment.DATABASE_URL, { prepare: false }); + if (Environment.NODE_ENV === "production") { + // this set up is for supabase datbase, so we disable prepare, since transaction mode doesnt support it + const client = postgres(Environment.DATABASE_URL, { prepare: false }); - const db = drizzle(client, { schema }); + const db = drizzle(client, { schema }); - return db; - } + return db; + } - const { POSTGRES_PASSWORD, POSTGRES_USER, POSTGRES_DATABASE } = Environment; + const { POSTGRES_PASSWORD, POSTGRES_USER, POSTGRES_DATABASE } = Environment; - const connectionString = `postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@127.0.0.1:5432/${POSTGRES_DATABASE}`; - const client = postgres(connectionString, { prepare: false }); + const connectionString = `postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@127.0.0.1:5432/${POSTGRES_DATABASE}`; + const client = postgres(connectionString, { prepare: false }); - const db = drizzle(client, { schema }); + const db = drizzle(client, { schema }); - return db; + return db; } const db = await getDb(); diff --git a/apps/api/src/features/entry/providers/index.ts b/apps/api/src/features/entry/providers/index.ts index c5644a3..b39f8d2 100644 --- a/apps/api/src/features/entry/providers/index.ts +++ b/apps/api/src/features/entry/providers/index.ts @@ -2,130 +2,130 @@ import { db } from "@/config/database"; import type { TReturnHanler } from "@/shared/models/promises"; import { and, eq, ilike, isNull } from "drizzle-orm"; import type { - TInsertEntry, - TNewEntry, - TReadEntry, + TInsertEntry, + TNewEntry, + TReadEntry, } from "../models/entry.models"; import { Entry } from "../schema"; export class EntryProvider { - static async getPrivateEntryById({ - entryId, - userId, - }: { - entryId: string; - userId: string; - }): Promise { - const [entry] = await db - .select() - .from(Entry) - .where(and(eq(Entry.id, entryId), eq(Entry.user_id, userId))); + static async getPrivateEntryById({ + entryId, + userId, + }: { + entryId: string; + userId: string; + }): Promise { + const [entry] = await db + .select() + .from(Entry) + .where(and(eq(Entry.id, entryId), eq(Entry.user_id, userId))); - return entry; - } + return entry; + } - static async getPublicEntryById({ - entryId, - }: { - entryId: string; - }): Promise { - const [entry] = await db.select().from(Entry).where(eq(Entry.id, entryId)); + static async getPublicEntryById({ + entryId, + }: { + entryId: string; + }): Promise { + const [entry] = await db.select().from(Entry).where(eq(Entry.id, entryId)); - return entry; - } + return entry; + } - static async getEntriesListByUserId(userId: string): Promise { - const entries = await db - .select() - .from(Entry) - .where(eq(Entry.user_id, userId)); + static async getEntriesListByUserId(userId: string): Promise { + const entries = await db + .select() + .from(Entry) + .where(eq(Entry.user_id, userId)); - return entries; - } + return entries; + } - static async getPrivateEntryListByTitle({ - title, - userId, - }: { - title: string; - userId: string; - }): Promise { - const entries = await db - .select() - .from(Entry) - .where(and(ilike(Entry.title, title), eq(Entry.user_id, userId))); + static async getPrivateEntryListByTitle({ + title, + userId, + }: { + title: string; + userId: string; + }): Promise { + const entries = await db + .select() + .from(Entry) + .where(and(ilike(Entry.title, title), eq(Entry.user_id, userId))); - return entries; - } + return entries; + } - static async createEntry({ - entry, - userId, - }: { - entry: TInsertEntry; - userId: TReadEntry["user_id"]; - }): Promise> { - const newEntryValues: TNewEntry = { - content: { - type: "doc", - content: [ - { - type: "paragraph", - }, - ], - }, - ...entry, - user_id: userId, - }; + static async createEntry({ + entry, + userId, + }: { + entry: TInsertEntry; + userId: TReadEntry["user_id"]; + }): Promise> { + const newEntryValues: TNewEntry = { + content: { + type: "doc", + content: [ + { + type: "paragraph", + }, + ], + }, + ...entry, + user_id: userId, + }; - try { - const [newEntry] = await db - .insert(Entry) - .values(newEntryValues) - .returning(); + try { + const [newEntry] = await db + .insert(Entry) + .values(newEntryValues) + .returning(); - if (!newEntry) throw new Error("Failed to create entry"); + if (!newEntry) throw new Error("Failed to create entry"); - return { error: null, data: newEntry }; - } catch (error) { - return { error: "error creating new entry", data: null }; - } - } + return { error: null, data: newEntry }; + } catch (error) { + return { error: "error creating new entry", data: null }; + } + } - static async deleteEntry({ - entryId, - userId, - }: { - entryId: string; - userId: string; - }): Promise<{ error: string | null }> { - try { - await db - .update(Entry) - .set({ end_date: new Date() }) - .where(and(eq(Entry.id, entryId), eq(Entry.user_id, userId))); - return { error: null }; - } catch (error) { - return { error: "unknown" }; - } - } + static async deleteEntry({ + entryId, + userId, + }: { + entryId: string; + userId: string; + }): Promise<{ error: string | null }> { + try { + await db + .update(Entry) + .set({ end_date: new Date() }) + .where(and(eq(Entry.id, entryId), eq(Entry.user_id, userId))); + return { error: null }; + } catch (error) { + return { error: "unknown" }; + } + } - static async updateEntry({ - values, - entryId, - }: { - entryId: TReadEntry["id"]; - values: TInsertEntry; - }): Promise<{ error: string | null }> { - try { - await db - .update(Entry) - .set(values) - .where(and(eq(Entry.id, entryId), isNull(Entry.end_date))); + static async updateEntry({ + values, + entryId, + }: { + entryId: TReadEntry["id"]; + values: TInsertEntry; + }): Promise<{ error: string | null }> { + try { + await db + .update(Entry) + .set(values) + .where(and(eq(Entry.id, entryId), isNull(Entry.end_date))); - return { error: null }; - } catch (error) { - return { error: "error updating entry" }; - } - } + return { error: null }; + } catch (error) { + return { error: "error updating entry" }; + } + } } diff --git a/apps/api/src/features/entry/schema/entry.schema.ts b/apps/api/src/features/entry/schema/entry.schema.ts index b5079cc..f04d250 100644 --- a/apps/api/src/features/entry/schema/entry.schema.ts +++ b/apps/api/src/features/entry/schema/entry.schema.ts @@ -1,41 +1,41 @@ import { Users } from "@/features/user/schema"; import { - boolean, - integer, - json, - pgTable, - timestamp, - uuid, - varchar, + boolean, + integer, + json, + pgTable, + timestamp, + uuid, + varchar, } from "drizzle-orm/pg-core"; type TDocumentContent = { - type: "doc" | string; - content: Record[]; + type: "doc" | string; + content: Record[]; }; export const Entry = pgTable("entry", { - id: uuid("id").primaryKey().defaultRandom(), - user_id: uuid("user_id") - .references(() => Users.id, { onDelete: "cascade", onUpdate: "cascade" }) - .notNull(), - title: varchar("title", { length: 80 }).default("Untintled").notNull(), - created_at: timestamp("created_at").defaultNow().notNull(), - updated_at: timestamp("updated_at") - .defaultNow() - .notNull() - .$onUpdateFn(() => new Date()), - word_count: integer("word_count").default(0).notNull(), - content: json("content") - .default({ - type: "doc", - content: [ - { - type: "paragraph", - }, - ], - }) - .$type(), - is_private: boolean("is_private").default(false).notNull(), - end_date: timestamp("end_date"), + id: uuid("id").primaryKey().defaultRandom(), + user_id: uuid("user_id") + .references(() => Users.id, { onDelete: "cascade", onUpdate: "cascade" }) + .notNull(), + title: varchar("title", { length: 80 }).default("Untintled").notNull(), + created_at: timestamp("created_at").defaultNow().notNull(), + updated_at: timestamp("updated_at") + .defaultNow() + .notNull() + .$onUpdateFn(() => new Date()), + word_count: integer("word_count").default(0).notNull(), + content: json("content") + .default({ + type: "doc", + content: [ + { + type: "paragraph", + }, + ], + }) + .$type(), + is_private: boolean("is_private").default(false).notNull(), + end_date: timestamp("end_date"), });