Typed records
Generate Dart classes for your collections from a schema snapshot with dart run vaultbase:types.
dart run vaultbase:types writes a Dart file with a class per collection, from
the same schema snapshot the TypeScript vb-types reads. No Node needed.
# Capture the schema once and commit itcurl -H "Authorization: Bearer $ADMIN_TOKEN" \ https://api.example.com/api/v1/admin/migrations/snapshot \ > vaultbase-schema.json
# Anyone can regenerate from the committed filedart run vaultbase:types --schema=vaultbase-schema.json# writes lib/vaultbase_schema.g.dart; --out=<path> to change it
# Or straight from a dev serverdart run vaultbase:types --url=https://api.example.com --admin-token=$ADMIN_TOKENThe file is formatted for your package, so a dart format check passes.
Regenerate it when the schema changes.
What you get
Section titled “What you get”import 'vaultbase_schema.g.dart';
final page = await vb.collections.posts.list(sort: '-created');for (final post in page.items) { post.title; // String post.views; // num? post.tags; // List<String> post.publishAt; // DateTime? post.place; // ({double lat, double lng})?}
await vb.collections.posts.create( PostsCreate(title: 'Hi', rating: 4.5, pinned: false).toJson());
final site = await vb.collections.siteSettings.get(); // a singletonPer collection:
-
XRecord extends RecordModel, a getter per field. Required fields are non-null ('',0orfalsewhen absent, for example whenfields=left them out); optional fields are nullable; lists are never null. TheRecordModelhelpers (getString,expandOne, …) still work. -
XCreatefor base and auth collections: the fields to create a record with, required ones required.toJson()leaves out what you didn’t set and encodes dates and geo points the way the server takes them. Auth records are made by signing up:await vb.auth.collection('users').register(u.email, u.password, data: u.toJson()); -
vb.collections.<name>:Collection<XRecord>, orSingleton<XRecord>for singletons.
Field names become camelCase (publish_at → publishAt). One that clashes
with a Dart keyword or a RecordModel member gets Field added: class →
classField, data → dataField. Password fields are never generated.
| Field type | Getter type |
|---|---|
| text, email, url, editor, color, relation, select, file | String |
| number | num |
| bool | bool |
| date, autodate | DateTime? |
select / file with multiple | List<String> |
| vector | List<double> |
| geoPoint | ({double lat, double lng})? |
| json | Object? |
| group | List<XFieldItem>, or XField? with multiple: false |
A group field gets a class of its own, named after the collection and the
field: OrdersItemsItem for the items of orders.items, OrdersAddress for a
single-object address. It has a final member per subfield (typed as above;
required ones non-null), the item’s id (null on a new item), fromJson and
toJson. Send items back with their ids to keep them:
final order = await vb.collections.orders.create(OrdersCreate(items: [ const OrdersItemsItem(sku: 'A-1', qty: 2),]).toJson());
final items = order.items; // List<OrdersItemsItem>items.first.id; // set by the serverawait vb.collections.orders.update(order.id, { 'items': [for (final i in items) i.toJson(), const OrdersItemsItem(sku: 'B-2', qty: 1).toJson()],});