@abejarano/ts-mongodb-criteria 1.8.0 → 1.8.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
@@ -389,6 +389,31 @@ yarn build
389
389
  6. **Push** to your branch (`git push origin feature/amazing-feature`)
390
390
  7. **Open** a Pull Request
391
391
 
392
+ ## CLI & Migrations
393
+
394
+ To use the migration features, you must first initialize the configuration in your project:
395
+
396
+ ```bash
397
+ bun run ts-mongo init
398
+ ```
399
+
400
+ This will create a `migrate-mongo-config.js` file in your project root. You must configure your MongoDB connection settings in this file (or via environment variables as defined in the config).
401
+
402
+ ### Commands
403
+
404
+ | Command | Description |
405
+ |---|---|
406
+ | `init` | Initialize configuration file |
407
+ | `migrate:create <name>` | Create a new migration file |
408
+ | `migrate:up` | Run pending migrations |
409
+ | `migrate:down` | Revert the last applied migration |
410
+ | `migrate:status` | Check the status of migrations |
411
+
412
+ Example:
413
+ ```bash
414
+ bun run ts-mongo migrate:create add-users-collection
415
+ ```
416
+
392
417
  ## 📄 License
393
418
 
394
419
  This project is licensed under the **MIT License**. See the [LICENSE](LICENSE) file for details.
@@ -1,10 +1,14 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
3
6
  Object.defineProperty(exports, "__esModule", { value: true });
4
7
  const child_process_1 = require("child_process");
8
+ const fs_1 = __importDefault(require("fs"));
9
+ const path_1 = __importDefault(require("path"));
5
10
  const args = process.argv.slice(2);
6
11
  const command = args[0];
7
- const otherArgs = args.slice(1).join(" ");
8
12
  if (!command) {
9
13
  console.error("Please provide a command: migrate:create, migrate:up, migrate:down, migrate:status");
10
14
  process.exit(1);
@@ -24,12 +28,41 @@ if (!validCommands.includes(normalizedCommand)) {
24
28
  console.error(`Available commands: ${validCommands.map(c => `migrate:${c}`).join(", ")}`);
25
29
  process.exit(1);
26
30
  }
31
+ let extraOptions = "";
32
+ // Logic to handle ESM projects automatically
33
+ if (normalizedCommand === "init") {
34
+ // Check if -m is already passed
35
+ if (!args.includes("-m") && !args.includes("--module")) {
36
+ const packageJsonPath = path_1.default.join(process.cwd(), "package.json");
37
+ if (fs_1.default.existsSync(packageJsonPath)) {
38
+ try {
39
+ const pkg = JSON.parse(fs_1.default.readFileSync(packageJsonPath, "utf-8"));
40
+ if (pkg.type === "module") {
41
+ extraOptions += " -m esm";
42
+ }
43
+ }
44
+ catch (e) {
45
+ // ignore
46
+ }
47
+ }
48
+ }
49
+ }
50
+ else if (["create", "up", "down", "status"].includes(normalizedCommand)) {
51
+ // Check if -f is passed
52
+ if (!args.includes("-f") && !args.includes("--file")) {
53
+ const cjsConfig = "migrate-mongo-config.cjs";
54
+ if (fs_1.default.existsSync(path_1.default.join(process.cwd(), cjsConfig))) {
55
+ extraOptions += ` -f ${cjsConfig}`;
56
+ }
57
+ }
58
+ }
27
59
  try {
28
60
  // Execute migrate-mongo from the dependency
29
61
  // Using `bunx` or `npx` might be safer to find the binary, or resolve it directly.
30
62
  // Since it is a dependency, it should be in node_modules/.bin/migrate-mongo
31
63
  const binPath = "./node_modules/.bin/migrate-mongo";
32
- const finalCommand = `${binPath} ${normalizedCommand} ${otherArgs}`;
64
+ const otherArgs = args.slice(1).join(" ");
65
+ const finalCommand = `${binPath} ${normalizedCommand} ${otherArgs}${extraOptions}`;
33
66
  console.log(`Running: ${finalCommand}`);
34
67
  (0, child_process_1.execSync)(finalCommand, { stdio: "inherit" });
35
68
  }
@@ -1,8 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
  import { execSync } from "child_process";
3
+ import fs from "fs";
4
+ import path from "path";
3
5
  const args = process.argv.slice(2);
4
6
  const command = args[0];
5
- const otherArgs = args.slice(1).join(" ");
6
7
  if (!command) {
7
8
  console.error("Please provide a command: migrate:create, migrate:up, migrate:down, migrate:status");
8
9
  process.exit(1);
@@ -22,12 +23,41 @@ if (!validCommands.includes(normalizedCommand)) {
22
23
  console.error(`Available commands: ${validCommands.map(c => `migrate:${c}`).join(", ")}`);
23
24
  process.exit(1);
24
25
  }
26
+ let extraOptions = "";
27
+ // Logic to handle ESM projects automatically
28
+ if (normalizedCommand === "init") {
29
+ // Check if -m is already passed
30
+ if (!args.includes("-m") && !args.includes("--module")) {
31
+ const packageJsonPath = path.join(process.cwd(), "package.json");
32
+ if (fs.existsSync(packageJsonPath)) {
33
+ try {
34
+ const pkg = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
35
+ if (pkg.type === "module") {
36
+ extraOptions += " -m esm";
37
+ }
38
+ }
39
+ catch (e) {
40
+ // ignore
41
+ }
42
+ }
43
+ }
44
+ }
45
+ else if (["create", "up", "down", "status"].includes(normalizedCommand)) {
46
+ // Check if -f is passed
47
+ if (!args.includes("-f") && !args.includes("--file")) {
48
+ const cjsConfig = "migrate-mongo-config.cjs";
49
+ if (fs.existsSync(path.join(process.cwd(), cjsConfig))) {
50
+ extraOptions += ` -f ${cjsConfig}`;
51
+ }
52
+ }
53
+ }
25
54
  try {
26
55
  // Execute migrate-mongo from the dependency
27
56
  // Using `bunx` or `npx` might be safer to find the binary, or resolve it directly.
28
57
  // Since it is a dependency, it should be in node_modules/.bin/migrate-mongo
29
58
  const binPath = "./node_modules/.bin/migrate-mongo";
30
- const finalCommand = `${binPath} ${normalizedCommand} ${otherArgs}`;
59
+ const otherArgs = args.slice(1).join(" ");
60
+ const finalCommand = `${binPath} ${normalizedCommand} ${otherArgs}${extraOptions}`;
31
61
  console.log(`Running: ${finalCommand}`);
32
62
  execSync(finalCommand, { stdio: "inherit" });
33
63
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@abejarano/ts-mongodb-criteria",
3
3
  "author": "angel bejarano / angel.bejarano@jaspesoft.com",
4
- "version": "1.8.0",
4
+ "version": "1.8.2",
5
5
  "description": "Patrón Criteria para consultas MongoDB en TypeScript",
6
6
  "main": "dist/cjs/index.js",
7
7
  "module": "dist/esm/index.js",
@@ -39,6 +39,7 @@
39
39
  "test:watch": "jest --watch",
40
40
  "test:coverage": "jest --coverage",
41
41
  "test:bun": "bun test tests/bun",
42
+ "ts-mongo": "bun src/bin/cli.ts",
42
43
  "migrate:create": "migrate-mongo create",
43
44
  "migrate:up": "migrate-mongo up",
44
45
  "migrate:down": "migrate-mongo down",