@webjsdev/cli 0.10.32 → 0.10.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/create.js CHANGED
@@ -49,6 +49,27 @@ function runInstall(appDir, pm) {
49
49
  return r.status === 0;
50
50
  }
51
51
 
52
+ /**
53
+ * Author the INITIAL Drizzle migration for the shipped schema, so the app boots
54
+ * with its tables and the very first `run dev` works with no manual step. The
55
+ * scaffold's schema is TypeScript (`db/schema.server.ts`); `db migrate` (run in
56
+ * `webjs.dev.before` / `webjs.start.before`) only applies migration SQL FILES, so
57
+ * with no file the shipped example hits "no such table". `db generate` turns the
58
+ * schema into that first `db/migrations/*.sql`. It runs OFFLINE (a schema-to-SQL
59
+ * diff, no database connection), so it is safe for sqlite AND postgres here, well
60
+ * before any `DATABASE_URL` exists. Needs `drizzle-kit`, so it only runs after a
61
+ * successful install; on `--no-install` the printed next-steps still show
62
+ * `db:generate`. Returns whether a migration was authored.
63
+ *
64
+ * @param {string} appDir
65
+ * @param {string} pm
66
+ * @returns {boolean}
67
+ */
68
+ function runDbGenerate(appDir, pm) {
69
+ const r = spawnSync(pm, ['run', 'db:generate'], { cwd: appDir, stdio: 'inherit' });
70
+ return r.status === 0;
71
+ }
72
+
52
73
  const __dirname = dirname(fileURLToPath(import.meta.url));
53
74
  const TEMPLATES = resolve(__dirname, '..', 'templates');
54
75
 
@@ -1676,11 +1697,21 @@ For AI agents, read this before editing scaffolded files:
1676
1697
  // (#541). Otherwise honour the invoking PM (npm / pnpm / yarn / bun).
1677
1698
  const pm = isBun ? 'bun' : detectPackageManager();
1678
1699
  let installed = false;
1700
+ let generatedMigration = false;
1679
1701
  if (shouldInstall) {
1680
1702
  console.log(`Running '${pm} install' in ${name}/ ...\n`);
1681
1703
  installed = runInstall(appDir, pm);
1682
1704
  if (!installed) {
1683
1705
  console.log(`\n[warn] ${pm} install failed. Run '${pm} install' manually in ${name}/ to finish setup.\n`);
1706
+ } else {
1707
+ // Author the initial migration NOW (drizzle-kit is installed), so the
1708
+ // shipped schema's tables exist and the very first `run dev` works with no
1709
+ // manual step (webjs.*.before applies the migration on boot). See runDbGenerate.
1710
+ console.log(`Authoring the initial database migration ('${pm} run db:generate') ...\n`);
1711
+ generatedMigration = runDbGenerate(appDir, pm);
1712
+ if (!generatedMigration) {
1713
+ console.log(`\n[warn] '${pm} run db:generate' failed. Run it manually in ${name}/ before '${pm} run dev'.\n`);
1714
+ }
1684
1715
  }
1685
1716
  }
1686
1717
 
@@ -1691,14 +1722,14 @@ For AI agents, read this before editing scaffolded files:
1691
1722
  // templates ship with @webjsdev/ui already initialised; the api
1692
1723
  // template has no UI but may add one later.
1693
1724
  const installSegment = installed ? '' : `${pm} install && `;
1694
- // Some examples query the db on their first request, so a migration must be
1695
- // authored first: `db:generate` writes it and the `webjs.dev.before` migrate
1696
- // applies it on `run dev` (Drizzle splits Prisma's `migrate dev` into
1697
- // generate-then-migrate). The saas example queries users (auth); the
1698
- // full-stack scaffold ships the gallery's /examples/todo route (queries todos).
1699
- // The api template has no such first-request query, so it boots with just
1700
- // `run dev`; once you add a db route, `db:generate` then `run dev` is the loop.
1701
- const dbSegment = isApi ? '' : `${pm} run db:generate && `;
1725
+ // The shipped schema is applied on the first `run dev` (webjs.*.before runs
1726
+ // `db migrate`), but only if a migration FILE exists. When we installed, we
1727
+ // already authored it above (runDbGenerate), so the run command is just
1728
+ // `run dev`. Otherwise (--no-install, or generate failed) the user authors it
1729
+ // first: `db:generate` writes the migration from db/schema.server.ts, then
1730
+ // `run dev` applies it (Drizzle splits Prisma's `migrate dev` into
1731
+ // generate-then-migrate).
1732
+ const dbSegment = generatedMigration ? '' : `${pm} run db:generate && `;
1702
1733
  const runCommand = `cd ${name} && ${installSegment}${dbSegment}${pm} run dev`;
1703
1734
  // Postgres needs a reachable DATABASE_URL before any migrate (sqlite uses a
1704
1735
  // local file with no .env). Point it at a running database; `dev` / `start`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webjsdev/cli",
3
- "version": "0.10.32",
3
+ "version": "0.10.33",
4
4
  "type": "module",
5
5
  "description": "webjs CLI - dev, start, create, db",
6
6
  "bin": {
@@ -549,6 +549,7 @@ Scripts (all wrap `drizzle-kit`):
549
549
  - `npm run db:studio`: `webjs db studio` (visual DB browser)
550
550
  - `npm run db:seed`: `webjs db seed` (run `db/seed.server.ts`)
551
551
  - `webjs.dev.before` and `webjs.start.before` both run `webjs db migrate` inside `webjs dev` / `webjs start` (idempotent; replaces the old `prestart` hook), so after you `db:generate` a migration it is applied on the next boot with no manual `db:migrate` step.
552
+ - The INITIAL migration for the shipped schema is authored by `webjs create` at setup time (right after install), so `db/migrations/` is populated and the first `run dev` works with no manual database step. You run `db:generate` yourself only when you CHANGE `db/schema.server.ts` (a new table or column), then the next `run dev` applies it.
552
553
 
553
554
  Always import `db` from `db/connection.server.ts` (the globalThis-cached
554
555
  singleton avoids opening a new connection on every dev-server reload), and