Types
Inspectable ORM SQL with redacted parameter kinds.
text: string | |
|---|---|
params: [string] | |
unsafe: bool |
One immutable migration history status.
id: string | |
|---|---|
status: string | |
checksum: string | |
duration_ms: int |
An immutable ORM column descriptor.
An opaque handle: only std.orm can produce one, and its contents are not reachable from moon source.
use std.orm
use std.orm.Model
use std.sql.SqlPool
struct User {
id: Optional<int>,
email: string,
active: bool,
version: int,
}
fn user_model() -> Model<User> {
orm.model<User>(
"users",
[
orm.column("id").primary_key().generated(),
orm.column("email").unique().indexed(),
orm.column("active").default_sql("TRUE"),
orm.column("version").version(),
],
)
}
fn main() { println(user_model().field("email")) }_handle: NativeHandle | The native resource. |
|---|
A typed ORM codec pair.
An opaque handle: only std.orm can produce one, and its contents are not reachable from moon source.
_handle: NativeHandle | The native resource. |
|---|
A typed ORM model descriptor.
An opaque handle: only std.orm can produce one, and its contents are not reachable from moon source.
_handle: NativeHandle | The native resource. |
|---|
A typed ORM field descriptor.
An opaque handle: only std.orm can produce one, and its contents are not reachable from moon source.
_handle: NativeHandle | The native resource. |
|---|
A typed parameterized ORM predicate.
An opaque handle: only std.orm can produce one, and its contents are not reachable from moon source.
_handle: NativeHandle | The native resource. |
|---|
A typed immutable ORM query.
An opaque handle: only std.orm can produce one, and its contents are not reachable from moon source.
use std.orm
use std.orm.Model
use std.sql.SqlPool
struct User { id: Optional<int>, email: string, active: bool }
async fn active_users(pool: SqlPool, users: Model<User>) -> Result<[User], Error> {
let active = users.field("active")
let email = users.field("email")
await orm
.query(pool, users)
.where(active.eq(true))
.where(email.ilike("%@example.com"))
.order_by(email, "asc")
.limit(100)
.all()
}
fn main() {}_handle: NativeHandle | The native resource. |
|---|
A bounded ORM row stream.
An opaque handle: only std.orm can produce one, and its contents are not reachable from moon source.
_handle: NativeHandle | The native resource. |
|---|
A typed immutable ORM partial update.
An opaque handle: only std.orm can produce one, and its contents are not reachable from moon source.
use std.orm
use std.orm.Model
use std.sql.SqlPool
struct User { id: Optional<int>, email: string, active: bool }
async fn write_users(pool: SqlPool, users: Model<User>) -> Result<int, Error> {
let created = await orm.insert(
pool,
users,
User { id: none, email: "moon@example.com", active: true },
)?
let email = users.field("email")
let active = users.field("active")
let changed = await orm
.query(pool, users)
.where(email.eq(created.email))
.update(orm.changes(users).set(active, false))?
let _found = await orm.find(pool, users, created.id.unwrap())?
let _removed = await orm.delete(pool, users, created)?
ok(changed)
}
fn main() {}_handle: NativeHandle | The native resource. |
|---|
An explicit typed ORM relation.
An opaque handle: only std.orm can produce one, and its contents are not reachable from moon source.
use std.orm
use std.orm.Model
use std.sql.SqlPool
struct User { id: Optional<int>, email: string }
struct Post { id: Optional<int>, user_id: Optional<int>, title: string }
fn relation(pool: SqlPool, users: Model<User>, posts: Model<Post>) {
let posts_for_user = orm.has_many(
users,
posts,
users.field("id"),
posts.field("user_id"),
)
let joined = orm
.query(pool, users)
.left_join(posts, users.field("id"), posts.field("user_id"))
.select_pair(users, posts)
println(posts_for_user)
println(joined)
}
fn main() {}_handle: NativeHandle | The native resource. |
|---|
A typed ORM inner join.
An opaque handle: only std.orm can produce one, and its contents are not reachable from moon source.
_handle: NativeHandle | The native resource. |
|---|
A typed ORM left join.
An opaque handle: only std.orm can produce one, and its contents are not reachable from moon source.
_handle: NativeHandle | The native resource. |
|---|
A typed ORM two-model projection.
An opaque handle: only std.orm can produce one, and its contents are not reachable from moon source.
_handle: NativeHandle | The native resource. |
|---|
An immutable checksum-protected migration.
An opaque handle: only std.orm can produce one, and its contents are not reachable from moon source.
use std.orm
use std.orm.Model
use std.sql
use std.sql.SqlPool
struct User { id: Optional<int>, email: string }
async fn migrate(pool: SqlPool, users: Model<User>) -> Result<int, Error> {
let up = [orm.create_table(users)]
for index in orm.create_indexes(users) { up.push(index) }
let migration = orm.migration(
"001_users",
up,
[sql.raw("DROP TABLE users", [])],
)
println(orm.migration_dry_run([migration], "up"))
let applied = await orm.migrate(pool, [migration])?
let status = await orm.migration_status(pool, [migration])?
println(status[0].status)
ok(applied.len())
}
fn main() {}_handle: NativeHandle | The native resource. |
|---|