@samhammer/migrate-mongo-sag 1.2.0 → 1.3.0
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/dist/migrate-mongo.js +1 -1
- package/package.json +4 -2
package/dist/migrate-mongo.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#! /usr/bin/env node
|
|
2
|
-
"use strict";const
|
|
2
|
+
"use strict";const i=require("migrate-mongo"),b=require("commander"),d=require("dotenv"),w=require("dotenv-expand"),R=require("@samhammer/vault-client-sag"),g=require("lodash"),D=require("child_process"),a=require("path"),l=require("fs/promises"),f=require("fs"),C=require("@elastic/ecs-winston-format"),m=require("winston"),T="@samhammer/migrate-mongo-sag",A="1.3.0",$={"migrate-mongo":"dist/migrate-mongo.js"},q=["dist/**/*"],k=["mongodb","migration"],M="SamhammerAG",x="MIT",B={build:"vite build","migrate-mongo":"vite build && node dist/migrate-mongo.js",format:"prettier --write --parser typescript ./src",lint:"eslint ./src",test:"vitest run"},N={"@rushstack/eslint-patch":"^1.10.3","@types/lodash":"^4.17.5","@types/migrate-mongo":"^8.2.0","@types/node":"^18.19.34","@typescript-eslint/eslint-plugin":"^7.13.0","@typescript-eslint/parser":"^7.13.0",eslint:"^8.57.0","eslint-config-prettier":"^8.10.0","eslint-plugin-import":"^2.29.1","eslint-plugin-prettier":"^5.1.3",mongodb:"^4.17.2",prettier:"^3.3.2",typescript:"^5.4.5",vite:"^5.2.13",vitest:"^1.6.0"},F={"@elastic/ecs-winston-format":"^1.5.3","@samhammer/vault-client-sag":"^1.1.1",commander:"^12.1.0",dotenv:"^16.4.5","dotenv-expand":"^11.0.6",lodash:"^4.17.21","migrate-mongo":"^11.0.0",winston:"^3.14.2"},O={mongodb:"^4.4.1 || ^5.0.0 || ^6.0.0"},S={node:"18.19.0",yarn:"4.0.2"},L={name:T,version:A,bin:$,files:q,keywords:k,author:M,license:x,scripts:B,devDependencies:N,dependencies:F,peerDependencies:O,volta:S},E=[".env.local",".env"];async function j(e){try{process.env.TRACE&&console.log("int env..."),await I(e),await G(),await P(),await V(),process.env.TRACE&&console.log("finished init env")}catch(o){console.error("init env failed",o)}}function V(){process.env.TRACE&&console.log("loading env files...");const e=d.config({path:E});w.expand({parsed:e.parsed})}async function P(){process.env.TRACE&&console.log("loading vault...");const e=_(E);process.env.TRACE&&console.log("requesting vault keys",e),await(await R.getVault()).loadSecretsToEnv(e),process.env.TRACE&&console.log("finished loading from vault")}function _(e){const o=g.clone(process.env),t=d.config({path:e,processEnv:o}),n=g.pickBy(t.parsed,p=>p.startsWith("VaultKey")),s=w.expand({parsed:n,processEnv:o});return g.invert(s.parsed)}async function I(e){process.env.TRACE&&console.log("loading command options...");const o=e.getOptionValue("env"),t=e.getOptionValue("brand"),n=e.getOptionValue("app"),s={};o&&(s.Environment=o),t&&(s.Brand=t),n&&(s.App=n),d.populate(process.env,s,{override:!0}),process.env.TRACE&&console.log("set options",s)}function G(){if(process.env.TRACE&&console.log("loading branch..."),process.env.Branch){process.env.TRACE&&console.log("skip branch cause its defined already",process.env.Branch);return}const o={Branch:D.execSync("git rev-parse --abbrev-ref HEAD").toString("utf-8").replace(/[\n\r\s]+$/,"")};d.populate(process.env,o),process.env.TRACE&&console.log("set branch",o)}async function U(e){const o=process.env.MongoDbOptions__UserName;return o&&await e.removeUser(o),await e.dropDatabase(),{databaseName:e.databaseName,userName:o??""}}async function u(){if(!process.env.DefaultMigrationsDir)throw new Error("enviroment variable DefaultMigrationsDir is required");if(!process.env.Brand)throw new Error("enviroment variable Brand is required");const e=await y(),o=a.join(e,process.env.DefaultMigrationsDir),t=a.join(e,process.env.Brand),n=await W();await v(o,n),await v(t,n),await h(n)}async function K(e){if(e&&!process.env.DefaultMigrationsDir)throw new Error("enviroment variable DefaultMigrationsDir is required");if(!e&&!process.env.Brand)throw new Error("enviroment variable Brand is required");const o=e?process.env.DefaultMigrationsDir:process.env.Brand,t=await y(),n=a.join(t,o);await h(n)}async function W(){const e=await H();return await z(e),e}async function H(){const e=a.join(process.cwd(),".temp");return f.existsSync(e)?process.env.TRACE&&console.log("using tempDir",e):(f.mkdirSync(e),process.env.TRACE&&console.log("created tempDir",e)),e}async function z(e){const o=await l.readdir(e,{withFileTypes:!0});for(const t of o){const n=a.join(e,t.name);process.env.TRACE&&console.log("remove tempFile",n),await l.rm(n)}process.env.TRACE&&console.log("cleaned up tempDir",e)}async function h(e){const o=await i.config.read();o.migrationsDir=e,i.config.set(o),process.env.TRACE&&console.log("configured new migrationDir",e)}async function v(e,o){if(!f.existsSync(e)){process.env.TRACE&&console.log("sourceDir does not exist, skipping",e);return}let t=await l.readdir(e,{withFileTypes:!0});t=t.filter(n=>n.isFile());for(const n of t){const s=`${e}${a.sep}${n.name}`,p=`${o}${a.sep}${n.name}`;process.env.TRACE&&console.log("copy file",s,p),await l.copyFile(s,p)}}async function y(){const o=(await i.config.read()).migrationsDir;return a.isAbsolute(o)?o:a.join(process.cwd(),o)}function c(){return m.createLogger({level:process.env.Logger_LogLevel,format:C({convertReqRes:!0}),transports:[new m.transports.Console,new m.transports.File({filename:process.env.Logger_LogFile,level:process.env.Logger_LogLevel})]})}const r=new b.Command;r.name("migrate-mongo").description("CLI to migrate mongodb").version(L.version).option("-e, --env <env>","set process.env.Environment").option("-b, --brand <brand>","set process.env.Brand").option("-a, --app <app>","set process.env.App").option("-t, --trace","set process.env.TRACE to enable trace outputs");r.hook("preSubcommand",async e=>{process.env.TRACE=e.getOptionValue("trace")?"on":"",process.env.TRACE&&console.log("hook pre-command..."),await j(e)});r.command("create [description]").description("create a new database migration with the provided description").option("-d, --default","enforce creation in defaultMigration folder").action(async(e,o)=>{const t=c();process.env.TRACE&&t.info("run command create...");try{await K(o.default);const n=await i.create(e),s=await i.config.read();t.info(`CREATED: ${s.migrationsDir}${a.sep}${n}`)}catch(n){t.error(`ERROR: ${n.message}`,n.stack),process.exit(1)}});r.command("up").description("run all pending database migrations").action(async()=>{const e=c();process.env.TRACE&&e.info("run command up...");const{db:o,client:t}=await i.database.connect();try{await u(),(await i.up(o,t)).forEach(s=>e.info(`MIGRATED UP: ${s}`)),e.info("Completed migration")}catch(n){e.error(`ERROR: ${n.message}`,n.stack),n.migrated.forEach(s=>e.error(`MIGRATED UP: ${s}`)),process.exit(1)}finally{await t.close()}});r.command("down").description("undo the last applied database migration").action(async()=>{const e=c();process.env.TRACE&&e.info("run command down...");const{db:o,client:t}=await i.database.connect();try{await u(),(await i.down(o,t)).forEach(s=>e.info(`MIGRATED DOWN: ${s}`))}catch(n){e.error(`ERROR: ${n.message}`,n.stack),process.exit(1)}finally{await t.close()}});r.command("status").description("print the changelog of the database").action(async()=>{const e=c();process.env.TRACE&&e.info("run command status...");const{db:o,client:t}=await i.database.connect();try{await u(),(await i.status(o)).forEach(s=>e.info(`${s.appliedAt}: ${s.fileName}`))}catch(n){e.error(`ERROR: ${n.message}`,n.stack),process.exit(1)}finally{await t.close()}});r.command("dropDatabase").description("deletes the database").action(async()=>{const e=c();process.env.TRACE&&e.info("run command dropDatabase...");const{db:o,client:t}=await i.database.connect();try{const n=await U(o);e.info("DROPPED DB:",n.databaseName,n.userName)}catch(n){e.error(`ERROR: ${n.message}`,n.stack),process.exit(1)}finally{await t.close()}});r.parse();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@samhammer/migrate-mongo-sag",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"bin": {
|
|
5
5
|
"migrate-mongo": "dist/migrate-mongo.js"
|
|
6
6
|
},
|
|
@@ -38,12 +38,14 @@
|
|
|
38
38
|
"vitest": "^1.6.0"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
+
"@elastic/ecs-winston-format": "^1.5.3",
|
|
41
42
|
"@samhammer/vault-client-sag": "^1.1.1",
|
|
42
43
|
"commander": "^12.1.0",
|
|
43
44
|
"dotenv": "^16.4.5",
|
|
44
45
|
"dotenv-expand": "^11.0.6",
|
|
45
46
|
"lodash": "^4.17.21",
|
|
46
|
-
"migrate-mongo": "^11.0.0"
|
|
47
|
+
"migrate-mongo": "^11.0.0",
|
|
48
|
+
"winston": "^3.14.2"
|
|
47
49
|
},
|
|
48
50
|
"peerDependencies": {
|
|
49
51
|
"mongodb": "^4.4.1 || ^5.0.0 || ^6.0.0"
|