|
1 | 1 | import { |
2 | | - AbstractPowerSyncDatabase, |
3 | | - ArrayComparator, |
4 | | - GetAllQuery, |
5 | | - QueryResult, |
6 | | - WatchedQueryDifferential, |
7 | | - WatchedQueryState |
| 2 | + AbstractPowerSyncDatabase, |
| 3 | + ArrayComparator, |
| 4 | + GetAllQuery, |
| 5 | + QueryResult, |
| 6 | + WatchedQueryDifferential, |
| 7 | + WatchedQueryState |
8 | 8 | } from '@powersync/common'; |
9 | 9 | import { PowerSyncDatabase } from '@powersync/web'; |
10 | 10 | import { v4 as uuid } from 'uuid'; |
11 | 11 | import { afterEach, beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest'; |
12 | | -import { testSchema } from './utils/testDb'; |
| 12 | +import { TestDatabase, testSchema } from './utils/testDb'; |
13 | 13 | vi.useRealTimers(); |
14 | 14 |
|
15 | 15 | /** |
@@ -270,6 +270,204 @@ describe('Watch Tests', { sequential: true }, () => { |
270 | 270 | expect(receivedCustomersUpdatesCount).equals(1); |
271 | 271 | }); |
272 | 272 |
|
| 273 | + it('should allow overriding table dependencies', async () => { |
| 274 | + const assetsAbortController = new AbortController(); |
| 275 | + |
| 276 | + type CustomerAssetJoin = TestDatabase['assets'] & { customer_name: string; customer_email: string }; |
| 277 | + const results: CustomerAssetJoin[][] = []; |
| 278 | + |
| 279 | + const onWatchAssets = (resultSet: CustomerAssetJoin[]) => { |
| 280 | + results.push(resultSet); |
| 281 | + }; |
| 282 | + |
| 283 | + const { id: customerId } = await powersync.get<{ id: string }>(`SELECT uuid() as id`); |
| 284 | + |
| 285 | + await powersync.execute( |
| 286 | + /* sql */ ` |
| 287 | + INSERT INTO |
| 288 | + customers (id, name, email) |
| 289 | + VALUES |
| 290 | + (?, ?, ?) |
| 291 | + `, |
| 292 | + [customerId, 'bob', '[email protected]'] |
| 293 | + ); |
| 294 | + |
| 295 | + await powersync.execute( |
| 296 | + /* sql */ ` |
| 297 | + INSERT into |
| 298 | + assets (id, make, model, customer_id) |
| 299 | + VALUES |
| 300 | + (uuid (), 'sync_engine', 'powersync', ?) |
| 301 | + `, |
| 302 | + [customerId] |
| 303 | + ); |
| 304 | + |
| 305 | + powersync.watch( |
| 306 | + /* sql */ |
| 307 | + ` |
| 308 | + SELECT |
| 309 | + assets.make, |
| 310 | + assets.model, |
| 311 | + assets.serial_number, |
| 312 | + customers.name AS customer_name, |
| 313 | + customers.email AS customer_email |
| 314 | + FROM |
| 315 | + assets |
| 316 | + LEFT JOIN customers ON assets.customer_id = customers.id; |
| 317 | + `, |
| 318 | + [], |
| 319 | + { onResult: (r) => onWatchAssets(r.rows?._array ?? []) }, |
| 320 | + { |
| 321 | + signal: assetsAbortController.signal, |
| 322 | + // Only trigger on changes to the customers table |
| 323 | + tables: ['customers'], |
| 324 | + throttleMs: 0 |
| 325 | + } |
| 326 | + ); |
| 327 | + |
| 328 | + await vi.waitFor( |
| 329 | + () => { |
| 330 | + expect(results.length).eq(1); |
| 331 | + expect(results[0].length).eq(1); |
| 332 | + }, |
| 333 | + { |
| 334 | + timeout: 1000 |
| 335 | + } |
| 336 | + ); |
| 337 | + |
| 338 | + // Do an update on the assets table, this should not trigger the watched query |
| 339 | + // due to the override |
| 340 | + for (let attemptCount = 0; attemptCount < 5; attemptCount++) { |
| 341 | + await powersync.execute( |
| 342 | + /* sql */ ` |
| 343 | + INSERT into |
| 344 | + assets (id, make, model, customer_id) |
| 345 | + VALUES |
| 346 | + (uuid (), 'sync_engine', 'powersync_v2', ?) |
| 347 | + `, |
| 348 | + [customerId] |
| 349 | + ); |
| 350 | + // Give some time for watched queries to fire (if they need to) |
| 351 | + await new Promise((r) => setTimeout(r, 100)); |
| 352 | + } |
| 353 | + |
| 354 | + // now trigger an update on the customers table, this should update the watched query |
| 355 | + await powersync.execute( |
| 356 | + /* sql */ ` |
| 357 | + INSERT INTO |
| 358 | + customers (id, name, email) |
| 359 | + VALUES |
| 360 | + (uuid (), ?, ?) |
| 361 | + `, |
| 362 | + |
| 363 | + ); |
| 364 | + |
| 365 | + await vi.waitFor( |
| 366 | + () => { |
| 367 | + expect(results.length).eq(2); |
| 368 | + }, |
| 369 | + { timeout: 1000 } |
| 370 | + ); |
| 371 | + }); |
| 372 | + |
| 373 | + it('should allow overriding table dependencies (query api)', async () => { |
| 374 | + const { id: customerId } = await powersync.get<{ id: string }>(`SELECT uuid() as id`); |
| 375 | + |
| 376 | + await powersync.execute( |
| 377 | + /* sql */ ` |
| 378 | + INSERT INTO |
| 379 | + customers (id, name, email) |
| 380 | + VALUES |
| 381 | + (?, ?, ?) |
| 382 | + `, |
| 383 | + [customerId, 'bob', '[email protected]'] |
| 384 | + ); |
| 385 | + |
| 386 | + await powersync.execute( |
| 387 | + /* sql */ ` |
| 388 | + INSERT into |
| 389 | + assets (id, make, model, customer_id) |
| 390 | + VALUES |
| 391 | + (uuid (), 'sync_engine', 'powersync', ?) |
| 392 | + `, |
| 393 | + [customerId] |
| 394 | + ); |
| 395 | + |
| 396 | + type CustomerAssetJoin = TestDatabase['assets'] & { customer_name: string; customer_email: string }; |
| 397 | + const results: CustomerAssetJoin[][] = []; |
| 398 | + |
| 399 | + const query = powersync |
| 400 | + .query<CustomerAssetJoin>({ |
| 401 | + sql: |
| 402 | + /* sql */ |
| 403 | + ` |
| 404 | + SELECT |
| 405 | + assets.make, |
| 406 | + assets.model, |
| 407 | + assets.serial_number, |
| 408 | + customers.name AS customer_name, |
| 409 | + customers.email AS customer_email |
| 410 | + FROM |
| 411 | + assets |
| 412 | + LEFT JOIN customers ON assets.customer_id = customers.id; |
| 413 | + ` |
| 414 | + }) |
| 415 | + .watch({ |
| 416 | + triggerOnTables: ['customers'], |
| 417 | + throttleMs: 0 |
| 418 | + }); |
| 419 | + |
| 420 | + query.registerListener({ |
| 421 | + onData: (data) => { |
| 422 | + results.push([...data]); |
| 423 | + } |
| 424 | + }); |
| 425 | + |
| 426 | + await vi.waitFor( |
| 427 | + () => { |
| 428 | + expect(results.length).eq(1); |
| 429 | + expect(results[0].length).eq(1); |
| 430 | + }, |
| 431 | + { |
| 432 | + timeout: 1000 |
| 433 | + } |
| 434 | + ); |
| 435 | + |
| 436 | + // Do an update on the assets table, this should not trigger the watched query |
| 437 | + // due to the override |
| 438 | + for (let attemptCount = 0; attemptCount < 5; attemptCount++) { |
| 439 | + await powersync.execute( |
| 440 | + /* sql */ ` |
| 441 | + INSERT into |
| 442 | + assets (id, make, model, customer_id) |
| 443 | + VALUES |
| 444 | + (uuid (), 'sync_engine', 'powersync_v2', ?) |
| 445 | + `, |
| 446 | + [customerId] |
| 447 | + ); |
| 448 | + // Give some time for watched queries to fire (if they need to) |
| 449 | + await new Promise((r) => setTimeout(r, 100)); |
| 450 | + } |
| 451 | + |
| 452 | + // now trigger an update on the customers table, this should update the watched query |
| 453 | + await powersync.execute( |
| 454 | + /* sql */ ` |
| 455 | + INSERT INTO |
| 456 | + customers (id, name, email) |
| 457 | + VALUES |
| 458 | + (uuid (), ?, ?) |
| 459 | + `, |
| 460 | + |
| 461 | + ); |
| 462 | + |
| 463 | + await vi.waitFor( |
| 464 | + () => { |
| 465 | + expect(results.length).eq(2); |
| 466 | + }, |
| 467 | + { timeout: 1000 } |
| 468 | + ); |
| 469 | + }); |
| 470 | + |
273 | 471 | it('should handle watch onError callback', async () => { |
274 | 472 | const abortController = new AbortController(); |
275 | 473 | const onResult = () => {}; // no-op |
|
0 commit comments