{"version":3,"file":"seed.cjs","names":["newPgConnectionConfig","EntityManager","PostgresDriver"],"sources":["../src/seed.ts"],"sourcesContent":["import { EntityManager, newPgConnectionConfig } from \"joist-core\";\nimport pg from \"pg\";\n\nimport { PostgresDriver } from \"./drivers/PostgresDriver.ts\";\n\n/**\n * Seeds your local/test database with data created via the factories.\n *\n * Unlike `seed` from `joist-test-utils`, this defaults to the `PostgresDriver`, so you can just\n * `import { seed } from \"joist-orm/pg\"` and pass only your seeding function.\n *\n * This will delete all data in the database, so is restricted to only `local` and `test`\n * `NODE_ENV`s. We also assume the project does not require a context to seed.\n *\n * @example\n * ```ts\n * import { seed } from \"joist-orm/pg\";\n *\n * seed(async (em) => {\n *   // create your data via factories here\n * });\n * ```\n */\nexport function seed<E extends EntityManager = EntityManager>(fn: (em: E) => Promise<void>): void {\n  const env = process.env.NODE_ENV;\n  if (env !== \"local\" && env !== \"test\") {\n    throw new Error(\"seed will only run with NODE_ENV=local or NODE_ENV=test because it resets the database\");\n  }\n\n  const pool = new pg.Pool(newPgConnectionConfig());\n\n  run<E>(pool, fn)\n    .then(async () => {\n      console.log(\"Seeded!\");\n      await pool.end();\n    })\n    .catch(async (err) => {\n      console.error(err);\n      await pool.end();\n      process.exit(1);\n    });\n}\n\n/** Flushes the database and runs `fn` against a fresh `EntityManager`. */\nasync function run<E extends EntityManager>(pool: pg.Pool, fn: (em: E) => Promise<void>): Promise<void> {\n  await pool.query(\"SELECT flush_database()\");\n  const em = new EntityManager({}, { driver: new PostgresDriver(pool) }) as E;\n  await fn(em);\n  await em.flush();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAuBA,SAAgB,KAA8C,IAAoC;CAChG,MAAM,MAAM,QAAQ,IAAI;CACxB,IAAI,QAAQ,WAAW,QAAQ,QAC7B,MAAM,IAAI,MAAM,wFAAwF;CAG1G,MAAM,OAAO,IAAI,GAAA,QAAG,MAAA,GAAKA,WAAAA,sBAAAA,CAAsB,CAAC;CAEhD,IAAO,MAAM,EAAE,CAAC,CACb,KAAK,YAAY;EAChB,QAAQ,IAAI,SAAS;EACrB,MAAM,KAAK,IAAI;CACjB,CAAC,CAAC,CACD,MAAM,OAAO,QAAQ;EACpB,QAAQ,MAAM,GAAG;EACjB,MAAM,KAAK,IAAI;EACf,QAAQ,KAAK,CAAC;CAChB,CAAC;AACL;;AAGA,eAAe,IAA6B,MAAe,IAA6C;CACtG,MAAM,KAAK,MAAM,yBAAyB;CAC1C,MAAM,KAAK,IAAIC,WAAAA,cAAc,CAAC,GAAG,EAAE,QAAQ,IAAIC,+BAAAA,eAAe,IAAI,EAAE,CAAC;CACrE,MAAM,GAAG,EAAE;CACX,MAAM,GAAG,MAAM;AACjB"}