> For the complete documentation index, see [llms.txt](https://db.coolstone.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://db.coolstone.dev/4/advanced/synchronization.md).

# Synchronization

## Overwrite from another Record <a href="#syncwith" id="syncwith"></a>

{% code lineNumbers="true" %}

```javascript
await db.get('tag').syncWith('_tag')
```

{% endcode %}

<table><thead><tr><th>Parameter</th><th>About</th><th>Type</th><th data-type="checkbox">Required</th></tr></thead><tbody><tr><td>_tag</td><td>The tag of the Record you want to get the content from.</td><td>String</td><td>true</td></tr></tbody></table>

## Watch a File

Watch the record for changes and emit events when the record is modified. The watcher will continue to run until you call the `stop` function returned by this method.

{% code lineNumbers="true" %}

```javascript
const { watcher, stop } = db.get('tag').watch()

for await (const event of watcher) {
       console.log(`Event type: ${event.eventType}`);
        console.log(`Filename: ${event.filename}`);
}

stop() // Stop watching
```

{% endcode %}

## Duplicate Entire Database

Copy all records from the current database to a new target directory, creating a new Dubnium instance for the target directory and writing each record's data to the corresponding file in the target directory.

{% code lineNumbers="true" %}

```javascript
db.copyTo('path')
```

{% endcode %}

<table><thead><tr><th>Parameter</th><th>About</th><th>Type</th><th data-type="checkbox">Required</th></tr></thead><tbody><tr><td>path</td><td>Path to new dir</td><td>String</td><td>true</td></tr></tbody></table>

## Replication / Continuous Sync

Set up real-time replication of records to a new target directory by listening for record creation, updates, and deletions in the current database and applying those changes to the target directory. This method creates a new Dubnium instance for the target directory and registers event listeners for 'create', 'edit', and 'delete' events emitted by the current database. When a record is created, edited, or deleted in the current database, the corresponding event listener will be triggered, and it will perform the same operation on the target database to keep it in sync. The method returns a function that can be called to stop the replication by removing the event listeners.

{% code lineNumbers="true" %}

```javascript
db.replicateTo('path')
```

{% endcode %}

<table><thead><tr><th>Parameter</th><th>About</th><th>Type</th><th data-type="checkbox">Required</th></tr></thead><tbody><tr><td>path</td><td>Path to new dir</td><td>String</td><td>true</td></tr></tbody></table>

### Stop Replication

<pre class="language-javascript" data-line-numbers><code class="lang-javascript"><strong>const { close } = db.replicateTo('path');
</strong>
close();
</code></pre>

### Flush Replication Queue

<pre class="language-javascript" data-line-numbers><code class="lang-javascript"><strong>const { flush } = db.replicateTo('path');
</strong>
await db.create('rep', { after: true });

await new Promise(r => flush().then(r));

await replica.has('rep');
</code></pre>

### Replication Status

<pre class="language-javascript" data-line-numbers><code class="lang-javascript"><strong>const { status, replica } = db.replicateTo('path');
</strong>
status.closed // true/false
status.pending // Number of tasks in queue
status.replicaDir // Dir of replica

replica // Dubnium instance at replica
</code></pre>
