1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/orm
2022-07-09 14:04:35 +03:00
..
orm_fn_test.v
orm_test.v
orm.v all: v.ast.byte_type -> v.ast.u8_type (#14964) 2022-07-06 13:32:30 +03:00
README.md doc: clear the explanation to 'sql: type' (#14915) 2022-07-09 14:04:35 +03:00

ORM

Attributes

Structs

  • [table: 'name'] sets a custom table name

Fields

  • [primary] sets the field as the primary key
  • [unique] sets the field as unique
  • [unique: 'foo'] adds the field to a unique group
  • [skip] field will be skipped
  • [sql: type] where type is a V type such as int or f64, or special type serial
  • [sql: 'name'] sets a custom column name for the field

Usage

struct Foo {
    id   int    [primary; sql: serial]
    name string [nonull]
}

Create

sql db {
    create table Foo
}

Drop

sql db {
    drop table Foo
}

Insert

var := Foo{
    name: 'abc'
}

sql db {
    insert var into Foo
}

Update

sql db {
    update Foo set name = 'cde' where name == 'abc'
}

Delete

sql db {
    delete from Foo where id > 10
}

Select

result := sql db {
    select from Foo where id == 1
}
result := sql db {
    select from Foo where id > 1 limit 5
}
result := sql db {
    select from Foo where id > 1 order by id
}