@mostajs/orm-cli 0.3.1 → 0.3.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.
Files changed (2) hide show
  1. package/bin/mostajs.sh +91 -0
  2. package/package.json +1 -1
package/bin/mostajs.sh CHANGED
@@ -548,6 +548,7 @@ menu_databases() {
548
548
  echo -e " ${CYAN}p${RESET}) APP_PORT : ${DIM}${APP_PORT:-3000}${RESET}"
549
549
  echo
550
550
  echo -e " ${CYAN}t${RESET}) Test all connections"
551
+ echo -e " ${CYAN}i${RESET}) ${BOLD}Import from project .env${RESET} (auto-detect app's real DB)"
551
552
  echo -e " ${CYAN}e${RESET}) Export to .env.local in project"
552
553
  echo -e " ${CYAN}r${RESET}) Reset config"
553
554
  echo -e " ${CYAN}b${RESET}) Back"
@@ -565,6 +566,7 @@ menu_databases() {
565
566
  n|N) save_var MOSTA_NET_TRANSPORT "$(ask 'MOSTA_NET_TRANSPORT (rest|sse|graphql|mcp|websocket|jsonrpc|grpc|odata)' "${MOSTA_NET_TRANSPORT:-rest}")";;
566
567
  p|P) save_var APP_PORT "$(ask 'APP_PORT' "${APP_PORT:-3000}")";;
567
568
  t|T) action_test_connections; return;;
569
+ i|I) action_import_env ;;
568
570
  e|E) export_env_local ;;
569
571
  r|R) confirm "Really reset config?" && rm -f "$CONFIG_FILE" && ok "Reset";;
570
572
  b|B) return;;
@@ -647,6 +649,95 @@ list_extra_bindings() {
647
649
  done
648
650
  }
649
651
 
652
+ # Import DB config from the project's own .env file.
653
+ # This is CRUCIAL when your app (Prisma, NextAuth, ...) already has a
654
+ # DATABASE_URL / MONGODB_URI / POSTGRES_URL. Seeding the wrong DB leads to
655
+ # the classic "users are in mosta-net /api/v1/users but login fails" bug.
656
+ action_import_env() {
657
+ header
658
+ echo -e "${BOLD}${MAGENTA}▶ Import DB config from project .env${RESET}"
659
+ echo
660
+
661
+ local candidates=(".env.local" ".env" ".env.production" ".env.development")
662
+ local -a available=()
663
+ for f in "${candidates[@]}"; do
664
+ [[ -f "$PROJECT_ROOT/$f" ]] && available+=("$f")
665
+ done
666
+
667
+ if [[ ${#available[@]} -eq 0 ]]; then
668
+ err "No .env* file found in $PROJECT_ROOT"
669
+ pause; return
670
+ fi
671
+
672
+ echo "Found env files :"
673
+ local i=1
674
+ for f in "${available[@]}"; do
675
+ echo -e " ${CYAN}$i${RESET}) $f"
676
+ i=$((i+1))
677
+ done
678
+ echo
679
+ local choice; choice=$(ask "Number" 1)
680
+ local src_file="${available[$((choice-1))]}"
681
+ [[ -z "$src_file" ]] && return
682
+ local src_path="$PROJECT_ROOT/$src_file"
683
+
684
+ # Extract common DB URI variables — priority order matters
685
+ # MONGODB_URI > DATABASE_URL > POSTGRES_URL > MYSQL_URL > SGBD_URI
686
+ local found_uri="" found_var="" found_dialect=""
687
+ for var in SGBD_URI DATABASE_URL MONGODB_URI POSTGRES_URL POSTGRES_URI MYSQL_URL MYSQL_URI POSTGRESQL_URL; do
688
+ # Read value, skip commented lines, strip quotes
689
+ local val
690
+ val=$(grep -E "^${var}=" "$src_path" 2>/dev/null | grep -v "^#" | head -1 | sed "s/^${var}=//" | sed 's/^"\(.*\)"$/\1/' | sed "s/^'\(.*\)'$/\1/")
691
+ if [[ -n "$val" ]]; then
692
+ found_uri="$val"
693
+ found_var="$var"
694
+ break
695
+ fi
696
+ done
697
+
698
+ if [[ -z "$found_uri" ]]; then
699
+ err "No DB URI found in $src_file"
700
+ info "Searched : SGBD_URI, DATABASE_URL, MONGODB_URI, POSTGRES_URL/URI, MYSQL_URL/URI"
701
+ pause; return
702
+ fi
703
+
704
+ # Detect dialect from URI scheme
705
+ found_dialect=$(detect_dialect_from_uri "$found_uri")
706
+ if [[ "$found_dialect" == "unknown" ]]; then
707
+ warn "Could not auto-detect dialect from URI: $found_uri"
708
+ found_dialect=$(ask "Dialect name (mongodb|postgres|mysql|...)" "")
709
+ [[ -z "$found_dialect" ]] && { pause; return; }
710
+ fi
711
+
712
+ info "Detected :"
713
+ echo -e " Variable : ${CYAN}$found_var${RESET}"
714
+ echo -e " URI : ${DIM}$found_uri${RESET}"
715
+ echo -e " Dialect : ${MAGENTA}$found_dialect${RESET}"
716
+ echo
717
+
718
+ # Special Prisma note
719
+ if [[ "$found_var" == "MONGODB_URI" ]] || [[ "$found_var" == "DATABASE_URL" ]]; then
720
+ dim " (This is likely the Prisma datasource — the same DB your app uses for login/auth.)"
721
+ echo
722
+ fi
723
+
724
+ if confirm "Save as DB_DIALECT + SGBD_URI ?"; then
725
+ save_var DB_DIALECT "$found_dialect"
726
+ save_var SGBD_URI "$found_uri"
727
+ # Suggest a strategy based on environment context
728
+ if [[ "$src_file" == ".env.production" ]]; then
729
+ save_var DB_SCHEMA_STRATEGY "validate"
730
+ info "Auto-set DB_SCHEMA_STRATEGY=validate (production)"
731
+ elif [[ -z "${DB_SCHEMA_STRATEGY:-}" ]]; then
732
+ save_var DB_SCHEMA_STRATEGY "update"
733
+ fi
734
+ ok "Config imported"
735
+ echo
736
+ info "Next step : menu 2 → t (test connection), then menu S → 4 (seed)"
737
+ fi
738
+ pause
739
+ }
740
+
650
741
  # Export a .env.local file compatible with @mostajs/orm convention
651
742
  export_env_local() {
652
743
  local target="$PROJECT_ROOT/.env.mostajs"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mostajs/orm-cli",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Universal CLI to integrate @mostajs/orm into any project — auto-detects Prisma, OpenAPI, JSON Schema. Interactive menu + subcommands. 13 databases.",
5
5
  "author": "Dr Hamid MADANI <drmdh@msn.com>",
6
6
  "license": "AGPL-3.0-or-later",