Skip to content

jtoloui/proto-store

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

proto-store

Releases Go GitHub tag (latest by date)

What is proto-store

Proto store is central repo holding packages in multiple languages exporting client and server gRPC instances which can be installed and setup within your servers or client side servers.

Example:

npm install @jtoloui/proto-store

client

// client setup
import { ChannelCredentials } from "@grpc/grpc-js";
import { MoviesServiceClient, CreateMovieRequest } from "@jtoloui/proto-store";


export const client = new MoviesServiceClient(
	"0.0.0.0:50051",
	ChannelCredentials.createInsecure(),
	{},
	{}
);

// client in use

const movieRequest = CreateMovieRequest.create({
	movie: {
		title,
		year,
		director,
	},
});

client.createMovie(movieRequest, (err, response) => {
	if (err) {
		logger.error(err.message);
		return res.status(500).json({ error: err.message });
	}

	return res.status(200).json({
		id: response?.id,
		title: response?.movie?.title,
		year: response?.movie?.year,
		director: response?.movie?.director,
	});
});

server

// server setup
import { Server, ServerCredentials } from "@grpc/grpc-js";
import { IMoviesService, moviesServiceDefinition } from "@jtoloui/proto-store";

const server = new Server();

const service: IMoviesService = {
	getMovieById,
	createMovie,
};

connectDB();

server.addService(moviesServiceDefinition, service);
server.bindAsync(
	"localhost:50051",
	ServerCredentials.createInsecure(),
	(err: Error | null, port: number) => {
		const logger = log("server");
		if (err) {
			console.log(err)
		} else {
			server.start();
		}
	}
);