@mostajs/orm-copy-dataPart of the
@mostajs/ormecosystem — the multi-dialect ORM with one API across 13 databases. This post zooms in on one-shot data copy: backup, migration, seeding.
@mostajs/orm-copy-data copies data
fan-out: read one source (a database,
or a CSV / JSON / SQL file) and write it to N
heterogeneous destinations — another database, a SQL dump, JSON, CSV —
in a single operation.
mostajs-copy) and API
(copyData), cron-ready, with a
--commandFile for repeatable jobs.@mostajs/orm EntitySchema, so it copies
data, not opaque rows.npm i @mostajs/orm-copy-dataThe @mostajs ecosystem has two replication-adjacent
tools, and they don’t overlap:
@mostajs/replicator
— continuous CQRS replication and CDC (a master and live
slaves, change capture that keeps running).@mostajs/orm-copy-data —
one-shot copy: a backup tonight, a migration this afternoon, a
dev seed right now. It runs, it finishes, it exits.If you want “keep these two databases in sync forever”, that’s the replicator. If you want “take prod and put it there, now (and maybe also dump it to a file)”, that’s this package.
Most backup scripts target one output. Here,
destinations is an array — the same read
pass feeds every target. One nightly job can produce a hot standby
database and a portable SQL dump and a
JSON archive, without reading the source three times:
mostajs-copy \
--source db --source-dialect postgres --source-uri "$PROD_URI" \
--dest db --dest-dialect sqlite --dest-uri "./backups/$(date +%Y%m%d).sqlite" \
--dest sql-dump --dest-file "./backups/$(date +%Y%m%d).sql" \
--dest json --dest-file "./backups/$(date +%Y%m%d).json" \
--schemas entities.json --create-tables --batch-size 1000Four formats out of one source, one command. --dest is
repeatable; --create-tables provisions empty database
targets; --batch-size (default 500) bounds memory on big
tables.
Sources and destinations are symmetric —
db | csv | json | sql in,
db | csv | json | sql-dump out — so the same binary covers
the whole lifecycle:
# Cross-dialect migration : Postgres → SQLite (one shot)
mostajs-copy --source db --source-dialect postgres --source-uri "$PROD" \
--dest db --dest-dialect sqlite --dest-uri ./local.sqlite \
--schemas entities.json --create-tables
# Restore : SQL dump → a fresh database
mostajs-copy --source sql --source-file ./backup.sql \
--dest db --dest-dialect sqlite --dest-uri ./restored.sqlite \
--schemas entities.json --create-tables
# Seed dev : JSON → SQLite
mostajs-copy --source json --source-file ./seed.json \
--dest db --dest-dialect sqlite --dest-uri ./dev.sqlite --create-tables
# Export for BI : Postgres → CSV (one file per entity)
mostajs-copy --source db --source-dialect postgres --source-uri "$PROD" \
--dest csv --dest-file ./export/ --schemas entities.jsonNeed a subset? --entities User,Product,Order filters
which entities are copied (default: all).
--commandFile + cronFor anything you run more than once, move the flags into a file (one
flag per line, # comments, quoted values):
# backup-daily.conf — Postgres → SQLite + SQL dump + JSON
--source db
--source-dialect postgres
--source-uri "postgresql://user:pass@localhost:5432/prod"
--dest db --dest-dialect sqlite --dest-uri "./backups/daily.sqlite"
--dest sql-dump --dest-file "./backups/daily.sql"
--dest json --dest-file "./backups/daily.json"
--schemas .mostajs/generated/entities.json
--create-tables
--batch-size 1000# /etc/cron.d/mostajs-backup
0 2 * * * cd /app && npx mostajs-copy --commandFile backup-daily.conf >> /var/log/backup.log 2>&1Extra CLI flags override or extend the file, so one base config covers many variants.
The API is one call —
copyData({ source, destinations, schemas, options }) —
returning a CopyResult with per-entity stats:
import { copyData } from '@mostajs/orm-copy-data'
import { UserSchema, OrderSchema } from './schemas'
const result = await copyData({
source: { type: 'db', dialect: 'mongodb', uri: process.env.MONGO_URL },
destinations: [
{ type: 'db', dialect: 'postgres', uri: process.env.PG_URL, createTables: true },
{ type: 'sql-dump', file: './backup/dump.sql' },
{ type: 'json', file: './backup/data.json' },
],
schemas: [UserSchema, OrderSchema],
options: { batchSize: 500, onProgress: (e, c, t) => console.log(`${e}: ${c}/${t}`) },
})
console.log(result.totalCopied, result.totalErrors)That example is the headline trick in code: MongoDB →
Postgres + SQL dump + JSON, in one pass. The individual
loadFromDb/Json/Csv/Sql and
writeToDb/Sql/Json/Csv building blocks are exported too, if
you want to compose your own pipeline.
| Use case | How |
|---|---|
| Daily backup | mostajs-copy --commandFile backup.conf via cron |
| Cross-dialect migration | Oracle → PostgreSQL, one shot |
| Dev seeding | JSON → SQLite for local dev |
| Export for analytics | Postgres → CSV for BI tools |
| Disaster recovery | SQL dump → new database |
| Data archiving | Prod DB → JSON/CSV |
db destination needs --create-tables
(and createDb if the database itself is missing) when the
target is empty — otherwise writes fail on absent tables. No silent “it
worked” on a half-provisioned target.--schemas entities.json, auto-detected at
.mostajs/generated/entities.json). It is not a binary
pg_dump clone — it’s portable, schema-aware, and
cross-dialect, which a native dump is not.@mostajs/replicator.Because it reads and writes through @mostajs/orm
EntitySchema, the entities you back up or migrate are the
same ones your app runs on — one model end to end. Its continuous
counterpart is @mostajs/replicator;
the @mostajs/orm-cli
uses this engine internally for its Prisma-bootstrap migration.
npm i @mostajs/orm-copy-data # + the driver(s) for your source AND destination dialects
mostajs-copy --help@mostajs/orm — 13
databases, one API, zero codegenIf “back up and migrate any of 13 databases with one command” is useful, a ⭐ on GitHub helps — it’s the signal AI dev tools use to surface the package.
Tags: #nodejs #typescript
#database #backup #migration
#postgresql #mongodb #sqlite
#csv #json #etl #orm
#mostajs
Auteur : Dr Hamid MADANI drmdh@msn.com