@kbml-tentacles/core
v3.0.0
Published
Type-safe dynamic model factory for effector: contracts, models, queries, view-models with SSR scope isolation.
Maintainers
Readme
@kbml-tentacles/core
Type-safe dynamic model factory for effector. Declare a contract with a fluent chain builder, then instantiate as many independent models as you like — each with reactive instances, ORM-like queries, and full SSR scope isolation.
npm install effector @kbml-tentacles/coreQuick start
import { createContract, createModel, eq } from "@kbml-tentacles/core";
const todoContract = createContract()
.store("id", (s) => s<number>().autoincrement())
.store("title", (s) => s<string>())
.store("done", (s) => s<boolean>().default(false))
.event("toggle", (e) => e<void>())
.pk("id");
const todoModel = createModel({
contract: todoContract,
fn: ({ $done, toggle }) => {
$done.on(toggle, (d) => !d);
return {};
},
});
todoModel.create({ title: "Learn Tentacles" });
todoModel.create({ title: "Ship it" });
// Reactive ORM-like query
const $active = todoModel.query().where("done", eq(false)).$list;
// Mutate through the same query API
todoModel.query().where("id", eq(1)).update({ done: true });
todoModel.query().where("done", eq(true)).delete();What's in the box
createContract/createViewContract/createPropsContract— fluent builders for model schemas, ephemeral view-model state, and view-model props.createModel— turns a contract into a persistent instance manager: lazy field proxies, a unifiedmodel.create(data | data[])event,model.clear(), refs (one/many, withonDelete: "cascade" | "restrict" | "nullify"), and inverse fields. Mutations flow through the query API:model.query().where(...).update({ ... })and.delete().createViewModel— view-models with bare stores, props normalization, and lifecycle (mounted/unmounted). Singleton view-models viauseView(vm, "name")keep one instance shared across surfaces.- Reactive queries —
model.query().where(...).orderBy(...).limit(...)returning$list,$count,$ids,$first, plus the write terminalsupdate/delete. Operators:eq,neq,gt,gte,lt,lte,oneOf,contains,includes,startsWith,endsWith,matches. Incremental updates: O(1) on field mutations. - Contract utilities —
pick,omit,partial,required,mergework uniformly across model, view, props, and form chains. - SSR-safe by design — deterministic SIDs,
serialize/forkhydration, scope-aware$dataMapand autoincrement counters.
Documentation
- Tutorials: your first model
- How-to: define a contract, relate models with refs, query a collection, enable SSR, build a view-model
- Reference:
docs/reference/core
Peer dependencies
effector ^23.0.0
License
MIT © Nikita Lumpov
