feat(db,contracts): full v0.1 data model migrations and shared zod contracts (W1-1)
- migrations 0002-0012 split by domain (foundation/instances/content/identity/ sources/assets/policy/execution/agent/ops/organization): 41 tables, uuid pk, created_at/updated_at + touch trigger, text+CHECK status sets, pgcrypto/pg_trgm - key constraints: source_release(site_id, site_release_id), observation(feed_id, guid), external_identity accepted partial unique, download_task(instance_id, torrent_hash), one active acquisition_plan per slot, one active path_binding per entity/season, asset_location root+path uniqueness, organization_record change_seq/change_xid cursor - secret table (ciphertext/nonce/kdf) + secret_ref columns; outbox dispatch fields (ADR-002) - src/types.ts generated from the migrated schema (scripts/gen-types.ts), drift-checked in tests; int8 -> number, date -> string parsers; migrator accepts migrationTableSchema - contracts: Row/Create/Update schemas per domain, public organization-record API (changeSeq cursor), GlobalSettings, CapabilityDescriptor + 14 capability IO schemas, typed SSE events, Api endpoint table - tests: migration up/down round trip on real PG in an isolated schema, constraint checks, contracts<->db column/enum sync, positive/negative parse for every exported schema Co-Authored-By: Claude Fable 5.1 <[email protected]>
This commit is contained in:
1 parent
7ba712ea55
commit
cc8b3a7df4
42 files changed
+7026
-25
No files matched your search
@@ -0,0 +1,261 @@
|
||||
import { z } from "zod";
|
||||
import { EntityBase, Id, JsonObject, NonNegInt, SERVER_MANAGED, Timestamp } from "./common";
|
||||
|
||||
// 来源域:站点、站点账号、Feed、站点资源、观察(REQ §4)
|
||||
// 含 passkey 的 Feed URL 只以 urlSecretRef 保存,对外只给脱敏的 urlDisplay。
|
||||
|
||||
export const SiteAdapter = z.enum(["nexusphp", "mteam_api", "generic_rss"]);
|
||||
export type SiteAdapter = z.infer<typeof SiteAdapter>;
|
||||
export const SiteHealth = z.enum(["unknown", "healthy", "degraded", "failing", "disabled"]);
|
||||
export type SiteHealth = z.infer<typeof SiteHealth>;
|
||||
export const CircuitState = z.enum(["closed", "open", "half_open"]);
|
||||
export type CircuitState = z.infer<typeof CircuitState>;
|
||||
|
||||
/** 站点级限流(快照、实时搜索、浏览器共享) */
|
||||
export const SiteRateLimit = z.object({
|
||||
requestsPerMinute: z.number().int().positive().optional(),
|
||||
minIntervalMs: z.number().int().nonnegative().optional(),
|
||||
concurrency: z.number().int().positive().optional(),
|
||||
});
|
||||
export type SiteRateLimit = z.infer<typeof SiteRateLimit>;
|
||||
|
||||
export const Site = EntityBase.extend({
|
||||
key: z.string().regex(/^[a-z][a-z0-9_-]*$/),
|
||||
name: z.string().min(1),
|
||||
adapter: SiteAdapter,
|
||||
baseUrl: z.url(),
|
||||
enabled: z.boolean(),
|
||||
rateLimit: SiteRateLimit,
|
||||
config: JsonObject,
|
||||
healthStatus: SiteHealth,
|
||||
circuitState: CircuitState,
|
||||
circuitOpenUntil: Timestamp.nullable(),
|
||||
snapshotEnabled: z.boolean(),
|
||||
snapshotIntervalSec: z.number().int().positive().nullable(),
|
||||
lastSnapshotAt: Timestamp.nullable(),
|
||||
snapshotCursor: JsonObject,
|
||||
});
|
||||
export type Site = z.infer<typeof Site>;
|
||||
export const SiteCreate = Site.pick({
|
||||
key: true,
|
||||
name: true,
|
||||
adapter: true,
|
||||
baseUrl: true,
|
||||
enabled: true,
|
||||
rateLimit: true,
|
||||
config: true,
|
||||
snapshotEnabled: true,
|
||||
snapshotIntervalSec: true,
|
||||
}).partial({ enabled: true, rateLimit: true, config: true, snapshotEnabled: true, snapshotIntervalSec: true });
|
||||
export type SiteCreate = z.infer<typeof SiteCreate>;
|
||||
export const SiteUpdate = SiteCreate.omit({ key: true }).partial();
|
||||
export type SiteUpdate = z.infer<typeof SiteUpdate>;
|
||||
|
||||
export const SiteAuthKind = z.enum(["cookie", "passkey", "api_key", "password"]);
|
||||
export type SiteAuthKind = z.infer<typeof SiteAuthKind>;
|
||||
export const SiteAccountStatus = z.enum(["unknown", "active", "invalid", "expired"]);
|
||||
export type SiteAccountStatus = z.infer<typeof SiteAccountStatus>;
|
||||
|
||||
export const SiteAccount = EntityBase.extend({
|
||||
siteId: Id,
|
||||
label: z.string().min(1),
|
||||
authKind: SiteAuthKind,
|
||||
secretRef: Id.nullable(),
|
||||
enabled: z.boolean(),
|
||||
status: SiteAccountStatus,
|
||||
lastVerifiedAt: Timestamp.nullable(),
|
||||
userInfo: JsonObject,
|
||||
});
|
||||
export type SiteAccount = z.infer<typeof SiteAccount>;
|
||||
export const SiteAccountCreate = SiteAccount.pick({ siteId: true, label: true, authKind: true, enabled: true })
|
||||
.partial({ enabled: true })
|
||||
.extend({ credential: z.string().min(1).max(16384) });
|
||||
export type SiteAccountCreate = z.infer<typeof SiteAccountCreate>;
|
||||
export const SiteAccountUpdate = SiteAccountCreate.omit({ siteId: true }).partial();
|
||||
export type SiteAccountUpdate = z.infer<typeof SiteAccountUpdate>;
|
||||
|
||||
export const FeedPurpose = z.enum(["movie", "series", "variety", "promotion", "search", "generic"]);
|
||||
export type FeedPurpose = z.infer<typeof FeedPurpose>;
|
||||
export const FeedKind = z.enum(["rss", "atom", "list", "query"]);
|
||||
export type FeedKind = z.infer<typeof FeedKind>;
|
||||
export const FeedHealth = z.enum(["unknown", "healthy", "degraded", "failing", "circuit_open", "disabled"]);
|
||||
export type FeedHealth = z.infer<typeof FeedHealth>;
|
||||
|
||||
export const FeedSource = EntityBase.extend({
|
||||
siteId: Id.nullable(),
|
||||
siteAccountId: Id.nullable(),
|
||||
name: z.string().min(1),
|
||||
purpose: FeedPurpose,
|
||||
kind: FeedKind,
|
||||
urlSecretRef: Id.nullable(),
|
||||
/** 脱敏 URL(passkey 等参数替换为 [REDACTED]) */
|
||||
urlDisplay: z.string(),
|
||||
parserVersion: z.string(),
|
||||
enabled: z.boolean(),
|
||||
pollIntervalSec: z.number().int().min(60),
|
||||
filter: JsonObject,
|
||||
cursor: JsonObject,
|
||||
etag: z.string().nullable(),
|
||||
lastModified: z.string().nullable(),
|
||||
lastFetchAt: Timestamp.nullable(),
|
||||
lastSuccessAt: Timestamp.nullable(),
|
||||
nextFetchAt: Timestamp.nullable(),
|
||||
consecutiveFailures: NonNegInt,
|
||||
backoffUntil: Timestamp.nullable(),
|
||||
healthStatus: FeedHealth,
|
||||
lastError: JsonObject.nullable(),
|
||||
});
|
||||
export type FeedSource = z.infer<typeof FeedSource>;
|
||||
/** url 为只写明文(可能含 passkey),服务端加密存为 secret 并生成 urlDisplay */
|
||||
export const FeedSourceCreate = FeedSource.pick({
|
||||
siteId: true,
|
||||
siteAccountId: true,
|
||||
name: true,
|
||||
purpose: true,
|
||||
kind: true,
|
||||
enabled: true,
|
||||
pollIntervalSec: true,
|
||||
filter: true,
|
||||
})
|
||||
.partial({
|
||||
siteId: true,
|
||||
siteAccountId: true,
|
||||
purpose: true,
|
||||
kind: true,
|
||||
enabled: true,
|
||||
pollIntervalSec: true,
|
||||
filter: true,
|
||||
})
|
||||
.extend({ url: z.url() });
|
||||
export type FeedSourceCreate = z.infer<typeof FeedSourceCreate>;
|
||||
export const FeedSourceUpdate = FeedSourceCreate.partial();
|
||||
export type FeedSourceUpdate = z.infer<typeof FeedSourceUpdate>;
|
||||
|
||||
export const ResolutionStatus = z.enum(["unresolved", "resolved", "ambiguous", "unidentifiable", "rejected"]);
|
||||
export type ResolutionStatus = z.infer<typeof ResolutionStatus>;
|
||||
export const ReleaseAvailability = z.enum(["active", "site_disabled", "expired", "unknown"]);
|
||||
export type ReleaseAvailability = z.infer<typeof ReleaseAvailability>;
|
||||
|
||||
/** 促销状态必须带观察时间与过期时间,下单前按策略复核(REQ §4.3) */
|
||||
export const Promotion = z.object({
|
||||
kind: z.string().optional(),
|
||||
observedAt: Timestamp.optional(),
|
||||
expiresAt: Timestamp.nullable().optional(),
|
||||
raw: z.unknown().optional(),
|
||||
});
|
||||
export type Promotion = z.infer<typeof Promotion>;
|
||||
|
||||
export const TorrentFileEntry = z.object({
|
||||
path: z.string(),
|
||||
size: NonNegInt,
|
||||
});
|
||||
export type TorrentFileEntry = z.infer<typeof TorrentFileEntry>;
|
||||
|
||||
export const SourceRelease = EntityBase.extend({
|
||||
siteId: Id,
|
||||
siteReleaseId: z.string().min(1),
|
||||
title: z.string(),
|
||||
subtitle: z.string().nullable(),
|
||||
categoryRaw: z.string().nullable(),
|
||||
tagsRaw: z.array(z.string()),
|
||||
sizeBytes: NonNegInt.nullable(),
|
||||
infohashV1: z.string().nullable(),
|
||||
infohashV2: z.string().nullable(),
|
||||
fileList: z.array(TorrentFileEntry).nullable(),
|
||||
promotion: Promotion,
|
||||
promotionExpiresAt: Timestamp.nullable(),
|
||||
seeders: z.number().int().nullable(),
|
||||
leechers: z.number().int().nullable(),
|
||||
/** 站点发布时间;区别于 firstSeenAt(本系统首次发现) */
|
||||
sourcePublishedAt: Timestamp.nullable(),
|
||||
firstSeenAt: Timestamp,
|
||||
lastSeenAt: Timestamp,
|
||||
detailFetchedAt: Timestamp.nullable(),
|
||||
/** 标题解析结果(标题/年份/季集/分辨率/来源/编码/发布组…) */
|
||||
parsed: JsonObject,
|
||||
parserVersion: z.string().nullable(),
|
||||
mediaEntityId: Id.nullable(),
|
||||
contentUnitId: Id.nullable(),
|
||||
resolutionStatus: ResolutionStatus,
|
||||
resolutionEvidence: JsonObject,
|
||||
availability: ReleaseAvailability,
|
||||
raw: JsonObject,
|
||||
});
|
||||
export type SourceRelease = z.infer<typeof SourceRelease>;
|
||||
/** 由适配器/任务写入(按 siteId+siteReleaseId upsert) */
|
||||
export const SourceReleaseCreate = SourceRelease.omit({
|
||||
...SERVER_MANAGED,
|
||||
firstSeenAt: true,
|
||||
lastSeenAt: true,
|
||||
}).partial({
|
||||
subtitle: true,
|
||||
categoryRaw: true,
|
||||
tagsRaw: true,
|
||||
sizeBytes: true,
|
||||
infohashV1: true,
|
||||
infohashV2: true,
|
||||
fileList: true,
|
||||
promotion: true,
|
||||
promotionExpiresAt: true,
|
||||
seeders: true,
|
||||
leechers: true,
|
||||
sourcePublishedAt: true,
|
||||
detailFetchedAt: true,
|
||||
parsed: true,
|
||||
parserVersion: true,
|
||||
mediaEntityId: true,
|
||||
contentUnitId: true,
|
||||
resolutionStatus: true,
|
||||
resolutionEvidence: true,
|
||||
availability: true,
|
||||
raw: true,
|
||||
});
|
||||
export type SourceReleaseCreate = z.infer<typeof SourceReleaseCreate>;
|
||||
export const SourceReleaseUpdate = SourceReleaseCreate.omit({ siteId: true, siteReleaseId: true }).partial();
|
||||
export type SourceReleaseUpdate = z.infer<typeof SourceReleaseUpdate>;
|
||||
|
||||
export const ObservationStatus = z.enum(["new", "linked", "ignored", "error"]);
|
||||
export type ObservationStatus = z.infer<typeof ObservationStatus>;
|
||||
|
||||
export const Observation = EntityBase.extend({
|
||||
feedId: Id,
|
||||
guid: z.string().min(1),
|
||||
contentDigest: z.string().min(1),
|
||||
sourceReleaseId: Id.nullable(),
|
||||
title: z.string(),
|
||||
/** 已脱敏的链接;原始下载链接不对外 */
|
||||
linkRedacted: z.string().nullable(),
|
||||
raw: JsonObject,
|
||||
sourcePublishedAt: Timestamp.nullable(),
|
||||
fetchedAt: Timestamp,
|
||||
firstSeenAt: Timestamp,
|
||||
lastSeenAt: Timestamp,
|
||||
status: ObservationStatus,
|
||||
resolveAttempts: NonNegInt,
|
||||
nextResolveAt: Timestamp.nullable(),
|
||||
lastError: JsonObject.nullable(),
|
||||
});
|
||||
export type Observation = z.infer<typeof Observation>;
|
||||
export const ObservationCreate = Observation.pick({
|
||||
feedId: true,
|
||||
guid: true,
|
||||
contentDigest: true,
|
||||
sourceReleaseId: true,
|
||||
title: true,
|
||||
linkRedacted: true,
|
||||
raw: true,
|
||||
sourcePublishedAt: true,
|
||||
fetchedAt: true,
|
||||
}).partial({ sourceReleaseId: true, linkRedacted: true, raw: true, sourcePublishedAt: true, fetchedAt: true });
|
||||
export type ObservationCreate = z.infer<typeof ObservationCreate>;
|
||||
export const ObservationUpdate = Observation.pick({
|
||||
contentDigest: true,
|
||||
sourceReleaseId: true,
|
||||
title: true,
|
||||
raw: true,
|
||||
status: true,
|
||||
resolveAttempts: true,
|
||||
nextResolveAt: true,
|
||||
lastError: true,
|
||||
}).partial();
|
||||
export type ObservationUpdate = z.infer<typeof ObservationUpdate>;
|
||||
Reference in new issue
Block a user