How to use moleculer - 10 common examples

To help you get started, we’ve selected a few moleculer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github moleculerjs / moleculer-apollo-server / test / unit / service.spec.js View on Github external
expect(res).toEqual({
				subscribe: [expect.any(Function), expect.any(Function)],
				resolve: expect.any(Function),
			});

			// Test first function
			expect(res.subscribe[0]()).toBe("iterator-result");

			expect(svc.pubsub.asyncIterator).toBeCalledTimes(1);
			expect(svc.pubsub.asyncIterator).toBeCalledWith(["a", "b"]);

			// Test second function without payload
			expect(await res.subscribe[1]()).toBe(false);

			// Test second function with payload
			const ctx = new Context(broker);
			expect(await res.subscribe[1]({ a: 5 }, { b: "John" }, ctx)).toBe("action response");

			expect(broker.call).toBeCalledTimes(1);
			expect(broker.call).toBeCalledWith(
				"posts.filter",
				{ b: "John", payload: { a: 5 } },
				ctx
			);
		});
	});
github moleculerjs / moleculer-apollo-server / test / unit / service.spec.js View on Github external
const res = svc.createAsyncIteratorResolver("posts.find");

			expect(res).toEqual({
				subscribe: expect.any(Function),
				resolve: expect.any(Function),
			});

			// Test subscribe
			const res2 = res.subscribe();

			expect(res2).toBe("iterator-result");
			expect(svc.pubsub.asyncIterator).toBeCalledTimes(1);
			expect(svc.pubsub.asyncIterator).toBeCalledWith([]);

			// Test resolve
			const ctx = new Context(broker);
			const res3 = await res.resolve({ a: 5 }, { b: "John" }, ctx);

			expect(res3).toBe("action response");
			expect(broker.call).toBeCalledTimes(1);
			expect(broker.call).toBeCalledWith("posts.find", { b: "John", payload: { a: 5 } }, ctx);
		});
github pankod / moleculerjs-boilerplate / test / Utils / DummyContext.ts View on Github external
export const getCall = (data: object) => {
		const ctx = Context.create(broker, endpoint, data);
		// eslint-disable-next-line
		ctx.call = jest.fn(async () => (broker.Promise as any).resolve({}));

		return ctx;
	};
}
github moleculerjs / moleculer-db / packages / moleculer-db-adapter-sequelize / examples / simple / index.js View on Github external
"use strict";

const { ServiceBroker } = require("moleculer");
const StoreService = require("../../../moleculer-db/index");
const ModuleChecker = require("../../../moleculer-db/test/checker");
const SequelizeAdapter = require("../../index");
const Sequelize = require("sequelize");
const Op = Sequelize.Op;

process.on("warning", e => console.warn(e.stack));

// Create broker
const broker = new ServiceBroker({
	logger: console,
	logLevel: "debug"
});
let adapter;

// Load my service
broker.createService(StoreService, {
	name: "posts",
	adapter: new SequelizeAdapter("sqlite://:memory:"),
	model: {
		name: "post",
		define: {
			title: Sequelize.STRING,
			content: Sequelize.TEXT,
			votes: Sequelize.INTEGER,
			author: Sequelize.INTEGER,
github moleculerjs / moleculer-db / packages / moleculer-db-adapter-mongoose / src / index.js View on Github external
init(broker, service) {
		this.broker = broker;
		this.service = service;

		if (this.service.schema.model) {
			this.model = this.service.schema.model;
		} else if (this.service.schema.schema) {
			if (!this.service.schema.modelName) {
				throw new ServiceSchemaError("`modelName` is required when `schema` is given in schema of service!");
			}
			this.schema = this.service.schema.schema;
			this.modelName = this.service.schema.modelName;
		}

		if (!this.model && !this.schema) {
			/* istanbul ignore next */
			throw new ServiceSchemaError("Missing `model` or `schema` definition in schema of service!");
		}
	}
github moleculerjs / moleculer-db / packages / moleculer-db-adapter-mongo / src / index.js View on Github external
init(broker, service) {
		this.broker = broker;
		this.service = service;

		if (!this.service.schema.collection) {
			/* istanbul ignore next */
			throw new ServiceSchemaError("Missing `collection` definition in schema of service!");
		}
	}
github moleculerjs / moleculer-db / packages / moleculer-db-adapter-sequelize / src / index.js View on Github external
init(broker, service) {
		this.broker = broker;
		this.service = service;

		if (!this.service.schema.model) {
			/* istanbul ignore next */
			throw new ServiceSchemaError("Missing `model` definition in schema of service!");
		}
	}
github moleculerjs / site / api-docs-generator.js View on Github external
if (param.default) {
				return param.name + " = " + param.default;
			}
			return param.name + "?";
		}
		return param.name;
	}
	if (param.type && param.type.name)
		return param.name + ": " + param.type.name.replace(/\n/g, "");

	return param.name;
}

// Read package.json to get API version number
const pkg = require("moleculer/package.json");
const apiVersion = semver.major(pkg.version) + "." + semver.minor(pkg.version);
console.log("API version:", apiVersion);

// Source files for API docs
const sourceFiles = [
	{ path: "service-broker.js", name: "ServiceBroker" },
	{ path: "service.js", name: "Service" },
	{ path: "context.js", name: "Context" }
]

// Target folder
const targetFolder = path.join(".", "source", "docs", apiVersion, "api");
console.log("Target folder:", targetFolder);
mkdir(targetFolder);

// Template
const templateFolder = path.join(__dirname, "api-template", "md");
github moleculerjs / moleculer-metrics / packages / moleculer-jaeger / examples / api / index.js View on Github external
"use strict";

const { ServiceBroker } 	= require("moleculer");
const { MoleculerError } 	= require("moleculer").Errors;
const JaegerService 		= require("../../index");
const ApiGateway			= require("moleculer-web");

const THROW_ERR = true;

// Create broker
const broker = new ServiceBroker({
	logger: console,
	logLevel: "debug",
	metrics: true,
	sampleCount: 1
});

broker.createService(ApiGateway);

// Load Jaeger service
broker.createService({
	mixins: [JaegerService],
	settings: {
		host: "192.168.0.181"
	}
});
github moleculerjs / moleculer-apollo-server / examples / simple / index.js View on Github external
"use strict";

const { ServiceBroker } = require("moleculer");
const { MoleculerClientError } = require("moleculer").Errors;

const ApiGateway = require("moleculer-web");
const { ApolloService } = require("../../index");

const broker = new ServiceBroker({ logLevel: "info", hotReload: true });

broker.createService({
	name: "api",

	mixins: [
		// Gateway
		ApiGateway,

		// GraphQL Apollo Server
		ApolloService({
			// API Gateway route options
			routeOptions: {
				path: "/graphql",
				cors: true,
				mappingPolicy: "restrict",
			},