O Language/Web/Database

From OnixOS

Database

Database programming has three common steps: open a connection, define a model, and apply schema changes.

Connection and model

def db = database("sqlite3", "example.db")
def users = model(db, "users", {
  "id": "int",
  "name": "text",
  "email": "text"
})

Schema and data

migrate(users)
create(users, {"name": "Ada", "email": "[email protected]"})
def result = fetch(users, {"name": "Ada"})
update(users, {"name": "Ada"}, {"email": "[email protected]"})
delete(users, {"name": "Ada"})

Use drop to remove a table and truncate to remove its rows. Use query for parameterized SQL and prefer parameters for external values.

def result = query(users, "SELECT * FROM users LIMIT ?", 10)

In a web project, olang web create model creates connections/main.olm when needed and adds the model and connection to lib/loader.olm.