@mostajs/orm-cli 0.4.0 → 0.4.2

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/README.md CHANGED
@@ -1,137 +1,189 @@
1
1
  # @mostajs/orm-cli
2
2
 
3
- > **Universal interactive CLI for @mostajs/orm integration.**
4
- > Auto-detects Prisma / OpenAPI / JSON Schema in any project, converts to EntitySchema[], tests with humans / mobiles / AI agents, and launches everything.
3
+ > **Automated migration from Prisma to @mostajs/orm.**
4
+ >
5
+ > `npx @mostajs/orm-cli bootstrap` scans your Prisma project, rewrites every `new PrismaClient(...)` site to use the @mostajs/orm-bridge (backing up the originals), installs the runtime, converts your schema, and applies DDL — in one command.
5
6
 
6
7
  [![npm version](https://img.shields.io/npm/v/@mostajs/orm-cli.svg)](https://www.npmjs.com/package/@mostajs/orm-cli)
7
8
  [![License: AGPL-3.0-or-later](https://img.shields.io/badge/License-AGPL%203.0-blue.svg)](LICENSE)
8
9
 
9
- ## Install
10
-
11
- ### Option 1 — npx (zero install)
10
+ ## Zero-touch migration
12
11
 
13
12
  ```bash
14
- cd your/project
15
- npx @mostajs/orm-cli
13
+ cd my-existing-prisma-app
14
+ npx @mostajs/orm-cli bootstrap
15
+ npm run dev
16
+ # db.User.findMany(...) now runs on any of 13 databases. Zero code change.
16
17
  ```
17
18
 
18
- ### Option 2 global
19
+ That single command does :
20
+
21
+ 1. **Codemod** — scans the repo for `new PrismaClient(...)`, detects each export name (`prisma`, `db`, `client`, default), and rewrites each site to `createPrismaLikeDb()` from `@mostajs/orm-bridge`. Originals backed up as `*.prisma.bak`.
22
+ 2. **Install** — `@mostajs/orm` + `@mostajs/orm-bridge` + `@mostajs/orm-adapter` + `server-only`.
23
+ 3. **Convert** — `prisma/schema.prisma` → `.mostajs/generated/entities.json`.
24
+ 4. **DDL** — writes `.mostajs/config.env` (SQLite defaults) and creates tables.
25
+
26
+ **Every step stops on error** (v0.4.1+) so you never see a lying success banner.
27
+
28
+ To undo :
19
29
 
20
30
  ```bash
21
- npm install -g @mostajs/orm-cli
22
- cd your/project
23
- mostajs
31
+ npx @mostajs/orm-cli install-bridge --restore --apply
24
32
  ```
25
33
 
26
- ### Option 3 — curl one-liner (Unix)
34
+ ## Install
35
+
36
+ ### Zero-install (recommended)
27
37
 
28
38
  ```bash
29
- curl -fsSL https://raw.githubusercontent.com/apolocine/mosta-orm-cli/main/install.sh | bash
39
+ npx @mostajs/orm-cli bootstrap
30
40
  ```
31
41
 
32
- ## Usage
42
+ ### Global
43
+
44
+ ```bash
45
+ npm install -g @mostajs/orm-cli
46
+ cd your/project
47
+ mostajs bootstrap
48
+ ```
33
49
 
34
- ### Interactive menu (recommended)
50
+ ## Subcommands
51
+
52
+ | Command | What it does |
53
+ |---|---|
54
+ | `mostajs bootstrap` | Full migration : codemod + install + convert + DDL |
55
+ | `mostajs install-bridge` | Run the codemod (dry-run by default) |
56
+ | `mostajs install-bridge --apply` | Write the codemod changes |
57
+ | `mostajs install-bridge --file <path>` | Restrict to one file |
58
+ | `mostajs install-bridge --restore --apply` | Revert `.prisma.bak` backups |
59
+ | `mostajs convert` | Auto-detect schema (Prisma / OpenAPI / JSONSchema) and convert |
60
+ | `mostajs detect` | Print what's detected in the project |
61
+ | `mostajs health` | Verify Node / pnpm / schemas / entities.json state |
62
+ | `mostajs hash <password> [cost]` | Bcrypt a password (for seed data) |
63
+ | `mostajs verify <password> <hash>` | Check a password/hash pair |
64
+ | `mostajs diagnose [email] [password]` | Walk through login diagnostics |
65
+ | `mostajs help` | Usage |
66
+ | `mostajs version` | Print version |
67
+
68
+ ## Interactive menu
35
69
 
36
70
  ```bash
37
71
  cd your/project
38
72
  mostajs
39
73
  ```
40
74
 
41
- The CLI auto-detects :
42
- - **Prisma** : `prisma/schema.prisma`
43
- - **OpenAPI** : `openapi.yaml`, `openapi.json`, `api.yaml`, `spec/openapi.yaml`, etc.
44
- - **JSON Schema** : `schemas/*.json`
45
-
46
- Menu :
75
+ Detected schemas are listed. Pick :
47
76
 
48
77
  ```
49
78
  1) Convert schema → EntitySchema[]
50
- 2) Configure database URIs (13 databases)
51
- 3) Initialize dialects (connect + create tables)
52
- 4) Tests menu (human / mobile / AI / curl / playwright)
53
- 5) Start services (Next.js + mosta-net)
79
+ 2) Configure database URIs
80
+ 3) Initialize dialects (connect + create tables)
81
+ 4) Tests menu (human / mobile / AI agent / curl / playwright)
82
+ 5) Start services
54
83
  6) Metrics & status
55
84
  7) View logs
56
85
  8) Health checks
57
- 9) Generate boilerplate (src/db.ts / .env.example)
86
+ 9) Generate boilerplate (src/db.ts / .env.example)
87
+ s) Seeding (upload / validate / hash / apply)
88
+ b) Bootstrap — one-shot Prisma migration
89
+ i) Install bridge (codemod)
58
90
  0) About / Help
91
+ q) Quit
59
92
  ```
60
93
 
61
- ### Non-interactive subcommands
94
+ ## The codemod, in detail
95
+
96
+ `mostajs install-bridge` is safe by default — **dry-run** reports what it would change, without touching files :
62
97
 
63
- ```bash
64
- mostajs convert # auto-detect + convert
65
- mostajs detect # print detected schemas
66
- mostajs health # check tools + project state
67
- mostajs version
68
- mostajs help
69
98
  ```
99
+ $ mostajs install-bridge
100
+ ▶ mostajs install-bridge — scanning /home/me/my-app
70
101
 
71
- ## What it does
102
+ Found 3 PrismaClient instantiation site(s):
103
+ → src/lib/db.ts (const db)
104
+ → src/server/prisma.ts (const prisma)
105
+ → scripts/seed.ts (const prisma)
72
106
 
73
- ### Tests menu everything you need to verify
107
+ Dry-run no files written. Re-run with --apply to execute.
108
+ ```
74
109
 
75
- - **Human** : opens browser on `http://localhost:3000`
76
- - **Mobile** : generates QR code for LAN URL (needs `qrencode`)
77
- - **AI** : displays ready-to-paste Claude Desktop MCP config
78
- - **curl** : smoke-tests all endpoints with status codes + times
79
- - **Playwright** : runs existing test suite
110
+ Detection rules :
111
+ - Must contain `import ... from '@prisma/client'` AND `new PrismaClient(`
112
+ - Files that already use `@mostajs/orm-bridge/prisma-client` are skipped (idempotent re-runs)
113
+ - Export shape is preserved : `const db`, `const prisma`, `let client`, `export default`, and `const x = ...; export { x }`
80
114
 
81
- ### Config stored per-project
115
+ Replacement format :
82
116
 
83
- ```
84
- your-project/.mostajs/
85
- ├── config.env # URIs + ports
86
- ├── generated/entities.ts # EntitySchema[] (auto-generated)
87
- └── logs/ # dev / convert / init logs
117
+ ```ts
118
+ // Auto-generated by `mostajs install-bridge` on 2026-04-14T02:03:38Z
119
+ // Original file backed up as <this-file>.prisma.bak
120
+ // Every db/prisma/client call is now routed to @mostajs/orm (13 dialects).
121
+ import { createPrismaLikeDb } from '@mostajs/orm-bridge/prisma-client'
122
+
123
+ export const db = createPrismaLikeDb()
88
124
  ```
89
125
 
90
- ### Supported databases (13)
126
+ ## Supported schemas at input
91
127
 
92
- PostgreSQL · MySQL · MariaDB · SQLite · MS SQL Server · Oracle · DB2 · HANA · HSQLDB · Spanner · Sybase · CockroachDB · MongoDB
128
+ The CLI auto-detects :
129
+ - **Prisma** — `prisma/schema.prisma`
130
+ - **OpenAPI** — `openapi.yaml`, `openapi.json`, `api.yaml`, `spec/openapi.yaml`, …
131
+ - **JSON Schema** — `schemas/*.json`
93
132
 
94
- ## Example workflow (any Prisma app)
133
+ Conversion goes through `@mostajs/orm-adapter` (4 adapters).
95
134
 
96
- ```bash
97
- $ cd my-nextjs-app
98
- $ mostajs
135
+ ## Supported databases (13)
99
136
 
100
- Project : /path/to/my-nextjs-app
101
- Manager : pnpm
102
- Detected:
103
- ✓ Prisma schema (40 models)
104
- ⚠ entities.ts not generated
137
+ SQLite · PostgreSQL · MySQL · MariaDB · MongoDB · Oracle · SQL Server · CockroachDB · DB2 · SAP HANA · HSQLDB · Spanner · Sybase
105
138
 
106
- Choice [1]: 1 # Convert
107
- entities : 40
108
- warnings : 0
109
- ✓ Saved : .mostajs/generated/entities.ts
139
+ Switch database by editing `.mostajs/config.env` (or `.env`) :
110
140
 
111
- Choice [1]: 9 # Generate boilerplate
112
- ✓ Written : src/db.ts (Prisma bridge)
141
+ ```bash
142
+ DB_DIALECT=postgres
143
+ SGBD_URI=postgres://user:pass@host:5432/mydb
144
+ ```
145
+
146
+ Then re-run `mostajs` → menu 3 (init DDL) → menu S → 4 (apply seeds). Same code.
113
147
 
114
- # now replace `new PrismaClient()` with `import { prisma } from './db.js'`
115
- # ... your existing Prisma code runs on 13 databases
148
+ ## Project layout
149
+
150
+ ```
151
+ your-project/
152
+ ├── prisma/schema.prisma # unchanged
153
+ ├── src/lib/db.ts # 3 lines now (createPrismaLikeDb)
154
+ ├── src/lib/db.ts.prisma.bak # original, in case you want to revert
155
+ └── .mostajs/
156
+ ├── config.env # DB_DIALECT, SGBD_URI, DB_SCHEMA_STRATEGY
157
+ ├── generated/entities.json # converted schema (13-DB-ready)
158
+ ├── seeds/*.json # one per entity, hashed by menu S → h
159
+ └── logs/ # convert / init / seed / dev
116
160
  ```
117
161
 
118
- ## Strategy
162
+ ## Example — FitZoneGym
119
163
 
120
- - **Schema conversion** : via [@mostajs/orm-adapter](https://www.npmjs.com/package/@mostajs/orm-adapter) 4 adapters (Prisma, JSON Schema, OpenAPI, Native)
121
- - **Runtime interception** : via [@mostajs/orm-bridge](https://www.npmjs.com/package/@mostajs/orm-bridge) — route Prisma calls to any of the 13 databases
122
- - **Zero rewrite** : your existing `prisma.user.findMany()` stays unchanged
164
+ FitZoneGym (production-grade Next.js 15 + Prisma, 40 models, 67 files importing Prisma) was migrated end-to-end with :
123
165
 
124
- ## Links
166
+ ```bash
167
+ cd FitZoneGym
168
+ npx @mostajs/orm-cli bootstrap # 15 PrismaClient sites rewritten, schema converted, DDL applied
169
+ # manually : add seeds to .mostajs/seeds/User.json
170
+ # manually : mostajs — menu S → h → 4
171
+ npm run dev # login alice@example.com / alice123 → 302 + session
172
+ ```
125
173
 
126
- - npm : https://www.npmjs.com/package/@mostajs/orm-cli
127
- - GitHub : https://github.com/apolocine/mosta-orm-cli
128
- - Ecosystem : [@mostajs/orm](https://www.npmjs.com/package/@mostajs/orm), [@mostajs/orm-adapter](https://www.npmjs.com/package/@mostajs/orm-adapter), [@mostajs/orm-bridge](https://www.npmjs.com/package/@mostajs/orm-bridge)
174
+ Files modified by the user : **0** (codemod owned them all). Login, dashboard, API routes all work on SQLite instead of MongoDB.
129
175
 
130
176
  ## License
131
177
 
132
178
  **AGPL-3.0-or-later** + commercial license available.
133
179
 
134
- For commercial use in closed-source projects : drmdh@msn.com
180
+ For closed-source commercial use : drmdh@msn.com
181
+
182
+ ## Ecosystem
183
+
184
+ - [@mostajs/orm](https://www.npmjs.com/package/@mostajs/orm) — the ORM (13 databases)
185
+ - [@mostajs/orm-bridge](https://www.npmjs.com/package/@mostajs/orm-bridge) — runtime drop-in for PrismaClient
186
+ - [@mostajs/orm-adapter](https://www.npmjs.com/package/@mostajs/orm-adapter) — schema format converters
135
187
 
136
188
  ## Author
137
189
 
@@ -95,7 +95,13 @@ function detectExportShape(source) {
95
95
 
96
96
  // ---------- Rewrite ----------
97
97
  function buildReplacement(shape) {
98
- const header = `// Auto-generated by \`mostajs install-bridge\` on ${new Date().toISOString()}\n// Original file backed up as <this-file>.prisma.bak\n// Every db/prisma/client call is now routed to @mostajs/orm (13 dialects).\nimport 'server-only'\nimport { createPrismaLikeDb } from '@mostajs/orm-bridge/prisma-client'\n`;
98
+ // Note : we do NOT emit `import 'server-only'` because FitZoneGym-class
99
+ // codebases often mix pages/ and app/ directories. `server-only` crashes
100
+ // the pages/ build even though the actual bundle never evaluates the
101
+ // forbidden import chain (lazy dialect loading in @mostajs/orm@1.9.3+).
102
+ // Projects that want the extra guarantee can add `import 'server-only'`
103
+ // manually — but it's not necessary for correctness.
104
+ const header = `// Auto-generated by \`mostajs install-bridge\` on ${new Date().toISOString()}\n// Original file backed up as <this-file>.prisma.bak\n// Every db/prisma/client call is now routed to @mostajs/orm (13 dialects).\nimport { createPrismaLikeDb } from '@mostajs/orm-bridge/prisma-client'\n`;
99
105
  if (shape.kind === 'default') {
100
106
  return `${header}\nexport default createPrismaLikeDb()\n`;
101
107
  }
package/bin/mostajs.sh CHANGED
@@ -2460,22 +2460,42 @@ run_subcommand() {
2460
2460
  ;;
2461
2461
  bootstrap|b)
2462
2462
  # mostajs bootstrap : the full zero-touch migration for a Prisma project.
2463
- # 1. Run the codemod on the whole tree (install-bridge --apply)
2464
- # 2. npm install @mostajs/orm @mostajs/orm-bridge server-only
2463
+ # 1. Rewrite every `new PrismaClient(...)` site (install-bridge --apply)
2464
+ # 2. npm install @mostajs/orm @mostajs/orm-bridge @mostajs/orm-adapter server-only
2465
2465
  # 3. Convert prisma/schema.prisma → entities.json
2466
2466
  # 4. Write .mostajs/config.env + init SQLite DDL
2467
- # 5. Tell the user what remains (.env, seeds, run dev).
2467
+ #
2468
+ # Hard stop-on-error : no step proceeds if the previous one failed.
2468
2469
  local cli_dir
2469
2470
  cli_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
2470
2471
  detect_project
2471
2472
  [[ ${#DETECTED_TYPES[@]} -eq 0 ]] && { err "No schema found. Bootstrap needs a prisma/schema.prisma (or OpenAPI/JSONSchema)."; exit 1; }
2472
2473
 
2474
+ local BS_OK_CODEMOD=0 BS_OK_DEPS=0 BS_OK_CONVERT=0 BS_OK_DDL=0
2475
+
2476
+ # ─── Step 1 ───
2473
2477
  echo -e "\n\e[1m▶ Step 1/4 : rewrite PrismaClient sites\e[0m"
2474
- node "$cli_dir/bin/install-bridge.mjs" --apply || { err "Codemod failed"; exit 1; }
2478
+ if node "$cli_dir/bin/install-bridge.mjs" --apply; then
2479
+ BS_OK_CODEMOD=1
2480
+ else
2481
+ err "Step 1 failed — codemod returned non-zero. Aborting."; exit 1
2482
+ fi
2475
2483
 
2476
- echo -e "\n\e[1m▶ Step 2/4 : install runtime deps\e[0m"
2477
- ( cd "$PROJECT_ROOT" && $PKG_MANAGER install @mostajs/orm @mostajs/orm-bridge server-only --legacy-peer-deps 2>&1 | tail -3 )
2484
+ # ─── Step 2 ───
2485
+ echo -e "\n\e[1m▶ Step 2/4 : install runtime deps (this can take 1-2 min)\e[0m"
2486
+ info " installing : @mostajs/orm @mostajs/orm-bridge @mostajs/orm-adapter server-only"
2487
+ if ( cd "$PROJECT_ROOT" && $PKG_MANAGER install \
2488
+ @mostajs/orm @mostajs/orm-bridge @mostajs/orm-adapter server-only \
2489
+ --legacy-peer-deps ); then
2490
+ BS_OK_DEPS=1
2491
+ ok " deps installed"
2492
+ else
2493
+ err "Step 2 failed — \`$PKG_MANAGER install\` returned non-zero."
2494
+ err "Fix your package manager / registry access and re-run \`mostajs bootstrap\`."
2495
+ exit 1
2496
+ fi
2478
2497
 
2498
+ # ─── Step 3 ───
2479
2499
  echo -e "\n\e[1m▶ Step 3/4 : convert schema + init DDL\e[0m"
2480
2500
  local type="${DETECTED_TYPES[0]}" input
2481
2501
  case "$type" in
@@ -2483,7 +2503,16 @@ run_subcommand() {
2483
2503
  openapi) input="$OPENAPI_FILE" ;;
2484
2504
  jsonschema) input="${JSON_SCHEMAS[0]}" ;;
2485
2505
  esac
2486
- run_adapter_convert "$type" "$input" "$GENERATED_DIR/entities.ts"
2506
+
2507
+ if run_adapter_convert "$type" "$input" "$GENERATED_DIR/entities.ts" && \
2508
+ [[ -s "$GENERATED_DIR/entities.json" || -s "$GENERATED_DIR/entities.ts" ]]; then
2509
+ BS_OK_CONVERT=1
2510
+ ok " schema converted → $GENERATED_DIR/entities.json"
2511
+ else
2512
+ err "Step 3.1 failed — schema conversion did not produce entities.json."
2513
+ err "Re-run manually : $CLI_NAME convert (or menu 1)"
2514
+ exit 1
2515
+ fi
2487
2516
 
2488
2517
  mkdir -p "$CONFIG_DIR"
2489
2518
  if [[ ! -f "$CONFIG_DIR/config.env" ]]; then
@@ -2493,25 +2522,42 @@ SGBD_URI=./data.sqlite
2493
2522
  DB_SCHEMA_STRATEGY=update
2494
2523
  CFG
2495
2524
  ok " wrote $CONFIG_DIR/config.env (defaults: sqlite ./data.sqlite)"
2525
+ # Reload config so action_init_dialects picks up the new values
2526
+ load_env
2496
2527
  fi
2497
- action_init_dialects || warn "DDL init returned non-zero — inspect and retry with menu 3"
2498
2528
 
2529
+ if action_init_dialects; then
2530
+ BS_OK_DDL=1
2531
+ ok " DDL applied"
2532
+ else
2533
+ err "Step 3.2 failed — DDL init returned non-zero."
2534
+ err "Re-run manually : $CLI_NAME (menu 3)"
2535
+ exit 1
2536
+ fi
2537
+
2538
+ # ─── Step 4 ───
2499
2539
  echo -e "\n\e[1m▶ Step 4/4 : done\e[0m"
2500
- cat <<DONE
2540
+ if (( BS_OK_CODEMOD && BS_OK_DEPS && BS_OK_CONVERT && BS_OK_DDL )); then
2541
+ cat <<DONE
2501
2542
 
2502
2543
  ✓ Bridge installed in-place. Original files backed up as *.prisma.bak
2503
2544
  ✓ Schema converted : $GENERATED_DIR/entities.json
2504
- SQLite DDL applied : $PROJECT_ROOT/data.sqlite
2545
+ ✓ DDL applied (DB_DIALECT=\$(grep ^DB_DIALECT $CONFIG_DIR/config.env | cut -d= -f2))
2505
2546
 
2506
2547
  Next :
2507
2548
  - Add seeds to $CONFIG_DIR/seeds/*.json (one file per entity)
2508
2549
  - $CLI_NAME # menu S → h (hash) → 4 (apply)
2509
2550
  - npm run dev
2551
+ - Open http://localhost:3000/login
2510
2552
 
2511
2553
  To undo the codemod :
2512
2554
  $CLI_NAME install-bridge --restore --apply
2513
2555
 
2514
2556
  DONE
2557
+ else
2558
+ err "Bootstrap finished with partial success — see messages above."
2559
+ exit 1
2560
+ fi
2515
2561
  ;;
2516
2562
  version|-v|--version)
2517
2563
  echo "$CLI_NAME $VERSION"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mostajs/orm-cli",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "Universal CLI to integrate @mostajs/orm into any project — one-shot `mostajs bootstrap` migrates a Prisma project (codemod + deps + schema convert + DDL) to 13 databases with zero code change.",
5
5
  "author": "Dr Hamid MADANI <drmdh@msn.com>",
6
6
  "license": "AGPL-3.0-or-later",