@mostajs/orm-cli 0.6.0 → 0.6.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 +100 -6
  2. package/package.json +1 -1
package/bin/mostajs.sh CHANGED
@@ -579,10 +579,10 @@ menu_install_bridge() {
579
579
  header
580
580
  echo -e "${BOLD}${MAGENTA}▶ Install bridge — PrismaClient codemod${RESET}"
581
581
  echo
582
- echo " ${CYAN}1${RESET}) Dry-run : list files that would be rewritten (default)"
583
- echo " ${CYAN}2${RESET}) Apply : rewrite PrismaClient sites to createPrismaLikeDb()"
584
- echo " ${CYAN}3${RESET}) Restore : revert .prisma.bak files (dry-run)"
585
- echo " ${CYAN}4${RESET}) Restore : revert .prisma.bak files (apply)"
582
+ echo -e " ${CYAN}1${RESET}) Dry-run : list files that would be rewritten (default)"
583
+ echo -e " ${CYAN}2${RESET}) Apply : rewrite PrismaClient sites to createPrismaLikeDb()"
584
+ echo -e " ${CYAN}3${RESET}) Restore : revert .prisma.bak files (dry-run)"
585
+ echo -e " ${CYAN}4${RESET}) Restore : revert .prisma.bak files (apply)"
586
586
  echo " ${RED}0${RESET}) Back"
587
587
  echo
588
588
  local cli_dir
@@ -4056,6 +4056,20 @@ SGBD_URI=$bs_uri
4056
4056
  DB_SCHEMA_STRATEGY=$bs_strategy
4057
4057
  CFG
4058
4058
  ok " wrote $CONFIG_DIR/config.env (dialect=$bs_dialect)"
4059
+
4060
+ # Also write to .env so the bridge picks it up at runtime
4061
+ # (the bridge reads .env, not .mostajs/config.env)
4062
+ local dotenv="$PROJECT_ROOT/.env"
4063
+ touch "$dotenv"
4064
+ # Remove old entries if present, then append
4065
+ sed -i '/^DB_DIALECT=/d; /^SGBD_URI=/d; /^DB_SCHEMA_STRATEGY=/d' "$dotenv"
4066
+ cat >> "$dotenv" <<ENVVARS
4067
+ DB_DIALECT=$bs_dialect
4068
+ SGBD_URI=$bs_uri
4069
+ DB_SCHEMA_STRATEGY=$bs_strategy
4070
+ ENVVARS
4071
+ ok " wrote DB_DIALECT + SGBD_URI to .env"
4072
+
4059
4073
  load_env
4060
4074
  fi
4061
4075
 
@@ -4075,9 +4089,10 @@ CFG
4075
4089
  echo -e " How do you want to populate them?"
4076
4090
  echo
4077
4091
  echo -e " ${CYAN}1${RESET}) Apply seed files ${DIM}(.mostajs/seeds/*.json → mostajs seed)${RESET}"
4078
- echo -e " ${CYAN}2${RESET}) Replicate from old DB ${DIM}(old Prisma DB = master → new DB = slave)${RESET}"
4092
+ echo -e " ${CYAN}2${RESET}) Replicate from old DB ${DIM}(old Prisma DB = master → new DB = slave, continuous)${RESET}"
4079
4093
  echo -e " ${CYAN}3${RESET}) Start with empty tables ${DIM}(app handles its own data)${RESET}"
4080
- echo -e " ${CYAN}4${RESET}) Auto-replicate old → new ${DIM}(init schema + snapshot all data)${RESET}"
4094
+ echo -e " ${CYAN}4${RESET}) Auto-replicate old → new ${DIM}(one-shot snapshot + scaffold replicator)${RESET}"
4095
+ echo -e " ${CYAN}5${RESET}) Simple data copy ${DIM}(one-shot copy, no replicator setup)${RESET}"
4081
4096
  echo
4082
4097
  local dm_choice
4083
4098
  dm_choice=$(ask "Choice" "3")
@@ -4089,6 +4104,85 @@ CFG
4089
4104
  3)
4090
4105
  ok " Empty tables — ready for your app to populate."
4091
4106
  ;;
