|
1 | | -// import { actor, setup } from "rivetkit"; |
2 | | -// import { db } from "@rivetkit/db/drizzle"; |
3 | | -// import * as schema from "./db/schema"; |
4 | | -// import migrations from "../drizzle/migrations"; |
| 1 | +import { actor, setup } from "@rivetkit/actor"; |
| 2 | +import { db } from "@rivetkit/db/drizzle"; |
| 3 | +import { desc } from "drizzle-orm"; |
| 4 | +import migrations from "../drizzle/migrations"; |
| 5 | +import * as schema from "./db/schema"; |
5 | 6 |
|
6 | | -// export const counter = actor({ |
7 | | -// db: db({ schema, migrations }), |
8 | | -// state: { |
9 | | -// count: 0, |
10 | | -// }, |
11 | | -// onAuth: () => { |
12 | | -// // Configure auth here |
13 | | -// }, |
14 | | -// actions: { |
15 | | -// increment: (c, x: number) => { |
16 | | -// // createState or state fix fix fix |
17 | | -// c.db.c.state.count += x; |
18 | | -// return c.state.count; |
19 | | -// }, |
20 | | -// }, |
21 | | -// }); |
| 7 | +export const chat = actor({ |
| 8 | + db: db({ schema, migrations }), |
| 9 | + onAuth: () => {}, |
| 10 | + actions: { |
| 11 | + // Callable functions from clients: https://rivet.gg/docs/actors/actions |
| 12 | + sendMessage: async (c, sender: string, text: string) => { |
| 13 | + const message = { sender, text, timestamp: Date.now() }; |
| 14 | + // State changes are automatically persisted |
| 15 | + await c.db.insert(schema.messagesTable).values(message); |
| 16 | + // Send events to all connected clients: https://rivet.gg/docs/actors/events |
| 17 | + c.broadcast("newMessage", message); |
| 18 | + return message; |
| 19 | + }, |
22 | 20 |
|
23 | | -// export const registry = setup({ |
24 | | -// use: { counter }, |
25 | | -// }); |
| 21 | + getHistory: (c) => |
| 22 | + c.db |
| 23 | + .select() |
| 24 | + .from(schema.messagesTable) |
| 25 | + .orderBy(desc(schema.messagesTable.timestamp)) |
| 26 | + .limit(100), |
| 27 | + }, |
| 28 | +}); |
| 29 | + |
| 30 | +export const registry = setup({ |
| 31 | + use: { chat }, |
| 32 | +}); |
0 commit comments