Class Collection - Trebossalol/localdb GitHub Wiki

What is a collection ?

A collection is a list, which holds objects which look the same. In this example down below we'll create a collection which holds users.

Declaration

class Collection<DocType = DocumentLike>

Create a collection using your user interface

interface User {
    username: string
    coins: number
}

const accounts = new Collection<User>('accounts', {
  // Options
})  

Options

folderPath : string - A path to a folder, where all collection details should be stored (note: This will be automatically specified by your Database instance!)

normalize : (document) => string - A callback which will normalize the output json string (default: (doc) => JSON.stringify(doc, null, 3))

docIdGenerator : (document) => string - A callback which must generate a uniqe ID for every document (default: (_doc) => (Math.random() * (999999 - 111111) + 111111).toString())

fileNameGenerator : (collection) => string - A callback which will generate the filename of the collection (note: This will be automatically specified by your Database instance!)

onErrorBehaviour : string - A string which specifies the behaviour of the collection if an error occues CREATE_BACKUP_AND_OVERWRITE -> Creates a backup of the error-version and creates a new empty collection OVERWRITE -> Overwrites the error-version to an empty collection (Data will be lost!) LOG_ERROR -> Throws an exception undefined -> LOG_ERROR

onRestartBehaviour : string - A string which specifies the behaviour of the collection on init OVERWRITE -> Overwrites the collection (Data will be lost!) undefined -> Normal behaviour

Methods

You can see every method here, or in your prefered code editor, because this library has almost fully typescript support!

⚠️ **GitHub.com Fallback** ⚠️