Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/beige-poems-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/service-sync-rules': minor
---

Added Schema generators for Kotlin, Swift and DotNet
8 changes: 2 additions & 6 deletions packages/sync-rules/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
export * from './BucketDescription.js';
export * from './DartSchemaGenerator.js';
export * from './BucketParameterQuerier.js';
export * from './errors.js';
export * from './events/SqlEventDescriptor.js';
export * from './events/SqlEventSourceQuery.js';
export * from './ExpressionType.js';
export * from './generators.js';
export * from './IdSequence.js';
export * from './JsLegacySchemaGenerator.js';
export * from './json_schema.js';
export * from './request_functions.js';
export * from './SchemaGenerator.js';
export * from './schema-generators/schema-generators.js';
export * from './SourceTableInterface.js';
export * from './sql_filters.js';
export * from './sql_functions.js';
Expand All @@ -18,7 +16,5 @@ export * from './SqlParameterQuery.js';
export * from './SqlSyncRules.js';
export * from './StaticSchema.js';
export * from './TablePattern.js';
export * from './TsSchemaGenerator.js';
export * from './types.js';
export * from './utils.js';
export * from './BucketParameterQuerier.js';
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ColumnDefinition, ExpressionType, TYPE_INTEGER, TYPE_REAL, TYPE_TEXT } from './ExpressionType.js';
import { ColumnDefinition, ExpressionType } from '../ExpressionType.js';
import { SqlSyncRules } from '../SqlSyncRules.js';
import { SourceSchema } from '../types.js';
import { GenerateSchemaOptions, SchemaGenerator } from './SchemaGenerator.js';
import { SqlSyncRules } from './SqlSyncRules.js';
import { SourceSchema } from './types.js';

export class DartSchemaGenerator extends SchemaGenerator {
readonly key = 'dart';
Expand Down Expand Up @@ -41,7 +41,7 @@ ${generated.join('\n')}
}

private generateColumn(column: ColumnDefinition) {
return `Column.${dartColumnType(column)}('${column.name}')`;
return `Column.${this.columnType(column)}('${column.name}')`;
}
}

Expand Down Expand Up @@ -97,28 +97,15 @@ export class DartFlutterFlowSchemaGenerator extends SchemaGenerator {
view_name: null,
local_only: localOnly,
insert_only: false,
columns: columns.map(this.generateColumn),
columns: columns.map((c) => this.generateColumn(c)),
indexes: []
};
}

private generateColumn(definition: ColumnDefinition): object {
return {
name: definition.name,
type: dartColumnType(definition)
type: this.columnType(definition)
};
}
}

const dartColumnType = (def: ColumnDefinition) => {
const t = def.type;
if (t.typeFlags & TYPE_TEXT) {
return 'text';
} else if (t.typeFlags & TYPE_REAL) {
return 'real';
} else if (t.typeFlags & TYPE_INTEGER) {
return 'integer';
} else {
return 'text';
}
};
71 changes: 71 additions & 0 deletions packages/sync-rules/src/schema-generators/DotNetSchemaGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { ColumnDefinition, TYPE_INTEGER, TYPE_REAL, TYPE_TEXT } from '../ExpressionType.js';
import { SqlSyncRules } from '../SqlSyncRules.js';
import { SourceSchema } from '../types.js';
import { GenerateSchemaOptions, SchemaGenerator } from './SchemaGenerator.js';

export class DotNetSchemaGenerator extends SchemaGenerator {
readonly key = 'dotnet';
readonly label = '.Net';
readonly mediaType = 'text/x-csharp';
readonly fileName = 'Schema.cs';

generate(source: SqlSyncRules, schema: SourceSchema, options?: GenerateSchemaOptions): string {
const tables = super.getAllTables(source, schema);

return `using PowerSync.Common.DB.Schema;

class AppSchema
{
${tables.map((table) => this.generateTable(table.name, table.columns, options)).join('\n\n ')}

public static Schema PowerSyncSchema = new Schema(new Dictionary<string, Table>
{
${tables.map((table) => `{"${table.name}", ${this.toUpperCaseFirstLetter(table.name)}}`).join(',\n ')}
});
}`;
}

private toUpperCaseFirstLetter(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}

private generateTable(name: string, columns: ColumnDefinition[], options?: GenerateSchemaOptions): string {
const generated = columns.map((c, i) => {
const last = i === columns.length - 1;
const base = this.generateColumn(c);
let withFormatting: string;
if (last) {
withFormatting = ` ${base}`;
} else {
withFormatting = ` ${base},`;
}

if (options?.includeTypeComments && c.originalType != null) {
return `${withFormatting} // ${c.originalType}`;
} else {
return withFormatting;
}
});
return `public static Table ${this.toUpperCaseFirstLetter(name)} = new Table(new Dictionary<string, ColumnType>
{
${generated.join('\n ')}
});`;
}

private generateColumn(column: ColumnDefinition): string {
return `{ "${column.name}", ${cSharpColumnType(column)} }`;
}
}

