ucoreUon

UON (Unnarize Object Notation) database with streaming cursor.

Strict Path Resolution: Unnarize enforces strict sandboxing. All file paths used in this library are resolved relative to the directory of the executing script, regardless of where the unnarize command is run. Absolute paths are typically rebased or rejected to prevent sandbox escape.

Functions

FunctionReturnsDescription
load(path) bool Load and parse UON file schema
parse(source) bool Parse UON from string
get(table) cursor Get streaming cursor for table
next(cursor) map/nil Get next row from cursor
generate(schema, data) string Generate formatted UON string from maps

UON File Format

UON files have two sections: @schema for structure definition and @flow for data.

// sample.uon

@schema {
    users: [id, name, email, age],
    products: [id, name, price, stock]
}

@flow {
    users: [
        { id: 1, name: "Alice", email: "alice@example.com", age: 28 },
        { id: 2, name: "Bob", email: "bob@example.com", age: 34 }
    ],
    
    products: [
        { id: 1, name: "Laptop", price: 999, stock: 50 },
        { id: 2, name: "Mouse", price: 29, stock: 200 }
    ]
}

load(path)

Loads a UON file and parses its schema. Returns true on success.

var success = ucoreUon.load("data/sample.uon");
if (!success) {
    print("Failed to load UON file");
}

parse(source)

Parses UON content from a string (instead of file). Useful for embedded data or testing.

var uonContent = "@schema { users: [id, name] } @flow { users: [{ id: 1, name: \"Test\" }] }";
ucoreUon.parse(uonContent);

// Now query it like normal
var cursor = ucoreUon.get("users");
var user = ucoreUon.next(cursor);
print(user["name"]); // "Test"

get(table)

Returns a streaming cursor for the specified table. Uses lazy loading for memory efficiency.

var cursor = ucoreUon.get("users");

next(cursor)

Returns the next row as a map, or nil when end of table is reached.

var row = ucoreUon.next(cursor);
while (row) {
    print(row["name"] + " - " + row["email"]);
    row = ucoreUon.next(cursor);
}

Complete Example

// Load database
ucoreUon.load("data/sample.uon");

// Query users table
print("=== Users ===");
var userCursor = ucoreUon.get("users");
var user = ucoreUon.next(userCursor);

while (user) {
    print("[" + user["id"] + "] " + user["name"]);
    user = ucoreUon.next(userCursor);
}

// Query products table
print("=== Products ===");
var prodCursor = ucoreUon.get("products");
var prod = ucoreUon.next(prodCursor);

while (prod) {
    print(prod["name"] + " - $" + prod["price"]);
    prod = ucoreUon.next(prodCursor);
}

generate(schema, data)

Generates a formatted UON string from schema and data maps. Useful for creating UON files programmatically.

// Define schema
var schema = map();
schema["users"] = ["id", "name", "email"];

// Create data
var users = [];
var u1 = map();
u1["id"] = 1;
u1["name"] = "Alice";
u1["email"] = "alice@example.com";
push(users, u1);

var data = map();
data["users"] = users;

// Generate UON content
var content = ucoreUon.generate(schema, data);
ucoreSystem.writeFile("output.uon", content);

Example

./bin/unnarize examples/corelib/uon/demo.unna