mgc 1.2.0 → 1.2.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/bin/generate.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import path from "path";
|
|
4
|
+
import { fileURLToPath } from "url";
|
|
5
|
+
import { spawnSync } from "child_process";
|
|
4
6
|
import { program } from "commander";
|
|
5
7
|
|
|
6
8
|
import { generateModule } from "../services/filecopy.service.js";
|
|
7
9
|
|
|
10
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
11
|
+
|
|
8
12
|
program
|
|
9
13
|
.command("gen <modulePath>")
|
|
10
14
|
.description("Generate a module project")
|
|
@@ -21,4 +25,12 @@ program
|
|
|
21
25
|
generateModule(modulePath, currentDir);
|
|
22
26
|
});
|
|
23
27
|
|
|
28
|
+
program
|
|
29
|
+
.command("setup")
|
|
30
|
+
.description("Run the MGC setup")
|
|
31
|
+
.action(() => {
|
|
32
|
+
const setupPath = path.join(__dirname, "setup.js");
|
|
33
|
+
spawnSync(process.execPath, [setupPath], { stdio: "inherit" });
|
|
34
|
+
});
|
|
35
|
+
|
|
24
36
|
program.parse(process.argv);
|
package/bin/setup.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# RabbitMQ Module
|
|
2
|
+
|
|
3
|
+
## Required env variables
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
RABBITMQ_URL="amqp://"
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Usae
|
|
10
|
+
|
|
11
|
+
### Packages Required
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
yarn add amqplib zod
|
|
15
|
+
yarn add -D @types/amqplib
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Implementation
|
|
19
|
+
|
|
20
|
+
### Send to the Queue
|
|
21
|
+
```ts
|
|
22
|
+
import * as mq from "./mq.service";
|
|
23
|
+
|
|
24
|
+
type CreateUserSchema = {
|
|
25
|
+
name: string;
|
|
26
|
+
email: string;
|
|
27
|
+
password: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// send the data to the Queue
|
|
31
|
+
export async function createUserService(body: CreateUserSchema) {
|
|
32
|
+
await mq.produce("user", body); // here "user" is name of the queue
|
|
33
|
+
|
|
34
|
+
return "user will be created soon";
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Consume data from the queue
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
// NOTE: pass the type as the generic for typesafe data consumption
|
|
42
|
+
mq.consume<CreateUserSchema>("user", async (data) => { // here "user" is name of the queue
|
|
43
|
+
saveToDB(data);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
async function saveToDB(data: CreateUserSchema) {
|
|
47
|
+
console.log(data);
|
|
48
|
+
// put your db query to insert data here
|
|
49
|
+
}
|
|
50
|
+
```
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import "dotenv/config";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
const envSchema = z.object({
|
|
5
|
+
RABBITMQ_URL: z.string().startsWith("amqp://").optional(),
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
export const env = envSchema.parse(process.env);
|
|
9
|
+
|
|
10
|
+
export default {
|
|
11
|
+
rabbitmq: {
|
|
12
|
+
url: env.RABBITMQ_URL || "amqp://0.0.0.0:5672",
|
|
13
|
+
},
|
|
14
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as amqp from "amqplib";
|
|
2
|
+
import mqConfig from "./mq.config";
|
|
3
|
+
|
|
4
|
+
async function connect() {
|
|
5
|
+
const connection = await amqp.connect(mqConfig.rabbitmq.url);
|
|
6
|
+
return connection.createChannel();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export async function consume<T>(
|
|
10
|
+
queueName: string,
|
|
11
|
+
callback: (msg: T) => void
|
|
12
|
+
) {
|
|
13
|
+
const channel = await connect();
|
|
14
|
+
await channel.assertQueue(queueName);
|
|
15
|
+
|
|
16
|
+
channel.consume(queueName, (message) => {
|
|
17
|
+
const data = JSON.parse(message!.content.toString());
|
|
18
|
+
callback(data);
|
|
19
|
+
channel.ack(message!); // HINT: acknowledge that the message has been received which then will be removed from the queue
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function produce(queueName: string, msg: any) {
|
|
24
|
+
const channel = await connect();
|
|
25
|
+
|
|
26
|
+
await channel.assertQueue(queueName);
|
|
27
|
+
|
|
28
|
+
channel.sendToQueue(queueName, Buffer.from(JSON.stringify(msg)));
|
|
29
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mgc",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "A cli based tool for generating your saved modules",
|
|
5
5
|
"author": "Admond Tamang",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "bin/generate",
|
|
8
8
|
"bin": {
|
|
9
|
-
"
|
|
9
|
+
"mgc": "bin/generate.js",
|
|
10
|
+
"mgc-setup": "bin/setup.js"
|
|
10
11
|
},
|
|
11
12
|
"repository": {
|
|
12
13
|
"type": "git",
|
|
@@ -40,4 +41,4 @@
|
|
|
40
41
|
"devDependencies": {
|
|
41
42
|
"@types/nodemailer": "^6.4.9"
|
|
42
43
|
}
|
|
43
|
-
}
|
|
44
|
+
}
|