Skip to content

Commit a547fc6

Browse files
ImSingeeChriztiaan
andauthored
make drizzle toPowerSyncTable support mode (#435)
Co-authored-by: Christiaan Landman <[email protected]>
1 parent 53fd64e commit a547fc6

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

.changeset/flat-toes-judge.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/drizzle-driver': minor
3+
---
4+
5+
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.

packages/drizzle-driver/src/utils/schema.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ import {
77
type BaseColumnType,
88
type TableV2Options
99
} from '@powersync/common';
10-
import { InferSelectModel, isTable, Relations } from 'drizzle-orm';
11-
import type { Casing } from 'drizzle-orm';
10+
import { entityKind, InferSelectModel, isTable, Relations, type Casing } from 'drizzle-orm';
1211
import { CasingCache } from 'drizzle-orm/casing';
1312
import {
1413
getTableConfig,
14+
SQLiteBoolean,
1515
SQLiteInteger,
1616
SQLiteReal,
1717
SQLiteText,
18+
SQLiteTextJson,
19+
SQLiteTimestamp,
20+
type SQLiteColumn,
1821
type SQLiteTableWithColumns,
19-
type TableConfig,
20-
type SQLiteColumn
22+
type TableConfig
2123
} from 'drizzle-orm/sqlite-core';
2224

2325
export type ExtractPowerSyncColumns<T extends SQLiteTableWithColumns<any>> = {
@@ -44,13 +46,16 @@ export function toPowerSyncTable<T extends SQLiteTableWithColumns<any>>(
4446

4547
let mappedType: BaseColumnType<number | string | null>;
4648
switch (drizzleColumn.columnType) {
47-
case SQLiteText.name:
49+
case SQLiteText[entityKind]:
50+
case SQLiteTextJson[entityKind]:
4851
mappedType = column.text;
4952
break;
50-
case SQLiteInteger.name:
53+
case SQLiteInteger[entityKind]:
54+
case SQLiteTimestamp[entityKind]:
55+
case SQLiteBoolean[entityKind]:
5156
mappedType = column.integer;
5257
break;
53-
case SQLiteReal.name:
58+
case SQLiteReal[entityKind]:
5459
mappedType = column.real;
5560
break;
5661
default:

packages/drizzle-driver/tests/sqlite/schema.test.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,25 @@ describe('toPowerSyncTable', () => {
99
const lists = sqliteTable('lists', {
1010
id: text('id').primaryKey(),
1111
name: text('name').notNull(),
12+
info: text('info', { mode: 'json' }),
1213
owner_id: text('owner_id'),
1314
counter: integer('counter'),
14-
completion: real('completion')
15+
completion: real('completion'),
16+
verified: integer('verified', { mode: 'boolean' }),
17+
created_at: integer('created_at', { mode: 'timestamp' }),
18+
updated_at: integer('updated_at', { mode: 'timestamp_ms' })
1519
});
1620
const convertedList = toPowerSyncTable(lists);
1721

1822
const expectedLists = new Table({
1923
name: column.text,
24+
info: column.text,
2025
owner_id: column.text,
2126
counter: column.integer,
22-
completion: column.real
27+
completion: column.real,
28+
verified: column.integer,
29+
created_at: column.integer,
30+
updated_at: column.integer
2331
});
2432

2533
expect(convertedList).toEqual(expectedLists);

0 commit comments

Comments
 (0)