4107
+ 5)
4108
+ # ─── Simple one-shot data copy (no replicator) ───
4109
+ echo
4110
+ echo -e "${BOLD}▶ Simple data copy : old DB → new DB${RESET}"
4111
+
4112
+ local old_uri_simple=""
4113
+ if [[ -f "$PROJECT_ROOT/.env" ]]; then
4114
+ old_uri_simple=$(grep -E '^DATABASE_URL=' "$PROJECT_ROOT/.env" 2>/dev/null | head -1 | sed 's/^DATABASE_URL=//' | tr -d '"' | tr -d "'")
4115
+ fi
4116
+ if [[ -z "$old_uri_simple" ]]; then
4117
+ old_uri_simple=$(ask "Old DATABASE_URL (source)" "postgresql://user:pass@localhost:5432/olddb")
4118
+ else
4119
+ info " Detected old DATABASE_URL : ${old_uri_simple:0:50}..."
4120
+ if ! confirm "Use this as source?"; then
4121
+ old_uri_simple=$(ask "Old DATABASE_URL" "$old_uri_simple")
4122
+ fi
4123
+ fi
4124
+
4125
+ local old_d_simple="postgres"
4126
+ case "$old_uri_simple" in
4127
+ postgresql://*|postgres://*) old_d_simple="postgres" ;;
4128
+ mysql://*) old_d_simple="mysql" ;;
4129
+ mongodb://*) old_d_simple="mongodb" ;;
4130
+ mariadb://*) old_d_simple="mariadb" ;;
4131
+ oracle://*) old_d_simple="oracle" ;;
4132
+ mssql://*) old_d_simple="mssql" ;;
4133
+ *sqlite*|*.db|*.sqlite) old_d_simple="sqlite" ;;
4134
+ esac
4135
+
4136
+ echo
4137
+ info " Source : $old_d_simple → Target : $DB_DIALECT"
4138
+ info " Copying data (one-shot, no replicator setup)..."
4139
+
4140
+ RUNTIME_ROOT="$PROJECT_ROOT" OLD_URI="$old_uri_simple" OLD_DIALECT="$old_d_simple" \
4141
+ NEW_URI="$SGBD_URI" NEW_DIALECT="$DB_DIALECT" \
4142
+ node --input-type=module -e "
4143
+ import { readFileSync, existsSync } from 'node:fs';
4144
+ import { resolve } from 'node:path';
4145
+
4146
+ const orm = await import(resolve(process.env.RUNTIME_ROOT, 'node_modules/@mostajs/orm/dist/index.js'));
4147
+
4148
+ const entPath = resolve(process.env.RUNTIME_ROOT, '.mostajs/generated/entities.json');
4149
+ const schemas = existsSync(entPath) ? JSON.parse(readFileSync(entPath, 'utf8')) : [];
4150
+ orm.registerSchemas(schemas);
4151
+
4152
+ const src = await orm.createIsolatedDialect(
4153
+ { dialect: process.env.OLD_DIALECT, uri: process.env.OLD_URI, schemaStrategy: 'none' }, schemas);
4154
+ const dst = await orm.createIsolatedDialect(
4155
+ { dialect: process.env.NEW_DIALECT, uri: process.env.NEW_URI, schemaStrategy: 'update' }, schemas);
4156
+ await dst.initSchema(schemas);
4157
+
4158
+ let totalCopied = 0, totalErrors = 0;
4159
+ for (const schema of schemas) {
4160
+ try {
4161
+ const rows = await src.find(schema, {});
4162
+ let copied = 0, errors = 0;
4163
+ for (const row of rows) {
4164
+ try {
4165
+ const { createdAt, updatedAt, ...data } = row;
4166
+ await dst.create(schema, data);
4167
+ copied++;
4168
+ } catch (e) {
4169
+ errors++;
4170
+ }
4171
+ }
4172
+ console.log(' ' + schema.name + ' : ' + copied + ' copied, ' + errors + ' errors');
4173
+ totalCopied += copied;
4174
+ totalErrors += errors;
4175
+ } catch (e) {
4176
+ console.log(' ' + schema.name + ' : ERROR ' + e.message.slice(0,80));
4177
+ totalErrors++;
4178
+ }
4179
+ }
4180
+ console.log('');
4181
+ console.log(' Total : ' + totalCopied + ' records copied, ' + totalErrors + ' errors');
4182
+ await src.disconnect();
4183
+ await dst.disconnect();
4184
+ " 2>&1 | sed 's/^/ /'
4185
+ ;;
4092
4186
  2|4)
4093
4187
  # ─── Replication from old DB ───
4094
4188
  echo
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mostajs/orm-cli",
3
- "version": "0.6.0",
3
+ "version": "0.6.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",