decentralised message store
1import { sql } from "drizzle-orm";
2import { integer, text, index } from "drizzle-orm/sqlite-core";
3import { sqliteTable } from "drizzle-orm/sqlite-core";
4import { createInsertSchema, createSelectSchema } from "drizzle-zod";
5import { z } from "zod";
6
7// NOTE:: for an initial mvp, we are supporting only sqlite and storing all the messages in a single table.
8// for an actual production release, we will likely be storing messages in separate tables or separate sqlite files (depending on config).
9export const messagesTable = sqliteTable(
10 "messages",
11 {
12 // we do incrementing numbers for now but for goodness sake we need to come up with something better
13 // TODO: id by snowflakes or something more sane.
14 id: text("id").primaryKey(),
15 channelAtUri: text("channel_at_uri"),
16 authorDid: text("author_did").notNull(),
17 content: text("content").notNull(),
18 createdAt: integer("created_at", { mode: "timestamp" })
19 .notNull()
20 .default(sql`(unixepoch('now'))`),
21 },
22 (table) => [
23 index("messages_channel_idx").on(table.channelAtUri, table.createdAt),
24 index("messages_author_idx").on(table.authorDid),
25 ],
26);
27
28export const shardMessagesSelectSchema = createSelectSchema(messagesTable);
29export const shardMessageSelectSchemaArray = z.array(shardMessagesSelectSchema);
30export const shardMessagesInsertSchema = createInsertSchema(messagesTable);
31
32export type ShardMessageSelect = z.infer<typeof shardMessagesSelectSchema>;
33export type ShardMessageInsert = z.infer<typeof shardMessagesInsertSchema>;