Skip to content

Commit f01c062

Browse files
added imports
1 parent 181f06f commit f01c062

File tree

6 files changed

+60
-60
lines changed

6 files changed

+60
-60
lines changed

packages/sync-rules/src/schema-generators/DartSchemaGenerator.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ColumnDefinition, ExpressionType, TYPE_INTEGER, TYPE_REAL, TYPE_TEXT } from '../ExpressionType.js';
1+
import { ColumnDefinition, ExpressionType } from '../ExpressionType.js';
22
import { SqlSyncRules } from '../SqlSyncRules.js';
33
import { SourceSchema } from '../types.js';
44
import { GenerateSchemaOptions, SchemaGenerator } from './SchemaGenerator.js';
@@ -41,7 +41,7 @@ ${generated.join('\n')}
4141
}
4242

4343
private generateColumn(column: ColumnDefinition) {
44-
return `Column.${dartColumnType(column)}('${column.name}')`;
44+
return `Column.${this.columnType(column)}('${column.name}')`;
4545
}
4646
}
4747

@@ -97,28 +97,15 @@ export class DartFlutterFlowSchemaGenerator extends SchemaGenerator {
9797
view_name: null,
9898
local_only: localOnly,
9999
insert_only: false,
100-
columns: columns.map(this.generateColumn),
100+
columns: columns.map((c) => this.generateColumn(c)),
101101
indexes: []
102102
};
103103
}
104104

105105
private generateColumn(definition: ColumnDefinition): object {
106106
return {
107107
name: definition.name,
108-
type: dartColumnType(definition)
108+
type: this.columnType(definition)
109109
};
110110
}
111111
}
112-
113-
const dartColumnType = (def: ColumnDefinition) => {
114-
const t = def.type;
115-
if (t.typeFlags & TYPE_TEXT) {
116-
return 'text';
117-
} else if (t.typeFlags & TYPE_REAL) {
118-
return 'real';
119-
} else if (t.typeFlags & TYPE_INTEGER) {
120-
return 'integer';
121-
} else {
122-
return 'text';
123-
}
124-
};

packages/sync-rules/src/schema-generators/DotNetSchemaGenerator.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import { GenerateSchemaOptions, SchemaGenerator } from './SchemaGenerator.js';
55

66
export class DotNetSchemaGenerator extends SchemaGenerator {
77
readonly key = 'dotnet';
8-
readonly label = 'DotNet';
8+
readonly label = '.Net';
99
readonly mediaType = 'text/x-csharp';
1010
readonly fileName = 'Schema.cs';
1111

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

15-
return `class AppSchema
15+
return `using PowerSync.Common.DB.Schema;
16+
17+
class AppSchema
1618
{
1719
${tables.map((table) => this.generateTable(table.name, table.columns, options)).join('\n\n ')}
1820

packages/sync-rules/src/schema-generators/KotlinSchemaGenerator.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ColumnDefinition, TYPE_INTEGER, TYPE_REAL, TYPE_TEXT } from '../ExpressionType.js';
1+
import { ColumnDefinition } from '../ExpressionType.js';
22
import { SqlSyncRules } from '../SqlSyncRules.js';
33
import { SourceSchema } from '../types.js';
44
import { GenerateSchemaOptions, SchemaGenerator } from './SchemaGenerator.js';
@@ -12,7 +12,11 @@ export class KotlinSchemaGenerator extends SchemaGenerator {
1212
generate(source: SqlSyncRules, schema: SourceSchema, options?: GenerateSchemaOptions): string {
1313
const tables = super.getAllTables(source, schema);
1414

15-
return `val schema = Schema(
15+
return `import com.powersync.db.schema.Column
16+
import com.powersync.db.schema.Schema
17+
import com.powersync.db.schema.Table
18+
19+
val schema = Schema(
1620
${tables.map((table) => this.generateTable(table.name, table.columns, options)).join(',\n ')}
1721
)`;
1822
}
@@ -43,19 +47,6 @@ ${generated.join('\n')}
4347
}
4448

4549
private generateColumn(column: ColumnDefinition): string {
46-
return `Column.${kotlinColumnType(column)}("${column.name}")`;
50+
return `Column.${this.columnType(column)}("${column.name}")`;
4751
}
4852
}
49-
50-
const kotlinColumnType = (def: ColumnDefinition): string => {
51-
const t = def.type;
52-
if (t.typeFlags & TYPE_TEXT) {
53-
return 'text';
54-
} else if (t.typeFlags & TYPE_REAL) {
55-
return 'real';
56-
} else if (t.typeFlags & TYPE_INTEGER) {
57-
return 'integer';
58-
} else {
59-
return 'text';
60-
}
61-
};

packages/sync-rules/src/schema-generators/SchemaGenerator.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ColumnDefinition } from '../ExpressionType.js';
1+
import { ColumnDefinition, TYPE_INTEGER, TYPE_REAL, TYPE_TEXT } from '../ExpressionType.js';
22
import { SqlSyncRules } from '../SqlSyncRules.js';
33
import { SourceSchema } from '../types.js';
44

@@ -38,4 +38,21 @@ export abstract class SchemaGenerator {
3838
abstract readonly fileName: string;
3939

4040
abstract generate(source: SqlSyncRules, schema: SourceSchema, options?: GenerateSchemaOptions): string;
41+
42+
/**
43+
* @param def The column definition to generate the type for.
44+
* @returns The SDK column type for the given column definition.
45+
*/
46+
columnType(def: ColumnDefinition): string {
47+
const { type } = def;
48+
if (type.typeFlags & TYPE_TEXT) {
49+
return 'text';
50+
} else if (type.typeFlags & TYPE_REAL) {
51+
return 'real';
52+
} else if (type.typeFlags & TYPE_INTEGER) {
53+
return 'integer';
54+
} else {
55+
return 'text';
56+
}
57+
}
4158
}

packages/sync-rules/src/schema-generators/SwiftSchemaGenerator.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ColumnDefinition, TYPE_INTEGER, TYPE_REAL, TYPE_TEXT } from '../ExpressionType.js';
1+
import { ColumnDefinition } from '../ExpressionType.js';
22
import { SqlSyncRules } from '../SqlSyncRules.js';
33
import { SourceSchema } from '../types.js';
44
import { GenerateSchemaOptions, SchemaGenerator } from './SchemaGenerator.js';
@@ -12,7 +12,9 @@ export class SwiftSchemaGenerator extends SchemaGenerator {
1212
generate(source: SqlSyncRules, schema: SourceSchema, options?: GenerateSchemaOptions): string {
1313
const tables = super.getAllTables(source, schema);
1414

15-
return `let schema = Schema(
15+
return `import PowerSync
16+
17+
let schema = Schema(
1618
${tables.map((table) => this.generateTable(table.name, table.columns, options)).join(',\n ')}
1719
)`;
1820
}
@@ -43,19 +45,6 @@ ${generated.join('\n')}
4345
}
4446

4547
private generateColumn(column: ColumnDefinition): string {
46-
return `.${swiftColumnType(column)}("${column.name}")`;
48+
return `.${this.columnType(column)}("${column.name}")`;
4749
}
4850
}
49-
50-
const swiftColumnType = (def: ColumnDefinition): string => {
51-
const t = def.type;
52-
if (t.typeFlags & TYPE_TEXT) {
53-
return 'text';
54-
} else if (t.typeFlags & TYPE_REAL) {
55-
return 'real';
56-
} else if (t.typeFlags & TYPE_INTEGER) {
57-
return 'integer';
58-
} else {
59-
return 'text';
60-
}
61-
};

packages/sync-rules/test/src/generate_schema.test.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,11 @@ export type Database = (typeof AppSchema)['types'];
181181
});
182182

