Skip to content

Commit

Permalink
Refactor api db schema to use common timestamps definition
Browse files Browse the repository at this point in the history
  • Loading branch information
brettimus committed Dec 10, 2024
1 parent fb89e7b commit fb092d0
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions api/src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import { z } from "zod";

const timestamps = {
createdAt: text("created_at")
.notNull()
.default(sql`(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`),
updatedAt: text("updated_at")
.notNull()
.default(sql`(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`),
};

export const appRoutes = sqliteTable("app_routes", {
id: integer("id", { mode: "number" }).primaryKey(),
path: text("path", { mode: "text" }),
Expand Down Expand Up @@ -63,12 +72,7 @@ export const appRequests = sqliteTable("app_requests", {
requestBody: text("request_body", { mode: "json" }),
// The hono route corresponding to this request
requestRoute: text("request_route"),
createdAt: text("created_at")
.notNull()
.default(sql`(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`),
updatedAt: text("updated_at")
.notNull()
.default(sql`(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`),
...timestamps,
// responseId: integer("response_id").references(() => appResponses.id),
});

Expand All @@ -86,13 +90,8 @@ export const appResponses = sqliteTable("app_responses", {
[key: string]: string;
}>(),
isFailure: integer("is_failure", { mode: "boolean" }).default(false),
createdAt: text("created_at")
.notNull()
.default(sql`(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`),
updatedAt: text("updated_at")
.notNull()
.default(sql`(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`),
requestId: integer("request_id").references(() => appRequests.id),
...timestamps,
});

export const appResponseRelations = relations(appResponses, ({ one }) => ({
Expand Down Expand Up @@ -164,12 +163,7 @@ export const tokens = sqliteTable("tokens", {
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
value: text("value").notNull().unique(),
expiresAt: text("expires_at"),
createdAt: text("created_at")
.notNull()
.default(sql`(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`),
updatedAt: text("updated_at")
.notNull()
.default(sql`(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`),
...timestamps,
});

export type Token = typeof tokens.$inferSelect;
Expand All @@ -179,7 +173,5 @@ export type NewToken = typeof tokens.$inferInsert;
export const aiRequestLogs = sqliteTable("ai_request_logs", {
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
log: text("log", { mode: "json" }).notNull(),
createdAt: text("created_at")
.notNull()
.default(sql`(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`),
createdAt: timestamps.createdAt
});

0 comments on commit fb092d0

Please sign in to comment.