Skip to content

Commit 5187459

Browse files
committed
upgrade project root files
1 parent 0a55cbf commit 5187459

File tree

13 files changed

+1563
-667
lines changed

13 files changed

+1563
-667
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "moleculer-template-project-typescript",
3-
"version": "2.0.0",
3+
"version": "3.0.0",
44
"description": "Project template for Moleculer-based projects with typescript",
55
"main": "meta.js",
66
"scripts": {

template/.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ build/Release
3636
node_modules/
3737
jspm_packages/
3838

39-
# Typescript v1 declaration files
40-
typings/
41-
4239
# Optional npm cache directory
4340
.npm
4441

@@ -64,4 +61,4 @@ typings/
6461
.idea
6562

6663
# Don't track transpiled files
67-
dist/
64+
dist/

template/.vscode/launch.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"program": "${workspaceRoot}/node_modules/moleculer/bin/moleculer-runner.js",
1212
"sourceMaps": true,
1313
"runtimeArgs": [
14-
"--nolazy",
1514
"-r",
1615
"ts-node/register"
1716
],
@@ -30,7 +29,6 @@
3029
],
3130
"cwd": "${workspaceRoot}",
3231
"runtimeArgs": [
33-
"--inspect-brk",
3432
"--nolazy"
3533
]
3634
}

template/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM node:16-alpine
22

3-
# Working directory
3+
RUN mkdir /app
44
WORKDIR /app
55

66
# Install dependencies

template/docker-compose.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
NAMESPACE=
22
LOGGER=true
33
LOGLEVEL=info
4-
SERVICEDIR=dist/services
4+
SERVICEDIR=services
55

