|
| 1 | +import { AbstractPowerSyncDatabase, column, Schema, Table } from '@powersync/common'; |
| 2 | +import { PowerSyncDatabase } from '@powersync/web'; |
| 3 | +import { eq, relations } from 'drizzle-orm'; |
| 4 | +import { sqliteTable, text } from 'drizzle-orm/sqlite-core'; |
| 5 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 6 | +import * as SUT from '../../src/sqlite/PowerSyncSQLiteDatabase'; |
| 7 | + |
| 8 | +const users = new Table({ |
| 9 | + name: column.text |
| 10 | +}); |
| 11 | + |
| 12 | +const posts = new Table({ |
| 13 | + content: column.text, |
| 14 | + title: column.text, |
| 15 | + user_id: column.text |
| 16 | +}); |
| 17 | + |
| 18 | +const drizzleUsers = sqliteTable('users', { |
| 19 | + id: text('id').primaryKey().notNull(), |
| 20 | + name: text('name').notNull() |
| 21 | +}); |
| 22 | + |
| 23 | +const drizzlePosts = sqliteTable('posts', { |
| 24 | + id: text('id').primaryKey().notNull(), |
| 25 | + content: text('content').notNull(), |
| 26 | + title: text('title').notNull(), |
| 27 | + user_id: text('user_id') |
| 28 | + .notNull() |
| 29 | + .references(() => drizzleUsers.id) |
| 30 | +}); |
| 31 | + |
| 32 | +const usersRelations = relations(drizzleUsers, ({ one, many }) => ({ |
| 33 | + posts: many(drizzlePosts) |
| 34 | +})); |
| 35 | + |
| 36 | +const postsRelations = relations(drizzlePosts, ({ one }) => ({ |
| 37 | + user: one(drizzleUsers, { |
| 38 | + fields: [drizzlePosts.user_id], |
| 39 | + references: [drizzleUsers.id] |
| 40 | + }) |
| 41 | +})); |
| 42 | + |
| 43 | +const PsSchema = new Schema({ users, posts }); |
| 44 | +const DrizzleSchema = { users: drizzleUsers, posts: drizzlePosts, usersRelations, postsRelations }; |
| 45 | + |
| 46 | +describe('Relationship tests', () => { |
| 47 | + let powerSyncDb: AbstractPowerSyncDatabase; |
| 48 | + let db: SUT.PowerSyncSQLiteDatabase<typeof DrizzleSchema>; |
| 49 | + |
| 50 | + beforeEach(async () => { |
| 51 | + powerSyncDb = new PowerSyncDatabase({ |
| 52 | + database: { |
| 53 | + dbFilename: 'test.db' |
| 54 | + }, |
| 55 | + schema: PsSchema |
| 56 | + }); |
| 57 | + db = SUT.wrapPowerSyncWithDrizzle(powerSyncDb, { schema: DrizzleSchema, logger: { logQuery: () => {} } }); |
| 58 | + |
| 59 | + await powerSyncDb.init(); |
| 60 | + |
| 61 | + await db.insert(drizzleUsers).values({ id: '1', name: 'Alice' }); |
| 62 | + await db.insert(drizzlePosts).values({ id: '33', content: 'Post content', title: 'Post title', user_id: '1' }); |
| 63 | + }); |
| 64 | + |
| 65 | + afterEach(async () => { |
| 66 | + await powerSyncDb?.disconnectAndClear(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should retrieve a user with posts', async () => { |
| 70 | + const result = await db.query.users.findMany({ with: { posts: true } }); |
| 71 | + |
| 72 | + expect(result).toEqual([ |
| 73 | + { id: '1', name: 'Alice', posts: [{ id: '33', content: 'Post content', title: 'Post title', user_id: '1' }] } |
| 74 | + ]); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should retrieve a post with its user', async () => { |
| 78 | + const result = await db.query.posts.findMany({ with: { user: true } }); |
| 79 | + |
| 80 | + expect(result).toEqual([ |
| 81 | + { |
| 82 | + id: '33', |
| 83 | + content: 'Post content', |
| 84 | + title: 'Post title', |
| 85 | + user_id: '1', |
| 86 | + user: { id: '1', name: 'Alice' } |
| 87 | + } |
| 88 | + ]); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should return a user and posts using leftJoin', async () => { |
| 92 | + const result = await db |
| 93 | + .select() |
| 94 | + .from(drizzleUsers) |
| 95 | + .leftJoin(drizzlePosts, eq(drizzleUsers.id, drizzlePosts.user_id)); |
| 96 | + |
| 97 | + expect(result[0].users).toEqual({ id: '1', name: 'Alice' }); |
| 98 | + expect(result[0].posts).toEqual({ id: '33', content: 'Post content', title: 'Post title', user_id: '1' }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should return a user and posts using rightJoin', async () => { |
| 102 | + const result = await db |
| 103 | + .select() |
| 104 | + .from(drizzleUsers) |
| 105 | + .rightJoin(drizzlePosts, eq(drizzleUsers.id, drizzlePosts.user_id)); |
| 106 | + |
| 107 | + expect(result[0].users).toEqual({ id: '1', name: 'Alice' }); |
| 108 | + expect(result[0].posts).toEqual({ id: '33', content: 'Post content', title: 'Post title', user_id: '1' }); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should return a user and posts using fullJoin', async () => { |
| 112 | + const result = await db |
| 113 | + .select() |
| 114 | + .from(drizzleUsers) |
| 115 | + .fullJoin(drizzlePosts, eq(drizzleUsers.id, drizzlePosts.user_id)); |
| 116 | + |
| 117 | + expect(result[0].users).toEqual({ id: '1', name: 'Alice' }); |
| 118 | + expect(result[0].posts).toEqual({ id: '33', content: 'Post content', title: 'Post title', user_id: '1' }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments