# svyDataSeed

The `svyDataSeed` scope helps you move data between environments by exporting database tables to CSV files and importing them back. This is useful for:

* Bootstrapping a new cloud environment with reference data
* Setting up test environments with a known dataset
* Sharing a consistent data state across multiple developers

Seed files are stored as media files in your Servoy solution under `medias/dataseeds/`. Each database gets its own zip file containing one CSV per table.

## Exporting database tables to seed files

### `createDataSeedFiles()`

Exports all tables from a selected database. A dialog appears asking which database to export.

```javascript
scopes.svyDataSeed.createDataSeedFiles();
```

Optional parameters let you customize the export behavior:

| Parameter                   | Type            | Default         | Description                                                       |
| --------------------------- | --------------- | --------------- | ----------------------------------------------------------------- |
| `customPathToSvyCloudUtils` | `String`        | —               | Custom output path instead of the workspace                       |
| `returnDataseedFile`        | `Boolean`       | `true` in cloud | When `true`, returns the zip file instead of writing to workspace |
| `runFullTableRecalc`        | `Boolean`       | `false`         | Recalculates stored calcs before export (slow on large datasets)  |
| `noZip`                     | `Boolean`       | `false`         | Exports as plain CSV folder instead of a zip                      |
| `excludeTableNames`         | `Array<String>` | `['temp_%']`    | Table names to skip; supports `%` as a wildcard                   |
| `columnNameRegex`           | `RegExp`        | —               | Skip special character checks on matching column names            |

### `createDataSeedFile(selectedDB, ...)`

Exports a specific database by name. Use this when you don't want the dialog, or when running in an automated context.

```javascript
var zipFile = scopes.svyDataSeed.createDataSeedFile('my_database');
```

Has the same optional parameters as `createDataSeedFiles()`, plus:

| Parameter           | Type                                   | Description                                                                                 |
| ------------------- | -------------------------------------- | ------------------------------------------------------------------------------------------- |
| `additionalFilters` | `Array<{fieldName, value, required?}>` | Filter rows by column value. Set `required: true` to skip tables that don't have the column |
| `limitTableCount`   | `Number`                               | Maximum rows per table — useful for generating sample data                                  |

## Importing seed files into a database

### `runDataseedFromMedia()`

Imports all seed files found in the solution's media library. Run this during deployment or environment initialization.

```javascript
scopes.svyDataSeed.runDataseedFromMedia();
```

| Parameter                | Type                       | Default | Description                                                                   |
| ------------------------ | -------------------------- | ------- | ----------------------------------------------------------------------------- |
| `clearTablesNotInSeed`   | `Boolean`                  | `false` | Truncate tables that have no corresponding seed file                          |
| `dataseedFile`           | `Array<JSFile or JSMedia>` | —       | Import specific files instead of all media seeds                              |
| `dbNameToImport`         | `String`                   | —       | Required when `dataseedFile` is set — specifies which database to import into |
| `executeInTransaction`   | `Boolean`                  | `false` | Wrap the import in a single database transaction                              |
| `deleteExistingData`     | `Boolean`                  | `true`  | Truncate existing table data before import                                    |
| `statusCallBackFunction` | `Function`                 | —       | Callback called with `(status, tableName, current, total)` during import      |

The status callback receives these values for `status`:

* `'started'` — table import is beginning
* `'running'` — rows are being inserted
* `'done'` — table import is complete

### `getExistingDataseeds()`

Returns an array of `DataseedFile` objects for all seed files currently in the solution's media library.

```javascript
var seeds = scopes.svyDataSeed.getExistingDataseeds();
seeds.forEach(function(seed) {
    application.output(seed.dbName + ' — ' + seed.fileName);
});
```

## How seed files are structured

Each exported database becomes a zip file (or folder when `noZip` is `true`) containing one CSV file per table. The CSV uses comma delimiters and double-quote text qualifiers.

* Datetime columns are exported in UTC and re-converted on import
* Binary (media) columns are base64-encoded
* Tables prefixed with `temp_` are excluded by default
* Metadata tables and i18n tables are skipped automatically

Large tables are split into multiple CSV files (named `tablename#2.csv`, `tablename#3.csv`, etc.) when they exceed 200 MB.

{% hint style="info" %}
When importing, all existing data is truncated before inserting seed rows. Set `deleteExistingData: false` if you want to add rows without clearing first.
{% endhint %}

***

See also: [svyCloud](/reference/servoyextensions/modules/svycloudutils/svycloud.md) for environment detection and tenant session management, and [svyDeployUtils](/reference/servoyextensions/modules/svycloudutils/svydeployutils.md) for running database migrations alongside your seed import.


---

# 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://docs.servoy.com/reference/servoyextensions/modules/svycloudutils/svydataseed.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.
