|
| 1 | +import { column, Schema, Table } from '@powersync/common'; |
| 2 | +import { index, integer, real, sqliteTable, text } from 'drizzle-orm/sqlite-core'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | +import { DrizzleTableWithPowerSyncOptions, toPowerSyncSchema, toPowerSyncTable } from '../../src/utils/schema'; |
| 5 | + |
| 6 | +describe('toPowerSyncTable', () => { |
| 7 | + it('basic conversion', () => { |
| 8 | + const lists = sqliteTable('lists', { |
| 9 | + id: text('id').primaryKey(), |
| 10 | + name: text('name').notNull(), |
| 11 | + owner_id: text('owner_id'), |
| 12 | + counter: integer('counter'), |
| 13 | + completion: real('completion') |
| 14 | + }); |
| 15 | + const convertedList = toPowerSyncTable(lists); |
| 16 | + |
| 17 | + const expectedLists = new Table({ |
| 18 | + name: column.text, |
| 19 | + owner_id: column.text, |
| 20 | + counter: column.integer, |
| 21 | + completion: column.real |
| 22 | + }); |
| 23 | + |
| 24 | + expect(convertedList).toEqual(expectedLists); |
| 25 | + }); |
| 26 | + |
| 27 | + it('conversion with index', () => { |
| 28 | + const lists = sqliteTable( |
| 29 | + 'lists', |
| 30 | + { |
| 31 | + id: text('id').primaryKey(), |
| 32 | + name: text('name').notNull(), |
| 33 | + owner_id: text('owner_id') |
| 34 | + }, |
| 35 | + (lists) => ({ |
| 36 | + owner: index('owner').on(lists.owner_id) |
| 37 | + }) |
| 38 | + ); |
| 39 | + const convertedList = toPowerSyncTable(lists); |
| 40 | + |
| 41 | + const expectedLists = new Table( |
| 42 | + { |
| 43 | + name: column.text, |
| 44 | + owner_id: column.text |
| 45 | + }, |
| 46 | + { indexes: { owner: ['owner_id'] } } |
| 47 | + ); |
| 48 | + |
| 49 | + expect(convertedList).toEqual(expectedLists); |
| 50 | + }); |
| 51 | + |
| 52 | + it('conversion with options', () => { |
| 53 | + const lists = sqliteTable('lists', { |
| 54 | + id: text('id').primaryKey(), |
| 55 | + name: text('name').notNull() |
| 56 | + }); |
| 57 | + |
| 58 | + const convertedList = toPowerSyncTable(lists, { localOnly: true, insertOnly: true, viewName: 'listsView' }); |
| 59 | + |
| 60 | + const expectedLists = new Table( |
| 61 | + { |
| 62 | + name: column.text |
| 63 | + }, |
| 64 | + { localOnly: true, insertOnly: true, viewName: 'listsView' } |
| 65 | + ); |
| 66 | + |
| 67 | + expect(convertedList).toEqual(expectedLists); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +describe('toPowerSyncSchema', () => { |
| 72 | + it('basic conversion', () => { |
| 73 | + const lists = sqliteTable('lists', { |
| 74 | + id: text('id').primaryKey(), |
| 75 | + name: text('name').notNull(), |
| 76 | + owner_id: text('owner_id'), |
| 77 | + counter: integer('counter'), |
| 78 | + completion: real('completion') |
| 79 | + }); |
| 80 | + |
| 81 | + const todos = sqliteTable('todos', { |
| 82 | + id: text('id').primaryKey(), |
| 83 | + list_id: text('list_id').references(() => lists.id), |
| 84 | + description: text('description') |
| 85 | + }); |
| 86 | + |
| 87 | + const drizzleSchema = { |
| 88 | + lists, |
| 89 | + todos |
| 90 | + }; |
| 91 | + const convertedSchema = toPowerSyncSchema(drizzleSchema); |
| 92 | + |
| 93 | + const expectedSchema = new Schema({ |
| 94 | + lists: new Table({ |
| 95 | + name: column.text, |
| 96 | + owner_id: column.text, |
| 97 | + counter: column.integer, |
| 98 | + completion: column.real |
| 99 | + }), |
| 100 | + todos: new Table({ |
| 101 | + list_id: column.text, |
| 102 | + description: column.text |
| 103 | + }) |
| 104 | + }); |
| 105 | + |
| 106 | + expect(convertedSchema).toEqual(expectedSchema); |
| 107 | + }); |
| 108 | + |
| 109 | + it('conversion with options', () => { |
| 110 | + const lists = sqliteTable('lists', { |
| 111 | + id: text('id').primaryKey(), |
| 112 | + name: text('name').notNull(), |
| 113 | + owner_id: text('owner_id'), |
| 114 | + counter: integer('counter'), |
| 115 | + completion: real('completion') |
| 116 | + }); |
| 117 | + |
| 118 | + const todos = sqliteTable('todos', { |
| 119 | + id: text('id').primaryKey(), |
| 120 | + list_id: text('list_id').references(() => lists.id), |
| 121 | + description: text('description') |
| 122 | + }); |
| 123 | + |
| 124 | + const drizzleSchemaWithOptions = { |
| 125 | + lists: { |
| 126 | + tableDefinition: lists, |
| 127 | + options: { localOnly: true, insertOnly: true, viewName: 'listsView' } |
| 128 | + } as DrizzleTableWithPowerSyncOptions, |
| 129 | + todos |
| 130 | + }; |
| 131 | + |
| 132 | + const convertedSchema = toPowerSyncSchema(drizzleSchemaWithOptions); |
| 133 | + |
| 134 | + const expectedSchema = new Schema({ |
| 135 | + lists: new Table( |
| 136 | + { |
| 137 | + name: column.text, |
| 138 | + owner_id: column.text, |
| 139 | + counter: column.integer, |
| 140 | + completion: column.real |
| 141 | + }, |
| 142 | + { localOnly: true, insertOnly: true, viewName: 'listsView' } |
| 143 | + ), |
| 144 | + todos: new Table({ |
| 145 | + list_id: column.text, |
| 146 | + description: column.text |
| 147 | + }) |
| 148 | + }); |
| 149 | + |
| 150 | + expect(convertedSchema).toEqual(expectedSchema); |
| 151 | + }); |
| 152 | + |
| 153 | + it('conversion with index', () => { |
| 154 | + const lists = sqliteTable('lists', { |
| 155 | + id: text('id').primaryKey(), |
| 156 | + name: text('name').notNull(), |
| 157 | + owner_id: text('owner_id'), |
| 158 | + counter: integer('counter'), |
| 159 | + completion: real('completion') |
| 160 | + }); |
| 161 | + |
| 162 | + const todos = sqliteTable( |
| 163 | + 'todos', |
| 164 | + { |
| 165 | + id: text('id').primaryKey(), |
| 166 | + list_id: text('list_id').references(() => lists.id), |
| 167 | + description: text('description') |
| 168 | + }, |
| 169 | + (todos) => ({ |
| 170 | + list: index('list').on(todos.list_id) |
| 171 | + }) |
| 172 | + ); |
| 173 | + |
| 174 | + const drizzleSchemaWithOptions = { |
| 175 | + lists, |
| 176 | + todos |
| 177 | + }; |
| 178 | + |
| 179 | + const convertedSchema = toPowerSyncSchema(drizzleSchemaWithOptions); |
| 180 | + |
| 181 | + const expectedSchema = new Schema({ |
| 182 | + lists: new Table({ |
| 183 | + name: column.text, |
| 184 | + owner_id: column.text, |
| 185 | + counter: column.integer, |
| 186 | + completion: column.real |
| 187 | + }), |
| 188 | + todos: new Table( |
| 189 | + { |
| 190 | + list_id: column.text, |
| 191 | + description: column.text |
| 192 | + }, |
| 193 | + { indexes: { list: ['list_id'] } } |
| 194 | + ) |
| 195 | + }); |
| 196 | + |
| 197 | + expect(convertedSchema).toEqual(expectedSchema); |
| 198 | + }); |
| 199 | +}); |
0 commit comments