Object-relational Mapping

Examples in V
// V has a built-in ORM that supports Postgres, and will soon support MySQL and SQLite.

// The benefits of V ORM:

// One syntax for all SQL dialects. Migrating to a different database becomes much easier.
// Queries are constructed with V syntax. There's no need to learn another syntax.
// Safety. It's impossible to construct a SQL query with an injection.
// Compile time checks. No more typos that can only be caught at runtime.
// Readability and simplicity. You don't need to manually parse the results and construct objects.
struct Customer { // struct name has to be the same as the table name for now
	id int // an integer id must be the first field
	name string
	nr_orders int
	country string

db := pg.connect(db_name, db_user)

// select count(*) from Customer
nr_customers := db.select count from Customer
println('number of all customers: $nr_customers')

// V syntax can be used to build queries
// db.select returns an array
uk_customers := db.select from Customer where country == 'uk' && nr_orders > 0
for customer in uk_customers {
	println('$customer.id - $customer.name')

// by adding `limit 1` we tell V that there will be only one object
customer := db.select from Customer where id == 1 limit 1
println('$customer.id - $customer.name')

// insert a new customer
new_customer := Customer{name: 'Bob', nr_orders: 10}