Skip to content

Commit 8216dd9

Browse files
authored
Merge pull request #10 from kefniark/feature/add-docs
Add Documentation
2 parents 369a220 + 2c919ac commit 8216dd9

18 files changed

Lines changed: 2658 additions & 23 deletions

File tree

.github/workflows/docs.yaml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Sample workflow for building and deploying a VitePress site to GitHub Pages
2+
#
3+
name: Deploy Doc to github pages
4+
5+
on:
6+
# Runs on pushes targeting the `main` branch. Change this to `master` if you're
7+
# using the `master` branch as the default branch.
8+
push:
9+
branches: [main]
10+
paths: ['docs/**']
11+
12+
# Allows you to run this workflow manually from the Actions tab
13+
workflow_dispatch:
14+
15+
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
16+
permissions:
17+
contents: read
18+
pages: write
19+
id-token: write
20+
21+
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
22+
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
23+
concurrency:
24+
group: pages
25+
cancel-in-progress: false
26+
27+
jobs:
28+
# Build job
29+
build:
30+
runs-on: ubuntu-latest
31+
steps:
32+
- name: Checkout
33+
uses: actions/checkout@v4
34+
with:
35+
fetch-depth: 0
36+
- name: Setup Node
37+
uses: actions/setup-node@v4
38+
with:
39+
node-version: 20
40+
cache: npm # or pnpm / yarn
41+
- name: Setup Pages
42+
uses: actions/configure-pages@v4
43+
- name: Install dependencies
44+
run: npm ci # or pnpm install / yarn install / bun install
45+
- name: Build with VitePress
46+
run: npm run docs:build # or pnpm docs:build / yarn docs:build / bun run docs:build
47+
- name: Upload artifact
48+
uses: actions/upload-pages-artifact@v3
49+
with:
50+
path: docs/.vitepress/dist
51+
52+
# Deployment job
53+
deploy:
54+
environment:
55+
name: github-pages
56+
url: ${{ steps.deployment.outputs.page_url }}
57+
needs: build
58+
runs-on: ubuntu-latest
59+
name: Deploy
60+
steps:
61+
- name: Deploy to GitHub Pages
62+
id: deployment
63+
uses: actions/deploy-pages@v4

.github/workflows/pull-request.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
on: [push, pull_request]
1+
on:
2+
push:
3+
branches: [main]
4+
pull_request:
5+
26
name: Test
37
jobs:
48
test:

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ coverage.*
1212
*.test
1313
bin/
1414
codegen/test/
15+
node_modules/
16+
docs/.vitepress/dist/
17+
docs/.vitepress/cache/
1518

1619
tests/**/client.go
1720
dist/

bench.log

Lines changed: 0 additions & 17 deletions
This file was deleted.

devenv.nix

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
pkgs.golangci-lint
1212
pkgs.gocover-cobertura
1313

14-
# Goose
15-
pkgs.goose
14+
# NodeJS (docs)
15+
pkgs.nodejs_20
1616

17+
# CLI Tools
18+
pkgs.goose
1719
pkgs.just
1820
];
1921

docs/.vitepress/config.mts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { defineConfig } from 'vitepress'
2+
3+
// https://vitepress.dev/reference/site-config
4+
export default defineConfig({
5+
title: "Mango SQL",
6+
description: "Documentation Website for mango sql (getting started, samples, playground, references, ...)",
7+
base: '/mango-sql/',
8+
themeConfig: {
9+
// https://vitepress.dev/reference/default-theme-config
10+
nav: [
11+
{ text: 'Home', link: '/' },
12+
{ text: 'Getting Started', link: '/getting-started' }
13+
],
14+
15+
sidebar: [
16+
{
17+
text: 'Getting Started',
18+
items: [
19+
{ text: 'Install CLI', link: '/getting-started/' },
20+
{ text: 'Client Usage', link: '/getting-started/usage' }
21+
]
22+
},
23+
{
24+
text: 'API Reference',
25+
items: [
26+
{ text: 'Mutations', link: '/api/mutations' },
27+
{ text: 'Queries', link: '/api/queries' },
28+
{ text: 'Transactions', link: '/api/transactions' },
29+
{ text: 'Custom Queries', link: '/api/custom-queries' },
30+
]
31+
},
32+
{
33+
text: 'Advanced Features',
34+
items: [
35+
// { text: 'ID Generation', link: '/api/mutations' },
36+
{ text: 'Soft Delete', link: '/features/soft-delete' },
37+
// { text: 'Logging', link: '/api/mutations' },
38+
]
39+
},
40+
// {
41+
// text: 'Examples',
42+
// items: [
43+
// { text: 'Markdown Examples', link: '/markdown-examples' },
44+
// { text: 'Runtime API Examples', link: '/api-examples' }
45+
// ]
46+
// }
47+
],
48+
49+
socialLinks: [
50+
{ icon: 'github', link: 'https://github.com/kefniark/mango-sql' }
51+
],
52+
53+
lastUpdated: {
54+
text: 'Updated at',
55+
formatOptions: {
56+
dateStyle: 'full',
57+
timeStyle: 'medium'
58+
}
59+
}
60+
}
61+
})

