Skip to content

Commit

Permalink
class method test
Browse files Browse the repository at this point in the history
  • Loading branch information
stevensJourney committed Dec 2, 2024
1 parent 1a1dca7 commit 9d1f2af
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
37 changes: 37 additions & 0 deletions packages/drizzle-driver/src/utils/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,40 @@ export function toPowerSyncSchema<

return new Schema(tables) as Schema<Expand<TablesFromSchemaEntries<T>>>;
}

export function toPowerSyncTables<
T extends Record<string, SQLiteTableWithColumns<any> | Relations | DrizzleTableWithPowerSyncOptions>
>(schemaEntries: T) {
const tables: Record<string, Table> = {};
for (const schemaEntry of Object.values(schemaEntries)) {
let maybeTable: SQLiteTableWithColumns<any> | Relations | undefined = undefined;
let maybeOptions: DrizzleTablePowerSyncOptions | undefined = undefined;

if (typeof schemaEntry === 'object' && 'tableDefinition' in schemaEntry) {
const tableWithOptions = schemaEntry as DrizzleTableWithPowerSyncOptions;
maybeTable = tableWithOptions.tableDefinition;
maybeOptions = tableWithOptions.options;
} else {
maybeTable = schemaEntry;
}

if (isTable(maybeTable)) {
const { name } = getTableConfig(maybeTable);
tables[name] = toPowerSyncTable(maybeTable as SQLiteTableWithColumns<TableConfig>, maybeOptions);
}
}

return tables;
}

export class DrizzleAppSchema<
T extends Record<string, SQLiteTableWithColumns<any> | Relations | DrizzleTableWithPowerSyncOptions>
> extends Schema {
constructor(drizzleSchema: T) {
super(toPowerSyncTables(drizzleSchema));
// This is just used for typing
this.types = {} as any;
}

types: Expand<TablesFromSchemaEntries<T>>;
}
18 changes: 17 additions & 1 deletion packages/drizzle-driver/tests/sqlite/schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { column, Schema, Table } from '@powersync/common';
import { index, integer, real, sqliteTable, text } from 'drizzle-orm/sqlite-core';
import { describe, expect, it } from 'vitest';
import { DrizzleTableWithPowerSyncOptions, toPowerSyncSchema, toPowerSyncTable } from '../../src/utils/schema';
import {
DrizzleAppSchema,
DrizzleTableWithPowerSyncOptions,
toPowerSyncSchema,
toPowerSyncTable
} from '../../src/utils/schema';

describe('toPowerSyncTable', () => {
it('basic conversion', () => {
Expand All @@ -24,6 +29,17 @@ describe('toPowerSyncTable', () => {
expect(convertedList).toEqual(expectedLists);
});

it('classed based types', () => {
const lists = sqliteTable('lists', {
id: text('id').primaryKey(),
name: text('name').notNull(),
owner_id: text('owner_id'),
counter: integer('counter'),
completion: real('completion')
});
const drizzle = new DrizzleAppSchema({ lists });
});

it('conversion with index', () => {
const lists = sqliteTable(
'lists',
Expand Down

0 comments on commit 9d1f2af

Please sign in to comment.