Something went wrong. Try again.
Synapse is a study workspace. Organize courses read and annotate the material, train with generated exercises and watch your progress forked from denishovart.com/synapse
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125// Drizzle schema - SQLite dialect (better-sqlite3).//// Structurally identical to schema.pg.ts: same table names, column keys and// JS-facing types, so every repository is written once against the shared query// builder and only the db handle + this schema object are swapped per dialect// (see database.module.ts). Timestamps are stored as TEXT (SQLite has no native// date type) with a `datetime('now')` default.import { sql } from 'drizzle-orm';import { integer, sqliteTable, text, index, uniqueIndex, primaryKey,} from 'drizzle-orm/sqlite-core';
const now = sql`(datetime('now'))`;
export const users = sqliteTable('users', { id: text('id') .primaryKey() .$defaultFn(() => crypto.randomUUID()), username: text('username').notNull().unique(), passwordHash: text('password_hash'), role: text('role').notNull().default('user'), provider: text('provider').notNull().default('local'), externalId: text('external_id'), createdAt: text('created_at').default(now),});
export const documents = sqliteTable( 'documents', { id: text('id') .primaryKey() .$defaultFn(() => crypto.randomUUID()), userId: text('user_id').references(() => users.id), originalName: text('original_name').notNull(), filename: text('filename').notNull(), filepath: text('filepath').notNull(), extractedText: text('extracted_text'), pageCount: integer('page_count'), uploadedAt: text('uploaded_at').default(now), renderPath: text('render_path'), contentHash: text('content_hash'), renderHash: text('render_hash'), textHash: text('text_hash'), }, (t) => ({ ownerIdx: index('idx_documents_owner').on(t.userId) }),);
export const appSettings = sqliteTable('app_settings', { key: text('key').primaryKey(), value: text('value').notNull(),});
// Local-first sync: append-only log of Loro update blobs per (user, doc).// Payload bytes live in the CAS; rows carry only the content hash. seq is a per (user, doc) monotonic cursor assigned on append.export const crdtUpdates = sqliteTable( 'crdt_updates', { id: text('id') .primaryKey() .$defaultFn(() => crypto.randomUUID()), userId: text('user_id').references(() => users.id), docId: text('doc_id').notNull(), seq: integer('seq').notNull(), payloadHash: text('payload_hash').notNull(), deviceId: text('device_id'), createdAt: text('created_at').default(now), }, // Unique so two devices racing to append cannot both mint the same seq: the // loser's insert fails and retries against the new high-water mark, instead // of silently landing a second row a caught-up puller would skip. (t) => ({ docIdx: uniqueIndex('idx_crdt_updates_doc').on(t.userId, t.docId, t.seq) }),);
// Folded snapshots written by the compaction job; the log rows they cover// (seq <= upto_seq) can then be dropped.export const crdtSnapshots = sqliteTable( 'crdt_snapshots', { id: text('id') .primaryKey() .$defaultFn(() => crypto.randomUUID()), userId: text('user_id').references(() => users.id), docId: text('doc_id').notNull(), uptoSeq: integer('upto_seq').notNull(), payloadHash: text('payload_hash').notNull(), createdAt: text('created_at').default(now), }, (t) => ({ docIdx: index('idx_crdt_snapshots_doc').on(t.userId, t.docId) }),);
// Which users hold a blob in the content store. A blob shared across users or// courses has one row per owner; the bytes are deleted only when the last row// goes. A null owner (auth disabled) is stored as the empty string so it fits// the composite primary key.export const blobOwners = sqliteTable( 'blob_owners', { hash: text('hash').notNull(), userId: text('user_id').notNull().default(''), createdAt: text('created_at').default(now), }, (t) => ({ pk: primaryKey({ columns: [t.hash, t.userId] }), hashIdx: index('idx_blob_owners_hash').on(t.hash), }),);
export const sessions = sqliteTable( 'sessions', { id: text('id').primaryKey(), userId: text('user_id') .notNull() .references(() => users.id, { onDelete: 'cascade' }), createdAt: text('created_at').notNull().default(now), expiresAt: text('expires_at').notNull(), }, (t) => ({ userIdx: index('idx_sessions_user').on(t.userId) }),);