|
| 1 | +const cds = require("@sap/cds"); |
| 2 | + |
| 3 | +module.exports = class Service1 extends cds.ApplicationService { |
| 4 | + init() { |
| 5 | + /* ========== Service1 running query on the database service using `cds.run` and friends using Fluent API ========== */ |
| 6 | + this.on("send11", async (req) => { |
| 7 | + const { id } = req.data; |
| 8 | + const query = SELECT.from`Entity1`.where("ID=" + id); |
| 9 | + cds.run(query); |
| 10 | + }); |
| 11 | + |
| 12 | + this.on("send12", async (req) => { |
| 13 | + const { id } = req.data; |
| 14 | + cds.read("Entity1").where("ID =" + id); |
| 15 | + }); |
| 16 | + |
| 17 | + this.on("send13", async (req) => { |
| 18 | + const { id } = req.data; |
| 19 | + cds.create("Entity1").entries({id: "" + id}); |
| 20 | + }); |
| 21 | + |
| 22 | + this.on("send14", async (req) => { |
| 23 | + const { id, amount } = req.data; |
| 24 | + cds.update("Entity1").set("col1 = col1" + amount).where("col1 = " + id); |
| 25 | + }); |
| 26 | + |
| 27 | + this.on("send15", async (req) => { |
| 28 | + const { id } = req.data; |
| 29 | + cds.upsert("Entity1").entries({id: "" + id}); |
| 30 | + }); |
| 31 | + |
| 32 | + this.on("send16", async (req) => { |
| 33 | + const { id } = req.data; |
| 34 | + cds.delete("Entity1").where("ID =" + id); |
| 35 | + }); |
| 36 | + |
| 37 | + /* ========== Service1 running query on itself by `await`-ing the query ========== */ |
| 38 | + this.on("send21", async (req) => { |
| 39 | + const { id } = req.data; |
| 40 | + const { Service1Entity } = this.entities; |
| 41 | + await SELECT.from(Service1Entity).where("ID=" + id); |
| 42 | + }); |
| 43 | + |
| 44 | + this.on("send22", async (req) => { |
| 45 | + const { id } = req.data; |
| 46 | + const { Service1Entity } = this.entities; |
| 47 | + await INSERT.into(Service1Entity).entries({ id: "" + id }); |
| 48 | + }); |
| 49 | + |
| 50 | + this.on("send23", async (req) => { |
| 51 | + const { id, amount } = req.data; |
| 52 | + const { Service1Entity } = this.entities; |
| 53 | + await UPDATE.entity(Service1Entity).set(`col1 = col1 -` + amount).where("id=" + id); |
| 54 | + }); |
| 55 | + |
| 56 | + this.on("send24", async (req) => { |
| 57 | + const { id } = req.data; |
| 58 | + const { Service1Entity } = this.entities; |
| 59 | + await UPSERT.into(Service1Entity).entries({ id: "" + id }); |
| 60 | + }); |
| 61 | + |
| 62 | + this.on("send25", async (req) => { |
| 63 | + const { id } = req.data; |
| 64 | + const { Service1Entity } = this.entities; |
| 65 | + await DELETE.from(Service1Entity).where("ID =" + id); |
| 66 | + }); |
| 67 | + |
| 68 | + /* ========== Service1 running query on itself using `this.run` and friends using Fluent API ========== */ |
| 69 | + this.on("send31", async (req) => { |
| 70 | + const { id } = req.data; |
| 71 | + const query = SELECT.from`Service1Entity`.where("ID=" + id); |
| 72 | + this.run(query); |
| 73 | + }); |
| 74 | + |
| 75 | + this.on("send32", async (req) => { |
| 76 | + const { id } = req.data; |
| 77 | + this.read(`Service1Entity`).where("ID =" + id); |
| 78 | + }); |
| 79 | + |
| 80 | + this.on("send33", async (req) => { |
| 81 | + const { id } = req.data; |
| 82 | + this.create(`Service1Entity`).entries({id: "" + id}); |
| 83 | + }); |
| 84 | + |
| 85 | + this.on("send34", async (req) => { |
| 86 | + const { id, amount } = req.data; |
| 87 | + this.update(`Service1Entity`).set("col1 = col1" + amount).where("col1 = " + id); |
| 88 | + }); |
| 89 | + |
| 90 | + this.on("send35", async (req) => { |
| 91 | + const { id } = req.data; |
| 92 | + this.upsert(`Service1Entity`).entries({id: "" + id}); |
| 93 | + }); |
| 94 | + |
| 95 | + this.on("send36", async (req) => { |
| 96 | + const { id } = req.data; |
| 97 | + this.delete(`Service1Entity`).where("ID =" + id); |
| 98 | + }); |
| 99 | + |
| 100 | + /* ========== Service1 running query on Service2 using `Service2.run` and friends ========== */ |
| 101 | + this.on("send41", async (req) => { |
| 102 | + const { id } = req.data; |
| 103 | + const { Service2 } = await cds.connect.to("Service2"); |
| 104 | + const query = SELECT.from`Service1Entity`.where("ID=" + id); |
| 105 | + Service2.run(query); |
| 106 | + }); |
| 107 | + |
| 108 | + this.on("send42", async (req) => { |
| 109 | + const { id } = req.data; |
| 110 | + const { Service2 } = await cds.connect.to("Service2"); |
| 111 | + Service2.read(`Service2Entity`).where("ID =" + id); |
| 112 | + }); |
| 113 | + |
| 114 | + this.on("send43", async (req) => { |
| 115 | + const { id } = req.data; |
| 116 | + const { Service2 } = await cds.connect.to("Service2"); |
| 117 | + Service2.create(`Service2Entity`).entries({id: "" + id}); |
| 118 | + }); |
| 119 | + |
| 120 | + this.on("send44", async (req) => { |
| 121 | + const { id, amount } = req.data; |
| 122 | + const { Service2 } = await cds.connect.to("Service2"); |
| 123 | + Service2.update(`Service2Entity`).set("col1 = col1" + amount).where("col1 = " + id); |
| 124 | + }); |
| 125 | + |
| 126 | + this.on("send45", async (req) => { |
| 127 | + const { id } = req.data; |
| 128 | + const { Service2 } = await cds.connect.to("Service2"); |
| 129 | + Service2.upsert(`Service2Entity`).entries({id: "" + id}); |
| 130 | + }); |
| 131 | + |
| 132 | + this.on("send46", async (req) => { |
| 133 | + const { id } = req.data; |
| 134 | + const { Service2 } = await cds.connect.to("Service2"); |
| 135 | + Service2.delete(`Service2Entity`).where("ID =" + id); |
| 136 | + }); |
| 137 | + } |
| 138 | +}; |
0 commit comments