183183
test('kotlin', () => {
184-
expect(new KotlinSchemaGenerator().generate(rules, schema)).toEqual(`val schema = Schema(
184+
expect(new KotlinSchemaGenerator().generate(rules, schema)).toEqual(`import com.powersync.db.schema.Column
185+
import com.powersync.db.schema.Schema
186+
import com.powersync.db.schema.Table
187+
188+
val schema = Schema(
185189
Table(
186190
name = "assets1",
187191
columns = listOf(
@@ -202,7 +206,11 @@ export type Database = (typeof AppSchema)['types'];
202206
)`);
203207

204208
expect(new KotlinSchemaGenerator().generate(rules, schema, { includeTypeComments: true }))
205-
.toEqual(`val schema = Schema(
209+
.toEqual(`import com.powersync.db.schema.Column
210+
import com.powersync.db.schema.Schema
211+
import com.powersync.db.schema.Table
212+
213+
val schema = Schema(
206214
Table(
207215
name = "assets1",
208216
columns = listOf(
@@ -224,7 +232,9 @@ export type Database = (typeof AppSchema)['types'];
224232
});
225233

226234
test('swift', () => {
227-
expect(new SwiftSchemaGenerator().generate(rules, schema)).toEqual(`let schema = Schema(
235+
expect(new SwiftSchemaGenerator().generate(rules, schema)).toEqual(`import PowerSync
236+
237+
let schema = Schema(
228238
Table(
229239
name: "assets1",
230240
columns: [
@@ -244,8 +254,9 @@ export type Database = (typeof AppSchema)['types'];
244254
)
245255
)`);
246256

247-
expect(new SwiftSchemaGenerator().generate(rules, schema, { includeTypeComments: true }))
248-
.toEqual(`let schema = Schema(
257+
expect(new SwiftSchemaGenerator().generate(rules, schema, { includeTypeComments: true })).toEqual(`import PowerSync
258+
259+
let schema = Schema(
249260
Table(
250261
name: "assets1",
251262
columns: [
@@ -267,7 +278,9 @@ export type Database = (typeof AppSchema)['types'];
267278
});
268279

269280
test('dotnet', () => {
270-
expect(new DotNetSchemaGenerator().generate(rules, schema)).toEqual(`class AppSchema
281+
expect(new DotNetSchemaGenerator().generate(rules, schema)).toEqual(`using PowerSync.Common.DB.Schema;
282+
283+
class AppSchema
271284
{
272285
public static Table Assets1 = new Table(new Dictionary<string, ColumnType>
273286
{
@@ -291,9 +304,10 @@ export type Database = (typeof AppSchema)['types'];
291304
});
292305
}`);
293306

294-
console.log(new DotNetSchemaGenerator().generate(rules, schema, { includeTypeComments: true }));
307+
expect(new DotNetSchemaGenerator().generate(rules, schema, { includeTypeComments: true }))
308+
.toEqual(`using PowerSync.Common.DB.Schema;
295309
296-
expect(new DotNetSchemaGenerator().generate(rules, schema, { includeTypeComments: true })).toEqual(`class AppSchema
310+
class AppSchema
297311
{
298312
public static Table Assets1 = new Table(new Dictionary<string, ColumnType>
299313
{

0 commit comments

Comments
 (0)