Skip to content

Commit

Permalink
add document metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
dickeyy committed Jun 5, 2024
1 parent bd9c353 commit 2a6a7ad
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 83 deletions.
49 changes: 25 additions & 24 deletions apps/api/package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
{
"name": "api",
"version": "1.0.50",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "bun run --watch src/index.ts",
"start": "bun src/index.ts",
"build": "bun build src/index.ts --target bun"
},
"dependencies": {
"@clerk/clerk-sdk-node": "^5.0.9",
"@elysiajs/cors": "^1.0.2",
"@grotto/logysia": "^0.1.3",
"drizzle-orm": "^0.30.10",
"elysia": "latest",
"elysia-clerk": "^0.4.0",
"postgres": "^3.4.4",
"stripe": "^15.8.0",
"svix": "^1.24.0"
},
"devDependencies": {
"bun-types": "latest",
"drizzle-kit": "^0.21.4"
},
"module": "src/index.js"
"name": "api",
"version": "1.0.50",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "bun run --watch src/index.ts",
"start": "bun src/index.ts",
"build": "bun build src/index.ts --target bun",
"db:push": "bunx drizzle-kit push --config drizzle.config.ts"
},
"dependencies": {
"@clerk/clerk-sdk-node": "^5.0.9",
"@elysiajs/cors": "^1.0.2",
"@grotto/logysia": "^0.1.3",
"drizzle-orm": "^0.30.10",
"elysia": "latest",
"elysia-clerk": "^0.4.0",
"postgres": "^3.4.4",
"stripe": "^15.8.0",
"svix": "^1.24.0"
},
"devDependencies": {
"bun-types": "latest",
"drizzle-kit": "^0.21.4"
},
"module": "src/index.js"
}
8 changes: 6 additions & 2 deletions apps/api/src/db/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { relations } from "drizzle-orm";
import { text, pgTable, varchar, bigint } from "drizzle-orm/pg-core";
import { text, pgTable, varchar, bigint, jsonb } from "drizzle-orm/pg-core";

export const users = pgTable("users", {
id: varchar("id", { length: 36 }).primaryKey(),
Expand Down Expand Up @@ -29,7 +29,11 @@ export const documents = pgTable("documents", {
}).notNull(),
updated_at: bigint("updated_at", {
mode: "number"
}).notNull()
}).notNull(),
metadata: jsonb("metadata").default({
font: "serif",
font_size: 18
})
});

export const documentRelations = relations(documents, ({ one }) => ({
Expand Down
35 changes: 34 additions & 1 deletion apps/api/src/lib/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ export async function getDocumentByID(id: string, userID: string): Promise<Docum
return doc as any;
}

export async function updateDocumentMetadata(
id: string,
metadata: any
): Promise<DocumentType | null> {
const now = Math.floor(new Date().getTime() / 1000);

const doc = await db.select().from(documents).where(eq(documents.id, id)).limit(1).execute();
if (!doc) {
return null;
}

try {
await db
.update(documents)
.set({ metadata: metadata, updated_at: now })
.where(eq(documents.id, id))
.execute();
} catch (e) {
console.error(e);
return null;
}

doc[0].metadata = metadata;
doc[0].updated_at = now;
doc[0].content = await decrypt(doc[0].content || "");
return doc[0] as any;
}

export async function updateDocumentByID(
id: string,
content: string
Expand Down Expand Up @@ -61,6 +89,7 @@ export async function updateDocumentByID(
}

doc[0].content = content;
doc[0].updated_at = now;
return doc[0] as any;
}

Expand All @@ -87,7 +116,11 @@ export async function createDocument(userID: string, title: string): Promise<Doc
content: null,
owner_id: userID,
created_at: now,
updated_at: now
updated_at: now,
metadata: {
font: "serif",
font_size: 18
}
};
try {
await db.insert(documents).values(doc).execute();
Expand Down
28 changes: 27 additions & 1 deletion apps/api/src/routes/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
getUserDocuments,
getDocumentByID,
updateDocumentByID,
deleteDocumentByID
deleteDocumentByID,
updateDocumentMetadata
} from "../lib/document";
import { validateClerkToken } from "../lib/clerk";
import { decrypt } from "../lib/crypto";
Expand All @@ -26,6 +27,12 @@ docs.ws("/ws", {
minLength: 0
})
),
metadata: t.Optional(
t.Object({
font: t.String(),
font_size: t.Number()
})
),
title: t.Optional(t.String())
}),
token: t.String()
Expand Down Expand Up @@ -57,6 +64,25 @@ docs.ws("/ws", {
message: "error"
});
}
} else if (message === "update metadata") {
console.log("update metadata");
if (data.metadata !== undefined) {
const doc = await updateDocumentMetadata(data.doc_id, data.metadata);
if (!doc) {
ws.send({
message: "error"
});
} else {
ws.send({
message: "success",
doc: doc
});
}
} else {
ws.send({
message: "error"
});
}
} else {
ws.send("invalid event");
}
Expand Down
6 changes: 5 additions & 1 deletion apps/api/src/types/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ export type DocumentType = {
content: string | null;
created_at: number;
updated_at: number;
}
metadata: {
font: string;
font_size: number;
};
};
10 changes: 9 additions & 1 deletion apps/web/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "@/styles/globals.css";
import { Inter as FontSans } from "next/font/google";
import { Averia_Serif_Libre as FontSerif } from "next/font/google";
import { JetBrains_Mono as FontMono } from "next/font/google";
import { cn } from "@/lib/utils";
import { ThemeProvider } from "@/components/theme-provider";
import { ClerkProvider } from "@clerk/nextjs";
Expand All @@ -19,6 +20,12 @@ const fontSerif = FontSerif({
weight: ["300", "400", "700"]
});

const fontMono = FontMono({
subsets: ["latin"],
variable: "--font-mono",
weight: ["300", "400", "500", "600", "700"]
});

export const metadata: Metadata = {
metadataBase: new URL("https://diary.kyle.so"),
title: "Diary - diary.kyle.so",
Expand Down Expand Up @@ -98,7 +105,8 @@ export default function RootLayout({ children }: { children: React.ReactNode })
className={cn(
"bg-background min-h-screen overflow-auto font-sans antialiased",
fontSans.variable,
fontSerif.variable
fontSerif.variable,
fontMono.variable
)}
>
<ThemeProvider
Expand Down
Loading

0 comments on commit 2a6a7ad

Please sign in to comment.