Download packages/storage-fabric/src/drizzle/sqlite.ts from AnthropicallyHuggingFaces/ProjectOS: direct link, hf CLI and curl.
- Browser
- Download file 17.4 kB
-
https://huggingface.co/spaces/AnthropicallyHuggingFaces/ProjectOS/resolve/main/packages/storage-fabric/src/drizzle/sqlite.ts
- Command line
-
hf download hf://spaces/AnthropicallyHuggingFaces/ProjectOS/packages/storage-fabric/src/drizzle/sqlite.ts
-
curl -L -o sqlite.ts https://huggingface.co/spaces/AnthropicallyHuggingFaces/ProjectOS/resolve/main/packages/storage-fabric/src/drizzle/sqlite.ts
17.4 kB
| import { sql } from 'drizzle-orm'; | |
| import { | |
| check, | |
| real, | |
| index, | |
| integer, | |
| sqliteTable, | |
| text, | |
| uniqueIndex, | |
| type AnySQLiteColumn | |
| } from 'drizzle-orm/sqlite-core'; | |
| type JsonObject = Record<string, unknown>; | |
| const now = sql`(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`; | |
| export const storageSchemaMigrations = sqliteTable('storage_schema_migrations', { | |
| version: integer('version').primaryKey(), | |
| name: text('name').notNull(), | |
| appliedAt: text('applied_at').notNull().default(now) | |
| }); | |
| export const projects = sqliteTable('projects', { | |
| projectId: text('project_id').primaryKey(), | |
| name: text('name').notNull(), | |
| description: text('description').notNull().default(''), | |
| stage: text('stage').notNull().default('Research'), | |
| stateJson: text('state_json', { mode: 'json' }).$type<JsonObject>().notNull().default({}), | |
| createdAt: text('created_at').notNull().default(now), | |
| updatedAt: text('updated_at').notNull().default(now), | |
| archivedAt: text('archived_at') | |
| }, (table) => [ | |
| check('projects_state_json_check', sql`json_valid(${table.stateJson})`), | |
| index('projects_stage_updated_idx').on(table.stage, table.updatedAt) | |
| ]); | |
| export const projectEvents = sqliteTable('project_events', { | |
| eventId: text('event_id').primaryKey(), | |
| projectId: text('project_id').notNull().references(() => projects.projectId, { onDelete: 'cascade' }), | |
| eventType: text('event_type').notNull(), | |
| payloadJson: text('payload_json', { mode: 'json' }).$type<JsonObject>().notNull().default({}), | |
| createdAt: text('created_at').notNull().default(now), | |
| createdBy: text('created_by') | |
| }, (table) => [ | |
| check('project_events_payload_json_check', sql`json_valid(${table.payloadJson})`), | |
| index('project_events_project_created_idx').on(table.projectId, table.createdAt) | |
| ]); | |
| export const snapshots = sqliteTable('snapshots', { | |
| snapshotId: text('snapshot_id').primaryKey(), | |
| projectId: text('project_id').notNull().references(() => projects.projectId, { onDelete: 'cascade' }), | |
| snapshotType: text('snapshot_type', { enum: ['manual', 'scheduled', 'pre_change', 'canon', 'frontier', 'archive'] }).notNull(), | |
| label: text('label').notNull(), | |
| source: text('source').notNull().default('local'), | |
| schemaVersion: text('schema_version').notNull().default('1'), | |
| stateJson: text('state_json', { mode: 'json' }).$type<JsonObject>().notNull(), | |
| checksum: text('checksum').notNull(), | |
| createdAt: text('created_at').notNull().default(now), | |
| createdBy: text('created_by'), | |
| metadataJson: text('metadata_json', { mode: 'json' }).$type<JsonObject>().notNull().default({}) | |
| }, (table) => [ | |
| check('snapshots_type_check', sql`${table.snapshotType} IN ('manual', 'scheduled', 'pre_change', 'canon', 'frontier', 'archive')`), | |
| check('snapshots_state_json_check', sql`json_valid(${table.stateJson})`), | |
| check('snapshots_metadata_json_check', sql`json_valid(${table.metadataJson})`), | |
| index('snapshots_project_created_idx').on(table.projectId, table.createdAt), | |
| index('snapshots_type_created_idx').on(table.snapshotType, table.createdAt) | |
| ]); | |
| export const snapshotItems = sqliteTable('snapshot_items', { | |
| snapshotItemId: text('snapshot_item_id').primaryKey(), | |
| snapshotId: text('snapshot_id').notNull().references(() => snapshots.snapshotId, { onDelete: 'cascade' }), | |
| entityType: text('entity_type').notNull(), | |
| entityId: text('entity_id').notNull(), | |
| ordinal: integer('ordinal').notNull().default(0), | |
| payloadJson: text('payload_json', { mode: 'json' }).$type<JsonObject>().notNull(), | |
| contentHash: text('content_hash').notNull() | |
| }, (table) => [ | |
| check('snapshot_items_ordinal_check', sql`${table.ordinal} >= 0`), | |
| check('snapshot_items_payload_json_check', sql`json_valid(${table.payloadJson})`), | |
| uniqueIndex('snapshot_items_snapshot_entity_uq').on(table.snapshotId, table.entityType, table.entityId), | |
| index('snapshot_items_entity_idx').on(table.entityType, table.entityId) | |
| ]); | |
| export const canonEntries = sqliteTable('canon_entries', { | |
| entryId: text('entry_id').primaryKey(), | |
| projectId: text('project_id').references(() => projects.projectId, { onDelete: 'set null' }), | |
| slug: text('slug').notNull(), | |
| title: text('title').notNull(), | |
| body: text('body').notNull(), | |
| entryType: text('entry_type', { enum: ['principle', 'decision', 'pattern', 'policy', 'knowledge', 'procedure'] }).notNull(), | |
| status: text('status', { enum: ['draft', 'active', 'superseded', 'archived'] }).notNull().default('draft'), | |
| version: integer('version').notNull().default(1), | |
| sourceSnapshotId: text('source_snapshot_id').references(() => snapshots.snapshotId, { onDelete: 'set null' }), | |
| supersedesEntryId: text('supersedes_entry_id').references((): AnySQLiteColumn => canonEntries.entryId, { onDelete: 'set null' }), | |
| metadataJson: text('metadata_json', { mode: 'json' }).$type<JsonObject>().notNull().default({}), | |
| validFrom: text('valid_from'), | |
| validUntil: text('valid_until'), | |
| createdAt: text('created_at').notNull().default(now), | |
| updatedAt: text('updated_at').notNull().default(now) | |
| }, (table) => [ | |
| check('canon_entries_type_check', sql`${table.entryType} IN ('principle', 'decision', 'pattern', 'policy', 'knowledge', 'procedure')`), | |
| check('canon_entries_status_check', sql`${table.status} IN ('draft', 'active', 'superseded', 'archived')`), | |
| check('canon_entries_version_check', sql`${table.version} > 0`), | |
| check('canon_entries_metadata_json_check', sql`json_valid(${table.metadataJson})`), | |
| uniqueIndex('canon_entries_project_slug_version_uq').on(table.projectId, table.slug, table.version), | |
| index('canon_entries_project_status_idx').on(table.projectId, table.status, table.updatedAt), | |
| index('canon_entries_type_status_idx').on(table.entryType, table.status) | |
| ]); | |
| export const canonRevisions = sqliteTable('canon_revisions', { | |
| revisionId: text('revision_id').primaryKey(), | |
| entryId: text('entry_id').notNull().references(() => canonEntries.entryId, { onDelete: 'cascade' }), | |
| version: integer('version').notNull(), | |
| title: text('title').notNull(), | |
| body: text('body').notNull(), | |
| sourceSnapshotId: text('source_snapshot_id').references(() => snapshots.snapshotId, { onDelete: 'set null' }), | |
| changeSummary: text('change_summary').notNull().default(''), | |
| metadataJson: text('metadata_json', { mode: 'json' }).$type<JsonObject>().notNull().default({}), | |
| createdAt: text('created_at').notNull().default(now), | |
| createdBy: text('created_by') | |
| }, (table) => [ | |
| check('canon_revisions_version_check', sql`${table.version} > 0`), | |
| check('canon_revisions_metadata_json_check', sql`json_valid(${table.metadataJson})`), | |
| uniqueIndex('canon_revisions_entry_version_uq').on(table.entryId, table.version), | |
| index('canon_revisions_entry_created_idx').on(table.entryId, table.createdAt) | |
| ]); | |
| export const frontierItems = sqliteTable('frontier_items', { | |
| frontierId: text('frontier_id').primaryKey(), | |
| projectId: text('project_id').references(() => projects.projectId, { onDelete: 'cascade' }), | |
| domain: text('domain').notNull(), | |
| itemType: text('item_type', { enum: ['technology', 'capability', 'practice', 'dependency', 'research', 'risk'] }).notNull(), | |
| title: text('title').notNull(), | |
| description: text('description').notNull().default(''), | |
| rationale: text('rationale').notNull().default(''), | |
| status: text('status', { enum: ['candidate', 'tracking', 'adopted', 'rejected', 'obsolete'] }).notNull().default('candidate'), | |
| confidence: real('confidence'), | |
| impact: text('impact', { enum: ['low', 'medium', 'high', 'critical'] }).notNull().default('medium'), | |
| firstSeenAt: text('first_seen_at').notNull().default(now), | |
| lastReviewedAt: text('last_reviewed_at'), | |
| reviewAfter: text('review_after'), | |
| metadataJson: text('metadata_json', { mode: 'json' }).$type<JsonObject>().notNull().default({}), | |
| createdAt: text('created_at').notNull().default(now), | |
| updatedAt: text('updated_at').notNull().default(now) | |
| }, (table) => [ | |
| check('frontier_items_type_check', sql`${table.itemType} IN ('technology', 'capability', 'practice', 'dependency', 'research', 'risk')`), | |
| check('frontier_items_status_check', sql`${table.status} IN ('candidate', 'tracking', 'adopted', 'rejected', 'obsolete')`), | |
| check('frontier_items_confidence_check', sql`${table.confidence} IS NULL OR (${table.confidence} >= 0 AND ${table.confidence} <= 1)`), | |
| check('frontier_items_impact_check', sql`${table.impact} IN ('low', 'medium', 'high', 'critical')`), | |
| check('frontier_items_metadata_json_check', sql`json_valid(${table.metadataJson})`), | |
| index('frontier_items_project_status_idx').on(table.projectId, table.status, table.updatedAt), | |
| index('frontier_items_domain_review_idx').on(table.domain, table.reviewAfter) | |
| ]); | |
| export const frontierObservations = sqliteTable('frontier_observations', { | |
| observationId: text('observation_id').primaryKey(), | |
| frontierId: text('frontier_id').notNull().references(() => frontierItems.frontierId, { onDelete: 'cascade' }), | |
| sourceName: text('source_name').notNull(), | |
| sourceUri: text('source_uri'), | |
| observedAt: text('observed_at').notNull().default(now), | |
| summary: text('summary').notNull(), | |
| evidenceJson: text('evidence_json', { mode: 'json' }).$type<JsonObject>().notNull().default({}), | |
| confidence: real('confidence'), | |
| createdAt: text('created_at').notNull().default(now) | |
| }, (table) => [ | |
| check('frontier_observations_confidence_check', sql`${table.confidence} IS NULL OR (${table.confidence} >= 0 AND ${table.confidence} <= 1)`), | |
| check('frontier_observations_evidence_json_check', sql`json_valid(${table.evidenceJson})`), | |
| index('frontier_observations_frontier_idx').on(table.frontierId, table.observedAt) | |
| ]); | |
| export const archiveRecords = sqliteTable('archive_records', { | |
| archiveId: text('archive_id').primaryKey(), | |
| projectId: text('project_id').references(() => projects.projectId, { onDelete: 'set null' }), | |
| resourceType: text('resource_type').notNull(), | |
| resourceId: text('resource_id').notNull(), | |
| snapshotId: text('snapshot_id').references(() => snapshots.snapshotId, { onDelete: 'set null' }), | |
| payloadJson: text('payload_json', { mode: 'json' }).$type<JsonObject>().notNull(), | |
| checksum: text('checksum').notNull(), | |
| archiveReason: text('archive_reason').notNull().default(''), | |
| archivedAt: text('archived_at').notNull().default(now), | |
| archivedBy: text('archived_by'), | |
| retentionUntil: text('retention_until'), | |
| restoredAt: text('restored_at'), | |
| metadataJson: text('metadata_json', { mode: 'json' }).$type<JsonObject>().notNull().default({}) | |
| }, (table) => [ | |
| check('archive_records_payload_json_check', sql`json_valid(${table.payloadJson})`), | |
| check('archive_records_metadata_json_check', sql`json_valid(${table.metadataJson})`), | |
| index('archive_records_project_archived_idx').on(table.projectId, table.archivedAt), | |
| index('archive_records_resource_idx').on(table.resourceType, table.resourceId, table.archivedAt), | |
| index('archive_records_retention_idx').on(table.retentionUntil) | |
| ]); | |
| export const knowledgeEmbeddings = sqliteTable('knowledge_embeddings', { | |
| embeddingId: text('embedding_id').primaryKey(), | |
| entityType: text('entity_type').notNull(), | |
| entityId: text('entity_id').notNull(), | |
| modelKey: text('model_key').notNull(), | |
| dimensions: integer('dimensions').notNull(), | |
| embeddingJson: text('embedding_json', { mode: 'json' }).$type<number[]>().notNull(), | |
| contentHash: text('content_hash').notNull(), | |
| createdAt: text('created_at').notNull().default(now) | |
| }, (table) => [ | |
| check('knowledge_embeddings_dimensions_check', sql`${table.dimensions} > 0`), | |
| check('knowledge_embeddings_array_check', sql`json_valid(${table.embeddingJson}) AND json_type(${table.embeddingJson}) = 'array'`), | |
| uniqueIndex('knowledge_embeddings_entity_model_uq').on(table.entityType, table.entityId, table.modelKey), | |
| index('knowledge_embeddings_entity_idx').on(table.entityType, table.entityId) | |
| ]); | |
| export const dependencyFindings = sqliteTable('dependency_findings', { | |
| findingId: text('finding_id').primaryKey(), | |
| projectId: text('project_id').notNull().references(() => projects.projectId, { onDelete: 'cascade' }), | |
| kind: text('kind', { enum: ['model', 'dataset', 'permission', 'tool', 'infrastructure'] }).notNull(), | |
| name: text('name').notNull(), | |
| description: text('description').notNull().default(''), | |
| status: text('status', { enum: ['open', 'resolved', 'waived'] }).notNull().default('open'), | |
| source: text('source').notNull().default('manual'), | |
| metadataJson: text('metadata_json', { mode: 'json' }).$type<JsonObject>().notNull().default({}), | |
| detectedAt: text('detected_at').notNull().default(now), | |
| resolvedAt: text('resolved_at') | |
| }, (table) => [ | |
| check('dependency_findings_kind_check', sql`${table.kind} IN ('model', 'dataset', 'permission', 'tool', 'infrastructure')`), | |
| check('dependency_findings_status_check', sql`${table.status} IN ('open', 'resolved', 'waived')`), | |
| check('dependency_findings_metadata_json_check', sql`json_valid(${table.metadataJson})`), | |
| index('dependency_findings_project_status_idx').on(table.projectId, table.status, table.detectedAt), | |
| index('dependency_findings_kind_name_idx').on(table.kind, table.name, table.status) | |
| ]); | |
| export const deltaRuns = sqliteTable('delta_runs', { | |
| deltaRunId: text('delta_run_id').primaryKey(), | |
| projectId: text('project_id').notNull().references(() => projects.projectId, { onDelete: 'cascade' }), | |
| currentLabel: text('current_label').notNull(), | |
| targetLabel: text('target_label').notNull(), | |
| currentStateJson: text('current_state_json', { mode: 'json' }).$type<JsonObject>().notNull(), | |
| targetStateJson: text('target_state_json', { mode: 'json' }).$type<JsonObject>().notNull(), | |
| status: text('status', { enum: ['complete', 'blocked'] }).notNull(), | |
| createdAt: text('created_at').notNull().default(now), | |
| createdBy: text('created_by') | |
| }, (table) => [ | |
| check('delta_runs_status_check', sql`${table.status} IN ('complete', 'blocked')`), | |
| check('delta_runs_current_json_check', sql`json_valid(${table.currentStateJson})`), | |
| check('delta_runs_target_json_check', sql`json_valid(${table.targetStateJson})`), | |
| index('delta_runs_project_created_idx').on(table.projectId, table.createdAt) | |
| ]); | |
| export const deltaGaps = sqliteTable('delta_gaps', { | |
| gapId: text('gap_id').primaryKey(), | |
| deltaRunId: text('delta_run_id').notNull().references(() => deltaRuns.deltaRunId, { onDelete: 'cascade' }), | |
| dimension: text('dimension').notNull(), | |
| currentValuesJson: text('current_values_json', { mode: 'json' }).$type<string[]>().notNull(), | |
| targetValuesJson: text('target_values_json', { mode: 'json' }).$type<string[]>().notNull(), | |
| missingValuesJson: text('missing_values_json', { mode: 'json' }).$type<string[]>().notNull(), | |
| severity: text('severity', { enum: ['gap', 'blocker'] }).notNull(), | |
| status: text('status', { enum: ['open', 'resolved', 'accepted'] }).notNull().default('open'), | |
| createdAt: text('created_at').notNull().default(now) | |
| }, (table) => [ | |
| check('delta_gaps_severity_check', sql`${table.severity} IN ('gap', 'blocker')`), | |
| check('delta_gaps_status_check', sql`${table.status} IN ('open', 'resolved', 'accepted')`), | |
| check('delta_gaps_values_json_check', sql`json_valid(${table.currentValuesJson}) AND json_valid(${table.targetValuesJson}) AND json_valid(${table.missingValuesJson})`), | |
| index('delta_gaps_run_status_idx').on(table.deltaRunId, table.status) | |
| ]); | |
| export const deltaDependencies = sqliteTable('delta_dependencies', { | |
| deltaDependencyId: text('delta_dependency_id').primaryKey(), | |
| deltaRunId: text('delta_run_id').notNull().references(() => deltaRuns.deltaRunId, { onDelete: 'cascade' }), | |
| findingId: text('finding_id').notNull().references(() => dependencyFindings.findingId, { onDelete: 'restrict' }), | |
| createdAt: text('created_at').notNull().default(now) | |
| }, (table) => [ | |
| uniqueIndex('delta_dependencies_run_finding_uq').on(table.deltaRunId, table.findingId), | |
| index('delta_dependencies_run_idx').on(table.deltaRunId) | |
| ]); | |
| export const governanceReviews = sqliteTable('governance_reviews', { | |
| reviewId: text('review_id').primaryKey(), | |
| projectId: text('project_id').notNull().references(() => projects.projectId, { onDelete: 'cascade' }), | |
| action: text('action').notNull(), | |
| resourceType: text('resource_type').notNull(), | |
| resourceId: text('resource_id').notNull(), | |
| decision: text('decision', { enum: ['pending', 'approved', 'rejected'] }).notNull().default('pending'), | |
| requestedBy: text('requested_by'), | |
| requestedAt: text('requested_at').notNull().default(now), | |
| reviewedBy: text('reviewed_by'), | |
| reviewedAt: text('reviewed_at'), | |
| reason: text('reason').notNull().default(''), | |
| metadataJson: text('metadata_json', { mode: 'json' }).$type<JsonObject>().notNull().default({}) | |
| }, (table) => [ | |
| check('governance_reviews_decision_check', sql`${table.decision} IN ('pending', 'approved', 'rejected')`), | |
| check('governance_reviews_metadata_json_check', sql`json_valid(${table.metadataJson})`), | |
| index('governance_reviews_project_decision_idx').on(table.projectId, table.decision, table.requestedAt), | |
| index('governance_reviews_resource_idx').on(table.action, table.resourceType, table.resourceId, table.decision) | |
| ]); | |
| export const appState = sqliteTable('app_state', { | |
| stateKey: text('state_key').primaryKey(), | |
| valueJson: text('value_json', { mode: 'json' }).$type<JsonObject | unknown[]>().notNull(), | |
| updatedAt: text('updated_at').notNull() | |
| }, (table) => [ | |
| check('app_state_json_check', sql`json_valid(${table.valueJson})`) | |
| ]); | |