a tool for shared writing and social publishing
at refactor/standard.site 414 lines 21 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").default(sql`create_identity_homepage()`).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 site_standard_publications = pgTable("site_standard_publications", { 177 uri: text("uri").primaryKey().notNull(), 178 data: jsonb("data").notNull(), 179 indexed_at: timestamp("indexed_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 180 identity_did: text("identity_did").notNull().references(() => identities.atp_did, { onDelete: "cascade" } ), 181}); 182 183export const custom_domain_routes = pgTable("custom_domain_routes", { 184 id: uuid("id").defaultRandom().primaryKey().notNull(), 185 domain: text("domain").notNull().references(() => custom_domains.domain), 186 route: text("route").notNull(), 187 edit_permission_token: uuid("edit_permission_token").notNull().references(() => permission_tokens.id, { onDelete: "cascade", onUpdate: "cascade" } ), 188 view_permission_token: uuid("view_permission_token").notNull().references(() => permission_tokens.id, { onDelete: "cascade", onUpdate: "cascade" } ), 189 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 190}, 191(table) => { 192 return { 193 edit_permission_token_idx: index("custom_domain_routes_edit_permission_token_idx").on(table.edit_permission_token), 194 custom_domain_routes_domain_route_key: unique("custom_domain_routes_domain_route_key").on(table.domain, table.route), 195 } 196}); 197 198export const site_standard_documents = pgTable("site_standard_documents", { 199 uri: text("uri").primaryKey().notNull(), 200 data: jsonb("data").notNull(), 201 indexed_at: timestamp("indexed_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 202 identity_did: text("identity_did").notNull().references(() => identities.atp_did, { onDelete: "cascade" } ), 203}); 204 205export const custom_domains = pgTable("custom_domains", { 206 domain: text("domain").primaryKey().notNull(), 207 identity: text("identity").default('').references(() => identities.email, { onDelete: "cascade", onUpdate: "cascade" } ), 208 confirmed: boolean("confirmed").notNull(), 209 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 210 identity_id: uuid("identity_id").references(() => identities.id, { onDelete: "cascade" } ), 211}); 212 213export const email_subscriptions_to_entity = pgTable("email_subscriptions_to_entity", { 214 id: uuid("id").defaultRandom().primaryKey().notNull(), 215 entity: uuid("entity").notNull().references(() => entities.id, { onDelete: "cascade" } ), 216 email: text("email").notNull(), 217 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 218 token: uuid("token").notNull().references(() => permission_tokens.id, { onDelete: "cascade" } ), 219 confirmed: boolean("confirmed").default(false).notNull(), 220 confirmation_code: text("confirmation_code").notNull(), 221}); 222 223export const documents = pgTable("documents", { 224 uri: text("uri").primaryKey().notNull(), 225 data: jsonb("data").notNull(), 226 indexed_at: timestamp("indexed_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 227}); 228 229export const atp_poll_votes = pgTable("atp_poll_votes", { 230 uri: text("uri").primaryKey().notNull(), 231 record: jsonb("record").notNull(), 232 voter_did: text("voter_did").notNull(), 233 poll_uri: text("poll_uri").notNull().references(() => atp_poll_records.uri, { onDelete: "cascade", onUpdate: "cascade" } ), 234 poll_cid: text("poll_cid").notNull(), 235 indexed_at: timestamp("indexed_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 236}, 237(table) => { 238 return { 239 poll_uri_idx: index("atp_poll_votes_poll_uri_idx").on(table.poll_uri), 240 voter_did_idx: index("atp_poll_votes_voter_did_idx").on(table.voter_did), 241 } 242}); 243 244export const atp_poll_records = pgTable("atp_poll_records", { 245 uri: text("uri").primaryKey().notNull(), 246 cid: text("cid").notNull(), 247 record: jsonb("record").notNull(), 248 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 249}); 250 251export const oauth_session_store = pgTable("oauth_session_store", { 252 key: text("key").primaryKey().notNull(), 253 session: jsonb("session").notNull(), 254}); 255 256export const bsky_follows = pgTable("bsky_follows", { 257 identity: text("identity").default('').notNull().references(() => identities.atp_did, { onDelete: "cascade" } ), 258 follows: text("follows").notNull().references(() => identities.atp_did, { onDelete: "cascade" } ), 259}, 260(table) => { 261 return { 262 bsky_follows_pkey: primaryKey({ columns: [table.identity, table.follows], name: "bsky_follows_pkey"}), 263 } 264}); 265 266export const subscribers_to_publications = pgTable("subscribers_to_publications", { 267 identity: text("identity").notNull().references(() => identities.email, { onUpdate: "cascade" } ), 268 publication: text("publication").notNull().references(() => publications.uri), 269 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 270}, 271(table) => { 272 return { 273 subscribers_to_publications_pkey: primaryKey({ columns: [table.identity, table.publication], name: "subscribers_to_publications_pkey"}), 274 } 275}); 276 277export const site_standard_documents_in_publications = pgTable("site_standard_documents_in_publications", { 278 publication: text("publication").notNull().references(() => site_standard_publications.uri, { onDelete: "cascade" } ), 279 document: text("document").notNull().references(() => site_standard_documents.uri, { onDelete: "cascade" } ), 280 indexed_at: timestamp("indexed_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 281}, 282(table) => { 283 return { 284 site_standard_documents_in_publications_pkey: primaryKey({ columns: [table.publication, table.document], name: "site_standard_documents_in_publications_pkey"}), 285 } 286}); 287 288export const documents_in_publications = pgTable("documents_in_publications", { 289 publication: text("publication").notNull().references(() => publications.uri, { onDelete: "cascade" } ), 290 document: text("document").notNull().references(() => documents.uri, { onDelete: "cascade" } ), 291 indexed_at: timestamp("indexed_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 292}, 293(table) => { 294 return { 295 publication_idx: index("documents_in_publications_publication_idx").on(table.publication), 296 documents_in_publications_pkey: primaryKey({ columns: [table.publication, table.document], name: "documents_in_publications_pkey"}), 297 } 298}); 299 300export const document_mentions_in_bsky = pgTable("document_mentions_in_bsky", { 301 uri: text("uri").notNull().references(() => bsky_posts.uri, { onDelete: "cascade" } ), 302 link: text("link").notNull(), 303 document: text("document").notNull().references(() => documents.uri, { onDelete: "cascade" } ), 304 indexed_at: timestamp("indexed_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 305}, 306(table) => { 307 return { 308 document_mentions_in_bsky_pkey: primaryKey({ columns: [table.uri, table.document], name: "document_mentions_in_bsky_pkey"}), 309 } 310}); 311 312export const permission_token_on_homepage = pgTable("permission_token_on_homepage", { 313 token: uuid("token").notNull().references(() => permission_tokens.id, { onDelete: "cascade", onUpdate: "cascade" } ), 314 identity: uuid("identity").notNull().references(() => identities.id, { onDelete: "cascade" } ), 315 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 316 archived: boolean("archived"), 317}, 318(table) => { 319 return { 320 permission_token_creator_pkey: primaryKey({ columns: [table.token, table.identity], name: "permission_token_creator_pkey"}), 321 } 322}); 323 324export const publication_domains = pgTable("publication_domains", { 325 publication: text("publication").notNull().references(() => publications.uri, { onDelete: "cascade" } ), 326 domain: text("domain").notNull().references(() => custom_domains.domain, { onDelete: "cascade" } ), 327 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 328 identity: text("identity").notNull().references(() => identities.atp_did, { onDelete: "cascade", onUpdate: "cascade" } ), 329}, 330(table) => { 331 return { 332 publication_idx: index("publication_domains_publication_idx").on(table.publication), 333 publication_domains_pkey: primaryKey({ columns: [table.publication, table.domain], name: "publication_domains_pkey"}), 334 } 335}); 336 337export const publication_subscriptions = pgTable("publication_subscriptions", { 338 publication: text("publication").notNull().references(() => publications.uri, { onDelete: "cascade" } ), 339 identity: text("identity").notNull().references(() => identities.atp_did, { onDelete: "cascade" } ), 340 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 341 record: jsonb("record").notNull(), 342 uri: text("uri").notNull(), 343}, 344(table) => { 345 return { 346 publication_idx: index("publication_subscriptions_publication_idx").on(table.publication), 347 publication_subscriptions_pkey: primaryKey({ columns: [table.publication, table.identity], name: "publication_subscriptions_pkey"}), 348 publication_subscriptions_uri_key: unique("publication_subscriptions_uri_key").on(table.uri), 349 } 350}); 351 352export const site_standard_subscriptions = pgTable("site_standard_subscriptions", { 353 publication: text("publication").notNull().references(() => site_standard_publications.uri, { onDelete: "cascade" } ), 354 identity: text("identity").notNull().references(() => identities.atp_did, { onDelete: "cascade" } ), 355 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 356 record: jsonb("record").notNull(), 357 uri: text("uri").notNull(), 358}, 359(table) => { 360 return { 361 site_standard_subscriptions_pkey: primaryKey({ columns: [table.publication, table.identity], name: "site_standard_subscriptions_pkey"}), 362 site_standard_subscriptions_uri_key: unique("site_standard_subscriptions_uri_key").on(table.uri), 363 } 364}); 365 366export const leaflets_to_documents = pgTable("leaflets_to_documents", { 367 leaflet: uuid("leaflet").notNull().references(() => permission_tokens.id, { onDelete: "cascade", onUpdate: "cascade" } ), 368 document: text("document").notNull().references(() => documents.uri, { onDelete: "cascade", onUpdate: "cascade" } ), 369 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 370 title: text("title").default('').notNull(), 371 description: text("description").default('').notNull(), 372 tags: text("tags").default('RRAY[').array(), 373 cover_image: text("cover_image"), 374}, 375(table) => { 376 return { 377 leaflets_to_documents_pkey: primaryKey({ columns: [table.leaflet, table.document], name: "leaflets_to_documents_pkey"}), 378 } 379}); 380 381export const permission_token_rights = pgTable("permission_token_rights", { 382 token: uuid("token").notNull().references(() => permission_tokens.id, { onDelete: "cascade", onUpdate: "cascade" } ), 383 entity_set: uuid("entity_set").notNull().references(() => entity_sets.id, { onDelete: "cascade", onUpdate: "cascade" } ), 384 read: boolean("read").default(false).notNull(), 385 write: boolean("write").default(false).notNull(), 386 created_at: timestamp("created_at", { withTimezone: true, mode: 'string' }).defaultNow().notNull(), 387 create_token: boolean("create_token").default(false).notNull(), 388 change_entity_set: boolean("change_entity_set").default(false).notNull(), 389}, 390(table) => { 391 return { 392 token_idx: index("permission_token_rights_token_idx").on(table.token), 393 entity_set_idx: index("permission_token_rights_entity_set_idx").on(table.entity_set), 394 permission_token_rights_pkey: primaryKey({ columns: [table.token, table.entity_set], name: "permission_token_rights_pkey"}), 395 } 396}); 397 398export const leaflets_in_publications = pgTable("leaflets_in_publications", { 399 publication: text("publication").notNull().references(() => publications.uri, { onDelete: "cascade" } ), 400 doc: text("doc").default('').references(() => documents.uri, { onDelete: "set null" } ), 401 leaflet: uuid("leaflet").notNull().references(() => permission_tokens.id, { onDelete: "cascade", onUpdate: "cascade" } ), 402 description: text("description").default('').notNull(), 403 title: text("title").default('').notNull(), 404 archived: boolean("archived"), 405 tags: text("tags").default('RRAY[').array(), 406 cover_image: text("cover_image"), 407}, 408(table) => { 409 return { 410 leaflet_idx: index("leaflets_in_publications_leaflet_idx").on(table.leaflet), 411 publication_idx: index("leaflets_in_publications_publication_idx").on(table.publication), 412 leaflets_in_publications_pkey: primaryKey({ columns: [table.publication, table.leaflet], name: "leaflets_in_publications_pkey"}), 413 } 414});