diff --git a/.changeset/flat-toes-judge.md b/.changeset/flat-toes-judge.md new file mode 100644 index 000000000..43f46299d --- /dev/null +++ b/.changeset/flat-toes-judge.md @@ -0,0 +1,5 @@ +--- +'@powersync/drizzle-driver': minor +--- + +Added support for column "mode" option. This allows the ORM to expose values as complex types such as JSON and Timestamp, but store them as primitives such as text and integer. diff --git a/packages/drizzle-driver/src/utils/schema.ts b/packages/drizzle-driver/src/utils/schema.ts index 2d9e3962d..993f03c39 100644 --- a/packages/drizzle-driver/src/utils/schema.ts +++ b/packages/drizzle-driver/src/utils/schema.ts @@ -7,17 +7,19 @@ import { type BaseColumnType, type TableV2Options } from '@powersync/common'; -import { InferSelectModel, isTable, Relations } from 'drizzle-orm'; -import type { Casing } from 'drizzle-orm'; +import { entityKind, InferSelectModel, isTable, Relations, type Casing } from 'drizzle-orm'; import { CasingCache } from 'drizzle-orm/casing'; import { getTableConfig, + SQLiteBoolean, SQLiteInteger, SQLiteReal, SQLiteText, + SQLiteTextJson, + SQLiteTimestamp, + type SQLiteColumn, type SQLiteTableWithColumns, - type TableConfig, - type SQLiteColumn + type TableConfig } from 'drizzle-orm/sqlite-core'; export type ExtractPowerSyncColumns> = { @@ -44,13 +46,16 @@ export function toPowerSyncTable>( let mappedType: BaseColumnType; switch (drizzleColumn.columnType) { - case SQLiteText.name: + case SQLiteText[entityKind]: + case SQLiteTextJson[entityKind]: mappedType = column.text; break; - case SQLiteInteger.name: + case SQLiteInteger[entityKind]: + case SQLiteTimestamp[entityKind]: + case SQLiteBoolean[entityKind]: mappedType = column.integer; break; - case SQLiteReal.name: + case SQLiteReal[entityKind]: mappedType = column.real; break; default: diff --git a/packages/drizzle-driver/tests/sqlite/schema.test.ts b/packages/drizzle-driver/tests/sqlite/schema.test.ts index 2eb6735cf..4a91f0a47 100644 --- a/packages/drizzle-driver/tests/sqlite/schema.test.ts +++ b/packages/drizzle-driver/tests/sqlite/schema.test.ts @@ -9,17 +9,25 @@ describe('toPowerSyncTable', () => { const lists = sqliteTable('lists', { id: text('id').primaryKey(), name: text('name').notNull(), + info: text('info', { mode: 'json' }), owner_id: text('owner_id'), counter: integer('counter'), - completion: real('completion') + completion: real('completion'), + verified: integer('verified', { mode: 'boolean' }), + created_at: integer('created_at', { mode: 'timestamp' }), + updated_at: integer('updated_at', { mode: 'timestamp_ms' }) }); const convertedList = toPowerSyncTable(lists); const expectedLists = new Table({ name: column.text, + info: column.text, owner_id: column.text, counter: column.integer, - completion: column.real + completion: column.real, + verified: column.integer, + created_at: column.integer, + updated_at: column.integer }); expect(convertedList).toEqual(expectedLists);