# 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>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://db.coolstone.dev/4/advanced/synchronization.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