const cSharpColumnType = (def: ColumnDefinition): string => {
const t = def.type;
if (t.typeFlags & TYPE_TEXT) {
return 'ColumnType.TEXT';
} else if (t.typeFlags & TYPE_REAL) {
return 'ColumnType.REAL';
} else if (t.typeFlags & TYPE_INTEGER) {
return 'ColumnType.INTEGER';
} else {
return 'ColumnType.TEXT';
}
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ColumnDefinition, TYPE_INTEGER, TYPE_REAL, TYPE_TEXT } from './ExpressionType.js';
import { ColumnDefinition, TYPE_INTEGER, TYPE_REAL, TYPE_TEXT } from '../ExpressionType.js';
import { SqlSyncRules } from '../SqlSyncRules.js';
import { SourceSchema } from '../types.js';
import { SchemaGenerator } from './SchemaGenerator.js';
import { SqlSyncRules } from './SqlSyncRules.js';
import { SourceSchema } from './types.js';

export class JsLegacySchemaGenerator extends SchemaGenerator {
readonly key = 'jsLegacy';
Expand Down
52 changes: 52 additions & 0 deletions packages/sync-rules/src/schema-generators/KotlinSchemaGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { ColumnDefinition } from '../ExpressionType.js';
import { SqlSyncRules } from '../SqlSyncRules.js';
import { SourceSchema } from '../types.js';
import { GenerateSchemaOptions, SchemaGenerator } from './SchemaGenerator.js';

export class KotlinSchemaGenerator extends SchemaGenerator {
readonly key = 'kotlin';
readonly label = 'Kotlin';
readonly mediaType = 'text/x-kotlin';
readonly fileName = 'schema.kt';

generate(source: SqlSyncRules, schema: SourceSchema, options?: GenerateSchemaOptions): string {
const tables = super.getAllTables(source, schema);

return `import com.powersync.db.schema.Column
import com.powersync.db.schema.Schema
import com.powersync.db.schema.Table

val schema = Schema(
${tables.map((table) => this.generateTable(table.name, table.columns, options)).join(',\n ')}
)`;
}

private generateTable(name: string, columns: ColumnDefinition[], options?: GenerateSchemaOptions): string {
const generated = columns.map((c, i) => {
const last = i === columns.length - 1;
const base = this.generateColumn(c);
let withFormatting: string;
if (last) {
withFormatting = ` ${base}`;
} else {
withFormatting = ` ${base},`;
}

if (options?.includeTypeComments && c.originalType != null) {
return `${withFormatting} // ${c.originalType}`;
} else {
return withFormatting;
}
});
return `Table(
name = "${name}",
columns = listOf(
${generated.join('\n')}
)
)`;
}

private generateColumn(column: ColumnDefinition): string {
return `Column.${this.columnType(column)}("${column.name}")`;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ColumnDefinition } from './ExpressionType.js';
import { SqlSyncRules } from './SqlSyncRules.js';
import { SourceSchema } from './types.js';
import { ColumnDefinition, TYPE_INTEGER, TYPE_REAL, TYPE_TEXT } from '../ExpressionType.js';
import { SqlSyncRules } from '../SqlSyncRules.js';
import { SourceSchema } from '../types.js';

export interface GenerateSchemaOptions {
includeTypeComments?: boolean;
Expand Down Expand Up @@ -38,4 +38,21 @@ export abstract class SchemaGenerator {
abstract readonly fileName: string;

abstract generate(source: SqlSyncRules, schema: SourceSchema, options?: GenerateSchemaOptions): string;

/**
* @param def The column definition to generate the type for.
* @returns The SDK column type for the given column definition.
*/
columnType(def: ColumnDefinition): string {
const { type } = def;
if (type.typeFlags & TYPE_TEXT) {
return 'text';
} else if (type.typeFlags & TYPE_REAL) {
return 'real';
} else if (type.typeFlags & TYPE_INTEGER) {
return 'integer';
} else {
return 'text';
}
}
}
50 changes: 50 additions & 0 deletions packages/sync-rules/src/schema-generators/SwiftSchemaGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { ColumnDefinition } from '../ExpressionType.js';
import { SqlSyncRules } from '../SqlSyncRules.js';
import { SourceSchema } from '../types.js';
import { GenerateSchemaOptions, SchemaGenerator } from './SchemaGenerator.js';

export class SwiftSchemaGenerator extends SchemaGenerator {
readonly key = 'swift';
readonly label = 'Swift';
readonly mediaType = 'text/x-swift';
readonly fileName = 'schema.swift';

generate(source: SqlSyncRules, schema: SourceSchema, options?: GenerateSchemaOptions): string {
const tables = super.getAllTables(source, schema);

return `import PowerSync

let schema = Schema(
${tables.map((table) => this.generateTable(table.name, table.columns, options)).join(',\n ')}
)`;
}

private generateTable(name: string, columns: ColumnDefinition[], options?: GenerateSchemaOptions): string {
const generated = columns.map((c, i) => {
const last = i === columns.length - 1;
const base = this.generateColumn(c);
let withFormatting: string;
if (last) {
withFormatting = ` ${base}`;
} else {
withFormatting = ` ${base},`;
}

if (options?.includeTypeComments && c.originalType != null) {
return `${withFormatting} // ${c.originalType}`;
} else {
return withFormatting;
}
});
return `Table(
name: "${name}",
columns: [
${generated.join('\n')}
]
)`;
}

private generateColumn(column: ColumnDefinition): string {
return `.${this.columnType(column)}("${column.name}")`;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ColumnDefinition, TYPE_INTEGER, TYPE_REAL, TYPE_TEXT } from './ExpressionType.js';
import { ColumnDefinition, TYPE_INTEGER, TYPE_REAL, TYPE_TEXT } from '../ExpressionType.js';
import { SqlSyncRules } from '../SqlSyncRules.js';
import { SourceSchema } from '../types.js';
import { GenerateSchemaOptions, SchemaGenerator } from './SchemaGenerator.js';
import { SqlSyncRules } from './SqlSyncRules.js';
import { SourceSchema } from './types.js';

export interface TsSchemaGeneratorOptions {
language?: TsSchemaLanguage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { DartFlutterFlowSchemaGenerator, DartSchemaGenerator } from './DartSchemaGenerator.js';
import { DotNetSchemaGenerator } from './DotNetSchemaGenerator.js';
import { JsLegacySchemaGenerator } from './JsLegacySchemaGenerator.js';
import { KotlinSchemaGenerator } from './KotlinSchemaGenerator.js';
import { SwiftSchemaGenerator } from './SwiftSchemaGenerator.js';
import { TsSchemaGenerator, TsSchemaLanguage } from './TsSchemaGenerator.js';

export const schemaGenerators = {
ts: new TsSchemaGenerator(),
dart: new DartSchemaGenerator(),
dotNet: new DotNetSchemaGenerator(),
flutterFlow: new DartFlutterFlowSchemaGenerator(),
js: new TsSchemaGenerator({ language: TsSchemaLanguage.js }),
jsLegacy: new JsLegacySchemaGenerator(),
dart: new DartSchemaGenerator(),
flutterFlow: new DartFlutterFlowSchemaGenerator()
kotlin: new KotlinSchemaGenerator(),
swift: new SwiftSchemaGenerator(),
ts: new TsSchemaGenerator()
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export * from './DartSchemaGenerator.js';
export * from './DotNetSchemaGenerator.js';
export * from './generators.js';
export * from './JsLegacySchemaGenerator.js';
export * from './KotlinSchemaGenerator.js';
export * from './SchemaGenerator.js';
export * from './SwiftSchemaGenerator.js';
export * from './TsSchemaGenerator.js';
Loading