Skip to content

Commit fb1ee31

Browse files
committed
Fix DB mixin code
1 parent bfbec5f commit fb1ee31

File tree

4 files changed

+15
-21
lines changed

4 files changed

+15
-21
lines changed

template/mixins/db.mixin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Context, ServiceSettingSchema, ServiceSchema } from "moleculer";
1+
import type { ServiceSettingSchema, ServiceSchema } from "moleculer";
22
import { Service as DbService } from "@moleculer/database";
33
import type { DatabaseMethods, DatabaseMixinOptions } from "@moleculer/database";
44
import _ from "lodash";
@@ -8,7 +8,7 @@ export type DbServiceMethods = DatabaseMethods & {
88
seedDB?(): Promise<void>;
99
};
1010

11-
export interface DbMixinOpts extends DatabaseMixinOptions{
11+
export interface DbMixinOpts extends DatabaseMixinOptions {
1212
collection: string;
1313
}
1414

@@ -89,4 +89,4 @@ export default function (opts: DbMixinOpts): DbServiceSchema {
8989
};
9090

9191
return schema;
92-
};
92+
}

template/services/api.service.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import type { Context, ServiceSchema } from "moleculer";
2-
import type { Server as HttpServer } from 'http';
2+
import type { Server as HttpServer } from "http";
33
import ApiGateway from "moleculer-web";
44
import type { ApiSettingsSchema, Route, IncomingRequest, GatewayResponse } from "moleculer-web";
55
{{#apiIO}}
66
import SocketIOService from "moleculer-io";
77
{{/apiIO}}
88
{{#apiGQL}}
99
import { ApolloService } from "moleculer-apollo-server";
10-
import type { ApolloServiceMethods, ApolloServiceLocalVars, ApolloServiceSettings } from "moleculer-apollo-server";
10+
import type {
11+
ApolloServiceMethods,
12+
ApolloServiceLocalVars,
13+
ApolloServiceSettings
14+
} from "moleculer-apollo-server";
1115
import { GraphQLJSONObject } from "graphql-type-json";
1216
{{/apiGQL}}
1317

@@ -26,6 +30,10 @@ interface LocalVars {
2630
io: any;
2731
}
2832

33+
if (process.env.TEST === "true") {
34+
process.env.PORT = "0"; // Use random ports during tests
35+
}
36+
2937
const ApiService: ServiceSchema<ApiSettingsSchema {{#apiGQL}}& ApolloServiceSettings, ApolloServiceMethods, LocalVars & ApolloServiceLocalVars{{/apiGQL}}> = {
3038
name: "api",
3139

@@ -60,7 +68,7 @@ const ApiService: ServiceSchema<ApiSettingsSchema {{#apiGQL}}& ApolloServiceSett
6068
/** More info: https://moleculer.services/docs/0.15/moleculer-web.html */
6169
settings: {
6270
// Exposed port
63-
port: process.env.PORT ? parseInt(process.env.PORT) : 3000,
71+
port: process.env.PORT != null ? parseInt(process.env.PORT) : 3000,
6472

6573
// Exposed IP
6674
ip: "0.0.0.0",

template/services/orders.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Context, Service, ServiceSchema, ServiceSettingSchema } from "moleculer";
1+
import type { Context, Service, ServiceSchema } from "moleculer";
22

33
interface WorkflowPayload {
44
card?: string;

template/test/unit/mixins/db.mixin.spec.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ describe("Test DB mixin", () => {
1515

1616
expect(schema.started).toBeDefined();
1717
expect(schema.events["cache.clean.my-collection"]).toBeInstanceOf(Function);
18-
expect(schema.methods.entityChanged).toBeInstanceOf(Function);
1918
});
2019

2120
it("check cache event handler", async () => {
@@ -82,18 +81,5 @@ describe("Test DB mixin", () => {
8281
expect(seedDBFn).toBeCalledWith();
8382
});
8483
});
85-
86-
it("should broadcast a cache clear event", async () => {
87-
const schema = DbMixin({ collection: "my-collection" });
88-
89-
const ctx = {
90-
broadcast: vi.fn()
91-
};
92-
93-
await schema.methods!.entityChanged(null, null, null, ctx);
94-
95-
expect(ctx.broadcast).toBeCalledTimes(1);
96-
expect(ctx.broadcast).toBeCalledWith("cache.clean.my-collection");
97-
});
9884
});
9985
});

0 commit comments

Comments
 (0)