Skip to content

(drizzle) add support for custom column types #471

Open
@simonflk

Description

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @powersync/[email protected] for the project I'm working on.

I wanted to add a custom timestamp column type to map between ISO-formatted date strings and Date objects like so:

import { customType } from 'drizzle-orm/sqlite-core';

// https://orm.drizzle.team/docs/custom-types
export const sqliteTimestamp = customType<{ data: Date; driverData: string }>({
  dataType() {
    return 'text';
  },
  fromDriver(value) {
    return new Date(value);
  },
  toDriver(value) {
    return value.toISOString();
  },
});

However, I ran into an issue passing this schema to DrizzleAppSchema:

Error: Unsupported column type: SQLiteCustomColumn

Here is the diff that solved my problem:

diff --git a/node_modules/@powersync/drizzle-driver/lib/src/utils/schema.js b/node_modules/@powersync/drizzle-driver/lib/src/utils/schema.js
index 9db8abc..94e46cb 100644
--- a/node_modules/@powersync/drizzle-driver/lib/src/utils/schema.js
+++ b/node_modules/@powersync/drizzle-driver/lib/src/utils/schema.js
@@ -1,7 +1,7 @@
 import { column, Schema, Table } from '@powersync/common';
 import { entityKind, isTable } from 'drizzle-orm';
 import { CasingCache } from 'drizzle-orm/casing';
-import { getTableConfig, SQLiteBoolean, SQLiteInteger, SQLiteReal, SQLiteText, SQLiteTextJson, SQLiteTimestamp } from 'drizzle-orm/sqlite-core';
+import { getTableConfig, SQLiteBoolean, SQLiteCustomColumn, SQLiteInteger, SQLiteReal, SQLiteText, SQLiteTextJson, SQLiteTimestamp } from 'drizzle-orm/sqlite-core';
 export function toPowerSyncTable(table, options) {
     var _a, _b;
     const { columns: drizzleColumns, indexes: drizzleIndexes } = getTableConfig(table);
@@ -14,6 +14,7 @@ export function toPowerSyncTable(table, options) {
             continue;
         }
         let mappedType;
+        MapColumn:
         switch (drizzleColumn.columnType) {
             case SQLiteText[entityKind]:
             case SQLiteTextJson[entityKind]:
@@ -27,6 +28,18 @@ export function toPowerSyncTable(table, options) {
             case SQLiteReal[entityKind]:
                 mappedType = column.real;
                 break;
+            case SQLiteCustomColumn[entityKind]:
+                switch (drizzleColumn.sqlName) {
+                    case 'text':
+                        mappedType = column.text;
+                        break MapColumn;
+                    case 'integer':
+                        mappedType = column.integer;
+                        break MapColumn;
+                    case 'real':
+                        mappedType = column.real;
+                        break MapColumn;
+                }
             default:
                 throw new Error(`Unsupported column type: ${drizzleColumn.columnType}`);
         }

This issue body was partially generated by patch-package.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions