Advertisement
Guest User

Untitled

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