Advertisement
Guest User

intro

a guest
Mar 28th, 2020
1,231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.79 KB | None | 0 0
  1. module.exports = (context, callback) => {
  2. var data = JSON.parse(context.body);
  3. console.log("============");
  4. console.log(data);
  5. console.log(typeof data);
  6.  
  7.  
  8. run(data);
  9.  
  10. callback
  11. .status(200)
  12. .succeed("job done");
  13.  
  14. };
  15.  
  16. const fs = require("fs");
  17. const Binary = require("mongodb").Binary;
  18. const fetch = require("node-fetch");
  19. // MongoDB connection for schema files.
  20. const MongoClient = require("mongodb").MongoClient;
  21.  
  22. //#### CUSTOM MODULE IMPORT ####
  23. var cli = require("./helper.js");
  24.  
  25. //#### ENVIRONMENT VARIABLE ####
  26. //MongoDB Credential
  27. const MONGO_URL = "mongodb://prisma:password@35.185.183.19:443/PrismaDB";
  28. const MONGO_DB = "PrismaDB";
  29. const MONGO_COLLECTION = "prisma";
  30.  
  31. //GitHub Credential
  32. const GITHUB_URL = "https://api.github.com/repos/shah05/tutorials/contents/";
  33. const GITHUB_USER = "shah05";
  34. const GITHUB_EMAIL = "alipitchay2006@gmail.com";
  35.  
  36. // GitHub Personal Token
  37. var headers = {
  38. "Content-Type": "application/json",
  39. Authorization: "token f633936b5ff929b6a0ba2a58a96ee1cdec45cc56"
  40. };
  41.  
  42. function run({ hostname, dbName, dbType, port, username, password }) {
  43. //1. Extract data db credentials from JSON body.
  44. console.log(hostname, dbName, username, password, dbType, port);
  45. console.log("==========================>");
  46.  
  47. //2. Check if the schema file of this DB exist in MongoDB.(i.e DB has been introspected previously)
  48. schemaId = hostname + "_" + dbName;
  49. try {
  50. //3. Connect to mongoDB
  51. MongoClient.connect(MONGO_URL, (err, client) => {
  52. if (err) return console.log(err);
  53.  
  54. const db = client.db(MONGO_DB);
  55. // client.close();
  56. //Promise to query MongoDB based on schemaIdls
  57. var myPromise = () => {
  58. return new Promise((resolve, reject) => {
  59. //MongoDB query.
  60. db.collection(MONGO_COLLECTION)
  61. .find({ schemaid: schemaId })
  62. .limit(1)
  63. .toArray(function(err, data) {
  64. if (err) reject(err);
  65. else {
  66. resolve(data[0]);
  67. //Close MongoDB connection
  68. client.close();
  69. }
  70. // err ? reject(err) : resolve(data[0]);
  71. });
  72. });
  73. };
  74.  
  75.  
  76. var n = new Date();
  77.  
  78.  
  79. //Pre 4. create folder
  80. cli.execute("sh function/start_introspect.sh " + n.toISOString() + "_" + hostname).then(result => {
  81. console.log(result, " - start_introspect");
  82. var prisma_project = "/tmp/" + n.toISOString() + "_" +hostname + "_prisma";
  83.  
  84. //4. Trigger the promise and check result of MongoDB query.
  85. myPromise().then(result => {
  86. //5. If schema file exist in DB, parse the content and return table names/column name/type names.
  87. if (typeof result != "undefined") {
  88. console.log("found");
  89. // console.log(result.schemacontent.buffer);
  90. // Write content to schema.prisma file.
  91. fs.writeFileSync(
  92. prisma_project + "/schema.prisma",
  93. result.schemacontent.buffer,
  94. function(err) {
  95. if (err) throw err;
  96. console.log("Successfully saved");
  97. }
  98. );
  99.  
  100. //Call existing_schema.sh script to parse and return tableName, columnName, typeName.
  101. cli.execute("sh "+ prisma_project + "/existing_schema.sh " + prisma_project).then(result => {
  102. console.log(result, "@@@@@@@@@@");
  103. console.log("------------- ================ -------------");
  104. console.log(retrieveTableColNames(result));
  105. // res.json(retrieveTableColNames(result));
  106. });
  107. } else {
  108. // 6. If schema file does not exist in DB, perform introspect via PrismaCLI, push schema file to MongoDB and push Photon API script to Github
  109. console.log("not found");
  110. // Call new_schema.sh script to parse and return tableName, columnName and typeName
  111. console.log("ENV====>", username, password, hostname, port, dbName);
  112. console.log(
  113. "/bin/sh " + prisma_project + "/new_schema.sh " +
  114. username +
  115. " " +
  116. password +
  117. " " +
  118. hostname +
  119. " " +
  120. port +
  121. " " +
  122. dbName +
  123. " " +
  124. prisma_project
  125. )
  126. cli
  127. .execute(
  128. "/bin/sh " + prisma_project + "/new_schema.sh " +
  129. username +
  130. " " +
  131. password +
  132. " " +
  133. hostname +
  134. " " +
  135. port +
  136. " " +
  137. dbName +
  138. " " +
  139. prisma_project
  140. )
  141. .then(result => {
  142. console.log("RESULT =================");
  143. console.log(result);
  144. console.log("RESULT =================");
  145. //Call function to transform output from bash script to JSON.
  146. console.log(retrieveTableColNames(result));
  147. //Call function to push schema.prisma file to MongoDB.
  148. MongoClient.connect(MONGO_URL, (err, client) => {
  149. const db = client.db(MONGO_DB);
  150. pushSchemaToDB(hostname, dbName, db, prisma_project, n);
  151. client.close();
  152. });
  153.  
  154. //Execute script to generate Photon API and call function to push to GitHub
  155. cli
  156. .execute(
  157. "sh " + prisma_project + "/generate_api.sh " + hostname + " " + dbName + " " + prisma_project
  158. )
  159. .then(result => {
  160. pushToGitHub(hostname + "_" + dbName, prisma_project, n);
  161. });
  162. });
  163. }
  164. });
  165. });
  166. });
  167. } catch (error) {
  168. console.log("ERROR: ", error);
  169. }
  170. }
  171. function retrieveTableColNames(scriptOutput) {
  172. scriptOutput = scriptOutput.trim();
  173. console.log(scriptOutput)
  174. data = scriptOutput.split("|");
  175. tableNames = data[0].split(" ");
  176. colNames = data[1].split(" ");
  177. typeNames = data[2].split(" ");
  178.  
  179. final_res = [];
  180. for (let i = 0; i < tableNames.length; i++) {
  181. index = -1;
  182. col = [];
  183. for (let j = 0; j < colNames.length; j++) {
  184. // Update idIndex everytime "model" is detected. (indicates the start of column names for a given table).
  185. if (colNames[j] == "model") {
  186. index += 1;
  187. }
  188. //only push when id is same i so that the col names are associated to the correct table names.
  189. if (index == i) {
  190. // Exclude "model" as column names.
  191. if (colNames[j] !== "model") {
  192. //Create object
  193. colType = {
  194. name: colNames[j],
  195. type: typeNames[j]
  196. };
  197. col.push(colType);
  198. }
  199. }
  200. }
  201. //construct json object and push in list.
  202. tableData = {
  203. table: tableNames[i],
  204. columns: col
  205. };
  206. final_res.push(tableData);
  207. }
  208. return JSON.stringify(final_res);
  209. }
  210. function pushToGitHub(apiFileName, prisma_project, n) {
  211. url = GITHUB_URL + n.toISOString() + "_" + apiFileName;
  212. var fileContent = fs.readFileSync(prisma_project + "/node_modules/@prisma/client/index.js", {
  213. // <<<<<<<<<<<<<<<<<<< not being updated
  214. encoding: "base64"
  215. });
  216.  
  217. var data = {
  218. message: "update file",
  219. committer: {
  220. name: GITHUB_USER,
  221. email: GITHUB_EMAIL
  222. },
  223. content: fileContent
  224. };
  225.  
  226. var jsonData = JSON.stringify(data);
  227.  
  228. //REST Call to update in GitHub
  229. fetch(url, { method: "PUT", headers: headers, body: jsonData })
  230. .then(res => {
  231. console.log("FINAL---- PUSHED TO GITHUB-----------", res);
  232. })
  233. .then(json => {
  234. console.log(json);
  235. // Do something with the returned data.
  236. });
  237. }
  238.  
  239. function pushSchemaToDB(hostname, dbName, db, prisma_project, n) {
  240. var data = fs.readFileSync(prisma_project + "/schema.prisma");
  241. var insert_data = {};
  242. insert_data = Binary(data);
  243.  
  244.  
  245. //schema file object
  246. schemaFile = {
  247. schemaid: n.toISOString() + "_" + hostname + "_" + dbName,
  248. schemacontent: insert_data
  249. };
  250.  
  251. var collection = db.collection(MONGO_COLLECTION);
  252. collection.insert(schemaFile, function(err, result) {
  253. if (err) return console.log(err);
  254. });
  255. }
  256.  
  257. // run({
  258. // hostname: "192.168.68.8",
  259. // dbName: "Company",
  260. // dbType: "mysql",
  261. // port: "3308",
  262. // username: "root",
  263. // password: "root"
  264. // });
  265.  
  266. // run({
  267. // hostname: "192.168.68.8",
  268. // dbName: "shahidan",
  269. // dbType: "mysql",
  270. // port: "3306",
  271. // username: "root",
  272. // password: "root"
  273. // });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement