v/vlib/orm
2024-06-10 11:54:25 +03:00
..
orm.v orm: ilike for case insensitive text search 2024-06-10 11:54:25 +03:00
orm_create_and_drop_test.v ci: mark all orm tests as flaky (mainly to reduce the false positives on windows+MSVC, due to 'cannot open file' sqlite3.obj, when it is rebuilt in parallel) 2023-12-12 12:11:19 +02:00
orm_custom_operators_test.v ci: mark all orm tests as flaky (mainly to reduce the false positives on windows+MSVC, due to 'cannot open file' sqlite3.obj, when it is rebuilt in parallel) 2023-12-12 12:11:19 +02:00
orm_fk_test.v ci: mark all orm tests as flaky (mainly to reduce the false positives on windows+MSVC, due to 'cannot open file' sqlite3.obj, when it is rebuilt in parallel) 2023-12-12 12:11:19 +02:00
orm_fn_calls_test.v ci: mark all orm tests as flaky (mainly to reduce the false positives on windows+MSVC, due to 'cannot open file' sqlite3.obj, when it is rebuilt in parallel) 2023-12-12 12:11:19 +02:00
orm_fn_test.v v,breaking: add ability to read enum, fn, interface and sumtype attributes in compile-time, change builtin StructAttribute to VAttribute (#21149) 2024-03-31 09:14:33 +03:00
orm_insert_reserved_name_test.v ci: mark all orm tests as flaky (mainly to reduce the false positives on windows+MSVC, due to 'cannot open file' sqlite3.obj, when it is rebuilt in parallel) 2023-12-12 12:11:19 +02:00
orm_insert_test.v orm: fix orm insert issue if table missing [Issue : #20017] (#20580) 2024-02-10 05:52:09 +03:00
orm_interface_test.v ci: mark all orm tests as flaky (mainly to reduce the false positives on windows+MSVC, due to 'cannot open file' sqlite3.obj, when it is rebuilt in parallel) 2023-12-12 12:11:19 +02:00
orm_last_id_test.v ci: mark all orm tests as flaky (mainly to reduce the false positives on windows+MSVC, due to 'cannot open file' sqlite3.obj, when it is rebuilt in parallel) 2023-12-12 12:11:19 +02:00
orm_mut_db_test.v ci: mark all orm tests as flaky (mainly to reduce the false positives on windows+MSVC, due to 'cannot open file' sqlite3.obj, when it is rebuilt in parallel) 2023-12-12 12:11:19 +02:00
orm_null_test.v ci: mark all orm tests as flaky (mainly to reduce the false positives on windows+MSVC, due to 'cannot open file' sqlite3.obj, when it is rebuilt in parallel) 2023-12-12 12:11:19 +02:00
orm_option_array_test.v ci: mark all orm tests as flaky (mainly to reduce the false positives on windows+MSVC, due to 'cannot open file' sqlite3.obj, when it is rebuilt in parallel) 2023-12-12 12:11:19 +02:00
orm_option_time_test.v orm: fix orm_option_time_test.v after 2d0ed2c made insert in parent tables with child ones missing fail 2024-02-11 10:02:36 +02:00
orm_references_test.v ci: mark all orm tests as flaky (mainly to reduce the false positives on windows+MSVC, due to 'cannot open file' sqlite3.obj, when it is rebuilt in parallel) 2023-12-12 12:11:19 +02:00
orm_result_test.v ci: mark all orm tests as flaky (mainly to reduce the false positives on windows+MSVC, due to 'cannot open file' sqlite3.obj, when it is rebuilt in parallel) 2023-12-12 12:11:19 +02:00
orm_sql_or_blocks_test.v ci: mark all orm tests as flaky (mainly to reduce the false positives on windows+MSVC, due to 'cannot open file' sqlite3.obj, when it is rebuilt in parallel) 2023-12-12 12:11:19 +02:00
orm_string_interpolation_in_where_test.v ci: mark all orm tests as flaky (mainly to reduce the false positives on windows+MSVC, due to 'cannot open file' sqlite3.obj, when it is rebuilt in parallel) 2023-12-12 12:11:19 +02:00
orm_test.v time: update unix time acces, fix issues related to deviating unix times (#21293) 2024-04-17 00:33:37 +03:00
README.md builtin: str.last_index(); pref: hide-auto-str; 2024-03-28 18:26:30 +03:00

ORM

Null

Use option fields in V structs for fields which can be NULL. Regular, non-option fields are defied as NOT NULL when creating tables.

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] or [sql: '-'] field will be skipped
  • [sql: type] where type is a V type such as int or f64
  • [sql: serial] lets the DB backend choose a column type for a auto-increment field
  • [sql: 'name'] sets a custom column name for the field
  • [sql_type: 'SQL TYPE'] sets the sql type which is used in sql
  • [default: 'raw_sql] inserts raw_sql verbatim in a "DEFAULT" clause when create a new table, allowing for values like CURRENT_TIME
  • [fkey: 'parent_id'] sets foreign key for an field which holds an array

Usage

import time

@[table: 'foos']
struct Foo {
    id          int         @[primary; sql: serial]
    name        string
    created_at  time.Time   @[default: 'CURRENT_TIME']
    updated_at  ?string     @[sql_type: 'TIMESTAMP']
    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 defaults to none
    // deleted_at defaults to none
    children: [
        Child{
            name: 'abc'
        },
        Child{
            name: 'def'
        },
    ]
}

foo_id := sql db {
    insert foo into Foo
}!

If the id field is marked as serial and primary, the insert expression returns the database ID of the newly added object. Getting an ID of a newly added DB row is often useful.

When inserting, [sql: serial] fields, and fields with a [default: 'raw_sql'] attribute are not sent to the database when the value being sent is the default for the V struct field (e.g., 0 int, or an empty string). This allows the database to insert default values for auto-increment fields and where you have specified a default.

Update

sql db {
    update Foo set updated_at = time.now() where name == 'abc' && updated_at is none
}!

Note that is none and !is none can be used to select for NULL fields.

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
	}!
}