Skip to content

Commit 65ca2b2

Browse files
authored
Add query event type definitions (#5)
* Added binlog query event type definition * Added changeset * Try EventEmitter type declaration for Zongji class * Only emit rotate events when the binlog file has actually changed. * Revert rotate event, change. Rotate events should always be emited on listener startup. * Added filter function support to include/exclude tables in a db schema based on the result of the function. * Updated changeset * Updated dev-packages github action to not run on pushes to main.
1 parent 427ac6b commit 65ca2b2

File tree

5 files changed

+57
-9
lines changed

5 files changed

+57
-9
lines changed

.changeset/five-otters-attack.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@powersync/mysql-zongji': minor
3+
---
4+
5+
- Added type definitions for binlog query event
6+
- Added the ability to provide a table filter function for including/excluding binlog events
7+
- Updated Zongji class type definition

.github/workflows/dev-packages.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ name: Create Dev Release
44

55
on:
66
push:
7+
branches:
8+
- '**'
9+
- '!main' # excludes main, this is handled by changesets
10+
tags-ignore:
11+
- '**'
712

813
jobs:
914
publish:

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,14 @@ ZongJi.prototype._skipSchema = function (database, table) {
325325
let included =
326326
includes === undefined ||
327327
(database in includes &&
328-
(includes[database] === true || (Array.isArray(includes[database]) && includes[database].indexOf(table) > -1)));
328+
(includes[database] === true ||
329+
(Array.isArray(includes[database]) && includes[database].indexOf(table) > -1) ||
330+
(typeof includes[database] === 'function' && includes[database](table))));
329331
let excluded =
330332
database in excludes &&
331-
(excludes[database] === true || (Array.isArray(excludes[database]) && excludes[database].indexOf(table) > -1));
333+
(excludes[database] === true ||
334+
(Array.isArray(excludes[database]) && excludes[database].indexOf(table) > -1) ||
335+
(typeof excludes[database] === 'function' && excludes[database](table)));
332336

333337
return excluded || !included;
334338
};

test/filtering.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ tap.test('Unit test', (test) => {
4545
test.end();
4646
});
4747

48+
test.test('FilterFunction includes', (test) => {
49+
zongji._filters({
50+
includeSchema: { db1: (table) => table === 'just_me' }
51+
});
52+
test.ok(!zongji._skipSchema('db1', 'just_me'));
53+
test.ok(zongji._skipSchema('db2', 'anything_else'));
54+
test.ok(zongji._skipSchema('db1', 'not_me'));
55+
56+
test.end();
57+
});
58+
4859
test.test((test) => {
4960
zongji._filters({
5061
excludeSchema: { db1: ['not_me'] }
@@ -57,6 +68,18 @@ tap.test('Unit test', (test) => {
5768
test.end();
5869
});
5970

71+
test.test('FilterFunction excludes', (test) => {
72+
zongji._filters({
73+
excludeSchema: { db1: (table) => table === 'not_me' }
74+
});
75+
76+
test.ok(!zongji._skipSchema('db1', 'anything_else'));
77+
test.ok(!zongji._skipSchema('db2', 'anything_else'));
78+
test.ok(zongji._skipSchema('db1', 'not_me'));
79+
80+
test.end();
81+
});
82+
6083
test.test((test) => {
6184
zongji._filters({
6285
excludeEvents: ['rotate']
@@ -79,7 +102,7 @@ tap.test('Unit test', (test) => {
79102
test.end();
80103
});
81104

82-
tap.test('Exclue all the schema', (test) => {
105+
tap.test('Exclude all the schema', (test) => {
83106
const zongji = new ZongJi(settings.connection);
84107

85108
const eventLog = [];

types/index.d.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Socket } from 'net';
2+
import { EventEmitter } from 'events';
23

34
/**
45
* The types described here were added on an adhoc basis based on requirements in the Powersync Service.
@@ -20,10 +21,11 @@ export type ZongjiOptions = {
2021

2122
/**
2223
* Record specifying a database and specific tables. ie. ['MyDatabase']: ['table1', 'table2']
23-
* Alternatively specifying true will include all tables in the database.
24+
* OR a filter function that returns true for tables that should be included
25+
* OR specifying true will include all tables in the database.
2426
*/
2527
interface DatabaseFilter {
26-
[databaseName: string]: string[] | true;
28+
[databaseName: string]: string[] | true | ((table: string) => boolean);
2729
}
2830

2931
export type StartOptions = {
@@ -153,13 +155,22 @@ export type BinLogTableMapEvent = BaseBinLogEvent & {
153155
columnTypes: number[];
154156
};
155157

158+
export type BinLogQueryEvent = BaseBinLogEvent & {
159+
query: string;
160+
executionTime: number;
161+
errorCode: number;
162+
schema: string;
163+
statusVars: string;
164+
};
165+
156166
export type BinLogEvent =
157167
| BinLogRotationEvent
158168
| BinLogGTIDLogEvent
159169
| BinLogXidEvent
160170
| BinLogRowEvent
161171
| BinLogRowUpdateEvent
162-
| BinLogTableMapEvent;
172+
| BinLogTableMapEvent
173+
| BinLogQueryEvent;
163174

164175
// @vlasky/mysql Connection
165176
export interface MySQLConnection {
@@ -168,7 +179,7 @@ export interface MySQLConnection {
168179
query(sql: string, callback: (error: any, results: any, fields: any) => void): void;
169180
}
170181

171-
export declare class ZongJi {
182+
export declare class ZongJi extends EventEmitter {
172183
stopped: boolean;
173184
connection: MySQLConnection;
174185
constructor(options: ZongjiOptions);
@@ -177,6 +188,4 @@ export declare class ZongJi {
177188
stop(): void;
178189
pause(): void;
179190
resume(): void;
180-
181-
on(type: 'binlog' | string, callback: (event: BinLogEvent) => void): void;
182191
}

0 commit comments

Comments
 (0)