You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: MyApp/_pages/autoquery/autogen.md
+37-33Lines changed: 37 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,11 @@
2
2
title: AutoQuery AutoGen CRUD Services
3
3
---
4
4
5
-
Long time users of ServiceStack will know it's a staunch proponent of **code-first development** where your C# Types retains the master authority of your App's logic, although there are a number of times where you have to work with existing databases which would require significant effort to create the initial code-first Data Models. Historically we've pointed people to use [OrmLite's T4 Template Support](/ormlite/autogen-t4#t4-template-support) which provides a decent initial stab, however it's limited in its capability and offers a sub par development experience.
5
+
Long time users of ServiceStack will know it's a staunch proponent of **code-first development** where your C# Types retains the master authority of your App's logic, although there are a number of times where you have to work with existing databases which would require significant effort to create the initial code-first Data Models.
6
+
7
+
### AutoGen vs TypeScript Data Models
8
+
9
+
AutoGen's approach relies on runtime C# reflection to inspect your RDBMS schema and dynamically register AutoQuery CRUD Services; if you prefer an alternative flow based on generating source code from exported DB metadata and TypeScript Data Models instead, see [Generate CRUD APIs and UIs for existing DBs](/autoquery/okai-db) which uses the `okai` tool.
@@ -261,7 +265,7 @@ public class ConfigureDb : IHostingStartup
261
265
}
262
266
```
263
267
264
-
The [sqlite](https://gist.github.com/gistlyn/768d7b330b8c977f43310b954ceea668) gist registers an
268
+
The [sqlite](https://gist.github.com/gistlyn/768d7b330b8c977f43310b954ceea668) gist registers an
265
269
[OrmLite.Sqlite](https://github.com/ServiceStack/ServiceStack.OrmLite) RDBMS connection with our App which we want to configure to connect to a **northwind.sqlite** database:
266
270
267
271
```csharp
@@ -312,7 +316,7 @@ Once running you can view your Apps metadata page at `https://localhost:5001` to
312
316
313
317
#### Create Dart gRPC Console App
314
318
315
-
It's also now accessible via [ServiceStack's gRPC endpoint](/grpc/) which opens your generated Services up to [Google's high-performance gRPC ecosystem](https://grpc.io) which enables typed, high-performance integrations into exciting platforms like [Flutter](https://flutter.dev) which uses the [Dart](https://dart.dev) programming language to create Reactive, high-performance native Android and iOS Apps.
319
+
It's also now accessible via [ServiceStack's gRPC endpoint](/grpc/) which opens your generated Services up to [Google's high-performance gRPC ecosystem](https://grpc.io) which enables typed, high-performance integrations into exciting platforms like [Flutter](https://flutter.dev) which uses the [Dart](https://dart.dev) programming language to create Reactive, high-performance native Android and iOS Apps.
316
320
317
321
We can test Dart's gRPC integration and development workflow in a new Dart Console App we can create with:
318
322
@@ -329,7 +333,7 @@ dependencies:
329
333
fixnum: ^0.10.11
330
334
async: ^2.2.0
331
335
protobuf: ^1.0.1
332
-
grpc: ^2.1.3
336
+
grpc: ^2.1.3
333
337
```
334
338
335
339
When you save **pubspec.yaml** Dart's VS Code extension will automatically fetch any new dependencies which can also be manually run with:
Whilst the `AutoRegister = true` flag on its face may seem magical, it's simply an instruction that tells ServiceStack to register the **new** AutoQuery Services it already knows about and register them as if they were normal code-first Services that we had written ourselves.
420
424
421
-
More accurately, behind-the-scenes it uses the Metadata Type structure it constructed in generating the Services & Types, i.e. the same Types used to project into its Add ServiceStack Reference's generated C#, TypeScript, (and other languages) which are also the same Types that are manipulated when customizing code-generation, gets used to generate .NET Types in memory on Startup with Reflection.Emit.
425
+
More accurately, behind-the-scenes it uses the Metadata Type structure it constructed in generating the Services & Types, i.e. the same Types used to project into its Add ServiceStack Reference's generated C#, TypeScript, (and other languages) which are also the same Types that are manipulated when customizing code-generation, gets used to generate .NET Types in memory on Startup with Reflection.Emit.
422
426
423
-
Barring any issues with the projection into IL, externally the end result is indistinguishable to a normal code-first ServiceStack Service manually created by a developer - An important point as to why these solutions compose well with the rest of ServiceStack, just as an AutoQuery Service is a normal ServiceStack Service, these auto generated & auto registered ServiceStack Services are regular Auto Query Services.
427
+
Barring any issues with the projection into IL, externally the end result is indistinguishable to a normal code-first ServiceStack Service manually created by a developer - An important point as to why these solutions compose well with the rest of ServiceStack, just as an AutoQuery Service is a normal ServiceStack Service, these auto generated & auto registered ServiceStack Services are regular Auto Query Services.
424
428
425
429
The primary difference is that they only exist in a .NET Assembly in memory created on Startup, not in code so they're not "statically visible" to a C# compiler, IDE, tools, etc. But otherwise they're regular typed ServiceStack Services and can take advantage of the ecosystem around Services including [Add ServiceStack Reference](/add-servicestack-reference) & other Metadata Pages and Services, etc.
These will generated Service Contracts & DTO Types with the Multitenancy [NamedConnection](/autoquery/rdbms#named-connection) & OrmLite `[Schema]` attribute required for routing AutoQuery Services to use the appropriate RDBMS connection of Schema.
466
-
469
+
These will generated Service Contracts & DTO Types with the Multitenancy [NamedConnection](/autoquery/rdbms#named-connection) & OrmLite `[Schema]` attribute required for routing AutoQuery Services to use the appropriate RDBMS connection of Schema.
470
+
467
471
Although there are potential conflicts if there are identical table names in each RDBMS/Schema as it has to go back and rewrite the Metadata References to use a non-ambiguous name, first tries using the NamedConnection, then the schema then a combination when both exists, if it's still ambiguous it gives up and ignores it. If you do run into conflicts, the recommendation is to "eject" the generated `.cs` sources and manually update them to use your preferred unique names.
468
472
469
473
### Customize Code Generation to include App Conventions
@@ -484,7 +488,7 @@ For an illustration of this in action, here's a typical scenario of how the Nort
484
488
485
489
- Controlling which Tables **not to generate Services for** in `ignoreTables`
486
490
- Which tables not to generate **Write Crud Services** for in `readOnlyTables`
487
-
- Which tables to **restrict access** to in different roles in `protectTableByRole`
491
+
- Which tables to **restrict access** to in different roles in `protectTableByRole`
488
492
- Example of **additional validation** to existing tables in `tableRequiredFields`
489
493
- Adds the `[ValidateNotEmpty]` attribute to Services accessing the table and the `[Required]` OrmLite attribute for the Data Model DTO Type.
if (authRepo.GetUserAuthByUserName(email) != null)
645
+
if (authRepo.GetUserAuthByUserName(email) != null)
642
646
return;
643
-
647
+
644
648
authRepo.CreateUserAuth(new UserAuth {
645
649
Email = email,
646
650
DisplayName = name,
@@ -657,7 +661,7 @@ Of which you can also find published on [NorthwindCrud's home page](https://gith
657
661
658
662
### AutoGen Customizations
659
663
660
-
AutoGen's [Instantly Servicify existing Systems](/servicify) feature works by automatically generating the AutoQuery & Crud APIs and Data Models for all tables in the configured RDBMS's. Further customization of the DataModel Names, the user-defined Route Path they're hosted at & the name of individual AutoQuery APIs for each operation using the `GenerateOperationsFilter`.
664
+
AutoGen's [Instantly Servicify existing Systems](/servicify) feature works by automatically generating the AutoQuery & Crud APIs and Data Models for all tables in the configured RDBMS's. Further customization of the DataModel Names, the user-defined Route Path they're hosted at & the name of individual AutoQuery APIs for each operation using the `GenerateOperationsFilter`.
661
665
662
666
So if you had an existing table name called `applications` the default convention based names would be:
663
667
- Data Model: `Applications`
@@ -716,16 +720,16 @@ As NorthwindCrud is [configured to use JWT](https://github.com/NetCoreApps/North
Copy file name to clipboardExpand all lines: MyApp/_pages/autoquery/okai-db.md
+21-10Lines changed: 21 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,17 +2,28 @@
2
2
title: Generate CRUD APIs and UIs for existing DBs
3
3
---
4
4
5
-
A core piece of functionality in the [Text to Blazor CRUD App](/autoquery/text-to-blazor) feature is distilling an AI Prompt into TypeScript classes that can be [further customized](/autoquery/okai-models#customize-data-models)
6
-
to generate AutoQuery CRUD APIs and Admin UIs for managing the underlying RDBMS tables.
5
+
### AutoGen vs TypeScript Data Models
7
6
8
-
## TypeScript Data Models
7
+
AutoGen's approach relies on runtime C# reflection to inspect your RDBMS schema and dynamically register AutoQuery CRUD Services; this page instead documents an alternative flow based on generating source code from exported DB metadata and TypeScript Data Models using the `okai` tool. If you prefer the reflection-based approach, see [AutoQuery AutoGen CRUD Services](/autoquery/autogen).
9
8
10
-
Using TypeScript is an effortless way to define data models, offering a DSL-like minimal boilerplate format that's human-friendly to read and write which can leverage TypeScript's powerful Type System is validated against the referenced [api.d.ts](https://okai.servicestack.com/api.d.ts) schema to provide a rich authoring experience
11
-
with strong typing and intellisense - containing all the C# Types, interfaces, and attributes used in defining APIs, DTOs and Data Models.
9
+
## Generate CRUD APIs from TypeScript Data Models
10
+
11
+
A core feature of the [okai](/autoquery/okai-models) tool is the ability to convert [customized TypeScript Data Models](/autoquery/okai-models#customize-data-models) into C# AutoQuery CRUD APIs, RDBMS DataModel tables and DB Migrations.
12
+
13
+
This enables a flexible way to generate AutoQuery CRUD APIs for existing RDBMS tables, by:
14
+
15
+
1. Exporting the existing RDBMS metadata to json
16
+
2. Use `okai` to convert the json metadata into TypeScript Data Models
17
+
3. Perform any customizations to the TypeScript Data Model as needed
18
+
4. Use `okai` to generate the AutoQuery CRUD APIs, RDBMS DataModel tables and DB Migrations
19
+
20
+
### Why TypeScript?
21
+
22
+
Using TypeScript is an effortless way to define data models, offering a DSL-like minimal boilerplate format that's human-friendly to read and write which can leverage TypeScript's powerful Type System is validated against the referenced [api.d.ts](https://okai.servicestack.com/api.d.ts) schema to provide a rich authoring experience with strong typing and intellisense - containing all the C# Types, interfaces, and attributes used in defining APIs, DTOs and Data Models.
12
23
13
24
### Blueprint for Code Generation
14
25
15
-
The TypeScript Data Models serve as the blueprint for generating everything needed to support the feature
26
+
The TypeScript Data Models serve as the blueprint for generating everything needed to support the feature
16
27
in your App, including the AutoQuery **CRUD APIs**, **Admin UIs** and **DB Migrations** that can re-create the necessary tables from scratch.
17
28
18
29
## 1. Generate RDBMS Metadata
@@ -34,7 +45,7 @@ This task can then be run from the command line with:
34
45
dotnet run --AppTasks=App.json
35
46
:::
36
47
37
-
Which generates `App_Data/App.json` containing the table definition metadata for all tables in
48
+
Which generates `App_Data/App.json` containing the table definition metadata for all tables in
38
49
the specified RDBMS, e.g:
39
50
40
51
```json
@@ -74,7 +85,7 @@ the specified RDBMS, e.g:
74
85
75
86
### Different Connection or DB Schema
76
87
77
-
If you prefer to generate the metadata for a different connection or schema, you can create a new AppTask
88
+
If you prefer to generate the metadata for a different connection or schema, you can create a new AppTask
78
89
with your preferred `namedConnection` and/or `schema`, e.g:
79
90
80
91
```csharp
@@ -92,11 +103,11 @@ dotnet run --AppTasks=Sales.json
92
103
## 2. Generate TypeScript Data Models
93
104
94
105
The next step is to generate TypeScript Data Models from the captured metadata which can be done with the `okai` tool
95
-
by running the `convert` command with the path to the `App.json` JSON table definitions which will generate the
106
+
by running the `convert` command with the path to the `App.json` JSON table definitions which will generate the
96
107
TypeScript Data Models to stdout which can be redirected to a file in your **ServiceModel** project, e.g:
0 commit comments