> For the complete documentation index, see [llms.txt](https://rednexie.gitbook.io/permadb/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rednexie.gitbook.io/permadb/javascript.md).

# Javascript

vanilla javascript

PermaDB also provides a vanilla javascript library to make data management easier.

The vanilla javascript library is based on IndexedDB.

To use the library in the clientside, you first have to import the required script.

To do that, you can add the script tag to your html:

```html
<script src="https://cdn.jsdelivr.net/npm/perma.db@0.0.4/lib/browser/script.js"></script>
```

### new PermaDB(db\_name, \[options])

**Returns** [**database**](https://github.com/Rednexie/perma.db/blob/main/DOCS.md#database)

By creating a new instance of the PermaDB class, you create and open a new IndexedDB database.\
You can customize the options of the database with arguments.

**db\_name**

*String*

The database name to be created. Default is `perma.db`.

## database

```javascript
const database = new PermaDB();
```

`new PermaDB(db_name): database`

### Database Methods

All methods of PermaDB's javascript library are asynchronous.

* [database#get](#get-key)
* [database#set](#set-key-value)
* [database#update](#update-key-value)
* [database#delete](#delete-key)
* [database#has](#has-key)
* [database#delete](#delete-key)
* [database#type](#type-key)
* [database#all](#all)
* [database#clear](#clear)
* [database#removeDB](#removedb)
* [database#keys](#keys)
* [database#values](#values)
* [database#object](#object)
* [database#length](#length)

#### set(key, value)

`await set(key, value): value`

Stores the value in the database, so you can access it with the key after.

*key* String

*value* Buffer || String || Date || Number || null

#### get(key)

`await get(key): value`

Gets the value with the given key in the database.

*key* String

*returns* Buffer || String || Date || Number || null

#### update(key, value)

`update(key, value): changed`

If the given key exists in the database, changes its value and returns `true`. If it doesn't exist, returns `false`.

*key* String

*value* Buffer || String || Date || Number || null\
\
*returns* Boolean

#### delete(key)

`delete(key): success`

Checks if the given key exists in the database. If it does, deletes it and returns `true`. Else returns `false`.

*key* String

*returns* Boolean

#### has(key)

`has(key) : exists`

Checks if the given key exists in the database. If it does, returns `true`. Else returns `false`.

*key* String\
\
*returns* Boolean

#### type(key)

`type(key): typeof value`

Checks if the given key exists in the database. If it does, returns the type of the value. Else returns `null`.

*key* String\
\
*returns* String

#### all()

`all(): [{key, value}]`

Gets all key-value pairs in the database inside an array.

*returns* Array

#### clear()

`clear()`

Deletes all key-value pairs in the database.

*returns* Boolean

#### removeDB()

`removeDB()`

Deletes the database after closing it.

*returns* Boolean

#### keys()

`keys(): [...keys]`

Returns the array of the `keys`stored in the database.\
\
*returns* Array

#### values()

`values(): [...values]`

Returns the array of the values stored in the database.\
\
*returns* Array

#### object()

`objectSync(): {key: value}`

Returns the key-value pairs as a Javascript Object.

*returns* Object

#### length()

`length(): length`

Gets the number of existing key-value pairs in the database.\
\
*returns* Boolean
