v / vlib / orm
0 issues 1 contributor 4 branches 0 releases
Clone with HTTPS:

About

README
  V 99.7% 272k loc
  JavaScript 0.1% 344 loc
  C 0% 63 loc
  C++ 0% 95 loc
  Go 0% 63 loc
52 years ago
..

ORM

Attributes

Structs

Fields

Usage

struct Foo {
    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
}

Create

sql db {
    create table Foo
}

Drop

sql db {
    drop table Foo
}

Insert

var := Foo{
    name:       'abc'
    created_at: time.now()
    updated_at: time.now().str()
    deleted_at: time.now()
}

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
}

Example

import pg

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'
    }) or {
        println(err)
        return
    }

    defer {
        db.close()
    }

    sql db {
        create table Member
    }

    new_member := Member{
        name: 'John Doe'
    }

    sql db {
        insert new_member into Member
    }

    selected_member := sql db {
        select from Member where name == 'John Doe' limit 1
    }

    sql db {
        update Member set name = 'Hitalo' where id == selected_member.id
    }
}