Skip to content

Commit 8f2daa8

Browse files
authored
Drizzle watch relationship tests. (#428)
1 parent 0094e52 commit 8f2daa8

File tree

1 file changed

+94
-2
lines changed

1 file changed

+94
-2
lines changed

packages/drizzle-driver/tests/sqlite/watch.test.ts

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AbstractPowerSyncDatabase, column, Schema, Table } from '@powersync/common';
22
import { PowerSyncDatabase } from '@powersync/web';
3-
import { count, eq, sql } from 'drizzle-orm';
3+
import { count, eq, relations, sql } from 'drizzle-orm';
44
import { integer, sqliteTable, text, uniqueIndex } from 'drizzle-orm/sqlite-core';
55
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
66
import * as SUT from '../../src/sqlite/db';
@@ -52,7 +52,18 @@ const customers = sqliteTable('customers', {
5252
email: text('email')
5353
});
5454

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 };
5667

5768
/**
5869
* There seems to be an issue with Vitest browser mode's setTimeout and
@@ -280,4 +291,85 @@ describe('Watch Tests', () => {
280291
expect(receivedWithManagedOverflowCount).greaterThan(2);
281292
expect(receivedWithManagedOverflowCount).toBeLessThanOrEqual(4);
282293
});
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+
});
283375
});

0 commit comments

Comments
 (0)