DEV Community

Hector Angel Gomez Robaina
Hector Angel Gomez Robaina

Posted on

Chaca 2.2: mock data generation, now in the browser

Chaca is a TypeScript library for generating realistic, relational mock data — schemas that reference each other, exported to JSON, CSV, SQL, Java, Python and more. Until now, it only ran in Node. Version 2.2 changes that.

🌐 Chaca is now isomorphic

You can install and use Chaca in any browser app — React, Vue, Svelte, vanilla JS. No configuration needed: your bundler automatically resolves the browser-safe build through the package exports map. The package now ships both ESM and CommonJS builds, each with its own type definitions.

The key piece is transform, which serializes your data to any supported format fully in memory — no filesystem involved. That makes it the natural way to get file contents in the browser, for example to trigger a download:

import { chaca, modules } from "chaca";

const schema = chaca.schema({
  id: chaca.key(() => modules.id.uuid()),
  name: () => modules.person.firstName(),
});

const [file] = await schema.transform(50, {
  filename: "users",
  format: "json",
});

// file.filename -> "users.json"
// file.content  -> serialized string, ready to download or display
Enter fullscreen mode Exit fullscreen mode

transform supports every format: json, csv, yaml, postgresql, sqlite, mysql, java, python, typescript and javascript.

Note: export (which writes files to disk) is not available in the browser build and throws a descriptive error pointing you to transform. In Node, export keeps working exactly as before.

🗄️ New export formats: SQLite and MySQL

Two new SQL targets join postgresql, sharing all of its options (keys, uniques, nulls, refs, generateIds, declarationOnly, ...):

await dataset.export({
  filename: "data",
  location: "./data",
  format: "sqlite", // or "mysql"
});
Enter fullscreen mode Exit fullscreen mode

Both are also available from the CLI (chaca sqlite, chaca mysql) and generate scripts with native types for each engine:

  • SQLite: PRAGMA foreign_keys = ON, INTEGER PRIMARY KEY for generated ids, TEXT for strings and ISO dates, REAL for floats.
  • MySQL: INT AUTO_INCREMENT PRIMARY KEY, VARCHAR(255)/TEXT, DATETIME(3) for dates, table-level FOREIGN KEY constraints, and reserved words safely quoted with backticks.

🧯 A single Errors namespace

Every Chaca exception is now grouped under one namespace, so error handling is easier to discover:

import { Errors } from "chaca";

try {
  await schema.export(/* ... */);
} catch (e) {
  if (e instanceof Errors.TryRefANoKeyFieldError) {
    /* handle the specific error */
  }
  if (e instanceof Errors.ChacaError) {
    /* catch-all for any chaca error */
  }
}
Enter fullscreen mode Exit fullscreen mode

The existing flat exports keep working, and three error classes announced in 2.0.0 but never actually exported are finally reachable.

🪛 40+ fixes

This release closes a long list of bugs. Some highlights:

  • chaca.pick and isArray with { min, max } could never reach max — the upper bound is now inclusive, as documented.
  • The Java exporter generated code that didn't compile (missing semicolons, wrong float literals, invalid bigint types, reserved-word identifiers). It compiles now.
  • PostgreSQL export: single quotes in strings are properly escaped, all-null columns get a TEXT type, dates keep their time as TIMESTAMP, and reserved-word identifiers are double quoted.
  • An array ref field that runs out of values no longer pads the array with null — it stops at the references it could actually resolve. Check your code if you relied on the padding; as a bonus, large isArray refs are noticeably faster.
  • modules.date.past/soon/between no longer mutate the Date you pass in.
  • modules.internet.email no longer produces pedro@yahoo.com.com, and modules.datatype.hexadecimal no longer outputs the letter G.
  • CSV missing values are empty cells instead of the literal string undefined, and YAML no longer silently drops bigint values.

The full list — including a few small behavior changes worth checking (ethereumAddress now includes the 0x prefix, color.rgb CSS output no longer carries the hex prefix, and the continent typos Oseania/Antartica were corrected) — is in the changelog.

Try it

npm install chaca
Enter fullscreen mode Exit fullscreen mode

Docs and guides at chaca.app. If you build something with it — or hit a bug — issues and PRs are welcome on GitHub.

Top comments (0)