lis-db
LIS (Local Interface for Storage), is a "key-value" frontend database build on indexedDB and with typescript support[1].
API
The library offers three functions (write, read and erase)
write
write( key:string, value:any ): Promise<void>[2] Can save persistent data with "key-value" structure, similar to localStorage API.
import { write } from 'lis-db'
write( 'hello', 'Hello World!' ).then( () => {
console.log( 'Data Saved' )
} )
read
read<T>( ...keys:string[] ): Promise<T[]>[2] is used to read data from the database. It takes as many keys as you want and its promise is resolved with an array of the saved values. You can set the array elements type with the generic type T[3]
import { read } from 'lis-db'
read<string>( 'hello' ).then( values:string[] => {
const [ hello ] = values
console.log( hello )
} )
Also you can use as operator with a tupla to get values with a different type
import { read, write } from 'lis-db'
write( 'num', 7 ).then( () => {
read( 'num', 'hello' ).then( values => {
type tupla = [ number, string ]
const [ num, hello ] = values as tupla
console.log( typeof num, typeof hello )
} )
} )
Warning: Undeclared values will be null
import { read } from 'lis-db'
read( 'x' ).then( values => {
const [ x ] = values
console.log( x )
} )
erase
erase( key:string ): Promise<void>[2] is used to erase data from the database
import { erase, read } from 'lis-db'
read<string>( 'hello' ).then( values => {
const [ hello ] = values
console.log( hello )
erase( 'hello' ).then( () => {
read<string>( 'hello' ).then( values => {
const [ hello ] = values
console.log( hello )
} )
} )
} )
- If do you use this library with javascript, types features will be incompatible.
- This async function will resolve its promise when finish its task.
- If you don't use it, the promise will be resolved with
unknown[] type