@powersync/service-module-mysql 0.10.2 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,165 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import {
3
+ normalizeConnectionConfig,
4
+ parseMySQLConnectionParam,
5
+ parseMySQLConnectionParams
6
+ } from '@module/types/types.js';
7
+
8
+ describe('config', () => {
9
+ test('Should resolve database', () => {
10
+ const normalized = normalizeConnectionConfig({
11
+ type: 'mysql',
12
+ uri: 'mysql://user:pass@localhost:3306/mydb'
13
+ });
14
+ expect(normalized.database).equals('mydb');
15
+ });
16
+
17
+ describe('connection parameters', () => {
18
+ test('parses all connection parameters from URL query string', () => {
19
+ const normalized = normalizeConnectionConfig({
20
+ type: 'mysql',
21
+ uri: 'mysql://user:pass@localhost:3306/mydb?connectTimeout=5000&connectionLimit=20&queueLimit=100'
22
+ });
23
+ expect(normalized.connectionParams.connectTimeout).equals(5000);
24
+ expect(normalized.connectionParams.connectionLimit).equals(20);
25
+ expect(normalized.connectionParams.queueLimit).equals(100);
26
+ });
27
+
28
+ test('URL without connection parameters returns empty connectionParams', () => {
29
+ const normalized = normalizeConnectionConfig({
30
+ type: 'mysql',
31
+ uri: 'mysql://user:pass@localhost:3306/mydb'
32
+ });
33
+ expect(normalized.connectionParams).toEqual({});
34
+ });
35
+
36
+ test('parameters can be partially specified', () => {
37
+ const normalized = normalizeConnectionConfig({
38
+ type: 'mysql',
39
+ uri: 'mysql://user:pass@localhost:3306/mydb?connectTimeout=5000&queueLimit=50'
40
+ });
41
+ expect(normalized.connectionParams.connectTimeout).equals(5000);
42
+ expect(normalized.connectionParams.queueLimit).equals(50);
43
+ expect(normalized.connectionParams.connectionLimit).toBeUndefined();
44
+ });
45
+
46
+ test('ignores invalid (non-numeric) connection parameter values', () => {
47
+ const normalized = normalizeConnectionConfig({
48
+ type: 'mysql',
49
+ uri: 'mysql://user:pass@localhost:3306/mydb?connectTimeout=abc&connectionLimit=xyz'
50
+ });
51
+ expect(normalized.connectionParams.connectTimeout).toBeUndefined();
52
+ expect(normalized.connectionParams.connectionLimit).toBeUndefined();
53
+ });
54
+
55
+ test('ignores negative connection parameter values', () => {
56
+ const normalized = normalizeConnectionConfig({
57
+ type: 'mysql',
58
+ uri: 'mysql://user:pass@localhost:3306/mydb?connectTimeout=-5000&connectionLimit=-10'
59
+ });
60
+ expect(normalized.connectionParams.connectTimeout).toBeUndefined();
61
+ expect(normalized.connectionParams.connectionLimit).toBeUndefined();
62
+ });
63
+
64
+ test('ignores zero connection parameter values', () => {
65
+ const normalized = normalizeConnectionConfig({
66
+ type: 'mysql',
67
+ uri: 'mysql://user:pass@localhost:3306/mydb?connectTimeout=0&connectionLimit=0&queueLimit=0'
68
+ });
69
+ expect(normalized.connectionParams.connectTimeout).toBeUndefined();
70
+ expect(normalized.connectionParams.connectionLimit).toBeUndefined();
71
+ expect(normalized.connectionParams.queueLimit).toBeUndefined();
72
+ });
73
+
74
+ test('works without URI (config-only)', () => {
75
+ const normalized = normalizeConnectionConfig({
76
+ type: 'mysql',
77
+ hostname: 'localhost',
78
+ port: 3306,
79
+ database: 'mydb',
80
+ username: 'user',
81
+ password: 'pass'
82
+ });
83
+ expect(normalized.connectionParams).toEqual({});
84
+ });
85
+ });
86
+ });
87
+
88
+ describe('parseMySQLConnectionParam', () => {
89
+ test('returns undefined when no value provided', () => {
90
+ expect(parseMySQLConnectionParam(undefined)).toBeUndefined();
91
+ expect(parseMySQLConnectionParam(null)).toBeUndefined();
92
+ });
93
+
94
+ test('parses valid numeric string', () => {
95
+ expect(parseMySQLConnectionParam('5000')).equals(5000);
96
+ });
97
+
98
+ test('parses fractional values', () => {
99
+ expect(parseMySQLConnectionParam('1500.5')).equals(1500.5);
100
+ });
101
+
102
+ test('ignores non-numeric string', () => {
103
+ expect(parseMySQLConnectionParam('abc')).toBeUndefined();
104
+ });
105
+
106
+ test('ignores empty string', () => {
107
+ expect(parseMySQLConnectionParam('')).toBeUndefined();
108
+ });
109
+
110
+ test('ignores negative value', () => {
111
+ expect(parseMySQLConnectionParam('-5000')).toBeUndefined();
112
+ });
113
+
114
+ test('ignores zero', () => {
115
+ expect(parseMySQLConnectionParam('0')).toBeUndefined();
116
+ });
117
+
118
+ test('ignores Infinity', () => {
119
+ expect(parseMySQLConnectionParam('Infinity')).toBeUndefined();
120
+ });
121
+
122
+ test('ignores NaN', () => {
123
+ expect(parseMySQLConnectionParam('NaN')).toBeUndefined();
124
+ });
125
+ });
126
+
127
+ describe('parseMySQLConnectionParams', () => {
128
+ test('parses all supported parameters', () => {
129
+ const params = new URLSearchParams('connectTimeout=5000&connectionLimit=20&queueLimit=100');
130
+ const result = parseMySQLConnectionParams(params);
131
+ expect(result).toEqual({
132
+ connectTimeout: 5000,
133
+ connectionLimit: 20,
134
+ queueLimit: 100
135
+ });
136
+ });
137
+
138
+ test('returns empty object when no connection params present', () => {
139
+ const params = new URLSearchParams('someOther=value');
140
+ const result = parseMySQLConnectionParams(params);
141
+ expect(result).toEqual({});
142
+ });
143
+
144
+ test('returns empty object for undefined searchParams', () => {
145
+ const result = parseMySQLConnectionParams(undefined);
146
+ expect(result).toEqual({});
147
+ });
148
+
149
+ test('ignores invalid values and only includes valid ones', () => {
150
+ const params = new URLSearchParams('connectTimeout=5000&connectionLimit=abc&queueLimit=-10');
151
+ const result = parseMySQLConnectionParams(params);
152
+ expect(result).toEqual({
153
+ connectTimeout: 5000
154
+ });
155
+ });
156
+
157
+ test('handles partial parameter specification', () => {
158
+ const params = new URLSearchParams('connectTimeout=5000&queueLimit=100');
159
+ const result = parseMySQLConnectionParams(params);
160
+ expect(result).toEqual({
161
+ connectTimeout: 5000,
162
+ queueLimit: 100
163
+ });
164
+ });
165
+ });