@samhammer/migrate-mongo-sag 1.12.0 → 1.15.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/README.md CHANGED
@@ -124,3 +124,9 @@ module.exports = {
124
124
  moduleSystem: "commonjs"
125
125
  };
126
126
  ```
127
+
128
+ ## How to publish
129
+
130
+ - Create & Push a tag with new version number
131
+ - The CICD actions will take this version number for npm package automatically
132
+ - Check github action to validated, that package was released to npm registry.
@@ -1,2 +1,2 @@
1
1
  #! /usr/bin/env node
2
- "use strict";const r=require("migrate-mongo"),C=require("commander"),g=require("dotenv"),h=require("dotenv-expand"),R=require("@samhammer/vault-client-sag"),v=require("lodash"),L=require("child_process"),i=require("path"),f=require("fs/promises"),m=require("fs"),T=require("@elastic/ecs-winston-format"),p=require("winston"),A=require("@elastic/elasticsearch"),$=require("moment"),q=require("readline"),x="@samhammer/migrate-mongo-sag",F="1.12.0",k={"migrate-mongo":"dist/migrate-mongo.js"},B=["dist/**/*"],V=["mongodb","migration"],M="SamhammerAG",S="MIT",_={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"},O={"@elastic/ecs-winston-format":"^1.5.3","@elastic/elasticsearch":"7.17","@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",moment:"^2.30.1",winston:"^3.14.2"},j={mongodb:"^4.4.1 || ^5.0.0 || ^6.0.0"},I={node:"18.19.0",yarn:"4.0.2"},P={name:x,version:F,bin:k,files:B,keywords:V,author:M,license:S,scripts:_,devDependencies:N,dependencies:O,peerDependencies:j,volta:I},y=[".env.local",".env"];async function U(e){try{process.env.TRACE&&console.log("int env..."),await W(e),await H(),await K(),await G(),z(),process.env.TRACE&&console.log("finished init env")}catch(n){console.error("init env failed",n)}}function G(){process.env.TRACE&&console.log("loading env files...");const e=g.config({path:y});h.expand({parsed:e.parsed})}async function K(){process.env.TRACE&&console.log("loading vault...");const e=J(y);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 J(e){const n=v.clone(process.env),t=g.config({path:e,processEnv:n}),o=v.pickBy(t.parsed,c=>c.startsWith("VaultKey")),s=h.expand({parsed:o,processEnv:n});return v.invert(s.parsed)}async function W(e){process.env.TRACE&&console.log("loading command options...");const n=e.getOptionValue("env"),t=e.getOptionValue("brand"),o=e.getOptionValue("app"),s={};n&&(s.Environment=n),t&&(s.Brand=t),o&&(s.App=o),g.populate(process.env,s,{override:!0}),process.env.TRACE&&console.log("set options",s)}function H(){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 n={Branch:L.execSync("git rev-parse --abbrev-ref HEAD").toString("utf-8").replace(/[\n\r\s]+$/,"")};g.populate(process.env,n),process.env.TRACE&&console.log("set branch",n)}function z(){const t={Logger_LogFile:process.env.Logger_LogFile||"logs/log.json",Logger_LogLevel:process.env.Logger_LogLevel||"info"};g.populate(process.env,t)}async function Q(e){const n=process.env.MongoDbOptions__UserName;return n&&await e.removeUser(n),await e.dropDatabase(),{databaseName:e.databaseName,userName:n??""}}async function w(){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 D(),n=i.join(e,process.env.DefaultMigrationsDir),t=i.join(e,process.env.Brand),o=await Y();await E(n,o),await E(t,o),await b(o)}async function X(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 n=e?process.env.DefaultMigrationsDir:process.env.Brand,t=await D(),o=i.join(t,n);await b(o)}async function Y(){const e=await Z();return await ee(e),e}async function Z(){const e=i.join(process.cwd(),".temp");return m.existsSync(e)?process.env.TRACE&&console.log("using tempDir",e):(m.mkdirSync(e),process.env.TRACE&&console.log("created tempDir",e)),e}async function ee(e){const n=await f.readdir(e,{withFileTypes:!0});for(const t of n){const o=i.join(e,t.name);process.env.TRACE&&console.log("remove tempFile",o),await f.rm(o)}process.env.TRACE&&console.log("cleaned up tempDir",e)}async function b(e){const n=await r.config.read();n.migrationsDir=e,r.config.set(n),process.env.TRACE&&console.log("configured new migrationDir",e)}async function E(e,n){if(!m.existsSync(e)){process.env.TRACE&&console.log("sourceDir does not exist, skipping",e);return}let t=await f.readdir(e,{withFileTypes:!0});t=t.filter(o=>o.isFile());for(const o of t){const s=`${e}${i.sep}${o.name}`,c=`${n}${i.sep}${o.name}`;process.env.TRACE&&console.log("copy file",s,c),await f.copyFile(s,c)}}async function D(){const n=(await r.config.read()).migrationsDir;return i.isAbsolute(n)?n:i.join(process.cwd(),n)}function ne(){const e=new p.transports.Console({format:p.format.simple()}),n=T(),t=p.format(c=>({...c,fields:{Brand:process.env.Brand,Environment:process.env.Environment,Branch:process.env.Branch}}))(),o=new p.transports.File({filename:process.env.Logger_LogFile,format:p.format.combine(t,n)});return p.createLogger({level:process.env.Logger_LogLevel,transports:[e,o]})}class oe{client=se();async syncLogValues(){if(!this.client)return;const n=m.createReadStream(process.env.Logger_LogFile),t=q.createInterface({input:n,crlfDelay:1/0}),o=[];for await(const u of t)o.push(JSON.parse(u));const s=`${process.env.Logger_ClientIndex}-${te()}`,c=o.flatMap(u=>[{index:{_index:s}},u]);await this.client.bulk({body:c})}}function te(){const e=$(Date.now());return`${e.year()}.${e.week()}`}function se(){return process.env.Logger_ClientUrl?new A.Client({node:process.env.Logger_ClientUrl,auth:{username:process.env.Logger_ClientUsername,password:process.env.Logger_ClientPassword}}):null}const l=new C.Command;let a,d;l.name("migrate-mongo").description("CLI to migrate mongodb").version(P.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");l.hook("preSubcommand",async e=>{process.env.TRACE=e.getOptionValue("trace")?"on":"",process.env.TRACE&&console.log("hook pre-command..."),await U(e),a=ne(),d=new oe});l.command("create [description]").description("create a new database migration with the provided description").option("-d, --default","enforce creation in defaultMigration folder").action(async(e,n)=>{process.env.TRACE&&a.info("run command create...");let t=0;try{await X(n.default);const o=await r.create(e),s=await r.config.read();a.info(`CREATED: ${s.migrationsDir}${i.sep}${o}`)}catch(o){a.error(`ERROR: ${o.message}`,o.stack),t=1}finally{await d.syncLogValues(),process.exit(t)}});l.command("up").description("run all pending database migrations").action(async()=>{process.env.TRACE&&a.info("run command up...");const{db:e,client:n}=await r.database.connect();let t=0;try{await w(),(await r.up(e,n)).forEach(s=>a.info(`MIGRATED UP: ${s}`)),a.info("Completed migration")}catch(o){a.error(`ERROR: ${o.message}`,o.stack),o.migrated.forEach(s=>a.error(`MIGRATED UP: ${s}`)),t=1}finally{await d.syncLogValues(),await n.close(),process.exit(t)}});l.command("down").description("undo the last applied database migration").action(async()=>{process.env.TRACE&&a.info("run command down...");const{db:e,client:n}=await r.database.connect();let t=0;try{await w(),(await r.down(e,n)).forEach(s=>a.info(`MIGRATED DOWN: ${s}`))}catch(o){a.error(`ERROR: ${o.message}`,o.stack),t=1}finally{await d.syncLogValues(),await n.close(),process.exit(t)}});l.command("status").description("print the changelog of the database").action(async()=>{process.env.TRACE&&a.info("run command status...");let e=0;const{db:n,client:t}=await r.database.connect();try{await w(),(await r.status(n)).forEach(s=>a.info(`${s.appliedAt}: ${s.fileName}`))}catch(o){a.error(`ERROR: ${o.message}`,o.stack),e=1}finally{await d.syncLogValues(),await t.close(),process.exit(e)}});l.command("dropDatabase").description("deletes the database").action(async()=>{process.env.TRACE&&a.info("run command dropDatabase...");let e=0;const{db:n,client:t}=await r.database.connect();try{const o=await Q(n);a.info("DROPPED DB:",o.databaseName,o.userName)}catch(o){a.error(`ERROR: ${o.message}`,o.stack),e=1}finally{await d.syncLogValues(),await t.close(),process.exit(e)}});l.parse();
2
+ "use strict";const r=require("migrate-mongo"),L=require("commander"),g=require("dotenv"),E=require("dotenv-expand"),A=require("@samhammer/vault-client-sag"),v=require("lodash"),T=require("child_process"),i=require("path"),m=require("fs/promises"),f=require("fs"),$=require("@elastic/ecs-winston-format"),p=require("winston"),q=require("@elastic/elasticsearch"),x=require("moment"),F=require("readline"),h="@samhammer/migrate-mongo-sag",b="0.0.0",k={"migrate-mongo":"dist/migrate-mongo.js"},B=["dist/**/*"],N=["mongodb","migration"],V="SamhammerAG",M="MIT",S={build:"vite build","migrate-mongo":"vite build && node dist/migrate-mongo.js",format:"prettier --write --parser typescript ./src",lint:"eslint ./src",test:"vitest run"},_={"@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.14",vitest:"^1.6.0"},O={"@elastic/ecs-winston-format":"^1.5.3","@elastic/elasticsearch":"7.17","@samhammer/vault-client-sag":"^1.2.0",commander:"^12.1.0",dotenv:"^16.4.5","dotenv-expand":"^11.0.6",lodash:"^4.17.21","migrate-mongo":"^11.0.0",moment:"^2.30.1",winston:"^3.14.2"},j={mongodb:"^4.4.1 || ^5.0.0 || ^6.0.0"},I={node:"18.19.0",yarn:"4.0.2"},P={name:h,version:b,bin:k,files:B,keywords:N,author:V,license:M,scripts:S,devDependencies:_,dependencies:O,peerDependencies:j,volta:I},D=[".env.local",".env"];async function U(e){try{process.env.TRACE&&console.log("int env..."),await W(e),await H(),await K(),await G(),z(),process.env.TRACE&&console.log("finished init env")}catch(n){console.error("init env failed",n)}}function G(){process.env.TRACE&&console.log("loading env files...");const e=g.config({path:D});E.expand({parsed:e.parsed})}async function K(){process.env.TRACE&&console.log("loading vault...");const e=J(D);process.env.TRACE&&console.log("requesting vault keys",e),await(await A.getVault()).loadSecretsToEnv(e),process.env.TRACE&&console.log("finished loading from vault")}function J(e){const n=v.clone(process.env),t=g.config({path:e,processEnv:n}),o=v.pickBy(t.parsed,c=>c.startsWith("VaultKey")),s=E.expand({parsed:o,processEnv:n});return v.invert(s.parsed)}async function W(e){process.env.TRACE&&console.log("loading command options...");const n=e.getOptionValue("env"),t=e.getOptionValue("brand"),o=e.getOptionValue("app"),s={};n&&(s.Environment=n),t&&(s.Brand=t),o&&(s.App=o),g.populate(process.env,s,{override:!0}),process.env.TRACE&&console.log("set options",s)}function H(){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 n={Branch:T.execSync("git rev-parse --abbrev-ref HEAD").toString("utf-8").replace(/[\n\r\s]+$/,"")};g.populate(process.env,n),process.env.TRACE&&console.log("set branch",n)}function z(){const t={Logger_LogFile:process.env.Logger_LogFile||"logs/log.json",Logger_LogLevel:process.env.Logger_LogLevel||"info"};g.populate(process.env,t)}async function Q(e){const n=process.env.MongoDbOptions__UserName;return n&&await e.removeUser(n),await e.dropDatabase(),{databaseName:e.databaseName,userName:n??""}}async function w(){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 R(),n=i.join(e,process.env.DefaultMigrationsDir),t=i.join(e,process.env.Brand),o=await Y();await y(n,o),await y(t,o),await C(o)}async function X(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 n=e?process.env.DefaultMigrationsDir:process.env.Brand,t=await R(),o=i.join(t,n);await C(o)}async function Y(){const e=await Z();return await ee(e),e}async function Z(){const e=i.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 ee(e){const n=await m.readdir(e,{withFileTypes:!0});for(const t of n){const o=i.join(e,t.name);process.env.TRACE&&console.log("remove tempFile",o),await m.rm(o)}process.env.TRACE&&console.log("cleaned up tempDir",e)}async function C(e){const n=await r.config.read();n.migrationsDir=e,r.config.set(n),process.env.TRACE&&console.log("configured new migrationDir",e)}async function y(e,n){if(!f.existsSync(e)){process.env.TRACE&&console.log("sourceDir does not exist, skipping",e);return}let t=await m.readdir(e,{withFileTypes:!0});t=t.filter(o=>o.isFile());for(const o of t){const s=`${e}${i.sep}${o.name}`,c=`${n}${i.sep}${o.name}`;process.env.TRACE&&console.log("copy file",s,c),await m.copyFile(s,c)}}async function R(){const n=(await r.config.read()).migrationsDir;return i.isAbsolute(n)?n:i.join(process.cwd(),n)}function ne(){const e=new p.transports.Console({format:p.format.simple()}),n=$(),t=p.format(c=>({...c,fields:{Brand:process.env.Brand,EnvironmentName:process.env.Environment,BranchName:process.env.Branch,AssemblyName:h,AssemblyVersion:b}}))(),o=new p.transports.File({filename:process.env.Logger_LogFile,format:p.format.combine(t,n)});return p.createLogger({level:process.env.Logger_LogLevel,transports:[e,o]})}class oe{client=se();async syncLogValues(){if(!this.client)return;const n=f.createReadStream(process.env.Logger_LogFile),t=F.createInterface({input:n,crlfDelay:1/0}),o=[];for await(const u of t)o.push(JSON.parse(u));const s=`${process.env.Logger_ClientIndex}-${te()}`,c=o.flatMap(u=>[{index:{_index:s}},u]);await this.client.bulk({body:c})}}function te(){const e=x(Date.now());return`${e.year()}.${e.week()}`}function se(){return process.env.Logger_ClientUrl?new q.Client({node:process.env.Logger_ClientUrl,auth:{username:process.env.Logger_ClientUsername,password:process.env.Logger_ClientPassword}}):null}const l=new L.Command;let a,d;l.name("migrate-mongo").description("CLI to migrate mongodb").version(P.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");l.hook("preSubcommand",async e=>{process.env.TRACE=e.getOptionValue("trace")?"on":"",process.env.TRACE&&console.log("hook pre-command..."),await U(e),a=ne(),d=new oe});l.command("create [description]").description("create a new database migration with the provided description").option("-d, --default","enforce creation in defaultMigration folder").action(async(e,n)=>{process.env.TRACE&&a.info("run command create...");let t=0;try{await X(n.default);const o=await r.create(e),s=await r.config.read();a.info(`CREATED: ${s.migrationsDir}${i.sep}${o}`)}catch(o){a.error(`ERROR: ${o.message}`,o.stack),t=1}finally{await d.syncLogValues(),process.exit(t)}});l.command("up").description("run all pending database migrations").action(async()=>{process.env.TRACE&&a.info("run command up...");const{db:e,client:n}=await r.database.connect();let t=0;try{await w(),(await r.up(e,n)).forEach(s=>a.info(`MIGRATED UP: ${s}`)),a.info("Completed migration")}catch(o){a.error(`ERROR: ${o.message}`,o.stack),o.migrated.forEach(s=>a.error(`MIGRATED UP: ${s}`)),t=1}finally{await d.syncLogValues(),await n.close(),process.exit(t)}});l.command("down").description("undo the last applied database migration").action(async()=>{process.env.TRACE&&a.info("run command down...");const{db:e,client:n}=await r.database.connect();let t=0;try{await w(),(await r.down(e,n)).forEach(s=>a.info(`MIGRATED DOWN: ${s}`))}catch(o){a.error(`ERROR: ${o.message}`,o.stack),t=1}finally{await d.syncLogValues(),await n.close(),process.exit(t)}});l.command("status").description("print the changelog of the database").action(async()=>{process.env.TRACE&&a.info("run command status...");let e=0;const{db:n,client:t}=await r.database.connect();try{await w(),(await r.status(n)).forEach(s=>a.info(`${s.appliedAt}: ${s.fileName}`))}catch(o){a.error(`ERROR: ${o.message}`,o.stack),e=1}finally{await d.syncLogValues(),await t.close(),process.exit(e)}});l.command("dropDatabase").description("deletes the database").action(async()=>{process.env.TRACE&&a.info("run command dropDatabase...");let e=0;const{db:n,client:t}=await r.database.connect();try{const o=await Q(n);a.info("DROPPED DB:",o.databaseName,o.userName)}catch(o){a.error(`ERROR: ${o.message}`,o.stack),e=1}finally{await d.syncLogValues(),await t.close(),process.exit(e)}});l.parse();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samhammer/migrate-mongo-sag",
3
- "version": "1.12.0",
3
+ "version": "1.15.0",
4
4
  "bin": {
5
5
  "migrate-mongo": "dist/migrate-mongo.js"
6
6
  },
@@ -34,13 +34,13 @@
34
34
  "mongodb": "^4.17.2",
35
35
  "prettier": "^3.3.2",
36
36
  "typescript": "^5.4.5",
37
- "vite": "^5.2.13",
37
+ "vite": "^5.2.14",
38
38
  "vitest": "^1.6.0"
39
39
  },
40
40
  "dependencies": {
41
41
  "@elastic/ecs-winston-format": "^1.5.3",
42
42
  "@elastic/elasticsearch": "7.17",
43
- "@samhammer/vault-client-sag": "^1.1.1",
43
+ "@samhammer/vault-client-sag": "^1.2.0",
44
44
  "commander": "^12.1.0",
45
45
  "dotenv": "^16.4.5",
46
46
  "dotenv-expand": "^11.0.6",