|
| 1 | +import { Socket } from 'net'; |
| 2 | + |
| 3 | +export type ZongjiOptions = { |
| 4 | + host: string; |
| 5 | + user: string; |
| 6 | + password: string; |
| 7 | + dateStrings?: boolean; |
| 8 | + timeZone?: string; |
| 9 | +}; |
| 10 | + |
| 11 | +interface DatabaseFilter { |
| 12 | + [databaseName: string]: string[] | true; |
| 13 | +} |
| 14 | + |
| 15 | +export type StartOptions = { |
| 16 | + includeEvents?: string[]; |
| 17 | + excludeEvents?: string[]; |
| 18 | + /** |
| 19 | + * Describe which databases and tables to include (Only for row events). Use database names as the key and pass an array of table names or true (for the entire database). |
| 20 | + * Example: { 'my_database': ['allow_table', 'another_table'], 'another_db': true } |
| 21 | + */ |
| 22 | + includeSchema?: DatabaseFilter; |
| 23 | + /** |
| 24 | + * Object describing which databases and tables to exclude (Same format as includeSchema) |
| 25 | + * Example: { 'other_db': ['disallowed_table'], 'ex_db': true } |
| 26 | + */ |
| 27 | + excludeSchema?: DatabaseFilter; |
| 28 | + /** |
| 29 | + * BinLog position filename to start reading events from |
| 30 | + */ |
| 31 | + filename?: string; |
| 32 | + /** |
| 33 | + * BinLog position offset to start reading events from in file specified |
| 34 | + */ |
| 35 | + position?: number; |
| 36 | + |
| 37 | + /** |
| 38 | + * Unique server ID for this replication client. |
| 39 | + */ |
| 40 | + serverId?: number; |
| 41 | +}; |
| 42 | + |
| 43 | +export type ColumnSchema = { |
| 44 | + COLUMN_NAME: string; |
| 45 | + COLLATION_NAME: string; |
| 46 | + CHARACTER_SET_NAME: string; |
| 47 | + COLUMN_COMMENT: string; |
| 48 | + COLUMN_TYPE: string; |
| 49 | +}; |
| 50 | + |
| 51 | +export type ColumnDefinition = { |
| 52 | + name: string; |
| 53 | + charset: string; |
| 54 | + type: number; |
| 55 | + metadata: Record<string, any>; |
| 56 | +}; |
| 57 | + |
| 58 | +export type TableMapEntry = { |
| 59 | + columnSchemas: ColumnSchema[]; |
| 60 | + parentSchema: string; |
| 61 | + tableName: string; |
| 62 | + columns: ColumnDefinition[]; |
| 63 | +}; |
| 64 | + |
| 65 | +export type BaseBinLogEvent = { |
| 66 | + timestamp: number; |
| 67 | + getEventName(): string; |
| 68 | + |
| 69 | + /** |
| 70 | + * Next position in BinLog file to read from after |
| 71 | + * this event. |
| 72 | + */ |
| 73 | + nextPosition: number; |
| 74 | + /** |
| 75 | + * Size of this event |
| 76 | + */ |
| 77 | + size: number; |
| 78 | + flags: number; |
| 79 | + useChecksum: boolean; |
| 80 | +}; |
| 81 | + |
| 82 | +export type BinLogRotationEvent = BaseBinLogEvent & { |
| 83 | + binlogName: string; |
| 84 | + position: number; |
| 85 | +}; |
| 86 | + |
| 87 | +export type BinLogGTIDLogEvent = BaseBinLogEvent & { |
| 88 | + serverId: Buffer; |
| 89 | + transactionRange: number; |
| 90 | +}; |
| 91 | + |
| 92 | +export type BinLogXidEvent = BaseBinLogEvent & { |
| 93 | + xid: number; |
| 94 | +}; |
| 95 | + |
| 96 | +export type BinLogMutationEvent = BaseBinLogEvent & { |
| 97 | + tableId: number; |
| 98 | + numberOfColumns: number; |
| 99 | + tableMap: Record<string, TableMapEntry>; |
| 100 | + rows: Record<string, any>[]; |
| 101 | +}; |
| 102 | + |
| 103 | +export type BinLogUpdateEvent = Omit<BinLogMutationEvent, 'rows'> & { |
| 104 | + rows: { |
| 105 | + before: Record<string, any>; |
| 106 | + after: Record<string, any>; |
| 107 | + }[]; |
| 108 | +}; |
| 109 | + |
| 110 | +export type BinLogEvent = BinLogRotationEvent | BinLogGTIDLogEvent | BinLogXidEvent | BinLogMutationEvent; |
| 111 | + |
| 112 | +// @vlasky/mysql Connection |
| 113 | +export interface MySQLConnection { |
| 114 | + _socket?: Socket; |
| 115 | + /** There are other forms of this method as well - this is the most basic one. */ |
| 116 | + query(sql: string, callback: (error: any, results: any, fields: any) => void): void; |
| 117 | +} |
| 118 | + |
| 119 | +export default class ZongJi { |
| 120 | + connection: MySQLConnection; |
| 121 | + constructor(options: ZongjiOptions); |
| 122 | + |
| 123 | + start(options: StartOptions): void; |
| 124 | + stop(): void; |
| 125 | + pause(): void; |
| 126 | + resume(): void; |
| 127 | + |
| 128 | + on(type: 'binlog' | string, callback: (event: BinLogEvent) => void); |
| 129 | +} |
0 commit comments