Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(drizzle) add support for custom column types #471

Open
simonflk opened this issue Jan 16, 2025 · 3 comments
Open

(drizzle) add support for custom column types #471

simonflk opened this issue Jan 16, 2025 · 3 comments

Comments

@simonflk
Copy link

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.

@Chriztiaan
Copy link
Contributor

Hey @simonflk, thanks for this! I'll give it a look over and see if I can get it into main in the next day or so.

@Chriztiaan
Copy link
Contributor

Hey @simonflk, I made a minor change - just for sanity could you verify this works for @powersync/[email protected]?

@simonflk
Copy link
Author

Thank you @Chriztiaan

I just tested this version, and it's working for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants