Skip to content

Commit

Permalink
make drizzle toPowerSyncTable support mode (#435)
Browse files Browse the repository at this point in the history
Co-authored-by: Christiaan Landman <[email protected]>
  • Loading branch information
ImSingee and Chriztiaan authored Dec 9, 2024
1 parent 53fd64e commit a547fc6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-toes-judge.md
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 12 additions & 7 deletions packages/drizzle-driver/src/utils/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends SQLiteTableWithColumns<any>> = {
Expand All @@ -44,13 +46,16 @@ export function toPowerSyncTable<T extends SQLiteTableWithColumns<any>>(

let mappedType: BaseColumnType<number | string | null>;
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:
Expand Down
12 changes: 10 additions & 2 deletions packages/drizzle-driver/tests/sqlite/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a547fc6

Please sign in to comment.