a tool for shared writing and social publishing
at update/looseleafs 356 lines 18 kB view raw
1import { pgTable, pgEnum, text, jsonb, foreignKey, timestamp, boolean, uuid, index, bigint, unique, uniqueIndex, smallint, primaryKey } from "drizzle-orm/pg-core" 2 import { sql } from "drizzle-orm" 3 4export const aal_level = pgEnum("aal_level", ['aal1', 'aal2', 'aal3']) 5export const code_challenge_method = pgEnum("code_challenge_method", ['s256', 'plain']) 6export const factor_status = pgEnum("factor_status", ['unverified', 'verified']) 7export const factor_type = pgEnum("factor_type", ['totp', 'webauthn', 'phone']) 8export const oauth_authorization_status = pgEnum("oauth_authorization_status", ['pending', 'approved', 'denied', 'expired']) 9export const oauth_client_type = pgEnum("oauth_client_type", ['public', 'confidential']) 10export const oauth_registration_type = pgEnum("oauth_registration_type", ['dynamic', 'manual']) 11export const oauth_response_type = pgEnum("oauth_response_type", ['code']) 12export const one_time_token_type = pgEnum("one_time_token_type", ['confirmation_token', 'reauthentication_token', 'recovery_token', 'email_change_token_new', 'email_change_token_current', 'phone_change_token']) 13export const key_status = pgEnum("key_status", ['default', 'valid', 'invalid', 'expired']) 14export const key_type = pgEnum("key_type", ['aead-ietf', 'aead-det', 'hmacsha512', 'hmacsha256', 'auth', 'shorthash', 'generichash', 'kdf', 'secretbox', 'secretstream', 'stream_xchacha20']) 15export const rsvp_status = pgEnum("rsvp_status", ['GOING', 'NOT_GOING', 'MAYBE']) 16export const action = pgEnum("action", ['INSERT', 'UPDATE', 'DELETE', 'TRUNCATE', 'ERROR']) 17export const equality_op = pgEnum("equality_op", ['eq', 'neq', 'lt', 'lte', 'gt', 'gte', 'in']) 18export const buckettype = pgEnum("buckettype", ['STANDARD', 'ANALYTICS', 'VECTOR']) 19 20 21export const oauth_state_store = pgTable("oauth_state_store", { 22 key: text("key").primaryKey().notNull(), 23 state: jsonb("state").notNull(), 24}); 25 26export const notifications = pgTable("notifications", { 27 recipient: text("recipient").notNull().references(() => identities.atp_did, { onDelete: "cascade", onUpdate: "cascade" } ), 28 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 29 read: boolean("read").default(false).notNull(), 30 data: jsonb("data").notNull(), 31 id: uuid("id").primaryKey().notNull(), 32}); 33 34export const publications = pgTable("publications", { 35 uri: text("uri").primaryKey().notNull(), 36 indexed_at: timestamp("indexed_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 37 name: text("name").notNull(), 38 identity_did: text("identity_did").notNull().references(() => identities.atp_did, { onDelete: "cascade" } ), 39 record: jsonb("record"), 40}, 41(table) => { 42 return { 43 identity_did_idx: index("publications_identity_did_idx").on(table.identity_did), 44 } 45}); 46 47export const comments_on_documents = pgTable("comments_on_documents", { 48 uri: text("uri").primaryKey().notNull(), 49 record: jsonb("record").notNull(), 50 document: text("document").references(() => documents.uri, { onDelete: "cascade", onUpdate: "cascade" } ), 51 indexed_at: timestamp("indexed_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 52 profile: text("profile").references(() => bsky_profiles.did, { onDelete: "set null", onUpdate: "cascade" } ), 53}); 54 55export const entities = pgTable("entities", { 56 id: uuid("id").primaryKey().notNull(), 57 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 58 set: uuid("set").notNull().references(() => entity_sets.id, { onDelete: "cascade", onUpdate: "cascade" } ), 59}, 60(table) => { 61 return { 62 set_idx: index("entities_set_idx").on(table.set), 63 } 64}); 65 66export const facts = pgTable("facts", { 67 id: uuid("id").primaryKey().notNull(), 68 entity: uuid("entity").notNull().references(() => entities.id, { onDelete: "cascade", onUpdate: "restrict" } ), 69 attribute: text("attribute").notNull(), 70 data: jsonb("data").notNull(), 71 created_at: timestamp("created_at", { mode: 'string' }).defaultNow().notNull(), 72 updated_at: timestamp("updated_at", { mode: 'string' }), 73 // You can use { mode: "bigint" } if numbers are exceeding js number limitations 74 version: bigint("version", { mode: "number" }).default(0).notNull(), 75}, 76(table) => { 77 return { 78 entity_idx: index("facts_entity_idx").on(table.entity), 79 } 80}); 81 82export const replicache_clients = pgTable("replicache_clients", { 83 client_id: text("client_id").primaryKey().notNull(), 84 client_group: text("client_group").notNull(), 85 // You can use { mode: "bigint" } if numbers are exceeding js number limitations 86 last_mutation: bigint("last_mutation", { mode: "number" }).notNull(), 87}, 88(table) => { 89 return { 90 client_group_idx: index("replicache_clients_client_group_idx").on(table.client_group), 91 } 92}); 93 94export const email_auth_tokens = pgTable("email_auth_tokens", { 95 id: uuid("id").defaultRandom().primaryKey().notNull(), 96 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 97 confirmed: boolean("confirmed").default(false).notNull(), 98 email: text("email"), 99 confirmation_code: text("confirmation_code").notNull(), 100 identity: uuid("identity").references(() => identities.id, { onDelete: "cascade", onUpdate: "cascade" } ), 101}); 102 103export const bsky_posts = pgTable("bsky_posts", { 104 uri: text("uri").primaryKey().notNull(), 105 indexed_at: timestamp("indexed_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 106 post_view: jsonb("post_view").notNull(), 107 cid: text("cid").notNull(), 108}); 109 110export const bsky_profiles = pgTable("bsky_profiles", { 111 did: text("did").primaryKey().notNull().references(() => identities.atp_did, { onDelete: "cascade" } ), 112 record: jsonb("record").notNull(), 113 indexed_at: timestamp("indexed_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 114 handle: text("handle"), 115}); 116 117export const entity_sets = pgTable("entity_sets", { 118 id: uuid("id").defaultRandom().primaryKey().notNull(), 119 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 120}); 121 122export const poll_votes_on_entity = pgTable("poll_votes_on_entity", { 123 id: uuid("id").defaultRandom().primaryKey().notNull(), 124 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 125 poll_entity: uuid("poll_entity").notNull().references(() => entities.id, { onDelete: "cascade", onUpdate: "cascade" } ), 126 option_entity: uuid("option_entity").notNull().references(() => entities.id, { onDelete: "cascade", onUpdate: "cascade" } ), 127 voter_token: uuid("voter_token").notNull(), 128}); 129 130export const permission_tokens = pgTable("permission_tokens", { 131 id: uuid("id").defaultRandom().primaryKey().notNull(), 132 root_entity: uuid("root_entity").notNull().references(() => entities.id, { onDelete: "cascade", onUpdate: "cascade" } ), 133 blocked_by_admin: boolean("blocked_by_admin"), 134}); 135 136export const identities = pgTable("identities", { 137 id: uuid("id").defaultRandom().primaryKey().notNull(), 138 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 139 home_page: uuid("home_page").notNull().references(() => permission_tokens.id, { onDelete: "cascade" } ), 140 email: text("email"), 141 atp_did: text("atp_did"), 142 interface_state: jsonb("interface_state"), 143}, 144(table) => { 145 return { 146 identities_email_key: unique("identities_email_key").on(table.email), 147 identities_atp_did_key: unique("identities_atp_did_key").on(table.atp_did), 148 } 149}); 150 151export const phone_number_auth_tokens = pgTable("phone_number_auth_tokens", { 152 id: uuid("id").defaultRandom().primaryKey().notNull(), 153 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 154 confirmed: boolean("confirmed").default(false).notNull(), 155 confirmation_code: text("confirmation_code").notNull(), 156 phone_number: text("phone_number").notNull(), 157 country_code: text("country_code").notNull(), 158}); 159 160export const phone_rsvps_to_entity = pgTable("phone_rsvps_to_entity", { 161 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 162 phone_number: text("phone_number").notNull(), 163 country_code: text("country_code").notNull(), 164 status: rsvp_status("status").notNull(), 165 id: uuid("id").defaultRandom().primaryKey().notNull(), 166 entity: uuid("entity").notNull().references(() => entities.id, { onDelete: "cascade", onUpdate: "cascade" } ), 167 name: text("name").default('').notNull(), 168 plus_ones: smallint("plus_ones").default(0).notNull(), 169}, 170(table) => { 171 return { 172 unique_phone_number_entities: uniqueIndex("unique_phone_number_entities").on(table.phone_number, table.entity), 173 } 174}); 175 176export const custom_domain_routes = pgTable("custom_domain_routes", { 177 id: uuid("id").defaultRandom().primaryKey().notNull(), 178 domain: text("domain").notNull().references(() => custom_domains.domain), 179 route: text("route").notNull(), 180 edit_permission_token: uuid("edit_permission_token").notNull().references(() => permission_tokens.id, { onDelete: "cascade", onUpdate: "cascade" } ), 181 view_permission_token: uuid("view_permission_token").notNull().references(() => permission_tokens.id, { onDelete: "cascade", onUpdate: "cascade" } ), 182 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 183}, 184(table) => { 185 return { 186 edit_permission_token_idx: index("custom_domain_routes_edit_permission_token_idx").on(table.edit_permission_token), 187 custom_domain_routes_domain_route_key: unique("custom_domain_routes_domain_route_key").on(table.domain, table.route), 188 } 189}); 190 191export const custom_domains = pgTable("custom_domains", { 192 domain: text("domain").primaryKey().notNull(), 193 identity: text("identity").default('').references(() => identities.email, { onDelete: "cascade", onUpdate: "cascade" } ), 194 confirmed: boolean("confirmed").notNull(), 195 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 196 identity_id: uuid("identity_id").references(() => identities.id, { onDelete: "cascade" } ), 197}); 198 199export const email_subscriptions_to_entity = pgTable("email_subscriptions_to_entity", { 200 id: uuid("id").defaultRandom().primaryKey().notNull(), 201 entity: uuid("entity").notNull().references(() => entities.id, { onDelete: "cascade" } ), 202 email: text("email").notNull(), 203 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 204 token: uuid("token").notNull().references(() => permission_tokens.id, { onDelete: "cascade" } ), 205 confirmed: boolean("confirmed").default(false).notNull(), 206 confirmation_code: text("confirmation_code").notNull(), 207}); 208 209export const documents = pgTable("documents", { 210 uri: text("uri").primaryKey().notNull(), 211 data: jsonb("data").notNull(), 212 indexed_at: timestamp("indexed_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 213}); 214 215export const atp_poll_votes = pgTable("atp_poll_votes", { 216 uri: text("uri").primaryKey().notNull(), 217 record: jsonb("record").notNull(), 218 voter_did: text("voter_did").notNull(), 219 poll_uri: text("poll_uri").notNull().references(() => atp_poll_records.uri, { onDelete: "cascade", onUpdate: "cascade" } ), 220 poll_cid: text("poll_cid").notNull(), 221 indexed_at: timestamp("indexed_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 222}, 223(table) => { 224 return { 225 poll_uri_idx: index("atp_poll_votes_poll_uri_idx").on(table.poll_uri), 226 voter_did_idx: index("atp_poll_votes_voter_did_idx").on(table.voter_did), 227 } 228}); 229 230export const atp_poll_records = pgTable("atp_poll_records", { 231 uri: text("uri").primaryKey().notNull(), 232 cid: text("cid").notNull(), 233 record: jsonb("record").notNull(), 234 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 235}); 236 237export const oauth_session_store = pgTable("oauth_session_store", { 238 key: text("key").primaryKey().notNull(), 239 session: jsonb("session").notNull(), 240}); 241 242export const bsky_follows = pgTable("bsky_follows", { 243 identity: text("identity").default('').notNull().references(() => identities.atp_did, { onDelete: "cascade" } ), 244 follows: text("follows").notNull().references(() => identities.atp_did, { onDelete: "cascade" } ), 245}, 246(table) => { 247 return { 248 bsky_follows_pkey: primaryKey({ columns: [table.identity, table.follows], name: "bsky_follows_pkey"}), 249 } 250}); 251 252export const subscribers_to_publications = pgTable("subscribers_to_publications", { 253 identity: text("identity").notNull().references(() => identities.email, { onUpdate: "cascade" } ), 254 publication: text("publication").notNull().references(() => publications.uri), 255 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 256}, 257(table) => { 258 return { 259 subscribers_to_publications_pkey: primaryKey({ columns: [table.identity, table.publication], name: "subscribers_to_publications_pkey"}), 260 } 261}); 262 263export const permission_token_on_homepage = pgTable("permission_token_on_homepage", { 264 token: uuid("token").notNull().references(() => permission_tokens.id, { onDelete: "cascade" } ), 265 identity: uuid("identity").notNull().references(() => identities.id, { onDelete: "cascade" } ), 266 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 267}, 268(table) => { 269 return { 270 permission_token_creator_pkey: primaryKey({ columns: [table.token, table.identity], name: "permission_token_creator_pkey"}), 271 } 272}); 273 274export const documents_in_publications = pgTable("documents_in_publications", { 275 publication: text("publication").notNull().references(() => publications.uri, { onDelete: "cascade" } ), 276 document: text("document").notNull().references(() => documents.uri, { onDelete: "cascade" } ), 277 indexed_at: timestamp("indexed_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 278}, 279(table) => { 280 return { 281 publication_idx: index("documents_in_publications_publication_idx").on(table.publication), 282 documents_in_publications_pkey: primaryKey({ columns: [table.publication, table.document], name: "documents_in_publications_pkey"}), 283 } 284}); 285 286export const document_mentions_in_bsky = pgTable("document_mentions_in_bsky", { 287 uri: text("uri").notNull().references(() => bsky_posts.uri, { onDelete: "cascade" } ), 288 link: text("link").notNull(), 289 document: text("document").notNull().references(() => documents.uri, { onDelete: "cascade" } ), 290 indexed_at: timestamp("indexed_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 291}, 292(table) => { 293 return { 294 document_mentions_in_bsky_pkey: primaryKey({ columns: [table.uri, table.document], name: "document_mentions_in_bsky_pkey"}), 295 } 296}); 297 298export const publication_domains = pgTable("publication_domains", { 299 publication: text("publication").notNull().references(() => publications.uri, { onDelete: "cascade" } ), 300 domain: text("domain").notNull().references(() => custom_domains.domain, { onDelete: "cascade" } ), 301 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 302 identity: text("identity").notNull().references(() => identities.atp_did, { onDelete: "cascade", onUpdate: "cascade" } ), 303}, 304(table) => { 305 return { 306 publication_idx: index("publication_domains_publication_idx").on(table.publication), 307 publication_domains_pkey: primaryKey({ columns: [table.publication, table.domain], name: "publication_domains_pkey"}), 308 } 309}); 310 311export const leaflets_in_publications = pgTable("leaflets_in_publications", { 312 publication: text("publication").notNull().references(() => publications.uri, { onDelete: "cascade" } ), 313 doc: text("doc").default('').references(() => documents.uri, { onDelete: "set null" } ), 314 leaflet: uuid("leaflet").notNull().references(() => permission_tokens.id, { onDelete: "cascade" } ), 315 description: text("description").default('').notNull(), 316 title: text("title").default('').notNull(), 317}, 318(table) => { 319 return { 320 leaflet_idx: index("leaflets_in_publications_leaflet_idx").on(table.leaflet), 321 publication_idx: index("leaflets_in_publications_publication_idx").on(table.publication), 322 leaflets_in_publications_pkey: primaryKey({ columns: [table.publication, table.leaflet], name: "leaflets_in_publications_pkey"}), 323 } 324}); 325 326export const publication_subscriptions = pgTable("publication_subscriptions", { 327 publication: text("publication").notNull().references(() => publications.uri, { onDelete: "cascade" } ), 328 identity: text("identity").notNull().references(() => identities.atp_did, { onDelete: "cascade" } ), 329 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 330 record: jsonb("record").notNull(), 331 uri: text("uri").notNull(), 332}, 333(table) => { 334 return { 335 publication_idx: index("publication_subscriptions_publication_idx").on(table.publication), 336 publication_subscriptions_pkey: primaryKey({ columns: [table.publication, table.identity], name: "publication_subscriptions_pkey"}), 337 publication_subscriptions_uri_key: unique("publication_subscriptions_uri_key").on(table.uri), 338 } 339}); 340 341export const permission_token_rights = pgTable("permission_token_rights", { 342 token: uuid("token").notNull().references(() => permission_tokens.id, { onDelete: "cascade", onUpdate: "cascade" } ), 343 entity_set: uuid("entity_set").notNull().references(() => entity_sets.id, { onDelete: "cascade", onUpdate: "cascade" } ), 344 read: boolean("read").default(false).notNull(), 345 write: boolean("write").default(false).notNull(), 346 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 347 create_token: boolean("create_token").default(false).notNull(), 348 change_entity_set: boolean("change_entity_set").default(false).notNull(), 349}, 350(table) => { 351 return { 352 token_idx: index("permission_token_rights_token_idx").on(table.token), 353 entity_set_idx: index("permission_token_rights_entity_set_idx").on(table.entity_set), 354 permission_token_rights_pkey: primaryKey({ columns: [table.token, table.entity_set], name: "permission_token_rights_pkey"}), 355 } 356});