Entities
An Entity is a typed data object: you declare a static schema, and instances validate, coerce and serialize their data against it.
Defining an entity
Extend Entity and declare a static schema. Each field descriptor
supports type, required, id, and entityClass (for nested entities):
import { Entity } from "/lib/altseven/dist/a7.js";
export class Todo extends Entity {
static schema = {
todoID: { type: "string", id: true },
title: { type: "string", required: true },
done: { type: "boolean" },
created: { type: "date" },
tags: { type: "array" },
priority: { type: "integer" }
};
}
Supported types: string, integer, float, boolean, date, array,
object (optionally with an entityClass for nested entities).
Creating instances
The constructor validates required fields, defines getters/setters for every schema field, and coerces values:
const todo = new Todo( {
todoID: "t1",
title: "Write the docs",
created: "2026-11-16T09:00:00Z", // string → Date
done: 1 // 1/0 → true/false
} );
todo.title; // "Write the docs"
todo.created; // Date object
todo.done; // true
todo.id; // "t1" — .id returns the field marked id: true
todo.validate(); // true
Validation is strict: assigning a wrong type throws
(Invalid type for property done. Expected boolean, but got string.), missing
required fields throw at construction, and date fields accept either Date
objects or parseable date strings.
Composite keys
Marking several fields with id: true defines a composite primary key. The
service layer joins the values into a single string key ("user42|post7"), so
entities with multi-field identities cache and bind correctly:
export class Enrollment extends Entity {
static schema = {
userID: { type: "integer", id: true },
courseID:{ type: "integer", id: true },
grade: { type: "string" }
};
}
Nested entities
An object field with an entityClass is instantiated automatically:
export class Comment extends Entity {
static schema = {
commentID: { type: "string", id: true },
author: { type: "object", entityClass: User } // another Entity class
};
}
const c = new Comment( { commentID: "c1", author: { userID: 7, name: "Rob" } } );
c.author instanceof User; // true — the plain object was coerced
Custom getters and setters
Subclasses can define their own getters/setters for schema fields; the Entity base detects them and wraps rather than replaces them — useful for computed values or side effects on assignment.
Serialization
Entities convert to and from plain objects at the API boundary:
todo.toFlatObject();
// { todoID: "t1", title: "Write the docs", done: true, created: Date, ... }
todo.fromFlatObject( { title: "Rewrite the docs" } ); // validates via setters
Service uses fromFlatObject() internally to load JSON responses into entity
instances, so your endpoints just return plain JSON.
Where entities live
Entities are pure data — no network, no DOM. Instantiate them in services (the service layer does it for you), and pass them around as values. Views render entity data from their state; they don’t own entities.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.