-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeGen.fs
819 lines (725 loc) · 40 KB
/
CodeGen.fs
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
module PgGen.CodeGen
open System.IO
open PgGen.StringBuffer
open Common
open System
open PgGen.CodeStatic
let createModuleName suffix (project:string) (domain:string) =
$"{project}.Domain.{domain}.{suffix}"
let ensureFolder(folder:string) =
if not (Directory.Exists folder) then
printfn $"Creating folder {folder}"
Directory.CreateDirectory folder |> ignore
// postgres prefers lowercase, so CatDog -> cat_dog
// todo: not sure we are doing this on sql generation side to be consistent
let postgrestify (s:string) =
s.Replace(" ","_").Replace("-","_").ToLowerInvariant()
let emitDomain (proj:string) (s:Schema) =
let domain = titleCase s.SName
let projCap = proj |> titleCase
let domainCode =
stringBuffer {
yield $"namespace {projCap}.Domain.{domain}\n"
let needsSystem =
[for t in s.Tables do
for c in t.Cols do
if c.CType = Timestamp then
yield true] |> Seq.isEmpty |> not
if needsSystem then
yield $"open System\n"
// Any enums in the schema that need F# types
for e in s.Enums do
yield $"\n"
yield $"type {e.EName |> toFSharp} =\n"
for v in e.EValues do
yield $" | {v |> toFSharp}\n"
yield $"\n"
for t in s.Tables do
let fsharpName = t.FSharpName()
let colsExceptKey = t.Cols |> List.filter (fun c -> c.CType <> Id) // FIXFIX - generalize to non int keys
let availCols = [ for c in t.Cols do
yield {| Name = c.CName;Type = c.CType.FSharpType(); IsArray = c.Array |}
for fr in t.FRefs do
if fr.Generate then
let colName = fr.Name |> Option.defaultValue $"id_{fr.ToTable}"
yield {| Name = colName;Type = "int" ; IsArray = false |}
else
failwithf "not implemented - not integer foreign key references"
]
yield $" // -------------------------------------------\n"
yield $" // {t.TName}\n"
yield $" // -------------------------------------------\n"
// CREATE type
yield $"type Create{fsharpName} = {{\n"
for col in colsExceptKey do
let optionModifier =
if col.Nullable then
" option"
else
""
let arrayModifier = if col.Array then " []" else ""
match col.Comment with
| None -> ()
| Some c ->
yield $"\n /// {c}\n"
yield $" {col.FSharpName()} : {col.CType.FSharpType()}{arrayModifier}{optionModifier}\n"
for fr in t.FRefs do
if fr.Generate then
let colName = fr.Name |> Option.defaultValue $"id_{fr.ToTable}" |> toFSharp
let optionModifier =
if fr.IsNullable then
" option"
else
""
yield $" {colName} : int{optionModifier}\n"
else
failwithf "Not implemented - not integer foreign key references"
for e in t.ERefs do
if e.Generate then
let colName = e.Name |> Option.defaultValue e.EName |> toFSharp
let optionModifier =
if e.IsNullable then
" option"
else
""
yield $" {colName} : {e.EName |> toFSharp}{optionModifier}\n"
else
failwithf "Not implemented - nongenerated enum references"
yield $"}}"
yield "\n"
// UPDATE type
yield $"type Update{fsharpName} = {{\n"
for col in t.Cols do // include key since we need to know what to update
let optionModifier =
if col.Nullable then
" option"
else
""
let arrayModifier = if col.Array then " []" else ""
match col.Comment with
| None -> ()
| Some c ->
yield $"\n /// {c}\n"
yield $" {col.FSharpName()} : {col.CType.FSharpType()}{arrayModifier}{optionModifier}\n"
for fr in t.FRefs do
if fr.Generate then
let colName = fr.Name |> Option.defaultValue $"id_{fr.ToTable}" |> toFSharp
let optionModifier =
if fr.IsNullable then
" option"
else
""
yield $" {colName} : int{optionModifier}\n"
for e in t.ERefs do
if e.Generate then
let colName = e.Name |> Option.defaultValue e.EName |> toFSharp
yield $" {colName} : {e.EName |> toFSharp}\n"
else
failwithf "Not implemented - nongenerated enum references"
yield $"}}"
yield "\n"
// Basic full field type
yield $"type {fsharpName} = {{\n"
for col in t.Cols do
let optionModifier =
if col.Nullable then
" option"
else
""
let arrayModifier = if col.Array then " []" else ""
match col.Comment with
| None -> ()
| Some c ->
yield $"\n /// {c}\n"
yield $" {col.FSharpName()} : {col.CType.FSharpType()}{arrayModifier}{optionModifier}\n"
for fr in t.FRefs do
if fr.Generate then
let colName = fr.Name |> Option.defaultValue $"id_{fr.ToTable}" |> toFSharp
let optionModifier =
if fr.IsNullable then
" option"
else
""
yield $" {colName} : int{optionModifier}\n"
for e in t.ERefs do
if e.Generate then
let colName = e.Name |> Option.defaultValue e.EName |> toFSharp
yield $" {colName} : {e.EName |> toFSharp}\n"
else
failwithf "Not implemented - nongenerated enum references"
yield $"}}"
// Tables that aren't using a simple integer key need a custom primary key type
match t.PKey with
| Some pk ->
yield $"\n"
yield $"type {t.PKeyTypeName()}= {{\n"
for colName in pk.Cols do
let c =
match availCols|> List.tryFind (fun c -> c.Name = colName) with
| Some x -> x
| None ->
failwithf $"Could not find column primary key referenced '{colName}' in table '{t.TName}' cols {availCols}"
let optionalArray = if c.IsArray then " []" else ""
yield $" {c.Name |> toFSharp} : {c.Type}{optionalArray}\n"
yield $"}}"
| None -> ()
yield "\n"
}
let storageCode =
stringBuffer {
yield $"module {projCap}.Domain.{domain}.Storage\n"
yield $"\n"
yield $"open {projCap}.Db\n"
yield $"open {projCap}.Domain.{domain}\n"
yield $"open Plough.ControlFlow\n"
// Any enums in the schema that need converter functions to/from F# types
for e in s.Enums do
let fSharpType = e.EName |> toFSharp
let fSharpVar = e.EName |> toFSharpLower
yield $"let {fSharpVar}ToEnum(v:{fSharpType}) =\n"
yield $" match v with\n"
for v in e.EValues do
yield $" | {fSharpType}.{v |> toFSharp} -> Db.{s.SName}.Types.{e.EName}.{v}\n"
yield $"\n"
yield $"let {fSharpVar}FromEnum(e:Db.{s.SName}.Types.{e.EName}) =\n"
yield $" match e with\n"
for v in e.EValues do
yield $" | Db.{s.SName}.Types.{e.EName}.{v} -> {fSharpType}.{v |> toFSharp}\n"
yield $" | x -> failwithf $\"Impossible {e.EName} enum value {{x}}\"\n"
yield $"\n"
for t in s.Tables do
let colsExceptKey = t.FullCols() |> List.filter (fun c -> c.CType <> Id) // FIXFIX - generalize to non int keys
let colNamesExceptKey = String.Join(",",[for c in colsExceptKey -> postgrestify c.CName])
let placeHolderDefs =
[for c in colsExceptKey ->
if c.Nullable then
let defaultVPostgres =
match c.CType with
| Int32 -> "-1"
| String -> "''"
| Timestamp -> "'0001-01-01'"
| Jsonb -> "'{}'::jsonb"
| _ -> failwithf $"Not implemented - default value for {c.CType}"
{| CName = c.CName ; CValue = $"(CASE WHEN @{c.CName} = {defaultVPostgres} THEN NULL ELSE @{c.CName} END)" |}
else
{| CName = c.CName ; CValue = $"@{c.CName}" |}
]
let insertPlaceHolders = String.Join(",",[for c in placeHolderDefs -> c.CValue])
let updatePlaceHolders = String.Join(",",[for c in placeHolderDefs -> $"{c.CName}={c.CValue}"])
let fullPostgresCols =
String.Join(",",[for c in t.FullCols() -> postgrestify c.CName])
let nonPKParametersForCall =
String.Join(",",[for c in colsExceptKey ->
let optionalNullHack =
if c.Nullable then
if c.Array then
failwith $"Not implemented - nullable array columns"
let defaultV =
match c.CType with
| Int32 -> "-1"
| String -> "\"\""
| Timestamp -> "\"0001-01-01\""
| Jsonb -> "\"{}\""
| _ -> failwithf $"Not implemented - default value for {c.CType}"
$"(request.{c.FSharpName()} |> Option.defaultValue {defaultV})"
else
$"request.{c.FSharpName()}"
let optionalEnumConversion =
match c.CType with
| Enum e ->
let fSharpVar = e |> toFSharpLower
$"({optionalNullHack}|> {fSharpVar}ToEnum)"
| _ -> $"{optionalNullHack}"
$"{postgrestify c.CName}={optionalEnumConversion}"])
let fsharpName = t.FSharpName()
let returning,readWhereClause,parameters =
match t.PKey with
| None -> "RETURNING id","id= @id","id=request"
| Some x ->
"", // return nothing where there's a custom primary key
String.Join(" AND ",[for c in x.Cols -> $"{postgrestify c}=@{c}"]),
String.Join(",",[for c in x.Cols -> $"{c}=request.{c |> toFSharp}"])
yield $"// -------------------------------------------\n"
yield $"// {fsharpName} CRUD operations\n"
yield $"// -------------------------------------------\n"
yield $"\n"
// ====================================================
// Create code
// ====================================================
yield $"let create{fsharpName} (request:Create{fsharpName}) =\n"
yield $" task {{\n"
yield $" use! conn = Db.openConnectionAsync()\n"
yield $" use cmd = Db.CreateCommand<\"\"\"INSERT INTO {s.SName}.{t.TName} (\n"
yield $" {colNamesExceptKey}) \n VALUES({insertPlaceHolders}) {returning}\n"
yield $" \"\"\",SingleRow=true>(conn)\n"
yield $" let newId = cmd.Execute({nonPKParametersForCall})\n"
match t.PKey with
| None ->
yield $" return newId.Value // returns option but should be reliable\n"
| Some _ ->
yield $" return () // complex primary key, no return value\n"
yield $" }}\n"
yield $"\n"
yield $"let read{fsharpName} (request:{t.PKeyTypeName()}) : Task<{fsharpName} option>=\n"
yield $" task {{\n"
yield $" use! conn = Db.openConnectionAsync()\n"
yield $" use cmd = Db.CreateCommand<\"\"\""
yield $" SELECT {fullPostgresCols} FROM {s.SName}.{t.TName} WHERE {readWhereClause}\n"
yield $" \"\"\",SingleRow=true>(conn)\n"
yield $" let result = cmd.Execute({parameters})\n"
yield $" return result |> Option.map(fun r->\n"
yield $" {{\n"
for col in t.FullCols() do
let optionalEnumMap =
match col.CType with
| Enum e -> $" |> {e |> toFSharpLower}FromEnum"
| _ -> ""
yield $" {col.FSharpName()} = r.{col.CName}{optionalEnumMap}\n"
yield $" }})\n"
yield $" }}\n"
yield $"\n"
// ====================================================
// Update code
// ====================================================
yield $"let update{fsharpName} (request:Update{fsharpName}) =\n"
yield $" task {{\n"
yield $" use! conn = Db.openConnectionAsync()\n"
yield $" use cmd = Db.CreateCommand<\"\"\"\n"
yield $" UPDATE {s.SName}.{t.TName} \n"
yield $" SET\n"
let updateParameters =
String.Join(",",
[ yield nonPKParametersForCall // already have these from insert
match t.PKey with // updates also need the primary key columns
| None ->
yield $"id=request.Id"
| Some cols ->
// already covered I think by the nonPKParametersForCall
//for col in cols.Cols do
// yield $"{col}=request.{col}"
()
])
yield $" {updatePlaceHolders}\n"
yield $" WHERE {readWhereClause}\n"
yield $" \"\"\",SingleRow=true>(conn)\n"
yield $" let result = cmd.Execute({updateParameters})\n"
yield $" return ()\n"
yield $" }}\n"
yield $"\n"
// ====================================================
// Delete code
// ====================================================
yield $"let delete{fsharpName} (id:{t.PKeyTypeName()}) =\n"
yield $" task {{\n"
yield $" failwithf \"Not implemented\"\n"
yield $" return \"unimplemented\"\n"
yield $" }}"
yield $"\n\n"
// ====================================================
// List code
// ====================================================
let emitRunPart() =
stringBuffer {
yield $" return result |> Seq.map(fun r->\n"
yield $" {{\n"
for col in t.FullCols() do
let optionalEnumMap =
match col.CType with
| Enum e -> $" |> {e |> toFSharpLower}FromEnum"
| _ -> ""
yield $" {col.FSharpName()} = r.{col.CName}{optionalEnumMap}\n"
yield $" }}:{fsharpName}) |> Array.ofSeq \n"
}
yield $"let list{fsharpName} (row:int option) (batchSize:int option) =\n"
yield $" task {{\n"
yield $" use! conn = Db.openConnectionAsync()\n"
yield $" match row,batchSize with\n"
yield $" | Some row, Some batchSize ->\n"
yield $" use cmd = Db.CreateCommand<\"\"\""
yield $"SELECT {fullPostgresCols} FROM {s.SName}.{t.TName} LIMIT @batch_size OFFSET @row\n"
yield $" \"\"\">(conn)\n"
yield $" let! result = cmd.TaskAsyncExecute(row=row,batch_size=batchSize)\n"
yield emitRunPart()
yield $" | None , Some batchSize ->\n"
yield $" use cmd = Db.CreateCommand<\"\"\""
yield $"SELECT {fullPostgresCols} FROM {s.SName}.{t.TName} LIMIT @batch_size\n"
yield $" \"\"\">(conn)\n"
yield $" let! result = cmd.TaskAsyncExecute(batch_size=batchSize)\n"
yield emitRunPart()
yield $" | Some row , None ->\n"
yield $" use cmd = Db.CreateCommand<\"\"\""
yield $"SELECT {fullPostgresCols} FROM {s.SName}.{t.TName} LIMIT @batch_size OFFSET @row\n"
yield $" \"\"\">(conn)\n"
yield $" let! result = cmd.TaskAsyncExecute(row=row,batch_size=50)\n"
yield emitRunPart()
yield $" | None , None ->\n"
yield $" use cmd = Db.CreateCommand<\"\"\""
yield $"SELECT {fullPostgresCols} FROM {s.SName}.{t.TName} "
yield $" \"\"\">(conn)\n"
yield $" let! result = cmd.TaskAsyncExecute()\n"
yield emitRunPart()
yield $" }}\n"
yield $"\n"
yield $"\n"
}
let apiWireup =
stringBuffer {
yield $"module {projCap}.Domain.{domain}.Api\n"
yield $"\n"
yield $"open {projCap}.Api.{domain}\n"
yield $"open {projCap}.Domain.{domain}\n"
yield $"\n"
yield $"// Wire up the api definition to the service layer calls here\n"
yield $"let api = {{\n"
for t in s.Tables do
let fsharpName = t.FSharpName()
yield $" // -------------------------------------------\n"
yield $" Create{fsharpName} = Service.create{fsharpName}\n"
yield $" Read{fsharpName} = Service.read{fsharpName}\n"
yield $" Update{fsharpName} = Service.update{fsharpName}\n"
yield $" Delete{fsharpName} = Service.delete{fsharpName}\n"
yield $" List{fsharpName} = Service.list{fsharpName}\n"
yield "}\n"
}
let serviceLayer =
stringBuffer {
yield $"module {projCap}.Domain.{domain}.Service\n"
yield $"\n"
yield $"open {projCap}.Common\n"
yield $"open {projCap}.Domain.{domain}\n"
yield $"open Plough.ControlFlow\n"
yield $"\n"
yield $"// PgGen: note - these are simple wrappers for now but business logic / transactions / multiple IO calls can be handled here\n"
for t in s.Tables do
let tableCap = t.FSharpName()
yield $"// -------------------------------------------\n"
yield $"let create{tableCap} (request:Create{tableCap}) =\n"
yield $" taskEither {{\n"
yield $" return! Storage.create{tableCap} request\n"
yield $" }}\n"
yield $"\n"
yield $"let read{tableCap} (request:{t.PKeyTypeName()}) =\n"
yield $" taskEither {{\n"
yield $" return! Storage.read{tableCap} request\n"
yield $" }}\n"
yield $"\n"
yield $"let update{tableCap} (request:Update{tableCap}) =\n"
yield $" taskEither {{\n"
yield $" return! Storage.update{tableCap} request\n"
yield $" }}\n"
yield $"\n"
yield $"let delete{tableCap} (request:{t.PKeyTypeName()}) =\n"
yield $" taskEither {{\n"
yield $" return! Storage.delete{tableCap} request\n"
yield $" }}\n"
yield $"\n"
yield $"let list{tableCap} (batchOffset:BatchOffset) =\n"
yield $" taskEither {{\n"
yield $" return! Storage.list{tableCap} batchOffset.Offset batchOffset.BatchSize\n"
yield $" }}\n"
yield $"\n"
}
let apiDef =
stringBuffer {
yield $"module {projCap}.Api.{domain}\n"
yield $"\n"
yield $"open Plough.ControlFlow\n"
yield $"open Plough.WebApi.Client\n"
yield $"open Plough.WebApi.Server\n"
yield $"open Plough.WebApi.Server.Plain\n"
yield $"open {projCap}.Common\n"
yield $"open {projCap}.Domain.{domain}\n"
yield $"\n"
yield $"type Api ={{\n"
for t in s.Tables do
let tableCap = t.FSharpName()
match t.PKey with
| None ->
yield $" Create{tableCap} : Create{tableCap} -> TaskEither<{t.PKeyTypeName()}>\n"
| Some _ ->
yield $" Create{tableCap} : Create{tableCap} -> TaskEither<unit>\n"
yield $" Read{tableCap} : {t.PKeyTypeName()} -> TaskEither<{tableCap} option>\n"
yield $" Update{tableCap} : Update{tableCap} -> TaskEither<unit>\n"
yield $" Delete{tableCap} : {t.PKeyTypeName()} -> TaskEither<string>\n"
yield $" List{tableCap} : BatchOffset -> TaskEither<{tableCap} []>\n"
yield $"}}\n"
// -------------------------------------------
// Server api
// -------------------------------------------
yield $"module Server =\n"
yield $" let build<'ctx> (api : Api) : ApiServer<'ctx> = subRoute \"/{s.SName}\" <| choose [\n"
yield $" GET >=> choose [\n"
for t in s.Tables do
let tableCap = t.FSharpName()
let canUseGet =
match t.PKey with
| None -> true
| Some _ -> false
if canUseGet then
yield $" routef \"/{t.TName}/read/%%i\" (makeJSONHandlerWithArgAsync api.Read{tableCap})\n"
yield $" route \"/{t.TName}/list\" >=> makeJSONHandlerWithQueryParamAsync api.List{tableCap} \n"
yield $" ]\n"
yield $" POST >=> choose [\n"
for t in s.Tables do
let tableCap = t.FSharpName()
let needsPostForGet =
match t.PKey with
| None -> false
| Some _ -> true
if needsPostForGet then
yield $" route \"/{t.TName}/read\" >=> makeJSONHandlerWithObjAsync api.Read{tableCap}\n"
yield $" route \"/{t.TName}/create\" >=> makeJSONHandlerWithObjAsync api.Create{tableCap}\n"
yield $" route \"/{t.TName}/update\" >=> makeJSONHandlerWithObjAsync api.Update{tableCap}\n"
yield $" route \"/{t.TName}/delete\" >=> makeJSONHandlerWithObjAsync api.Delete{tableCap}\n"
yield $" ]\n"
yield $" ]\n"
// -------------------------------------------
// Client
// -------------------------------------------
yield $"type Client(client : ClientBuilder) =\n"
yield $" inherit ClientBuilder(Nested(\"{s.SName}\", client))\n"
yield $"\n"
for t in s.Tables do
let tableCap = t.FSharpName()
let createReturnType =
match t.PKey with
| None -> t.PKeyTypeName()
| Some _ -> "unit"
yield $" member x.Create{tableCap}(doc:Create{tableCap}) : TaskEither<{createReturnType}> =\n"
yield $" x.Post(\"{t.TName}/create\",doc)\n"
yield $" member x.Read{tableCap}(docId:int) : TaskEither<{tableCap} option> =\n"
let needsPostForGet =
match t.PKey with
| None -> false
| Some _ -> true
if needsPostForGet then
yield $" x.Post(\"{t.TName}/read\",docId)\n"
else
yield $" x.Get <| $\"{t.TName}/read/{{docId}}\"\n"
yield $" member x.Update{tableCap}(doc:Update{tableCap}) : TaskEither<unit> =\n"
yield $" x.Post(\"{t.TName}/update\",doc)\n"
yield $" member x.Delete{tableCap}(docId:int) : TaskEither<string> =\n"
yield $" x.Post(\"{t.TName}/delete\",docId)\n"
yield $" member x.List{tableCap}(offset:int option,batchSize:int option) : TaskEither<{tableCap} []> =\n"
yield $" match offset,batchSize with\n"
yield $" | Some o,Some b -> $\"{t.TName}/list?offset={{offset}}&batch={{batchSize}}\"\n"
yield $" | None ,Some b -> $\"{t.TName}/list?batch={{batchSize}}\"\n"
yield $" | Some o,None -> $\"{t.TName}/list?offset={{offset}}\"\n"
yield $" | None ,None -> $\"{t.TName}/list\"\n"
yield $" |> x.Get\n"
// yield $" }}\n"
}
{| Storage = storageCode
ApiWireup = apiWireup
ApiDef = apiDef
Domain = domainCode
ServiceLayer = serviceLayer|}
let generateMasterServerApiFile (proj:string) (d:Db) =
stringBuffer {
yield $"module {proj |> titleCase}.Api.Server\n"
let dbCap = d.DName |> titleCase
let projCap = proj |> titleCase
yield $"open {projCap}.Api\n"
yield $"\n"
// Definition of the master Api type with functions
yield $"type {projCap}Api = {{\n"
for s in d.Schemas do
let schemaCap = titleCase s.SName
yield $" {schemaCap} : {schemaCap}.Api\n"
yield $"}}\n"
// Now wire it up with references to the subapis
yield $"let api = {{\n"
for s in d.Schemas do
let schemaCap = titleCase s.SName
yield $" {schemaCap} = {projCap}.Domain.{schemaCap}.Api.api\n"
yield $"}}"
yield $"\n"
}
let generateMasterClientApiFile (proj:string) (d:Db) =
let projCap = proj |> titleCase
stringBuffer {
yield $"namespace {projCap}.Api\n"
yield $"open Plough.WebApi.Client\n"
let dbCap = d.DName |> titleCase
//yield $"open {dbCap}.Api\n"
//yield $"\n"
// Definition of the master Api type with functions
yield "/// Top level api type definition\n"
yield $"type Api = {{\n"
for s in d.Schemas do
let schemaCap = titleCase s.SName
yield $" {schemaCap} : {schemaCap}.Api\n"
yield $"}}\n"
yield $"type {projCap} (client : ApiClient) =\n"
yield $" inherit ClientBuilder(Root(\"/api\", client))\n"
for s in d.Schemas do
let schemaCap = titleCase s.SName
yield $" member x.{schemaCap} = {schemaCap}.Client(x)\n"
yield $"\n"
}
let cleanSlash (path:string) =
path.Replace("\\","/")
let generate (proj:string) (folder:string) (d:Db) =
let projCap = titleCase proj
ensureFolder folder
let apiFolder = Path.Combine(folder, $"Api")
ensureFolder apiFolder
let generatedFileNames = [
for schema in d.Schemas do
let schemaCap = titleCase schema.SName
printfn $"Generating schema {schemaCap}"
let schemaSubFolder = $"{projCap}.Domain.{schemaCap}"
let schemaFolder = Path.Combine(folder, $"{projCap}.Domain.{schemaCap}")
ensureFolder schemaFolder
let code = emitDomain proj schema
let storageFile = Path.Combine(schemaSubFolder,$"{schemaCap}Storage.fs")
let serviceFile = Path.Combine(schemaSubFolder,$"{schemaCap}Service.fs")
let domainFile = Path.Combine(schemaSubFolder,$"{schemaCap}Domain.fs")
let apiDefFile = Path.Combine("Api",$"{schemaCap}Api.fs")
let apiWireupFile = Path.Combine("Api",$"{schemaCap}ApiWireup.fs")
yield domainFile
yield apiDefFile // api uses data structures from domainFile
yield storageFile
yield serviceFile
yield apiWireupFile
let fullStorageFile = Path.Combine(folder, storageFile)
printfn $" generating {fullStorageFile|> cleanSlash}"
File.WriteAllText(fullStorageFile, code.Storage)
let fullApiWireupFile = Path.Combine(folder, apiWireupFile)
printfn $" generating {fullApiWireupFile|> cleanSlash}"
File.WriteAllText(fullApiWireupFile, code.ApiWireup)
let fullServiceFile = Path.Combine(folder, serviceFile)
printfn $" generating {fullServiceFile|> cleanSlash}"
File.WriteAllText(fullServiceFile, code.ServiceLayer)
let fullDomainFile = Path.Combine(folder, domainFile)
printfn $" generating {fullDomainFile|> cleanSlash}"
File.WriteAllText(fullDomainFile, code.Domain)
let fullApiDef = Path.Combine(folder, apiDefFile)
printfn $" generating {fullApiDef|> cleanSlash}"
File.WriteAllText(fullApiDef, code.ApiDef)
// Generate master server API file to rule them all
let apiFile = Path.Combine("Api",$"{projCap}ServerApi.fs")
yield apiFile
let fullApiFile = Path.Combine(folder, apiFile)
printfn $" generating {fullApiFile|> cleanSlash}"
let apiFileContent = generateMasterServerApiFile proj d
File.WriteAllText(fullApiFile,apiFileContent)
let clientApiFile = Path.Combine(folder,"Api",$"{projCap}ClientApi.fs")
let apiFileContent = generateMasterClientApiFile proj d
File.WriteAllText(clientApiFile,apiFileContent)
]
let auxFileNames = [
let commonFolderName = "Common"
let fullCommonFolder = Path.Join(folder,commonFolderName)
if not <| Directory.Exists fullCommonFolder then
printfn $"Creating folder {fullCommonFolder}"
ensureFolder fullCommonFolder
let commonFile = Path.Join(commonFolderName,"Common.fs")
yield commonFile
let fullCommonFile = Path.Combine(folder, commonFile)
File.WriteAllText(fullCommonFile, commonFileSource proj)
let dbFile = Path.Join(commonFolderName,"Db.fs")
yield dbFile
let fullDbFile = Path.Combine(folder, dbFile)
File.WriteAllText(fullDbFile, dbFileSource proj)
]
let apiFiles,nonApiFiles = generatedFileNames |> List.partition (fun x -> x.StartsWith("Api/") || x.StartsWith("Api\\"))
let fileNames = auxFileNames @ nonApiFiles @ apiFiles
// might be a good idea long term to strongly type these different file types since we need different subsets
let domainFiles = nonApiFiles |> List.filter (fun x -> x.EndsWith("Domain.fs"))
// write out an fsproj file with all the individual files in it
let fsProjContent =
stringBuffer {
yield $"<Project Sdk=\"Microsoft.NET.Sdk\">\n"
yield $" <PropertyGroup>\n"
yield $" <TargetFramework>net7.0</TargetFramework>\n"
yield $" </PropertyGroup>\n"
yield $" <ItemGroup>\n"
for file in fileNames do
yield $" <Compile Include=\"{file|> cleanSlash}\" />\n"
yield $" </ItemGroup>\n"
yield $" <Import Project=\".paket\\Paket.Restore.targets\" />"
yield $"</Project>\n"
}
let fsProjPath = Path.Combine(folder, $"{projCap}.Backend.fsproj")
printfn $"Generating {fsProjPath|> cleanSlash}"
File.WriteAllText(fsProjPath, fsProjContent)
// write out a client api fsproj file with all the individual files in it
let apiProjContent =
stringBuffer {
yield $"<Project Sdk=\"Microsoft.NET.Sdk\">\n"
yield $" <PropertyGroup>\n"
yield $" <TargetFramework>net7.0</TargetFramework>\n"
yield $" </PropertyGroup>\n"
yield $" <ItemGroup>\n"
yield $" <Compile Include=\"../Common/Common.fs\" />\n"
for file in domainFiles do
yield $" <Compile Include=\"../{file|> cleanSlash}\" />\n"
for file in apiFiles do
if file.EndsWith("Wireup.fs") ||
file.EndsWith($"{projCap}ServerApi.fs") then
() // skip these files
else
yield $" <Compile Include=\"{file[4..]|> cleanSlash}\" />\n" // remove Api/ prefix
yield $" <Compile Include=\"{projCap}ClientApi.fs\" />\n"
yield $" </ItemGroup>\n"
yield $" <Import Project=\"..\\.paket\\Paket.Restore.targets\" />"
yield $"</Project>\n"
}
let apiProjPath = Path.Combine(folder,"Api",$"{projCap}Api.fsproj")
printfn $"Generating {apiProjPath|> cleanSlash}"
File.WriteAllText(apiProjPath, apiProjContent)
let compileTimeDbFile = Path.Combine(folder,compileTimeDbFile)
File.WriteAllText(compileTimeDbFile,$"Host=127.0.0.1;Username=read_write;Password=readwrite;Database={d.DName};Pooling=true")
let paketDepsFile = Path.Combine(folder, "paket.dependencies")
let paketRefsFile = Path.Combine(folder, "paket.references")
let configFolder = Path.Combine(folder, ".config")
ensureFolder configFolder
File.WriteAllText(Path.Combine(configFolder, "dotnet-tools.json"), dotnetToolsJson)
File.WriteAllText(paketDepsFile, paketFile)
File.WriteAllText(paketRefsFile, paketReferencesFile)
(* future work
// Using table api for bulk inserts
use table = new Db.enzyme.Tables.enzyme ()
let mutable counter = 0
for signature, accessions in enzymes do
if not <| lookup.ContainsKey(signature) then
let newRow =
table.NewRow(accessions = Some accessions, signature = signature, id_source = uniprotSource)
table.Rows.Add(newRow)
counter <- counter + 1
if counter % 1000 = 0 then printf $"... {counter}"
let updatedCount = table.Update(conn)
printfn $" added {updatedCount} records for enzymes in {elapsed ()} seconds"
for row in table.Rows do
lookup.Add(row.signature, row.id * 1<GlobalProteinId>)
// Example of bulk insert style, where we prefetch a range of ids, and write directly to the db (this is fastest / lowest level)
let! ids =
task {
use getIds = Db.CreateCommand<"
SELECT nextval('enzyme.protein_id_seq') FROM generate_series(1, @n)">(conn)
let! ids = getIds.TaskAsyncExecute(idsNeededCount)
return ids |> Seq.map Option.get |> Array.ofSeq
}
log "loadprots" $"reserved {ids.Length} ids"
use proteinWriter = conn.BeginBinaryImport(importProteinSeqCmd)
for protein in proteins do
if not <| cache.Protein.ContainsKey protein.Accession then
let idSource =
match protein.Origin with
| SwissProt -> swissprotSource
| Trembl -> tremblSource
| Uniprot -> uniprotSource
let md5 =
protein.Md5 |> Option.defaultValue (Common.md5String protein.Sequence)
proteinWriter.StartRow()
proteinWriter.Write(ids[added],NpgsqlTypes.NpgsqlDbType.Integer)
proteinWriter.Write(protein.Accession,NpgsqlTypes.NpgsqlDbType.Text)
proteinWriter.Write(protein.Name,NpgsqlTypes.NpgsqlDbType.Text)
proteinWriter.Write(protein.IsFragment,NpgsqlTypes.NpgsqlDbType.Boolean)
proteinWriter.Write(protein.IsPrecursor,NpgsqlTypes.NpgsqlDbType.Boolean)
proteinWriter.Write(md5,NpgsqlTypes.NpgsqlDbType.Text)
proteinWriter.Write(idSource,NpgsqlTypes.NpgsqlDbType.Integer)
proteinWriter.Write(protein.Sequence,NpgsqlTypes.NpgsqlDbType.Text)
cache.Protein.Add(protein.Accession, ProteinLookup(int(ids[added])* 1<GlobalProteinId>,md5))
*)