|
1 | 1 | import { AbstractPowerSyncDatabase, column, Schema, Table } from '@powersync/common';
|
2 | 2 | import { PowerSyncDatabase } from '@powersync/web';
|
3 |
| -import { count, eq, sql } from 'drizzle-orm'; |
| 3 | +import { count, eq, relations, sql } from 'drizzle-orm'; |
4 | 4 | import { integer, sqliteTable, text, uniqueIndex } from 'drizzle-orm/sqlite-core';
|
5 | 5 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
6 | 6 | import * as SUT from '../../src/sqlite/db';
|
@@ -52,7 +52,18 @@ const customers = sqliteTable('customers', {
|
52 | 52 | email: text('email')
|
53 | 53 | });
|
54 | 54 |
|
55 |
| -const DrizzleSchema = { assets, customers }; |
| 55 | +export const customersRelations = relations(customers, ({ many }) => ({ |
| 56 | + assets: many(assets) |
| 57 | +})); |
| 58 | + |
| 59 | +export const assetsRelations = relations(assets, ({ one }) => ({ |
| 60 | + customer: one(customers, { |
| 61 | + fields: [assets.customer_id], |
| 62 | + references: [customers.id] |
| 63 | + }) |
| 64 | +})); |
| 65 | + |
| 66 | +const DrizzleSchema = { assets, customers, assetsRelations, customersRelations }; |
56 | 67 |
|
57 | 68 | /**
|
58 | 69 | * There seems to be an issue with Vitest browser mode's setTimeout and
|
@@ -280,4 +291,85 @@ describe('Watch Tests', () => {
|
280 | 291 | expect(receivedWithManagedOverflowCount).greaterThan(2);
|
281 | 292 | expect(receivedWithManagedOverflowCount).toBeLessThanOrEqual(4);
|
282 | 293 | });
|
| 294 | + |
| 295 | + it('should contain relational data (one to many)', async () => { |
| 296 | + const abortController = new AbortController(); |
| 297 | + |
| 298 | + const query = db.query.customers.findMany({ with: { assets: true } }); |
| 299 | + |
| 300 | + let receivedResult: any = undefined; |
| 301 | + |
| 302 | + const receivedUpdates = new Promise<void>((resolve) => { |
| 303 | + const onUpdate = (update) => { |
| 304 | + receivedResult = update; |
| 305 | + resolve(); |
| 306 | + }; |
| 307 | + |
| 308 | + db.watch(query, { onResult: onUpdate }, { signal: abortController.signal }); |
| 309 | + }); |
| 310 | + |
| 311 | + const customerId = '39281cf9-9989-4b31-bb21-8f45ce3b3e60'; |
| 312 | + const assetId = '00000000-9989-4b31-bb21-8f45ce3b3e61'; |
| 313 | + await db |
| 314 | + .insert(customers) |
| 315 | + .values({ |
| 316 | + id: customerId, |
| 317 | + name: 'Alice' |
| 318 | + }) |
| 319 | + .execute(); |
| 320 | + |
| 321 | + await db |
| 322 | + .insert(assets) |
| 323 | + .values({ |
| 324 | + id: assetId, |
| 325 | + customer_id: customerId |
| 326 | + }) |
| 327 | + .execute(); |
| 328 | + |
| 329 | + await receivedUpdates; |
| 330 | + abortController.abort(); |
| 331 | + |
| 332 | + expect(receivedResult[0].assets[0]['id']).toEqual(assetId); |
| 333 | + expect(receivedResult[0].assets[0]['customer_id']).toEqual(customerId); |
| 334 | + }); |
| 335 | + |
| 336 | + it('should contain relational data (many to one)', async () => { |
| 337 | + const abortController = new AbortController(); |
| 338 | + |
| 339 | + const query = db.query.assets.findFirst({ with: { customer: true } }); |
| 340 | + |
| 341 | + let receivedResult: any = undefined; |
| 342 | + |
| 343 | + const receivedUpdates = new Promise<void>((resolve) => { |
| 344 | + const onUpdate = (update) => { |
| 345 | + receivedResult = update; |
| 346 | + resolve(); |
| 347 | + }; |
| 348 | + |
| 349 | + db.watch(query, { onResult: onUpdate }, { signal: abortController.signal }); |
| 350 | + }); |
| 351 | + |
| 352 | + const customerId = '39281cf9-9989-4b31-bb21-8f45ce3b3e60'; |
| 353 | + const assetId = '00000000-9989-4b31-bb21-8f45ce3b3e61'; |
| 354 | + await db |
| 355 | + .insert(customers) |
| 356 | + .values({ |
| 357 | + id: customerId, |
| 358 | + name: 'Alice' |
| 359 | + }) |
| 360 | + .execute(); |
| 361 | + |
| 362 | + await db |
| 363 | + .insert(assets) |
| 364 | + .values({ |
| 365 | + id: assetId, |
| 366 | + customer_id: customerId |
| 367 | + }) |
| 368 | + .execute(); |
| 369 | + |
| 370 | + await receivedUpdates; |
| 371 | + abortController.abort(); |
| 372 | + |
| 373 | + expect(receivedResult[0].customer['id']).toEqual(customerId); |
| 374 | + }); |
283 | 375 | });
|
0 commit comments