A Thing in ThingsDB is a key/value object where each key must be of type str (except for some reserved properties) and values can be any type.
For as long as a thing is used as a variable and not stored in a collection, a thing does not have an Id. Only when a thing is attached and therefore stored in a collection, ThingsDB will assign an Id to the #
property. This property is not accessible but can be read using the id() function. See examples below.
Function | Description |
---|---|
assign | Copies properties from a thing. |
clear | Remove all properties from a thing. |
copy | Copy a thing to a new thing. A Type is not preserved. |
del | Remove a property. |
dup | Duplicate a thing while preserving the Type. |
each | Iterate over all properties of a thing. |
equals | Test if two things are equal. |
filter | Return a new thing with properties that pass a given test. |
get | Return the value of a property on a thing by a given property name. |
has | Determine if a thing has a given property. |
id | Return id of the thing or nil when the thing is not stored. |
keys | Return a list with all the property names of a thing. |
len | Return the number of items. |
map | Return a list with the results of calling a provided closure on every property. |
remove | Remove properties that pass a given test and returns the removed values in a list. |
ren | Rename a property. |
restrict | Set or remove a value restriction on a thing. |
restriction | Return the restriction of the thing or nil when the thing is not restricted. |
search | Search for a given thing within a thing. |
set | Create a new or overwrite an existing property on a thing. |
to_type | Converts a thing into a typed thing. |
values | Return a list with all the property values of a thing. |
vmap | Returns a new thing with equal keys but values as a result of a given closure. |
wrap | Wrap the thing with a Type. |
// create a new thing
my_thing = {};
// keys following the naming rules can be created and accessed using the `.key` syntax
my_thing.color = 'blueish';
// Functions like `get` and `set` can also be used to create and read properties,
// and just as the bracket [..] notation they can be used to use keys which do not
// follow the naming convention, for example a key with spaces:
my_thing['with some spaces'] = 'almost any str is possible as key';
// my_thing does not have an Id as we are using it as a variable
assert (my_thing.id() == nil);
// attach to the collection will generate an Id
.my_thing = my_thing;
assert (my_thing.id() != nil);
Properties can be set immediately at initialization. Its quite common to wite a query with query params where the query param is used as key/value pairs in a thing. In this case it is possible to use a short syntax.
// this query is called with name='...'
// In this case writing "name: name," is not required and a "short" syntax can be used
my_thing = {
name:,
other: 'another key/value pair'
};