docs/api/custom-queries.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Custom Queries
2+
3+
One of the key features or Mango SQL is the ability to write your own queries.
4+
5+
Unlike most ORM or client RAW queries, these will be like the rest of MangoSQL fully typed and prepared
6+
7+
## queries.sql
8+
Create a `queries.sql` next to your schema, and here you can write your queries and name them to more easily recognize them in your codebase.
9+
10+
```sql [queries.sql]
11+
-- queryMany: UserNotDeleted
12+
SELECT *
13+
FROM users
14+
WHERE users.deleted_at IS NULL;
15+
```
16+
17+
## Usage
18+
19+
All the custom queries can be found under `db.Queries.*`
20+
21+
```go
22+
users, err := db.Queries.UserNotDeleted()
23+
```
24+
25+
And these custom queries also accept filters, so the `WHERE` condition can be changed dynamically at runtime.
26+
27+
```go
28+
users, err := db.Queries.UserNotDeleted(
29+
db.User.Query.Name.NotLike("%user3%"),
30+
)
31+
```

docs/api/mutations.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Database Client Mutations
2+
3+
::: info
4+
5+
For the following page, we will take as an example the following postgres schema. The usage should be similar regardless of the driver or settings.
6+
7+
::: code-group
8+
9+
```sql [Schema]
10+
CREATE TABLE users (
11+
id UUID PRIMARY KEY,
12+
name VARCHAR(64) NOT NULL,
13+
email VARCHAR(64) NOT NULL,
14+
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
15+
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
16+
deleted_at TIMESTAMP
17+
);
18+
```
19+
20+
```go [Generated Structs]
21+
// Base Model Returned by most API
22+
type UserModel struct {
23+
Id uuid.UUID `json:"id" db:"id"`
24+
Email string `json:"email" db:"email"`
25+
Name string `json:"name" db:"name"`
26+
CreatedAt time.Time `json:"created_at" db:"created_at"`
27+
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
28+
DeletedAt *time.Time `json:"deleted_at" db:"deleted_at"`
29+
}
30+
31+
// Input struct used only for creation
32+
type UserCreate struct {
33+
Email string `json:"email" db:"email"`
34+
Name string `json:"name" db:"name"`
35+
}
36+
37+
// Input struct used only for update
38+
type UserUpdate struct {
39+
Id uuid.UUID `json:"id" db:"id"`
40+
Email string `json:"email" db:"email"`
41+
Name string `json:"name" db:"name"`
42+
}
43+
```
44+
45+
```go [Generated Mutations]
46+
// Insert
47+
db.User.Insert(input UserCreate) (*UserModel, error)
48+
db.User.InsertMany(inputs []UserCreate) ([]UserPrimaryKeySerialized, error)
49+
50+
// Update
51+
db.User.Update(input UserUpdate) (*UserModel, error)
52+
db.User.UpdateMany(inputs []UserUpdate) ([]UserPrimaryKeySerialized, error)
53+
54+
// Upsert
55+
db.User.Upsert(input UserUpdate) (*UserModel, error)
56+
db.User.UpsertMany(inputs []UserUpdate) ([]UserPrimaryKeySerialized, error)
57+
58+
// Delete
59+
db.User.DeleteSoft(id UserPrimaryKey) error
60+
db.User.DeleteHard(id UserPrimaryKey)
61+
```
62+
63+
:::
64+
65+
## Insert
66+
67+
```go
68+
user, err := db.User.Insert(database.UserCreate{
69+
Name: "John Doe",
70+
Email: "john@email.com",
71+
})
72+
```
73+
74+
::: tip
75+
76+
If you have more than one entry to add to database, you also have a bulk insert alternative which takes a slices of input
77+
78+
```go
79+
userIds, err := db.User.InsertMany([]database.UserCreate{
80+
// ... many users
81+
})
82+
```
83+
84+
:::
85+
86+
## Update
87+
88+
```go
89+
user, err := db.User.Update(database.UserUpdate{
90+
Id: "00000000-0000-0000-0000-000000000000",
91+
Name: "John Doe 2",
92+
Email: "john@email.com",
93+
})
94+
```
95+
96+
::: tip
97+
98+
If you have more than one entry to add to database, you also have a bulk update alternative which takes a slices of input
99+
100+
```go
101+
userIds, err := db.User.UpdateMany([]database.UserUpdate{
102+
// ... many users
103+
})
104+
```
105+
106+
:::
107+
108+
## Upsert
109+
110+
Upsert stands for Insert in database if the entry doesn't exist yet, or update the existing entry. In both cases, the entry is returned.
111+
112+
This is really convenient for operations like user creation or session, when the api doesn't know if the user already exists in the database or not.
113+
114+
```go
115+
user, err := db.User.Upsert(database.UserUpdate{
116+
Id: "00000000-0000-0000-0000-000000000000",
117+
Name: "John Doe 2",
118+
Email: "john@email.com",
119+
})
120+
```
121+
122+
::: tip
123+
124+
If you have more than one entry to add to database, you also have a bulk upsert alternative which takes a slices of input
125+
126+
```go
127+
userIds, err := db.User.UpsertMany([]database.UserUpdate{
128+
// ... many users
129+
})
130+
```
131+
132+
:::
133+
134+
## Delete
135+
136+
```go
137+
// Default SQL Delete
138+
err = db.User.DeleteHard(3)
139+
```
140+
141+
And if you are using Soft Delete, a second method will we automatically generated
142+
143+
```go
144+
// Soft Delete backed by a Timestamp field
145+
err = db.User.DeleteSoft(3)
146+
```

0 commit comments

Comments
 (0)