/ Migrations / Schema mapping
Docs/Migrations/Schema mapping

Schema mapping

Documents don't have a fixed shape; tables do. The schema mapping is the bridge — one entry per field, saying which column it goes to and with what type.

How inference works

When you create a migration, NoSQLSync samples documents from the source and records every field path it sees, the types observed at that path, and how often it appears. Each field becomes a column:

  • Names are converted to snake_case — createdAt → created_at, userID → user_id. Characters that aren't valid in identifiers become underscores.
  • Nullability follows presence: a field missing from some documents becomes a nullable column.
  • Primary key defaults to the document ID — MongoDB's _id becomes an id column; for DynamoDB it is the table's partition key.

Mixed types

When a field has different types in different documents, NoSQLSync picks the type that can hold every value:

  • Integers mixed with decimals → double.
  • Anything mixed with an object or array → JSON.
  • Otherwise the widest type wins, in the order string → double → int → boolean → date → ObjectId. A field that is sometimes a string and sometimes a number becomes text.

Type reference

Source types map to target columns as follows:

SourcePostgreSQLMySQL / MariaDBSQL ServerBigQuerySnowflakeRedshiftClickHouse
ObjectIdVARCHAR(24)VARCHAR(24)NVARCHAR(24)STRINGVARCHAR(24)VARCHAR(24)String
stringTEXTTEXTNVARCHAR(MAX)STRINGTEXTVARCHAR(MAX)String
intINTEGERINTINTINT64NUMBERINTEGERInt64
doubleDOUBLE PRECISIONDOUBLEFLOATFLOAT64FLOATDOUBLE PRECISIONFloat64
booleanBOOLEANTINYINT(1)BITBOOLBOOLEANBOOLEANBool
dateTIMESTAMPTZDATETIMEDATETIME2TIMESTAMPTIMESTAMP_TZTIMESTAMPTZDateTime64(3,'UTC')
object / arrayJSONBJSONNVARCHAR(MAX)JSONVARIANTSUPERString
binaryBYTEALONGBLOBVARBINARY(MAX)BYTESBINARYVARBYTEString
null / unknownTEXTTEXTNVARCHAR(MAX)STRINGTEXTVARCHAR(MAX)String

Every column can be overridden in the Schema mapper — for example, narrowing a TEXT status field to VARCHAR(32).

i
SQL sources

When the source is a SQL database, copies within the same family (PostgreSQL → PostgreSQL, MySQL → MariaDB) keep the native column types. Cross-engine copies map by category and keep precision, length and time-zone awareness.

Nested objects and arrays

Each nested path can be stored in one of three ways. NoSQLSync suggests one based on your data; you can change it per path.

StrategyResultSuggested when
Flattenaddress.city → column address_cityThe object has the same few keys in at least ~90% of documents (up to 8 fields).
JSONOne JSONB / JSON / VARIANT column holding the whole valueThe shape varies, or the value is rarely queried.
Child tableA separate table with one row per array elementAn array of sub-documents you'll want to join or aggregate.

Child tables

Given an orders collection where each document has an items array, a child table looks like this:

PostgreSQL
CREATE TABLE orders (
  id          VARCHAR(24) PRIMARY KEY,
  customer_id VARCHAR(24),
  total       DOUBLE PRECISION,
  created_at  TIMESTAMPTZ
);

CREATE TABLE orders_items (
  orders_id   VARCHAR(24),   -- the parent order
  array_index INTEGER,       -- position in the array
  sku         TEXT,
  quantity    INTEGER,
  price       DOUBLE PRECISION,
  PRIMARY KEY (orders_id, array_index),
  FOREIGN KEY (orders_id) REFERENCES orders(id) ON DELETE CASCADE
);

Child tables are named <parent>_<path>. During live sync, whenever a parent document changes its child rows are replaced as a set, so element removals and reorders are reflected. They are available for MongoDB sources writing to PostgreSQL, MySQL or MariaDB. On other targets, arrays are stored as JSON.

Relationships and indexes

NoSQLSync looks for fields that reference other collections — ObjectId values and fields named like customerId or customer_id — and shows them as suggested relationships. On PostgreSQL and MySQL-family targets it creates an index on each, up to ten per table. Apart from child tables, foreign-key constraints between tables are not created, so loads never fail on ordering or orphaned references; add them yourself after cutover if you want them enforced.

Editing the mapping over the API

Introspection saves the result as the migration's mapping, keeping any columns you have already edited.

Re-run inference with nested choices
POST /migrations/:id/schema/introspect
{
  "nestedDecisions": {
    "address": "flatten",
    "items": "child-table",
    "metadata": "jsonb"
  }
}
Save a mapping
PUT /migrations/:id/schema
{
  "mappings": [
    { "sourceField": "_id",       "targetColumn": "id",         "sqlType": "VARCHAR(24)", "nullable": false, "isPrimaryKey": true },
    { "sourceField": "email",     "targetColumn": "email",      "sqlType": "TEXT",        "nullable": true,  "isPrimaryKey": false },
    { "sourceField": "createdAt", "targetColumn": "created_at", "sqlType": "TIMESTAMPTZ", "nullable": true,  "isPrimaryKey": false }
  ]
}

Exporting DDL and dbt models

From the Schema mapper you can download the mapping as:

  • SQL DDL — CREATE TABLE statements for the parent and all child tables, to review or apply yourself.
  • A dbt project — a zip with sources, staging models and basic tests for every table.

The same inference engine powers the free schema tools, so you can try it on a sample JSON file without an account.