|
| 1 | +import { readFileSync } from "node:fs"; |
| 2 | + |
| 3 | +const schema = readFileSync("schema.prisma", "utf8"); |
| 4 | +const endpointTypes = readFileSync("src/lib/endpoint/types.ts", "utf8"); |
| 5 | + |
| 6 | +function modelBody(name: string) { |
| 7 | + const match = schema.match( |
| 8 | + new RegExp(`model\\s+${name}\\s+\\{([\\s\\S]*?)\\n\\}`), |
| 9 | + ); |
| 10 | + if (!match) throw new Error(`Missing Prisma model: ${name}`); |
| 11 | + return match[1]; |
| 12 | +} |
| 13 | + |
| 14 | +function expectMatch(source: string, pattern: RegExp, message: string) { |
| 15 | + if (!pattern.test(source)) throw new Error(message); |
| 16 | +} |
| 17 | + |
| 18 | +const friend = modelBody("friend"); |
| 19 | +expectMatch( |
| 20 | + friend, |
| 21 | + /^\s*@@id\(\[owner_id,\s*id\]\)/m, |
| 22 | + "friend must be keyed by owner_id + id", |
| 23 | +); |
| 24 | +if (/^\s*id\s+String\s+@id\b/m.test(friend)) { |
| 25 | + throw new Error("friend.id must not be a global primary key"); |
| 26 | +} |
| 27 | + |
| 28 | +const chatMessage = modelBody("chat_message"); |
| 29 | +for (const [field, pattern] of [ |
| 30 | + ["id", /^\s*id\s+String\s+@id\b/m], |
| 31 | + ["owner_id", /^\s*owner_id\s+String\b/m], |
| 32 | + ["chat_user_id", /^\s*chat_user_id\s+String\b/m], |
| 33 | + ["sender_id", /^\s*sender_id\s+String\b/m], |
| 34 | + ["content", /^\s*content\s+String\b/m], |
| 35 | + ["status", /^\s*status\s+String\b/m], |
| 36 | + ["created_at", /^\s*created_at\s+DateTime\b/m], |
| 37 | + ["updated_at", /^\s*updated_at\s+DateTime\b/m], |
| 38 | + ["retry_count", /^\s*retry_count\s+Int\b/m], |
| 39 | + ["last_error", /^\s*last_error\s+String\?/m], |
| 40 | +] as const) { |
| 41 | + expectMatch(chatMessage, pattern, `chat_message is missing ${field}`); |
| 42 | +} |
| 43 | + |
| 44 | +const friendRequest = modelBody("friend_request"); |
| 45 | +for (const [field, pattern] of [ |
| 46 | + ["owner_id", /^\s*owner_id\s+String\b/m], |
| 47 | + ["remote_id", /^\s*remote_id\s+String\b/m], |
| 48 | + ["name", /^\s*name\s+String\b/m], |
| 49 | + ["avatar", /^\s*avatar\s+Bytes\?/m], |
| 50 | + ["bio", /^\s*bio\s+String\b/m], |
| 51 | + ["status", /^\s*status\s+String\b/m], |
| 52 | + ["created_at", /^\s*created_at\s+DateTime\b/m], |
| 53 | + ["responded_at", /^\s*responded_at\s+DateTime\?/m], |
| 54 | +] as const) { |
| 55 | + expectMatch(friendRequest, pattern, `friend_request is missing ${field}`); |
| 56 | +} |
| 57 | +expectMatch( |
| 58 | + friendRequest, |
| 59 | + /^\s*@@id\(\[owner_id,\s*remote_id\]\)/m, |
| 60 | + "friend_request must be keyed by owner_id + remote_id", |
| 61 | +); |
| 62 | + |
| 63 | +expectMatch(endpointTypes, /id:\s*string/, "Message type must include id"); |
| 64 | +expectMatch( |
| 65 | + endpointTypes, |
| 66 | + /sender_id:\s*string/, |
| 67 | + "Message type must include sender_id", |
| 68 | +); |
| 69 | +expectMatch( |
| 70 | + endpointTypes, |
| 71 | + /status:\s*MessageStatus/, |
| 72 | + "Message type must expose MessageStatus", |
| 73 | +); |
| 74 | +expectMatch( |
| 75 | + endpointTypes, |
| 76 | + /created_at:\s*string/, |
| 77 | + "Message type must expose sortable created_at", |
| 78 | +); |
| 79 | +expectMatch( |
| 80 | + endpointTypes, |
| 81 | + /retry_count:\s*number/, |
| 82 | + "Message type must include retry_count", |
| 83 | +); |
| 84 | +expectMatch( |
| 85 | + endpointTypes, |
| 86 | + /last_error\?:\s*string/, |
| 87 | + "Message type must include optional last_error", |
| 88 | +); |
0 commit comments