2021-04-10 17:38:27 +03:00
|
|
|
# ORM
|
|
|
|
|
|
|
|
## Attributes
|
|
|
|
|
2021-04-15 10:53:43 +03:00
|
|
|
### Structs
|
|
|
|
|
2021-06-08 00:48:42 +03:00
|
|
|
- `[table: 'name']` sets a custom table name
|
2021-04-15 10:53:43 +03:00
|
|
|
|
2021-04-10 17:38:27 +03:00
|
|
|
### Fields
|
|
|
|
|
2021-04-12 00:57:25 +03:00
|
|
|
- `[primary]` sets the field as the primary key
|
|
|
|
- `[unique]` sets the field as unique
|
|
|
|
- `[unique: 'foo']` adds the field to a unique group
|
2022-08-05 10:27:19 +03:00
|
|
|
- `[nonull]` set the field as not null
|
2023-05-03 20:33:16 +03:00
|
|
|
- `[skip]` or `[sql: '-']` field will be skipped
|
2022-07-09 14:04:35 +03:00
|
|
|
- `[sql: type]` where `type` is a V type such as `int` or `f64`, or special type `serial`
|
2021-04-15 10:53:43 +03:00
|
|
|
- `[sql: 'name']` sets a custom column name for the field
|
2022-07-13 18:03:30 +03:00
|
|
|
- `[sql_type: 'SQL TYPE']` sets the sql type which is used in sql
|
2022-07-27 00:59:32 +03:00
|
|
|
- `[default: 'sql defaults']` sets the default value or function when create a new table
|
2022-09-06 13:12:37 +03:00
|
|
|
- `[fkey: 'parent_id']` sets foreign key for an field which holds an array
|
2021-04-10 17:38:27 +03:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
```v ignore
|
2023-05-03 20:33:16 +03:00
|
|
|
import time
|
|
|
|
|
2021-04-10 17:38:27 +03:00
|
|
|
struct Foo {
|
2022-07-19 18:29:09 +03:00
|
|
|
id int [primary; sql: serial]
|
|
|
|
name string [nonull]
|
|
|
|
created_at time.Time [sql_type: 'DATETIME']
|
|
|
|
updated_at string [sql_type: 'DATETIME']
|
|
|
|
deleted_at time.Time
|
2022-09-06 13:12:37 +03:00
|
|
|
children []Child [fkey: 'parent_id']
|
2021-04-10 17:38:27 +03:00
|
|
|
}
|
2022-09-06 13:12:37 +03:00
|
|
|
|
|
|
|
struct Child {
|
|
|
|
id int [primary; sql: serial]
|
|
|
|
parent_id int
|
|
|
|
name string
|
|
|
|
}
|
2021-04-10 17:38:27 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
### Create
|
|
|
|
|
|
|
|
```v ignore
|
|
|
|
sql db {
|
|
|
|
create table Foo
|
2023-05-03 20:33:16 +03:00
|
|
|
}!
|
2021-04-10 17:38:27 +03:00
|
|
|
```
|
|
|
|
|
2021-04-12 00:57:25 +03:00
|
|
|
### Drop
|
|
|
|
|
|
|
|
```v ignore
|
|
|
|
sql db {
|
|
|
|
drop table Foo
|
2023-05-03 20:33:16 +03:00
|
|
|
}!
|
2021-04-12 00:57:25 +03:00
|
|
|
```
|
|
|
|
|
2021-04-10 17:38:27 +03:00
|
|
|
### Insert
|
|
|
|
|
|
|
|
```v ignore
|
2023-05-03 20:33:16 +03:00
|
|
|
foo := Foo{
|
2022-07-19 18:29:09 +03:00
|
|
|
name: 'abc'
|
|
|
|
created_at: time.now()
|
2023-05-03 20:33:16 +03:00
|
|
|
updated_at: time.now()
|
2022-07-19 18:29:09 +03:00
|
|
|
deleted_at: time.now()
|
2022-09-06 13:12:37 +03:00
|
|
|
children: [
|
|
|
|
Child{
|
|
|
|
name: 'abc'
|
|
|
|
},
|
|
|
|
Child{
|
|
|
|
name: 'def'
|
|
|
|
},
|
|
|
|
]
|
2021-04-10 17:38:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sql db {
|
2023-05-03 20:33:16 +03:00
|
|
|
insert foo into Foo
|
|
|
|
}!
|
2021-04-10 17:38:27 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
### Update
|
|
|
|
|
|
|
|
```v ignore
|
|
|
|
sql db {
|
2022-09-06 13:12:37 +03:00
|
|
|
update Foo set name = 'cde', updated_at = time.now() where name == 'abc'
|
2023-05-03 20:33:16 +03:00
|
|
|
}!
|
2021-04-10 17:38:27 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
### Delete
|
|
|
|
```v ignore
|
|
|
|
sql db {
|
|
|
|
delete from Foo where id > 10
|
2023-05-03 20:33:16 +03:00
|
|
|
}!
|
2021-04-10 17:38:27 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
### Select
|
|
|
|
```v ignore
|
|
|
|
result := sql db {
|
|
|
|
select from Foo where id == 1
|
2023-05-03 20:33:16 +03:00
|
|
|
}!
|
2021-04-10 17:38:27 +03:00
|
|
|
```
|
|
|
|
```v ignore
|
|
|
|
result := sql db {
|
2022-09-06 13:12:37 +03:00
|
|
|
select from Foo where id > 1 && name != 'lasanha' limit 5
|
2023-05-03 20:33:16 +03:00
|
|
|
}!
|
2021-04-10 17:38:27 +03:00
|
|
|
```
|
|
|
|
```v ignore
|
|
|
|
result := sql db {
|
|
|
|
select from Foo where id > 1 order by id
|
2023-05-03 20:33:16 +03:00
|
|
|
}!
|
2021-06-08 00:48:42 +03:00
|
|
|
```
|
2022-07-27 00:59:32 +03:00
|
|
|
|
|
|
|
### Example
|
|
|
|
```v ignore
|
2023-01-13 18:02:32 +03:00
|
|
|
import db.pg
|
2022-07-27 00:59:32 +03:00
|
|
|
|
|
|
|
struct Member {
|
|
|
|
id string [default: 'gen_random_uuid()'; primary; sql_type: 'uuid']
|
|
|
|
name string
|
|
|
|
created_at string [default: 'CURRENT_TIMESTAMP'; sql_type: 'TIMESTAMP']
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
db := pg.connect(pg.Config{
|
|
|
|
host: 'localhost'
|
|
|
|
port: 5432
|
|
|
|
user: 'user'
|
|
|
|
password: 'password'
|
|
|
|
dbname: 'dbname'
|
2023-05-03 20:33:16 +03:00
|
|
|
})!
|
2022-07-27 00:59:32 +03:00
|
|
|
|
|
|
|
defer {
|
|
|
|
db.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
create table Member
|
2023-05-03 20:33:16 +03:00
|
|
|
}!
|
2022-07-27 00:59:32 +03:00
|
|
|
|
|
|
|
new_member := Member{
|
|
|
|
name: 'John Doe'
|
|
|
|
}
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
insert new_member into Member
|
2023-05-03 20:33:16 +03:00
|
|
|
}!
|
2022-07-27 00:59:32 +03:00
|
|
|
|
2023-05-03 20:33:16 +03:00
|
|
|
selected_members := sql db {
|
2022-07-27 00:59:32 +03:00
|
|
|
select from Member where name == 'John Doe' limit 1
|
2023-05-03 20:33:16 +03:00
|
|
|
}!
|
|
|
|
john_doe := selected_members.first()
|
2022-07-27 00:59:32 +03:00
|
|
|
|
|
|
|
sql db {
|
2023-05-03 20:33:16 +03:00
|
|
|
update Member set name = 'Hitalo' where id == john_doe.id
|
|
|
|
}!
|
2022-07-27 00:59:32 +03:00
|
|
|
}
|
2022-08-05 10:27:19 +03:00
|
|
|
```
|