Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lemon-moles-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/drizzle-driver': patch
---

Added support for custom column types when converting a Drizzle schema to a PowerSync app schema.
48 changes: 30 additions & 18 deletions packages/drizzle-driver/src/utils/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { CasingCache } from 'drizzle-orm/casing';
import {
getTableConfig,
SQLiteBoolean,
SQLiteCustomColumn,
SQLiteInteger,
SQLiteReal,
SQLiteText,
Expand Down Expand Up @@ -44,24 +45,7 @@ export function toPowerSyncTable<T extends SQLiteTableWithColumns<any>>(
continue;
}

let mappedType: BaseColumnType<number | string | null>;
switch (drizzleColumn.columnType) {
case SQLiteText[entityKind]:
case SQLiteTextJson[entityKind]:
mappedType = column.text;
break;
case SQLiteInteger[entityKind]:
case SQLiteTimestamp[entityKind]:
case SQLiteBoolean[entityKind]:
mappedType = column.integer;
break;
case SQLiteReal[entityKind]:
mappedType = column.real;
break;
default:
throw new Error(`Unsupported column type: ${drizzleColumn.columnType}`);
}
columns[name] = mappedType;
columns[name] = mapDrizzleColumnToType(drizzleColumn);
}
const indexes: IndexShorthand = {};

Expand All @@ -82,6 +66,34 @@ export function toPowerSyncTable<T extends SQLiteTableWithColumns<any>>(
return new Table(columns, { ...options, indexes }) as Table<Expand<ExtractPowerSyncColumns<T>>>;
}

function mapDrizzleColumnToType(drizzleColumn: SQLiteColumn<any, object>): BaseColumnType<number | string | null> {
switch (drizzleColumn.columnType) {
case SQLiteText[entityKind]:
case SQLiteTextJson[entityKind]:
return column.text;
case SQLiteInteger[entityKind]:
case SQLiteTimestamp[entityKind]:
case SQLiteBoolean[entityKind]:
return column.integer;
case SQLiteReal[entityKind]:
return column.real;
case SQLiteCustomColumn[entityKind]:
const sqlName = (drizzleColumn as SQLiteCustomColumn<any>).getSQLType();
switch (sqlName) {
case 'text':
return column.text;
case 'integer':
return column.integer;
case 'real':
return column.real;
default:
throw new Error(`Unsupported custom column type: ${drizzleColumn.columnType}: ${sqlName}`);
}
default:
throw new Error(`Unsupported column type: ${drizzleColumn.columnType}`);
}
}

export type DrizzleTablePowerSyncOptions = Omit<TableV2Options, 'indexes'>;

export type DrizzleTableWithPowerSyncOptions = {
Expand Down
57 changes: 56 additions & 1 deletion packages/drizzle-driver/tests/sqlite/schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { column, Schema, Table } from '@powersync/common';
import { index, integer, real, sqliteTable, text } from 'drizzle-orm/sqlite-core';
import { customType, index, integer, real, sqliteTable, text } from 'drizzle-orm/sqlite-core';
import { describe, expect, it } from 'vitest';
import { DrizzleAppSchema, DrizzleTableWithPowerSyncOptions, toPowerSyncTable } from '../../src/utils/schema';
import { CasingCache } from 'drizzle-orm/casing';
Expand Down Expand Up @@ -105,6 +105,61 @@ describe('toPowerSyncTable', () => {

expect(convertedList).toEqual(expectedLists);
});

it('custom column conversion', () => {
const customSqliteText = customType<{ data: string; driverData: string }>({
dataType() {
return 'text';
},
fromDriver(value) {
return value;
},
toDriver(value) {
return value;
}
});

const customSqliteInteger = customType<{ data: number; driverData: number }>({
dataType() {
return 'integer';
},
fromDriver(value) {
return Number(value);
},
toDriver(value) {
return value;
}
});

const customSqliteReal = customType<{ data: number; driverData: number }>({
dataType() {
return 'real';
},
fromDriver(value) {
return Number(value);
},
toDriver(value) {
return value;
}
});

const lists = sqliteTable('lists', {
id: text('id').primaryKey(),
text_col: customSqliteText('text_col'),
int_col: customSqliteInteger('int_col'),
real_col: customSqliteReal('real_col')
});

const convertedList = toPowerSyncTable(lists);

const expectedLists = new Table({
text_col: column.text,
int_col: column.integer,
real_col: column.real
});

expect(convertedList).toEqual(expectedLists);
});
});

describe('DrizzleAppSchema constructor', () => {
Expand Down
Loading