title: Transform JS object into code / module (codegen)
body: <p>There are many things that do <code>x -&gt; JS</code> and I want to do something similar where <code>x</code> is a POJO and spits out an es module. </p>
  
  <p>Currently I am writing a string then saving it to a js file:</p>
  
  <pre><code>const mapJoin = (xs, fn) =&gt; xs.map(fn).join(', \n');
  
  const output = `
  import { Model, belongsTo, hasMany } from '@miragejs/server';
  
  export default {
    ${mapJoin(
      nodes,
      node =&gt;
        `${node.model}: Model${
          !node.fields.length
            ? ''
            : `.extend({
               ${mapJoin(
                 node.fields,
                 f =&gt;
                   `${f.name}: ${f.isConnection ? 'hasMany' : 'belongsTo'}('${
                     f.model
                   }')`
               )}
        })`
        }`
    )}
  }`;
  
  fs.writeFileSync('__generated__models.js', output);
  </code></pre>
  
  <p>This is very cumbersome and error prone. Is there an accepted way / library that handles this?</p>