Storable
Data store facilities
Import
_ <- fat.extra.Storable
file library, sdk library, enigma library, Error type, Text type, Void type and Method type are automatically imported with this import
Mixins
This library introduces two mixin types: Storable and EncryptedStorable
Storable
The Storable mixin provides methods for storing and retrieving objects in the filesystem using JSON serialization.
Prototype members
| Name | Signature | Brief |
|---|---|---|
| list | <> Keyset | Get list of ids for stored instances |
| load | (id: Text): Any | Load an object from the filesystem |
| save | <> Void | Save the current object instance |
| erase | <> Void | Delete the file associated with the id |
the
loadandsavemethods throwFileErroron failure
loadbackfills declared type defaults for keys absent from the stored record (stored values take precedence), so that records survive data model evolution, e.g. fields added after the record was written
erasealways targetsself.id, ignoring any arguments
EncryptedStorable
Extends Storable with encryption capabilities for safer data storage. Requires an implementation of getEncryptionKey method.
Usage example
_ <- fat.extra.Storable
# Define a type that includes Storable (or EncryptedStorable)
User = (
Storable # Include the Storable mixin
# EncryptedStorable # alternative implementation
# getEncryptionKey = (): Text -> '3ncryp1ptM3' # could get via KMS or config
## Argument slots
name: Text
email: Text
# Setters return new immutable instance copy with updated field
setName = (name: Text) -> self + User * { name }
setEmail = (email: Text) -> self + User * { email }
)
# Create a new user instance
newUser = User('Jane Doe', 'jane.doe@example.com')
# Save the new user
newUser.save
# Update a user's information and save the changes
updatedUser = newUser
.setName('Jane Smith')
.setEmail('jane.smith@example.com')
updatedUser.save
# List all saved users
userIds = User.list
# Load a user from the filesystem
userId = userIds(0) # ...or newUser.id
loadedUser = User.load(userId)
# Delete user's data from the filesystem
loadedUser.erase # always targets the instance's own id
Storable in Web Build
When using fry built with Emscripten (for example, when using FatScript Playground), this prototype uses embedded commands $storableSet, $storableGet, $storableList, and $storableRemove, which are only defined in the web build. Therefore, instead of using the conventional file system for storage, there is special support for using the browser's localStorage object.