|
1 | | -import { AbstractPowerSyncDatabase } from '@powersync/common'; |
2 | | -import { eq, sql } from 'drizzle-orm'; |
| 1 | +import { AbstractPowerSyncDatabase, column, Schema, Table } from '@powersync/common'; |
| 2 | +import { eq, relations, sql } from 'drizzle-orm'; |
3 | 3 | import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
4 | 4 | import * as SUT from '../../src/sqlite/PowerSyncSQLiteDatabase'; |
5 | 5 | import { DrizzleSchema, drizzleUsers, getDrizzleDb, getPowerSyncDb } from '../setup/db'; |
| 6 | +import { alias, sqliteTable, text } from 'drizzle-orm/sqlite-core'; |
| 7 | +import { DrizzleAppSchema, DrizzleTableWithPowerSyncOptions, toPowerSyncTable } from '../../src/utils/schema'; |
| 8 | +import { PowerSyncDatabase } from '@powersync/web'; |
6 | 9 |
|
7 | 10 | describe('Database operations', () => { |
8 | | - let powerSyncDb: AbstractPowerSyncDatabase; |
9 | | - let db: SUT.PowerSyncSQLiteDatabase<typeof DrizzleSchema>; |
| 11 | + // let powerSyncDb: AbstractPowerSyncDatabase; |
| 12 | + // let db: SUT.PowerSyncSQLiteDatabase<typeof DrizzleSchema>; |
10 | 13 |
|
11 | | - beforeEach(() => { |
12 | | - powerSyncDb = getPowerSyncDb(); |
13 | | - db = getDrizzleDb(powerSyncDb); |
| 14 | + // beforeEach(() => { |
| 15 | + // powerSyncDb = getPowerSyncDb(); |
| 16 | + // db = getDrizzleDb(powerSyncDb); |
| 17 | + // }); |
| 18 | + |
| 19 | + // afterEach(async () => { |
| 20 | + // await powerSyncDb.disconnectAndClear(); |
| 21 | + // }); |
| 22 | + |
| 23 | + // it('should insert a user and select that user', async () => { |
| 24 | + // await db.insert(drizzleUsers).values({ id: '1', name: 'John' }); |
| 25 | + // const result = await db.select().from(drizzleUsers); |
| 26 | + |
| 27 | + // expect(result.length).toEqual(1); |
| 28 | + // }); |
| 29 | + |
| 30 | + // it('should insert a user and delete that user', async () => { |
| 31 | + // await db.insert(drizzleUsers).values({ id: '2', name: 'Ben' }); |
| 32 | + // await db.delete(drizzleUsers).where(eq(drizzleUsers.name, 'Ben')); |
| 33 | + // const result = await db.select().from(drizzleUsers); |
| 34 | + |
| 35 | + // expect(result.length).toEqual(0); |
| 36 | + // }); |
| 37 | + |
| 38 | + // it('should insert a user and update that user', async () => { |
| 39 | + // await db.insert(drizzleUsers).values({ id: '3', name: 'Lucy' }); |
| 40 | + // await db.update(drizzleUsers).set({ name: 'Lucy Smith' }).where(eq(drizzleUsers.name, 'Lucy')); |
| 41 | + // const result = await db.select().from(drizzleUsers).get(); |
| 42 | + |
| 43 | + // expect(result!.name).toEqual('Lucy Smith'); |
| 44 | + // }); |
| 45 | + |
| 46 | + // it('should insert a user and update that user within a transaction', async () => { |
| 47 | + // await db.transaction(async (transaction) => { |
| 48 | + // await transaction.insert(drizzleUsers).values({ id: '4', name: 'James' }); |
| 49 | + // await transaction.update(drizzleUsers).set({ name: 'James Smith' }).where(eq(drizzleUsers.name, 'James')); |
| 50 | + // }); |
| 51 | + // const result = await db.select().from(drizzleUsers).get(); |
| 52 | + |
| 53 | + // expect(result!.name).toEqual('James Smith'); |
| 54 | + // }); |
| 55 | + |
| 56 | + // it('should insert a user and update that user within a transaction when raw sql is used', async () => { |
| 57 | + // await db.transaction(async (transaction) => { |
| 58 | + // await transaction.run(sql`INSERT INTO users (id, name) VALUES ('4', 'James');`); |
| 59 | + // await transaction.update(drizzleUsers).set({ name: 'James Smith' }).where(eq(drizzleUsers.name, 'James')); |
| 60 | + // }); |
| 61 | + |
| 62 | + // const result = await db.select().from(drizzleUsers).get(); |
| 63 | + |
| 64 | + // expect(result!.name).toEqual('James Smith'); |
| 65 | + // }); |
| 66 | + const users = new Table({ |
| 67 | + name: column.text |
14 | 68 | }); |
15 | 69 |
|
16 | | - afterEach(async () => { |
17 | | - await powerSyncDb.disconnectAndClear(); |
| 70 | + const posts = new Table({ |
| 71 | + content: column.text, |
| 72 | + title: column.text, |
| 73 | + xxx: column.text |
18 | 74 | }); |
19 | 75 |
|
20 | | - it('should insert a user and select that user', async () => { |
21 | | - await db.insert(drizzleUsers).values({ id: '1', name: 'John' }); |
22 | | - const result = await db.select().from(drizzleUsers); |
| 76 | + const drizzleUsers = sqliteTable('users', { |
| 77 | + id: text('id').primaryKey().notNull(), |
| 78 | + name: text('name').notNull() |
| 79 | + }); |
23 | 80 |
|
24 | | - expect(result.length).toEqual(1); |
| 81 | + const drizzlePosts = sqliteTable('posts', { |
| 82 | + id: text('id').primaryKey().notNull(), |
| 83 | + content: text('content').notNull(), |
| 84 | + title: text('title').notNull(), |
| 85 | + xxx: text('xxx') |
| 86 | + .notNull() |
| 87 | + .references(() => drizzleUsers.id) |
25 | 88 | }); |
26 | 89 |
|
27 | | - it('should insert a user and delete that user', async () => { |
28 | | - await db.insert(drizzleUsers).values({ id: '2', name: 'Ben' }); |
29 | | - await db.delete(drizzleUsers).where(eq(drizzleUsers.name, 'Ben')); |
30 | | - const result = await db.select().from(drizzleUsers); |
| 90 | + // Define relationships |
| 91 | + const usersRelations = relations(drizzleUsers, ({ one, many }) => ({ |
| 92 | + posts: many(drizzlePosts) // One user has many posts |
| 93 | + })); |
31 | 94 |
|
32 | | - expect(result.length).toEqual(0); |
33 | | - }); |
| 95 | + const postsRelations = relations(drizzlePosts, ({ one }) => ({ |
| 96 | + user: one(drizzleUsers, { |
| 97 | + fields: [drizzlePosts.xxx], // Foreign key in posts |
| 98 | + references: [drizzleUsers.id] // Primary key in users |
| 99 | + }) |
| 100 | + })); |
34 | 101 |
|
35 | | - it('should insert a user and update that user', async () => { |
36 | | - await db.insert(drizzleUsers).values({ id: '3', name: 'Lucy' }); |
37 | | - await db.update(drizzleUsers).set({ name: 'Lucy Smith' }).where(eq(drizzleUsers.name, 'Lucy')); |
38 | | - const result = await db.select().from(drizzleUsers).get(); |
| 102 | + const TestSchema = new Schema({ users, posts }); |
| 103 | + // const DrizzleSchema = { users: drizzleUsers, posts: drizzlePosts }; |
| 104 | + const DrizzleSchema = { users: drizzleUsers, posts: drizzlePosts, usersRelations, postsRelations }; |
39 | 105 |
|
40 | | - expect(result!.name).toEqual('Lucy Smith'); |
| 106 | + const psDb = new PowerSyncDatabase({ |
| 107 | + database: { |
| 108 | + dbFilename: 'test.db' |
| 109 | + }, |
| 110 | + flags: { |
| 111 | + useWebWorker: false |
| 112 | + }, |
| 113 | + schema: TestSchema |
41 | 114 | }); |
42 | 115 |
|
43 | | - it('should insert a user and update that user within a transaction', async () => { |
44 | | - await db.transaction(async (transaction) => { |
45 | | - await transaction.insert(drizzleUsers).values({ id: '4', name: 'James' }); |
46 | | - await transaction.update(drizzleUsers).set({ name: 'James Smith' }).where(eq(drizzleUsers.name, 'James')); |
47 | | - }); |
48 | | - const result = await db.select().from(drizzleUsers).get(); |
| 116 | + const db = SUT.wrapPowerSyncWithDrizzle(psDb, { schema: DrizzleSchema, logger: { logQuery: () => {} } }); |
| 117 | + it.only('left join', async () => { |
| 118 | + await db.insert(drizzleUsers).values({ id: '3', name: 'Lucy Smith' }); |
| 119 | + await db.insert(drizzlePosts).values({ id: '1', title: 'Post 1', content: 'Content 1', xxx: '3' }); |
49 | 120 |
|
50 | | - expect(result!.name).toEqual('James Smith'); |
51 | | - }); |
| 121 | + // const users = await db.select().from(drizzleUsers); |
| 122 | + // const posts = await db.select().from(drizzlePosts); |
| 123 | + // console.log(users); |
| 124 | + // console.log(posts); |
| 125 | + |
| 126 | + // Should fail without relationship definitions |
| 127 | + // console.warn(1); |
| 128 | + // console.log(db.query.users.findMany({ with: { posts: true } }).toSQL().sql); |
| 129 | + // const all = await db.query.users.findMany({ with: { posts: true } }); |
| 130 | + // console.log(all); |
52 | 131 |
|
53 | | - it('should insert a user and update that user within a transaction when raw sql is used', async () => { |
54 | | - await db.transaction(async (transaction) => { |
55 | | - await transaction.run(sql`INSERT INTO users (id, name) VALUES ('4', 'James');`); |
56 | | - await transaction.update(drizzleUsers).set({ name: 'James Smith' }).where(eq(drizzleUsers.name, 'James')); |
57 | | - }); |
| 132 | + // console.warn(2); |
| 133 | + // // const a = await psDb.execute('select * from users'); |
| 134 | + // // const c = {"1": "Lucy Smith", "1": "3"} |
| 135 | + // // console.log('yoink', a); |
| 136 | + // // const joined = await db |
| 137 | + // // .select() |
| 138 | + // // .from(drizzleUsers) |
| 139 | + // // .leftJoin(drizzlePosts, eq(drizzlePosts.userId, drizzleUsers.id)); |
58 | 140 |
|
59 | | - const result = await db.select().from(drizzleUsers).get(); |
| 141 | + const joined = await db.select().from(drizzleUsers).leftJoin(drizzlePosts, eq(drizzleUsers.id, drizzlePosts.xxx)); |
60 | 142 |
|
61 | | - expect(result!.name).toEqual('James Smith'); |
| 143 | + // console.log( |
| 144 | + // db |
| 145 | + // .select({ id: drizzleUsers.id, postsId: sql`"posts"."id"`.as('postsId') }) |
| 146 | + // .from(drizzleUsers) |
| 147 | + // .leftJoin(drizzlePosts, eq(drizzleUsers.id, drizzlePosts.xxx)) |
| 148 | + // .toSQL().sql |
| 149 | + // ); |
| 150 | + console.log(joined[0]); |
| 151 | + // console.log(); |
| 152 | + // console.log( |
| 153 | + // db.select().from(drizzleUsers).leftJoin(drizzlePosts, eq(drizzleUsers.id, drizzlePosts.xxx)).toSQL().sql |
| 154 | + // ); |
62 | 155 | }); |
63 | 156 | }); |
0 commit comments