66
{{#if_eq transporter "NATS"}}
77
TRANSPORTER=nats://nats:4222

template/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ services:
183183

184184
{{#apiGW}}
185185
traefik:
186-
image: traefik:v2.1
186+
image: traefik:v2.4
187187
command:
188188
- "--api.insecure=true" # Don't do that in production!
189189
- "--providers.docker=true"

template/k8s.yaml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ metadata:
88
data:
99
NAMESPACE: ""
1010
LOGLEVEL: info
11-
SERVICEDIR: dist/services
11+
SERVICEDIR: services
1212
{{#if_eq transporter "NATS"}}TRANSPORTER: nats://nats:4222{{/if_eq}}
1313
{{#if_eq transporter "Redis"}}TRANSPORTER: redis://redis:6379{{/if_eq}}
1414
{{#if_eq transporter "MQTT"}}TRANSPORTER: mqtt://mqtt:1883{{/if_eq}}
@@ -40,19 +40,24 @@ spec:
4040
#########################################################
4141
# Ingress for Moleculer API Gateway
4242
#########################################################
43-
apiVersion: networking.k8s.io/v1beta1
43+
apiVersion: networking.k8s.io/v1
4444
kind: Ingress
4545
metadata:
4646
name: ingress
47+
#annotations:
48+
# kubernetes.io/ingress.class: nginx
4749
spec:
4850
rules:
4951
- host: {{projectName}}.127.0.0.1.nip.io
5052
http:
5153
paths:
5254
- path: /
55+
pathType: Prefix
5356
backend:
54-
serviceName: api
55-
servicePort: 3000
57+
service:
58+
name: api
59+
port:
60+
number: 3000
5661

5762
---
5863
#########################################################
@@ -75,16 +80,13 @@ spec:
7580
containers:
7681
- name: api
7782
image: {{projectName}}
83+
imagePullPolicy: IfNotPresent
7884
envFrom:
7985
- configMapRef:
8086
name: common-env
8187
env:
8288
- name: SERVICES
8389
value: api
84-
- name: PORT
85-
value: "3000"
86-
ports:
87-
- containerPort: 3000
8890
{{/apiGW}}
8991

9092
---
@@ -108,6 +110,7 @@ spec:
108110
containers:
109111
- name: greeter
110112
image: {{projectName}}
113+
imagePullPolicy: IfNotPresent
111114
envFrom:
112115
- configMapRef:
113116
name: common-env
@@ -137,6 +140,7 @@ spec:
137140
containers:
138141
- name: products
139142
image: {{projectName}}
143+
imagePullPolicy: IfNotPresent
140144
envFrom:
141145
- configMapRef:
142146
name: common-env

template/mixins/db.mixin.ts

Lines changed: 69 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,83 @@
1-
"use strict";
2-
3-
import { existsSync } from "fs";
4-
import { sync } from "mkdirp";
5-
import { Context, Service, ServiceSchema } from "moleculer";
1+
import fs from "fs";
2+
import type { Context, Service, ServiceSchema } from "moleculer";
3+
import type { DbAdapter, MoleculerDB } from "moleculer-db";
64
import DbService from "moleculer-db";
5+
import MongoDbAdapter from "moleculer-db-adapter-mongo";
76

8-
export default class Connection implements Partial<ServiceSchema>, ThisType<Service>{
7+
export type DbServiceMethods = {
8+
seedDb?(): Promise<void>;
9+
};
910

10-
private cacheCleanEventName: string;
11-
private collection: string;
12-
private schema: Partial<ServiceSchema> & ThisType<Service>;
11+
type DbServiceSchema = Partial<ServiceSchema> &
12+
Partial<MoleculerDB<DbAdapter>> & {
13+
collection?: string;
14+
};
1315

14-
public constructor(public collectionName: string) {
15-
this.collection = collectionName;
16-
this.cacheCleanEventName = `cache.clean.${this.collection}`;
17-
this.schema = {
18-
mixins: [DbService],
19-
events: {
20-
/**
21-
* Subscribe to the cache clean event. If it's triggered
22-
* clean the cache entries for this service.
23-
*
24-
*/
25-
async [this.cacheCleanEventName]() {
26-
if (this.broker.cacher) {
27-
await this.broker.cacher.clean(`${this.fullName}.*`);
28-
}
29-
},
30-
},
31-
methods: {
32-
/**
33-
* Send a cache clearing event when an entity changed.
34-
*
35-
* @param {String} type
36-
* @param {any} json
37-
* @param {Context} ctx
38-
*/
39-
entityChanged: async (type: string, json: any, ctx: Context) => {
40-
await ctx.broadcast(this.cacheCleanEventName);
41-
},
42-
},
43-
async started() {
44-
// Check the count of items in the DB. If it's empty,
45-
// Call the `seedDB` method of the service.
46-
if (this.seedDB) {
47-
const count = await this.adapter.count();
48-
if (count === 0) {
49-
this.logger.info(`The '${this.collection}' collection is empty. Seeding the collection...`);
50-
await this.seedDB();
51-
this.logger.info("Seeding is done. Number of records:", await this.adapter.count());
52-
}
16+
export type DbServiceThis = Service & DbServiceMethods;
17+
18+
export default function createDbServiceMixin(collection: string): DbServiceSchema {
19+
const cacheCleanEventName = `cache.clean.${collection}`;
20+
21+
const schema: DbServiceSchema = {
22+
mixins: [DbService],
23+
24+
events: {
25+
/**
26+
* Subscribe to the cache clean event. If it's triggered
27+
* clean the cache entries for this service.
28+
*/
29+
async [cacheCleanEventName](this: DbServiceThis) {
30+
if (this.broker.cacher) {
31+
await this.broker.cacher.clean(`${this.fullName}.*`);
5332
}
5433
},
55-
};
56-
}
34+
},
5735

58-
public start(){
59-
if (process.env.MONGO_URI) {
60-
// Mongo adapter
61-
// eslint-disable-next-line @typescript-eslint/no-var-requires
62-
const MongoAdapter = require("moleculer-db-adapter-mongo");
63-
this.schema.adapter = new MongoAdapter(process.env.MONGO_URI);
64-
this.schema.collection = this.collection;
65-
} else if (process.env.NODE_ENV === "test") {
66-
// NeDB memory adapter for testing
67-
// @ts-ignore
68-
this.schema.adapter = new DbService.MemoryAdapter();
69-
} else {
70-
// NeDB file DB adapter
36+
methods: {
37+
/**
38+
* Send a cache clearing event when an entity changed.
39+
*/
40+
async entityChanged(type: string, json: unknown, ctx: Context): Promise<void> {
41+
await ctx.broadcast(cacheCleanEventName);
42+
},
43+
},
7144

72-
// Create data folder
73-
if (!existsSync("./data")) {
74-
sync("./data");
45+
async started(this: DbServiceThis) {
46+
// Check the count of items in the DB. If it's empty,
47+
// call the `seedDB` method of the service.
48+
if (this.seedDB) {
49+
const count = await this.adapter.count();
50+
if (count === 0) {
51+
this.logger.info(
52+
`The '${collection}' collection is empty. Seeding the collection...`,
53+
);
54+
await this.seedDB();
55+
this.logger.info(
56+
"Seeding is done. Number of records:",
57+
await this.adapter.count(),
58+
);
59+
}
7560
}
76-
// @ts-ignore
77-
this.schema.adapter = new DbService.MemoryAdapter({ filename: `./data/${this.collection}.db` });
78-
}
61+
},
62+
};
7963

80-
return this.schema;
81-
}
64+
if (process.env.MONGO_URI) {
65+
// Mongo adapter
66+
schema.adapter = new MongoDbAdapter(process.env.MONGO_URI);
67+
schema.collection = collection;
68+
} else if (process.env.NODE_ENV === "test") {
69+
// NeDB memory adapter for testing
70+
schema.adapter = new DbService.MemoryAdapter();
71+
} else {
72+
// NeDB file DB adapter
8273

83-
public get _collection(): string {
84-
return this.collection;
85-
}
74+
// Create data folder
75+
if (!fs.existsSync("./data")) {
76+
fs.mkdirSync("./data");
77+
}
8678

87-
public set _collection(value: string) {
88-
this.collection = value;
79+
schema.adapter = new DbService.MemoryAdapter({ filename: `./data/${collection}.db` });
8980
}
81+
82+
return schema;
9083
}

0 commit comments

Comments
 (0)