Skip to content

Commit

Permalink
Update release link and rerun examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kaashyapan committed May 27, 2023
1 parent bb34a1a commit 85117a7
Show file tree
Hide file tree
Showing 34 changed files with 636 additions and 630 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.wasm
.vscode
.ionide
13 changes: 7 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Sqlc plugin for F#
## Codegen F# from SQL
`sqlc` is a command line program that generates type-safe database access code from SQL.
`sqlc` is a command line program that generates type-safe database access code from SQL.\
Sqlc documentation - https://sqlc.dev

**Inputs**
Expand All @@ -23,7 +23,7 @@ Sqlc documentation - https://sqlc.dev
## Why this ?
Type safe DB access in F# is tedious with manually written data structures.\
SqlHydra is a great dotnet tool to generate F# boiler plate. Works great with ORMs.\
I found I was writing a lot of custom SQL and wanted a solution that can generate 100% of the code.
I found I was writing a lot of custom SQL and wanted a solution that can generate 90% of the code.

This is intended for devs who prefer to write SQL by hand.

Expand Down Expand Up @@ -53,8 +53,9 @@ This is intended for devs who prefer to write SQL by hand.
"plugins": [
{
"name": "fsharp",
"process": {
"cmd": "/home/ubuntu/bin/sqlc-gen-fsharp"
"wasm": {
"url": "https://github.com/kaashyapan/sqlc-gen-fsharp/releases/download/latest/sqlc-gen-fsharp_1.0.0.wasm",
"sha256": "85a42e4f3d70feb8eb725d7406593114723ab474ecd4c74d1f9edb4867515ea4"
}
}
],
Expand Down Expand Up @@ -88,12 +89,12 @@ See the test folder for a sample setup.
`namespace`: The namespace to use for the generated code.\
`out`: Output directory for generated code.\
`emit_exact_table_names`: If true, use the exact table name for generated models. Otherwise, guess a singular form. Defaults to *false*.\
`async`: If true, all query functions generated will be async. Defaults to *false*.
`async`: If true, all query functions generated will be async. Defaults to *false*.\
`type_affinity`: If true, all DB integers (except Bigint) will be mapped to F#int. All DB floats will be mapped to F#double. Defaults to *false*.


### TODO
- Support for enumerated column types.
- Postgis type support
- Optionally generate classes instead of records
- Autogenerate basic CRUD without writing SQL
- Autogenerate basic CRUD without writing SQL
17 changes: 17 additions & 0 deletions examples/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[*.{fs,fsx}]
fsharp_multiline_bracket_style = aligned
max_line_length=120
indent_size=2
fsharp_max_function_binding_width=80
fsharp_max_array_or_list_width=100
fsharp_array_or_list_multiline_formatter=character_width
fsharp_max_if_then_else_short_width=120
fsharp_max_record_width=100
fsharp_record_multiline_formatter=character_width
fsharp_max_if_then_short_width=100
fsharp_multiline_block_brackets_on_same_column=true
fsharp_multi_line_lambda_closing_newline=true
fsharp_keep_max_number_of_blank_lines=1
fsharp_max_dot_get_expression_width=120
fsharp_max_function_binding_width=120
fsharp_max_value_binding_width=120
5 changes: 1 addition & 4 deletions examples/authors/mysql/Models.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@ namespace Authors

open System

type Author =
{ Id: int64
Name: string
Bio: string option }
type Author = { Id: int64; Name: string; Bio: string option }
85 changes: 0 additions & 85 deletions examples/authors/postgres/Queries.fs

This file was deleted.

15 changes: 0 additions & 15 deletions examples/authors/postgres/Readers.fs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@ namespace Authors
open System
open Npgsql

type Author =
{ Id: int64
Name: string
Bio: string option }
type Author = { Id: int64; Name: string; Bio: string option }
84 changes: 84 additions & 0 deletions examples/authors/postgresql/Queries.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Code generated by sqlc. DO NOT EDIT.
// version: sqlc v1.18.0

namespace Authors

open System
open Npgsql
open Npgsql.FSharp
open Authors.Readers

module Sqls =

[<Literal>]
let createAuthor =
"""
INSERT INTO authors (
name, bio
) VALUES (
@name, @bio
)
"""

[<Literal>]
let deleteAuthor =
"""
DELETE FROM authors
WHERE id = @id
"""

[<Literal>]
let getAuthor =
"""
SELECT id, name, bio FROM authors
WHERE id = @id LIMIT 1
"""

[<Literal>]
let listAuthors =
"""
SELECT id, name, bio FROM authors
ORDER BY name
"""

[<RequireQualifiedAccessAttribute>]
type DB(conn: string) =
// https://www.connectionstrings.com/npgsql

/// This SQL will insert a single author into the table
member this.createAuthor(name: string, ?bio: string) =

let parameters = [ ("name", Sql.text name); ("bio", Sql.textOrNone bio) ]

conn
|> Sql.connect
|> Sql.query Sqls.createAuthor
|> Sql.parameters parameters
|> Sql.executeNonQuery

/// This SQL will delete a given author
member this.deleteAuthor(id: int64) =

let parameters = [ ("id", Sql.int64 id) ]

conn
|> Sql.connect
|> Sql.query Sqls.deleteAuthor
|> Sql.parameters parameters
|> Sql.executeNonQuery

/// This SQL will select a single author from the table
member this.getAuthor(id: int64) =

let parameters = [ ("id", Sql.int64 id) ]

conn
|> Sql.connect
|> Sql.query Sqls.getAuthor
|> Sql.parameters parameters
|> Sql.executeRow authorReader

/// This SQL will list all authors from the authors table
member this.listAuthors() =

conn |> Sql.connect |> Sql.query Sqls.listAuthors |> Sql.execute authorReader
13 changes: 13 additions & 0 deletions examples/authors/postgresql/Readers.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Code generated by sqlc. DO NOT EDIT.
// version: sqlc v1.18.0

namespace Authors

open System
open Npgsql
open Npgsql.FSharp

module Readers =

let authorReader (r: RowReader) : Author =
{ Author.Id = r.int64 "id"; Name = r.text "name"; Bio = r.textOrNone "bio" }
7 changes: 4 additions & 3 deletions examples/authors/sqlc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"plugins": [
{
"name": "fsharp",
"process": {
"cmd": "/home/ubuntu/bin/sqlc-gen-fsharp"
"wasm": {
"url": "https://github.com/kaashyapan/sqlc-gen-fsharp/releases/download/latest/sqlc-gen-fsharp_1.0.0.wasm",
"sha256": "85a42e4f3d70feb8eb725d7406593114723ab474ecd4c74d1f9edb4867515ea4"
}
}
],
Expand All @@ -15,7 +16,7 @@
"engine": "postgresql",
"codegen": [
{
"out": "postgres",
"out": "postgresql",
"plugin": "fsharp",
"options": {
"namespace": "Authors",
Expand Down
5 changes: 1 addition & 4 deletions examples/authors/sqlite/Models.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@ namespace Authors
open System
open Fumble

type Author =
{ Id: int
Name: string
Bio: string option }
type Author = { Id: int; Name: string; Bio: string option }
Loading

0 comments on commit 85117a7

Please sign in to comment.