-
Notifications
You must be signed in to change notification settings - Fork 5
Schema factory syntax #37
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
Open
LucDeCaf
wants to merge
12
commits into
main
Choose a base branch
from
feat/schema-builder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+319
−218
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
af3138a
Initial implementation
LucDeCaf af2d337
Merge branch 'main' into feat/schema-builder
LucDeCaf 08fb4e8
Remove dead code
LucDeCaf cc2ed59
Fix demos
LucDeCaf ddc77a3
Cleanup
LucDeCaf 2973c5c
Make Table less modifiable + small optimisations
LucDeCaf ab457e4
Rename _Builder classes to _Factory to better reflect design pattern
LucDeCaf 6d23c39
Allow passing TableFactory instances to schema instead of just Table …
LucDeCaf d29e03b
Prefer TableFactory -> SchemaFactory over Table -> SchemaFactory
LucDeCaf 50a4dd7
Merge branch 'main' into feat/schema-builder
LucDeCaf d93e331
Merge main and fix conflicts
LucDeCaf e64d193
Support { key, value } collection initalizer syntax
LucDeCaf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
22 changes: 9 additions & 13 deletions
22
...Sync/PowerSync.Common/DB/Schema/Column.cs → .../PowerSync.Common/DB/Schema/ColumnJSON.cs
This file contains hidden or 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,34 +1,30 @@ | ||
| namespace PowerSync.Common.DB.Schema; | ||
|
|
||
| using Newtonsoft.Json; | ||
|
|
||
| public enum ColumnType | ||
| { | ||
| TEXT, | ||
| INTEGER, | ||
| REAL | ||
| Text, | ||
| Integer, | ||
| Real | ||
| } | ||
|
|
||
| public class ColumnOptions(string Name, ColumnType? Type) | ||
| class ColumnJSONOptions(string Name, ColumnType? Type) | ||
| { | ||
| public string Name { get; set; } = Name; | ||
| public ColumnType? Type { get; set; } = Type; | ||
| } | ||
|
|
||
| public class Column(ColumnOptions options) | ||
| class ColumnJSON(ColumnJSONOptions options) | ||
| { | ||
| public const int MAX_AMOUNT_OF_COLUMNS = 1999; | ||
|
|
||
| public string Name { get; set; } = options.Name; | ||
|
|
||
| public ColumnType Type { get; set; } = options.Type ?? ColumnType.TEXT; | ||
| public ColumnType Type { get; set; } = options.Type ?? ColumnType.Text; | ||
|
|
||
| public string ToJSON() | ||
| public object ToJSONObject() | ||
| { | ||
| return JsonConvert.SerializeObject(new | ||
| return new | ||
| { | ||
| name = Name, | ||
| type = Type.ToString() | ||
| }); | ||
| }; | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,23 @@ | ||
| namespace PowerSync.Common.DB.Schema; | ||
|
|
||
| class IndexJSONOptions(string name, IndexedColumnJSON[]? columns = null) | ||
| { | ||
| public string Name { get; set; } = name; | ||
| public IndexedColumnJSON[]? Columns { get; set; } = columns ?? []; | ||
| } | ||
|
|
||
| class IndexJSON(IndexJSONOptions options) | ||
| { | ||
| public string Name { get; set; } = options.Name; | ||
|
|
||
| public IndexedColumnJSON[] Columns => options.Columns ?? []; | ||
|
|
||
| public object ToJSONObject(Table table) | ||
| { | ||
| return new | ||
| { | ||
| name = Name, | ||
| columns = Columns.Select(column => column.ToJSON(table)).ToList() | ||
| }; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,26 @@ | ||
| namespace PowerSync.Common.DB.Schema; | ||
|
|
||
| public class SchemaFactory | ||
| { | ||
| private List<Table> _tables; | ||
|
|
||
| public SchemaFactory(params Table[] tables) | ||
| { | ||
| _tables = tables.ToList(); | ||
| } | ||
|
|
||
| public SchemaFactory(params TableFactory[] tableFactories) | ||
| { | ||
| _tables = tableFactories.Select((f) => f.Create()).ToList(); | ||
| } | ||
|
|
||
| public Schema Create() | ||
| { | ||
| Dictionary<string, Table> tableMap = new(); | ||
| foreach (Table table in _tables) | ||
| { | ||
| tableMap[table.Name] = table; | ||
| } | ||
| return new Schema(tableMap); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,56 @@ | ||
| namespace PowerSync.Common.DB.Schema; | ||
|
|
||
| using System.Collections; | ||
|
|
||
| public class TableFactory() | ||
| { | ||
| public ColumnMap Columns { get; set; } = new(); | ||
| public IndexMap Indexes { get; set; } = new(); | ||
|
|
||
| public string Name { get; set; } = null!; | ||
| public bool LocalOnly { get; set; } = false; | ||
| public bool InsertOnly { get; set; } = false; | ||
| string? ViewName { get; set; } | ||
| bool? TrackMetadata { get; set; } | ||
| TrackPreviousOptions? TrackPreviousValues { get; set; } | ||
| bool? IgnoreEmptyUpdates { get; set; } | ||
|
|
||
| public Table Create() | ||
| { | ||
| if (string.IsNullOrWhiteSpace(Name)) | ||
| { | ||
| throw new Exception("Table name is required."); | ||
| } | ||
| TableOptions options = new( | ||
| indexes: Indexes.Indexes, | ||
| localOnly: LocalOnly, | ||
| insertOnly: InsertOnly, | ||
| viewName: ViewName, | ||
| trackMetadata: TrackMetadata, | ||
| trackPreviousValues: TrackPreviousValues, | ||
| ignoreEmptyUpdates: IgnoreEmptyUpdates | ||
| ); | ||
| return new Table(Name, Columns.Columns, options); | ||
| } | ||
| } | ||
|
|
||
| public class ColumnMap : IEnumerable | ||
| { | ||
| public Dictionary<string, ColumnType> Columns { get; } = new(); | ||
|
|
||
| public void Add(string key, ColumnType value) => Columns.Add(key, value); | ||
|
|
||
| public ColumnType this[string name] { set { Columns[name] = value; } } | ||
| public IEnumerator GetEnumerator() => Columns.GetEnumerator(); | ||
| } | ||
|
|
||
| public class IndexMap : IEnumerable | ||
| { | ||
| public Dictionary<string, List<string>> Indexes { get; } = new(); | ||
|
|
||
| public void Add(string key, List<string> value) => Indexes.Add(key, value); | ||
|
|
||
| public List<string> this[string name] { set { Indexes[name] = value; } } | ||
| public IEnumerator GetEnumerator() => Indexes.GetEnumerator(); | ||
| } | ||
|
|
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.