FileStorage class
A file-based storage implementation that persists data to the local filesystem.
Example
const legacyStorage = new FileStorage('./data')
const storageV2 = new FileStorageV2('./data-v2')
await storageV2.write({
user123: { name: 'John', lastSeen: new Date().toISOString() }
})
const result = await storageV2.read(['user123'])
console.log(result.user123.value)
await storageV2.delete(['user123'])
- Extends
-
FileStorageInternals
Remarks
FileStorage stores all data in a single JSON file named state.json within a specified folder.
This implementation is suitable for development scenarios, local testing, and single-instance
deployments where shared state across multiple instances is not required.
Values remain a key-value JSON object in state.json. V2 keeps generated storage versions in
state.versions.json so a value's own eTag property is not changed. All operations use
synchronous file I/O wrapped in Promise interfaces. Use FileStorageV2 for the
structured StorageV2 contract.
V2 supports create-only, replace, and expected-version conditions.
The inherited constructor creates the folder and state.json when needed, then loads values
and V2 version metadata into memory. It creates state.versions.json on the first V2 write.
Warning
This implementation does not provide:
- Thread safety for concurrent access
- Atomic operations across multiple keys
- Scale for large datasets
For production scenarios requiring these features, use a database-backed storage implementation.
Constructors
| File |
Creates the shared file storage implementation. |
Inherited Properties
| expirations | The expiration time associated with each storage key, expressed as Unix time in milliseconds. |
| state | The stored values keyed by storage key. |
| versions | The current version associated with each storage key. |
Methods
| delete(string[]) | Deletes legacy items from |
| read(string[]) | Reads legacy items from |
| write(Store |
Writes legacy items to |
Inherited Methods
| flush() | Writes the current storage state and metadata to disk. |
| get |
Gets the current version for a key, creating one when necessary. |
| is |
Determines whether a stored value has expired. |
| remove(string) | Removes a value and its version and expiration metadata. |
Constructor Details
FileStorage(string)
Creates the shared file storage implementation.
new FileStorage(folder: string)
Parameters
- folder
-
string
The folder containing the storage files.
Inherited Property Details
expirations
The expiration time associated with each storage key, expressed as Unix time in milliseconds.
expirations: Record<string, number>
Property Value
Record<string, number>
Inherited From FileStorageInternals.expirations
state
The stored values keyed by storage key.
state: Record<string, unknown>
Property Value
Record<string, unknown>
Inherited From FileStorageInternals.state
versions
The current version associated with each storage key.
versions: Record<string, string>
Property Value
Record<string, string>
Inherited From FileStorageInternals.versions
Method Details
delete(string[])
Deletes legacy items from state.json.
function delete(keys: string[]): Promise<void>
Parameters
- keys
-
string[]
The keys to delete; must not be empty. Missing keys are ignored.
Returns
Promise<void>
read(string[])
Reads legacy items from state.json.
function read(keys: string[]): Promise<StoreItem>
Parameters
- keys
-
string[]
The keys to read; must not be empty.
Returns
Promise<StoreItem>
Existing items keyed by storage key. Missing keys are omitted.
write(StoreItem)
Writes legacy items to state.json.
function write(changes: StoreItem): Promise<void>
Parameters
- changes
- StoreItem
The items to write, keyed by storage key.
Returns
Promise<void>
Inherited Method Details
flush()
Writes the current storage state and metadata to disk.
function flush()
Inherited From FileStorageInternals.flush
getOrCreateVersion(string)
Gets the current version for a key, creating one when necessary.
function getOrCreateVersion(key: string): string
Parameters
- key
-
string
The storage key.
Returns
string
The current version.
Inherited From FileStorageInternals.getOrCreateVersion
isExpired(string)
Determines whether a stored value has expired.
function isExpired(key: string): boolean
Parameters
- key
-
string
The storage key.
Returns
boolean
true when the value has expired; otherwise, false.
Inherited From FileStorageInternals.isExpired
remove(string)
Removes a value and its version and expiration metadata.
function remove(key: string)
Parameters
- key
-
string
The storage key.
Inherited From FileStorageInternals.remove