---
description: These functions either create new documents or modify existing ones.
---

# Document Functions

***

## .check(id)

Checks if the given document exists or not. Returns a Boolean value.

```javascript
const fileCheck = db.check("1234");
console.log(fileCheck);
```

***

## .size()

Returns the size of the current, or target, directory that is currently in use.

```javascript
const dirSize = db.size();
console.log(dirSize);
```

***

## .delete(id)

Deletes the specified document **permanently**.

```javascript
db.delete("1234");
```

***

## .insert(Object)

Creates a new document in the default or given directory. Unless serialization is enabled, ensure an "id" key is provided.

```javascript
db.insert({id: "237", name: "Dan T.", cash: 12.09})
```

***

## .insertBulk(\[ Objects ])

Creates a new document containing multiple objects. An "id" key is required to create a bulk document as serialization is not supported.

```javascript
db.insertBulk([ 
    {id: "01134"}, 
    {name: "Mike", nickname: "Mickey"},
    {balance: 0}
])
```

***

## .update({id, key, child, change, math?})

Updates a key and/or child key within the given document. The "math" parameter, which defaults to _false_, only supports simple math such as addition or subtraction

```javascript
db.update({
    id: "1234",
    key: "name",
    child: "age",
    change: 21,
    math: false
});
```

{% hint style="info" %}
Boolean "change" arguments **must** be converted to a String in order to properly apply it. Otherwise, it leads to a logic error.

If you wish to remove a key or child key from an object, you can declare the "change" as "**undefined",** following the same logic as Booleans.
{% endhint %}

***

## .mupdate(id, \[ Objects ])

Updates multiple keys within the given document. The Objects contained within the Array follow the same format as **.update()**, excluding the "id" parameter.

```javascript
db.mupdate("1234", [
    {
        key: "name",
        child: "middle",
        change: "Ray",
    },
    {
        key: "active",
        change: "true"
    }
])
```

***

## .append({ id, key, value,  position })

Appends a new key and value to an object within a bulk document. Provide a "position", an integer corresponding to which object in the array, to append the new key and value to that specific object.

```javascript
db.append({
    id: "01134", 
    key: "lastName", 
    value: "Milton", 
    position: 1
});
```

{% hint style="info" %}
The "position" argument defaults to the first object in an array.
{% endhint %}

***

## .replicate(id, {to, from, force?})

Copies a document from the directory specified in the "from" argument. The suffix "\_rep" is added to the document name if the file already exists and "force" is set to **true**.

{% code overflow="wrap" %}
```javascript
db.replicate("1010", {
    to: __dirname, 
    from: `${__dirname}/data/`
});
```
{% endcode %}

***

## .set(id, {data})

Overwrite the specified document with new data. The previous data will be stored in a cache as long as the process is still running and the function doesn't overwrite another document.

```javascript
db.set("3", {animal: "Rhino", "age": 10, active: true});
```

{% hint style="warning" %}
_**Everything**_ is overwritten. This includes the "id" key in the target document. Don't specify one unless you absolutely need to.&#x20;
{% endhint %}

***

## .undo()

Undo the most recent changes made to a document with **.set()** using the data stored in the cache.

```javascript
db.undo();
```
