-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added mechanism to map Drizzle table to a PowerSync table.
- Loading branch information
1 parent
b153571
commit 01039ba
Showing
5 changed files
with
72 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { wrapPowerSyncWithDrizzle, type PowerSyncSQLiteDatabase } from './sqlite/db'; | ||
import { toCompilableQuery } from './utils/compilableQuery'; | ||
import { toPowerSyncTable } from './utils/schema'; | ||
|
||
export { wrapPowerSyncWithDrizzle, toCompilableQuery, PowerSyncSQLiteDatabase }; | ||
export { wrapPowerSyncWithDrizzle, toCompilableQuery, toPowerSyncTable, PowerSyncSQLiteDatabase }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { column, Table, type BaseColumnType, type TableV2Options } from '@powersync/common'; | ||
import { | ||
SQLiteInteger, | ||
SQLiteReal, | ||
SQLiteText, | ||
type SQLiteTableWithColumns, | ||
type TableConfig | ||
} from 'drizzle-orm/sqlite-core'; | ||
|
||
export function toPowerSyncTable<T extends TableConfig>(table: SQLiteTableWithColumns<T>, options?: TableV2Options) { | ||
const columns: { [key: string]: BaseColumnType<number | string | null> } = {}; | ||
for (const [columnName, columnValue] of Object.entries(table)) { | ||
// Skip the id column | ||
if (columnName === 'id') { | ||
continue; | ||
} | ||
|
||
let mappedType: BaseColumnType<number | string | null>; | ||
switch (columnValue.columnType) { | ||
case SQLiteText.name: | ||
mappedType = column.text; | ||
break; | ||
case SQLiteInteger.name: | ||
mappedType = column.integer; | ||
break; | ||
case SQLiteReal.name: | ||
mappedType = column.real; | ||
break; | ||
default: | ||
throw new Error(`Unsupported column type: ${columnValue.columnType}`); | ||
} | ||
columns[columnName] = mappedType; | ||
} | ||
return new Table(columns, options); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.