-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
75 lines (67 loc) · 1.8 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* © 2024 rezamh67
* This software is licensed under the MIT License.
* See the LICENSE file for details.
*/
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const path = require('path');
// Load the protobuf
const PROTO_PATH = path.join(__dirname, 'protos', 'product.proto');
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
const productProto = grpc.loadPackageDefinition(packageDefinition).product;
// Create a gRPC client instance
const client = new productProto.ProductService('localhost:50051', grpc.credentials.createInsecure());
// Example functions to interact with the gRPC server
const listProducts = () => {
client.ListProducts({}, (err, response) => {
if (err) {
console.error(err);
} else {
console.log('Product List:', response.products);
}
});
};
const addProduct = (name, price) => {
const product = { name, price };
client.AddProduct(product, (err, response) => {
if (err) {
console.error(err);
} else {
console.log('Added Product:', response.product);
}
});
};
const updateProduct = (id, name, price) => {
const product = { id, name, price };
client.UpdateProduct(product, (err, response) => {
if (err) {
console.error(err);
} else {
console.log('Updated Product:', response.product);
}
});
};
const deleteProduct = (id) => {
client.DeleteProduct({ id }, (err, response) => {
if (err) {
console.error(err);
} else {
console.log('Deleted Product');
}
});
};
// Example usage
addProduct('Product 1', 9.99);
addProduct('Product 2', 19.99);
listProducts();
updateProduct(1, 'Updated Product 1', 14.99);
listProducts();
deleteProduct(2);
listProducts();