1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/orm
2023-08-10 05:39:32 +03:00
..
orm_create_and_drop_test.v orm: allow structs without the id field, more flexible primary keys (#18140) 2023-05-09 00:21:42 +03:00
orm_custom_operators_test.v all: like operator/keyword for V ORM (#18020) 2023-04-23 03:40:54 +03:00
orm_fn_calls_test.v orm: enforce that queries always return a Result, a query-resulting array can be used as a V array in place. (#17871) 2023-04-04 08:23:06 +03:00
orm_fn_test.v
orm_insert_reserved_name_test.v orm: enforce that queries always return a Result, a query-resulting array can be used as a V array in place. (#17871) 2023-04-04 08:23:06 +03:00
orm_insert_test.v orm: fix inserting sequential values (id=0), in tables with an i64 primary field (#18791) 2023-07-05 23:25:22 +03:00
orm_interface_test.v orm: enforce that queries always return a Result, a query-resulting array can be used as a V array in place. (#17871) 2023-04-04 08:23:06 +03:00
orm_last_id_test.v orm: enforce that queries always return a Result, a query-resulting array can be used as a V array in place. (#17871) 2023-04-04 08:23:06 +03:00
orm_mut_db_test.v orm: enforce that queries always return a Result, a query-resulting array can be used as a V array in place. (#17871) 2023-04-04 08:23:06 +03:00
orm_result_test.v orm: enforce that queries always return a Result, a query-resulting array can be used as a V array in place. (#17871) 2023-04-04 08:23:06 +03:00
orm_sql_or_blocks_test.v db.sqlite: make functions return results, breaking change (#19093) 2023-08-10 05:39:32 +03:00
orm_string_interpolation_in_where_test.v orm: enforce that queries always return a Result, a query-resulting array can be used as a V array in place. (#17871) 2023-04-04 08:23:06 +03:00
orm_test.v checker,orm: skip compile-time error msg for fields tagged with [skip] and [sql: '-'] (#18700) 2023-06-29 06:43:24 +03:00
orm.v orm: fix inserting sequential values (id=0), in tables with an i64 primary field (#18791) 2023-07-05 23:25:22 +03:00
README.md docs, orm: update examples (#18106) 2023-05-03 20:33:16 +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
  • [nonull] set the field as not null
  • [skip] or [sql: '-'] 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
  • [sql_type: 'SQL TYPE'] sets the sql type which is used in sql
  • [default: 'sql defaults'] sets the default value or function when create a new table
  • [fkey: 'parent_id'] sets foreign key for an field which holds an array

Usage

import time

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
    children    []Child     [fkey: 'parent_id']
}

struct Child {
    id        int    [primary; sql: serial]
    parent_id int
    name      string
}

Create

sql db {
    create table Foo
}!

Drop

sql db {
    drop table Foo
}!

Insert

foo := Foo{
    name:       'abc'
    created_at: time.now()
    updated_at: time.now()
    deleted_at: time.now()
    children: [
        Child{
            name: 'abc'
        },
        Child{
            name: 'def'
        },
    ]
}

sql db {
    insert foo into Foo
}!

Update

sql db {
    update Foo set name = 'cde', updated_at = time.now() 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 && name != 'lasanha' limit 5
}!
result := sql db {
    select from Foo where id > 1 order by id
}!

Example

import db.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'
	})!

	defer {
		db.close()
	}

	sql db {
		create table Member
	}!

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

	sql db {
		insert new_member into Member
	}!

	selected_members := sql db {
		select from Member where name == 'John Doe' limit 1
	}!
	john_doe := selected_members.first()

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