Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 27, 2024
1 parent 47892a9 commit 8fb9dba
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ let ws: WebSocket | undefined;
const store = reactive({
message: "",
messages: [] as { user: string, message: string, created_at: string }[],
messages: [] as { id: number, user: string, message: string, created_at: string }[],
});
const log = (user: string, ...args: string[]) => {
console.log("[ws]", user, ...args);
store.messages.push({
id: Math.random(),
message: args.join(" "),
user: user,
created_at: new Date().toLocaleString(),
Expand Down Expand Up @@ -65,7 +66,6 @@ onMounted(() => {
connect();
$fetch("/api/messages").then((res) => {
store.messages = res.messages;
console.log("messages", JSON.stringify(store.messages));
});
});
Expand Down Expand Up @@ -97,7 +97,7 @@ useServerHead({
<p class="text-white">{{ message.message }}</p>
</div>
</div>
<p class="text-gray-500 mt-1 text-xs ml-10">{{ message.date }}</p>
<p class="text-gray-500 mt-1 text-xs ml-10">{{ message.created_at }}</p>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion server/api/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default defineEventHandler(async (event) => {
export default defineEventHandler(async () => {
const messages = await getMessages(25);
return {
messages
Expand Down
4 changes: 2 additions & 2 deletions server/utils/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function addMessage(user: string, message: string) {

export async function getMessages(count = 10) {
const db = useDatabase();
const {rows } = await db.sql`SELECT * FROM messages ORDER BY created_at DESC LIMIT ${count}`
return rows;
const { rows } = await db.sql`SELECT * FROM messages ORDER BY created_at DESC LIMIT ${count}`
return rows as { id: number, user: string, message: string, created_at: string }[];
}

0 comments on commit 8fb9dba

Please sign in to comment.