Skip to content

Commit d39faa4

Browse files
committed
Add more std library docs
1 parent 52d509e commit d39faa4

File tree

9 files changed

+949
-10
lines changed

9 files changed

+949
-10
lines changed

docs/std/_meta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"label": "Overview"
66
},
77
{
8-
"type": "file",
8+
"type": "dir",
99
"name": "db",
1010
"label": "std.db"
1111
},

docs/std/db.mdx

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

docs/std/db/_meta.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{
3+
"type": "file",
4+
"name": "pg",
5+
"label": "std.env.pg"
6+
},
7+
{
8+
"type": "file",
9+
"name": "mysql",
10+
"label": "std.env.mysql"
11+
},
12+
{
13+
"type": "file",
14+
"name": "sqlite",
15+
"label": "std.env.sqlite"
16+
},
17+
{
18+
"type": "file",
19+
"name": "redis",
20+
"label": "std.env.redis"
21+
}
22+
]

docs/std/db/mysql.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# std.db.mysql

docs/std/db/pg.mdx

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
# std.db.pg
2+
3+
The `std.db.pg` module provides functions for interacting with PostgreSQL databases, including executing queries, handling transactions, and mapping results to objects.
4+
5+
## Core Functions
6+
7+
## query(sql: string, ...bindings: any)
8+
9+
Executes a SQL query and returns the results as an array of objects.
10+
11+
**Parameters**:
12+
- `sql`: The SQL query string
13+
- `bindings`: Optional parameters to bind to the query
14+
15+
**Return Type**: `Array<Object>`
16+
17+
**Example**:
18+
```rust
19+
use std.db.pg;
20+
21+
let results = pg.query("SELECT * FROM users WHERE age > $1", 18);
22+
// Returns an array of objects representing the query results
23+
```
24+
25+
## query_as(class: Class, sql: string, ...bindings: any)
26+
27+
Executes a SQL query and maps the results to instances of the specified class.
28+
29+
**Parameters**:
30+
- `class`: The class to map results to
31+
- `sql`: The SQL query string
32+
- `bindings`: Optional parameters to bind to the query
33+
34+
**Return Type**: `Array<Instance>`
35+
36+
**Example**:
37+
```rust
38+
use std.db.pg;
39+
40+
class User {
41+
id: number;
42+
name: string;
43+
age: number;
44+
}
45+
46+
let users = pg.query_as(User, "SELECT * FROM users WHERE age > $1", 18);
47+
// Returns an array of User instances
48+
```
49+
50+
## begin_transaction()
51+
52+
Starts a new database transaction. Returns a Transaction object that provides methods for executing queries within the transaction.
53+
54+
**Return Type**: `Transaction`
55+
56+
**Example**:
57+
```rust
58+
use std.db.pg;
59+
60+
let tx = pg.begin_transaction();
61+
// Returns a Transaction object
62+
```
63+
64+
## Transaction Methods
65+
66+
The following methods are available on Transaction objects returned by `begin_transaction()`.
67+
68+
### query(sql: string, ...bindings: any)
69+
70+
Executes a SQL query within the transaction and returns the results as an array of objects.
71+
72+
**Parameters**:
73+
- `sql`: The SQL query string
74+
- `bindings`: Optional parameters to bind to the query
75+
76+
**Return Type**: `Array<Object>`
77+
78+
**Example**:
79+
```rust
80+
use std.db.pg;
81+
82+
let tx = pg.begin_transaction();
83+
let results = tx.query("SELECT * FROM users WHERE age > $1", 18);
84+
```
85+
86+
### query_as(class: Class, sql: string, ...bindings: any)
87+
88+
Executes a SQL query within the transaction and maps the results to instances of the specified class.
89+
90+
**Parameters**:
91+
- `class`: The class to map results to
92+
- `sql`: The SQL query string
93+
- `bindings`: Optional parameters to bind to the query
94+
95+
**Return Type**: `Array<Instance>`
96+
97+
**Example**:
98+
```rust
99+
use std.db.pg;
100+
101+
class User {
102+
id: number;
103+
name: string;
104+
age: number;
105+
}
106+
107+
let tx = pg.begin_transaction();
108+
let users = tx.query_as(User, "SELECT * FROM users WHERE age > $1", 18);
109+
```
110+
111+
### commit()
112+
113+
Commits the transaction, saving all changes made within the transaction.
114+
115+
**Return Type**: `nil`
116+
117+
**Example**:
118+
```rust
119+
use std.db.pg;
120+
121+
let tx = pg.begin_transaction();
122+
// Execute queries...
123+
tx.commit();
124+
```
125+
126+
### rollback()
127+
128+
Rolls back the transaction, discarding all changes made within the transaction.
129+
130+
**Return Type**: `nil`
131+
132+
**Example**:
133+
```rust
134+
use std.db.pg;
135+
136+
let tx = pg.begin_transaction();
137+
// Execute queries...
138+
tx.rollback();
139+
```
140+
141+
## Usage Examples
142+
143+
### Basic Query Example
144+
145+
```rust
146+
use std.db.pg;
147+
148+
// Simple query
149+
let users = pg.query("SELECT * FROM users WHERE age > $1", 18);
150+
151+
// Iterate through results
152+
for user in users {
153+
println(user.name);
154+
}
155+
```
156+
157+
### Transaction Example
158+
159+
```rust
160+
use std.db.pg;
161+
162+
// Start transaction
163+
let tx = pg.begin_transaction();
164+
165+
// Update user balance
166+
tx.query("UPDATE users SET balance = balance - $1 WHERE id = $2", 100, 123);
167+
168+
// Insert transaction record
169+
tx.query("INSERT INTO transactions (user_id, amount) VALUES ($1, $2)", 123, -100);
170+
171+
// Commit if successful
172+
tx.commit();
173+
```
174+
175+
### Typed Query Example
176+
177+
```rust
178+
use std.db.pg;
179+
180+
// Define user class
181+
class User {
182+
id: number;
183+
name: string;
184+
email: string;
185+
}
186+
187+
// Query users and map to class
188+
let users = pg.query_as(User, "SELECT * FROM users WHERE active = true");
189+
190+
// Access typed properties
191+
for user in users {
192+
println(`${user.name} <${user.email}>`);
193+
}
194+
```
195+
196+
### Parameter Binding Example
197+
198+
```rust
199+
use std.db.pg;
200+
201+
// Query with different parameter types
202+
let results = pg.query(
203+
"SELECT * FROM users WHERE name = $1 AND age > $2 AND active = $3",
204+
"John", // string
205+
25, // number
206+
true // boolean
207+
);
208+
```
209+
210+
### Array Parameter Example
211+
212+
```rust
213+
use std.db.pg;
214+
215+
// Query with array parameter
216+
let user_ids = [1, 2, 3, 4];
217+
let users = pg.query("SELECT * FROM users WHERE id = ANY($1)", user_ids);
218+
```
219+
220+
### Date Parameter Example
221+
222+
```rust
223+
use std.db.pg;
224+
225+
// Query with date parameter
226+
let users = pg.query(
227+
"SELECT * FROM users WHERE created_at > $1",
228+
"2023-01-01" // Date string in YYYY-MM-DD format
229+
);
230+
```

0 commit comments

Comments
 (0)