Repository navigation
Expand file tree
/
Copy pathCrudUtils.cs
More file actions
314 lines (266 loc) · 10.7 KB
/
Copy pathCrudUtils.cs
File metadata and controls
314 lines (266 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System;
using System.Collections.Generic;
using System.Linq;
using ServiceStack.Data;
using ServiceStack.NativeTypes;
using ServiceStack.OrmLite;
using ServiceStack.Web;
namespace ServiceStack
{
public class AutoGenContext
{
public AutoGenContext(CrudCodeGenTypes instruction, string tableName, TableSchema tableSchema)
{
Instruction = instruction;
TableName = tableName;
TableSchema = tableSchema;
}
/// <summary>
/// AutoGen Request DTO Instruction
/// </summary>
public CrudCodeGenTypes Instruction { get; set; }
/// <summary>
/// Original Table Name
/// </summary>
public string TableName { get; }
/// <summary>
/// RDBMS TableSchema
/// </summary>
public TableSchema TableSchema { get; }
/// <summary>
/// Generated DataModel Name to use
/// </summary>
public string DataModelName { get; set; }
/// <summary>
/// Generated DataModel Name to use for Query Services
/// </summary>
public string PluralDataModelName { get; set; }
/// <summary>
/// Generated Route Path base to use
/// </summary>
public string RoutePathBase { get; set; }
/// <summary>
/// Generated Request DTO Name to use per operation: Query, Create, Update, Patch, Delete
/// </summary>
public Dictionary<string,string> OperationNames { get; set; } = new();
/// <summary>
/// RDBMS Dialect
/// </summary>
public IOrmLiteDialectProvider Dialect { get; set; }
/// <summary>
/// Return what table [Alias] name should be used (if any)
/// </summary>
public Func<string> GetTableAlias { get; set; }
/// <summary>
/// Return what table column [Alias] name should be used (if any)
/// </summary>
public Func<MetadataPropertyType, ColumnSchema, string> GetColumnAlias { get; set; }
}
public interface IGenerateCrudServices
{
List<string> IncludeCrudOperations { get; set; }
/// <summary>
/// Generate services
/// </summary>
List<CreateCrudServices> CreateServices { get; set; }
Action<AutoGenContext> GenerateOperationsFilter { get; set; }
Func<ColumnSchema, IOrmLiteDialectProvider, Type> ResolveColumnType { get; set; }
Action<MetadataTypes, MetadataTypesConfig, IRequest> MetadataTypesFilter { get; set; }
Action<MetadataType, IRequest> TypeFilter { get; set; }
Action<MetadataOperationType, IRequest> ServiceFilter { get; set; }
Func<MetadataType, bool> IncludeType { get; set; }
Func<MetadataOperationType, bool> IncludeService { get; set; }
bool AddDataContractAttributes { get; set; }
bool AddIndexesToDataMembers { get; set; }
string AccessRole { get; set; }
DbSchema GetCachedDbSchema(IDbConnectionFactory dbFactory, string schema = null, string namedConnection = null,
List<string> includeTables = null, List<string> excludeTables = null);
void Register(IAppHost appHost);
List<Type> GenerateMissingServices(AutoQueryFeature feature);
}
public static class CrudUtils
{
public static MetadataAttribute ToAttribute(string name, Dictionary<string, object> args = null,
Attribute attr = null) =>
new MetadataAttribute {
Name = name,
Attribute = attr,
Args = args?.Map(x => new MetadataPropertyType {
Name = x.Key,
Value = x.Value?.ToString(),
Type = x.Value?.GetType().Name,
})
};
public static MetadataType AddAttribute(this MetadataType type, string name,
Dictionary<string, object> args = null, Attribute attr = null)
{
var metaAttr = ToAttribute(name, args, attr);
type.Attributes ??= new List<MetadataAttribute>();
type.Attributes.Add(metaAttr);
return type;
}
public static MetadataType AddAttribute(this MetadataType type, Attribute attr)
{
var nativeTypesGen = HostContext.AssertPlugin<NativeTypesFeature>().DefaultGenerator;
var metaAttr = nativeTypesGen.ToMetadataAttribute(attr);
type.Attributes ??= new List<MetadataAttribute>();
type.Attributes.Add(metaAttr);
return type;
}
public static MetadataType AddAttributeIfNotExists<T>(this MetadataType type, T attr, Func<T, bool> test=null)
where T : Attribute
{
return type.Attributes?.Any(x => x.Attribute is T t && (test == null || test(t))) == true
? type
: AddAttribute(type, attr);
}
public static MetadataPropertyType AddAttribute(this MetadataPropertyType propType, Attribute attr)
{
var nativeTypesGen = HostContext.AssertPlugin<NativeTypesFeature>().DefaultGenerator;
var metaAttr = nativeTypesGen.ToMetadataAttribute(attr);
propType.Attributes ??= new List<MetadataAttribute>();
propType.Attributes.Add(metaAttr);
return propType;
}
public static MetadataPropertyType AddAttributeIfNotExists<T>(this MetadataPropertyType propType, T attr, Func<T, bool> test=null)
where T : Attribute
{
return propType.Attributes?.Any(x => x.Attribute is T t && (test == null || test(t))) == true
? propType
: AddAttribute(propType, attr);
}
}
/// <summary>
/// Instruction for which AutoCrud Services to generate
/// </summary>
public class CreateCrudServices : IMeta
{
/// <summary>
/// Which AutoCrud Operations to include:
/// - Query
/// - Create
/// - Update
/// - Patch
/// - Delete
/// </summary>
public List<string> IncludeCrudOperations { get; set; }
/// <summary>
/// The RDBMS Schema you want AutoQuery Services generated for
/// </summary>
public string Schema { get; set; }
/// <summary>
/// The NamedConnection you want AutoQuery Services generated for
/// </summary>
public string NamedConnection { get; set; }
/// <summary>
/// Include additional C# namespaces
/// </summary>
public List<string> AddNamespaces { get; set; }
/// <summary>
/// Allow List to specify only the tables you would like to have code-generated
/// </summary>
public List<string> IncludeTables { get; set; }
/// <summary>
/// Block list to specify which tables you would like excluded from being generated
/// </summary>
public List<string> ExcludeTables { get; set; }
/// <summary>
/// Allow List to specify only the types you would like to have code-generated, see:
/// https://docs.servicestack.net/csharp-add-servicestack-reference#includetypes
/// </summary>
public List<string> IncludeTypes { get; set; }
/// <summary>
/// Block list to specify which types you would like excluded from being generated. see:
/// https://docs.servicestack.net/csharp-add-servicestack-reference#excludetypes
/// </summary>
public List<string> ExcludeTypes { get; set; }
public Dictionary<string, string> Meta { get; set; }
}
public class CrudCodeGenTypes : NativeTypesBase, IMeta, IReturn<string>
{
/// <summary>
/// Either 'all' to include all AutoQuery Services or 'new' to include only missing Services and Types
/// </summary>
public string Include { get; set; }
/// <summary>
/// The language you want
/// csharp
/// typescript
/// java
/// kotlin
/// swift
/// dart
/// vbnet
/// fsharp
/// typescript.d
/// </summary>
public string Lang { get; set; }
/// <summary>
/// Which AutoCrud Operations to include:
/// - Query
/// - Create
/// - Update
/// - Patch
/// - Delete
/// </summary>
public List<string> IncludeCrudOperations { get; set; }
/// <summary>
/// The RDBMS Schema you want AutoQuery Services generated for
/// </summary>
public string Schema { get; set; }
/// <summary>
/// The NamedConnection you want AutoQuery Services generated for
/// </summary>
public string NamedConnection { get; set; }
/// <summary>
/// The Admin AuthSecret to access Service in Release mode
/// </summary>
public string AuthSecret { get; set; }
/// <summary>
/// Do not use cached DB Table Schemas, re-fetch latest
/// </summary>
public bool? NoCache { get; set; }
/// <summary>
/// Allow List to specify only the tables you would like to have code-generated
/// </summary>
public List<string> IncludeTables { get; set; }
/// <summary>
/// Block list to specify which tables you would like excluded from being generated
/// </summary>
public List<string> ExcludeTables { get; set; }
public Dictionary<string, string> Meta { get; set; }
}
public class CrudTables : IReturn<AutoCodeSchemaResponse>
{
public string Schema { get; set; }
public string NamedConnection { get; set; }
public string AuthSecret { get; set; }
/// <summary>
/// Allow List to specify only the tables you would like to have code-generated
/// </summary>
public List<string> IncludeTables { get; set; }
/// <summary>
/// Block list to specify which tables you would like excluded from being generated
/// </summary>
public List<string> ExcludeTables { get; set; }
public bool? NoCache { get; set; }
}
public class AutoCodeSchemaResponse
{
public List<TableSchema> Results { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
public class DbSchema
{
public string Schema { get; set; }
public string NamedConnection { get; set; }
public List<TableSchema> Tables { get; set; } = new List<TableSchema>();
}
public class TableSchema
{
public string Name { get; set; }
public ColumnSchema[] Columns { get; set; }
public string ErrorType { get; set; }
public string ErrorMessage { get; set; }
}
}