Models
Defining your model
Section titled “Defining your model”To define a model, start with buildModel(), then define your model’s Properties by repeated calls to .addProperty() on the return value:
import { buildModel, type ModelValue } from "benno";
export type ProfileThing = ModelValue<typeof profileModel>;export const profileModel = buildModel() .addProperty({ alias: "podRoot", property: "http://www.w3.org/ns/pim/space#storage", type: "url", required: true, }) .addProperty({ alias: "name", property: "http://xmlns.com/foaf/0.1/name", type: "string", required: false, });addProperty takes one argument, an object with the following possible keys:
property
Section titled “property”| Required | Type |
|---|---|
| Yes | String |
The URL of the Property you’re defining.
| Required | Type |
|---|---|
| Yes | Any of the following literal strings: "string", "stringWithLocale", "url", "integer", "decimal", "boolean", "date", "time", "datetime" |
The expected data type of the Property’s values. Values of different types will be ignored.
| Required | Type |
|---|---|
| No | String |
The key you want to use to access the Property’s value on the validated JavaScript object.
If left out, will be derived from property by taking either the fragment identifier (the bit after #) or, if there is no fragment identifier, the last path part. For example, "http://www.w3.org/ns/pim/space#storage" would default to "storage", and "http://xmlns.com/foaf/0.1/name" would default to "name".
required
Section titled “required”| Required | Type |
|---|---|
| No | Boolean |
Whether only Things with this Property set will be considered valid.
multipleValues
Section titled “multipleValues”| Required | Type |
|---|---|
| No | Boolean |
Whether to expect multiple values for this Property. If true, the resulting JavaScript object will have an array for this Property.
Fetching and storing data
Section titled “Fetching and storing data”When your model is defined, it offers several methods to interact with data on an actual Solid Pod.
.fetchOneFrom(url, options)
Section titled “.fetchOneFrom(url, options)”Fetches a Thing that matches the model definition.
Parameters
Section titled “Parameters”| Required | Type |
|---|---|
| Yes | String |
URL of the Thing to fetch.
options
Section titled “options”| Required | Type |
|---|---|
| No | { fetch?: typeof fetch } |
Optionally allows passing a custom fetch function. Primarily used to send authenticated requests.
Return value
Section titled “Return value”A Promise resolving to a Thing that matches the model definition, or null if no such Thing was found at the given URL.
.fetchAllFrom(url, options)
Section titled “.fetchAllFrom(url, options)”Fetches all Things in a given Dataset that match the model definition.
Parameters
Section titled “Parameters”| Required | Type |
|---|---|
| Yes | String |
URL of the Dataset containing the Things you want to fetch.
options
Section titled “options”| Required | Type |
|---|---|
| No | { fetch?: typeof fetch } |
Optionally allows passing a custom fetch function. Primarily used to send authenticated requests.
Return value
Section titled “Return value”A Promise resolving to the Things in the Dataset at the given URL that match the model definition, if any.
.saveAllTo(things, url, options)
Section titled “.saveAllTo(things, url, options)”Given Things that match the model definition, stores them at the given URL, replacing existing data.
Parameters
Section titled “Parameters”things
Section titled “things”| Required | Type |
|---|---|
| Yes | Array of Things matching the given model definition |
The Things you want to store in the user’s Pod.
| Required | Type |
|---|---|
| Yes | String |
URL you want to save the things to.
options
Section titled “options”| Required | Type |
|---|---|
| No | { fetch?: typeof fetch } |
Optionally allows passing a custom fetch function. Primarily used to send authenticated requests.
Return value
Section titled “Return value”A Promise resolve to the Things as saved to the Dataset at the given URL.
Compatibility with @inrupt/solid-client
Section titled “Compatibility with @inrupt/solid-client”Benno is built on top of @inrupt/solid-client, and can seamlessly interoperate with it. This makes it easy to adopt Benno in applications that already use solid-client.
To do so, models offer a number of methods that let you extra Things from Datasets, or convert solid-client Things into the plain JavaScript objects that are validated by Benno.
.getOneFromDataset(dataset, url)
Section titled “.getOneFromDataset(dataset, url)”Given a SolidDataset from solid-client, find a Thing with the given URL that matches the model definition, and return it.
Parameters
Section titled “Parameters”dataset
Section titled “dataset”| Required | Type |
|---|---|
| Yes | SolidDataset (from @inrupt/solid-client) |
A SolidDataset as provided by @inrupt/solid-client.
| Required | Type |
|---|---|
| Yes | String |
URL of the Thing to fetch from the given Dataset.
Return value
Section titled “Return value”A Thing matching the model definition, or null if no such Thing was present with the given URL in the given Dataset.
.getAllFromDataset(dataset)
Section titled “.getAllFromDataset(dataset)”Given a SolidDataset from solid-client, find all Things that match the model definition, and return them.
Parameters
Section titled “Parameters”dataset
Section titled “dataset”| Required | Type |
|---|---|
| Yes | SolidDataset (from @inrupt/solid-client) |
A SolidDataset as provided by @inrupt/solid-client.
Return value
Section titled “Return value”An array of Things matching the model definition.
.insertIntoDataset(dataset, things)
Section titled “.insertIntoDataset(dataset, things)”Given a SolidDataset from solid-client and one or more Things that match the model definition, return a new Dataset that includes the given Things.
Parameters
Section titled “Parameters”dataset
Section titled “dataset”| Required | Type |
|---|---|
| Yes | SolidDataset (from @inrupt/solid-client) |
A SolidDataset as provided by @inrupt/solid-client.
things
Section titled “things”| Required | Type |
|---|---|
| Yes | Thing (from @inrupt/solid-client), or an array of Things |
A Thing matching the model definition, or an array of such Things.
Return value
Section titled “Return value”A new SolidDataset that includes the given things.
.removeFromDataset(dataset, things)
Section titled “.removeFromDataset(dataset, things)”Given a SolidDataset from solid-client and one or more Things that match the model definition, return a new Dataset that does not include Things identified by the given Things’ URLs.
Parameters
Section titled “Parameters”dataset
Section titled “dataset”| Required | Type |
|---|---|
| Yes | SolidDataset (from @inrupt/solid-client) |
A SolidDataset as provided by @inrupt/solid-client.
things
Section titled “things”| Required | Type |
|---|---|
| Yes | Thing (from @inrupt/solid-client), or an array of Things |
A Thing matching the model definition, or an array of such Things.
Return value
Section titled “Return value”A new SolidDataset that does not include Things with the URLs of the given things.
.toThing(thing, options)
Section titled “.toThing(thing, options)”Given an object that matches the model definition, return a new Thing as used by @inrupt/solid-client that represents the same data.
Parameters
Section titled “Parameters”| Required | Type |
|---|---|
| Yes | An object matching the model definition. |
A Thing matching the model definition.
options
Section titled “options”| Required | Type |
|---|---|
| No | { name: string } or { url: string } |
An object with either the property name or the property url. If a name is given, the Thing’s URL will be based on the URL it gets saved on, with name forming the fragment identifier. If a url if given, that URL will be used. If neither is given, either the URL of the given Thing will be used (if known), or a random fragment identifier will be assigned.
Return value
Section titled “Return value”A Thing as used by @inrupt/solid-client.
.fromThing(thing)
Section titled “.fromThing(thing)”Given a Thing as provided by @inrupt/solid-client, return an object matching the model definition.
Parameters
Section titled “Parameters”| Required | Type |
|---|---|
| Yes | Thing (from @inrupt/solid-client) |
A Thing as provided by @inrupt/solid-client.
Return value
Section titled “Return value”An object matching the model definition, with the data contained in the given thing, or null if the data does not conform to the model definition.