Class Database - Trebossalol/localdb GitHub Wiki
class Database<Entries extends EntryInterface<Entries>>
First, it is recommended to create interfaces for your entries
interface User {
username: string;
}
interface Settings {
some_bool: boolean
some_string: string
}
Those are important for typescript, that you can autocomplete support.
Create an instance and pass the interfaces to the Entry you want.
To create an entry in your db, just pass an object in the Database#entries
section and include your entry-names and the corresponding entry type (Collection, Map, ...).
const db = new Database('mydb', {
absolutePath: './db',
entries: {
accounts: new Collection<User>('accounts'),
settings: new Map<Settings>('settings', { template: DefaultSettings })
}
})
absolutePath
:string
- A path to a folder, where all database details should be stored
entries
:EntryInterface<E>
- An object which declares your entries, example:{ accounts: new Collection<User>('accounts'), settings: new Map<Settings>('settings', { template: DefaultSettings }) }
collectionConfig
:CollectionConfig
- Configuration which will be applied to each entry
normalize
:(document) => string
- A callback which will normalize the output json string (default:(doc) => JSON.stringify(doc, null, 3)
)
async start()
- A method which will initalize the db, if this method isn't called before accessing entries, there will be an error
async access<N extends keyof Entries>(name: N)
: A method to access an entry, parametername
must be valid name of your entries. This method returns the entry (Collection, Map, ...)