100 lines
3.4 KiB
TypeScript
100 lines
3.4 KiB
TypeScript
import { index, integer, primaryKey, sqliteTable, text } from 'drizzle-orm/sqlite-core';
|
|
import { EVENT_ACTIONS } from '../../sync/core/model';
|
|
|
|
const timestamp = (name: string) => integer(name, { mode: 'timestamp_ms' });
|
|
const flag = (name: string) => integer(name, { mode: 'boolean' });
|
|
const count = (name: string) => integer(name).notNull().default(0);
|
|
|
|
export const projects = sqliteTable('projects', {
|
|
id: text('id').primaryKey(),
|
|
name: text('name').notNull(),
|
|
enabled: flag('enabled').notNull().default(true),
|
|
pausedReason: text('paused_reason'),
|
|
spSiteId: text('sp_site_id').notNull(),
|
|
spDriveId: text('sp_drive_id').notNull(),
|
|
spRootItemId: text('sp_root_item_id').notNull(),
|
|
spDisplayPath: text('sp_display_path').notNull(),
|
|
accHubId: text('acc_hub_id').notNull(),
|
|
accProjectId: text('acc_project_id').notNull(),
|
|
accRootFolderId: text('acc_root_folder_id').notNull(),
|
|
accDisplayPath: text('acc_display_path').notNull(),
|
|
deltaCursor: text('delta_cursor'),
|
|
firstSyncedAt: timestamp('first_synced_at'),
|
|
lastRunAt: timestamp('last_run_at'),
|
|
createdAt: timestamp('created_at').notNull()
|
|
});
|
|
|
|
export const items = sqliteTable(
|
|
'items',
|
|
{
|
|
projectId: text('project_id')
|
|
.notNull()
|
|
.references(() => projects.id, { onDelete: 'cascade' }),
|
|
sourceId: text('source_id').notNull(),
|
|
// desired (from SharePoint)
|
|
parentSourceId: text('parent_source_id').notNull(),
|
|
name: text('name').notNull(),
|
|
isFolder: flag('is_folder').notNull(),
|
|
contentTag: text('content_tag'),
|
|
sourceModifiedAt: timestamp('source_modified_at').notNull(),
|
|
size: integer('size'),
|
|
deleted: flag('deleted').notNull(),
|
|
// synced (what ACC has)
|
|
targetId: text('target_id'),
|
|
syncedParentSourceId: text('synced_parent_source_id'),
|
|
syncedName: text('synced_name'),
|
|
syncedContentTag: text('synced_content_tag'),
|
|
syncedDeleted: flag('synced_deleted').notNull(),
|
|
syncedAt: timestamp('synced_at'),
|
|
// bookkeeping
|
|
status: text('status', { enum: ['pending', 'synced', 'failed', 'deleted'] }).notNull(),
|
|
attemptCount: count('attempt_count'),
|
|
lastError: text('last_error'),
|
|
lastAttemptAt: timestamp('last_attempt_at')
|
|
},
|
|
(t) => [
|
|
primaryKey({ columns: [t.projectId, t.sourceId] }),
|
|
index('items_project_status').on(t.projectId, t.status)
|
|
]
|
|
);
|
|
|
|
export const runs = sqliteTable(
|
|
'runs',
|
|
{
|
|
id: text('id').primaryKey(),
|
|
projectId: text('project_id')
|
|
.notNull()
|
|
.references(() => projects.id, { onDelete: 'cascade' }),
|
|
trigger: text('trigger', { enum: ['schedule', 'manual'] }).notNull(),
|
|
startedAt: timestamp('started_at').notNull(),
|
|
finishedAt: timestamp('finished_at'),
|
|
outcome: text('outcome', { enum: ['success', 'partial', 'failed'] }),
|
|
error: text('error'),
|
|
created: count('created'),
|
|
updated: count('updated'),
|
|
renamed: count('renamed'),
|
|
moved: count('moved'),
|
|
deleted: count('deleted'),
|
|
restored: count('restored'),
|
|
failed: count('failed')
|
|
},
|
|
(t) => [index('runs_project_started').on(t.projectId, t.startedAt)]
|
|
);
|
|
|
|
export const runEvents = sqliteTable(
|
|
'run_events',
|
|
{
|
|
id: integer('id').primaryKey({ autoIncrement: true }),
|
|
runId: text('run_id')
|
|
.notNull()
|
|
.references(() => runs.id, { onDelete: 'cascade' }),
|
|
projectId: text('project_id').notNull(),
|
|
sourceId: text('source_id').notNull(),
|
|
path: text('path').notNull(),
|
|
action: text('action', { enum: EVENT_ACTIONS }).notNull(),
|
|
message: text('message'),
|
|
at: timestamp('at').notNull()
|
|
},
|
|
(t) => [index('run_events_run').on(t.runId)]
|
|
);
|