import-data.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //const { CloudQuery, CloudObject } = require("../lib/ncloud");
  2. // testCRUD()
  3. // //testQuery()
  4. // async function testQuery(){
  5. // let query = new CloudQuery("Doctor")
  6. // // query.equalTo("gender","女")
  7. // query.greaterThanAndEqualTo("age",40)
  8. // query.lessThan("age",41)
  9. // let list = await query.find();
  10. // console.log(list)
  11. // }
  12. // async function testCRUD(){
  13. // // 基本的增删查改测试
  14. // let query = new CloudQuery("Questions")
  15. // let QuestionsList = await query.find();
  16. // console.log("QuestionsList count",QuestionsList?.length)
  17. // let newQuestions = new CloudObject("Questions")
  18. // newQuestions.set({"questionText":"S"})
  19. // newQuestions = await newQuestions.save(newQuestions)
  20. // console.log("newQuestions",newQuestions)
  21. // //没有改
  22. // //destory有问题
  23. // await newQuestions.destory()
  24. // console.log("newQuestions 已删除",newQuestions)
  25. // }
  26. const { CloudQuery, CloudObject } = require("../lib/ncloud");
  27. const { Questionslist, optionlist } = require("./data");
  28. inportQuestionAndtion();
  29. let DataMap = {
  30. option: {},
  31. Questions: {}
  32. };
  33. async function inportQuestionAndtion() {
  34. // 导入问题数据
  35. let questionslist = Questionslist;
  36. //console.log("导入的问题列表:", questionslist); // 打印问题列表
  37. for (let index = 0; index < questionslist.length; index++) {
  38. let questions = questionslist[index];
  39. questions = await importObject("Questions", questions);
  40. }
  41. // 导入选项数据
  42. let optionslist = optionlist;
  43. console.log("导入的选项列表:", optionslist); // 打印选项列表
  44. for (let index = 0; index < optionslist.length; index++) {
  45. let options = optionslist[index];
  46. options = await importObject("option", options);
  47. }
  48. }
  49. async function importObject(className, data) {
  50. // 查重 srcId 数据源列表中的 objectId 并非数据库生成的唯一 ID,因此需要有一个 srcId 字段进行记录,并查重
  51. let query = new CloudQuery(className);
  52. let srcId = data.objectId;
  53. query.equalTo("srcId", srcId);
  54. let importObj = await query.first();
  55. // console.log(importObj); // 可以在这里打印查重结果
  56. // 导入
  57. // 导入前批量处理 Pointer 类型数据,进行重定向
  58. Object.keys(data)?.forEach(key => {
  59. let field = data[key];
  60. let srcId = field?.objectId;
  61. if (srcId) { // 是数组字段
  62. if (key == "question") {
  63. data[key] = DataMap?.["Questions"]?.[srcId]?.toPointer();
  64. }
  65. }
  66. });
  67. // 若未添加,则创建新对象并保存
  68. if (!importObj?.id) {
  69. importObj = new CloudObject(className);
  70. }
  71. // 保存或更新数据
  72. data.srcId = srcId;
  73. importObj.set(data);
  74. importObj = await importObj.save();
  75. DataMap[className][srcId] = importObj;
  76. }
  77. // const { CloudQuery, CloudObject } = require("../lib/ncloud");
  78. // const { Questionslist, optionlist } = require("./data");
  79. // inportQuestionAndtion()
  80. // DataMap = {
  81. // option:{},
  82. // Questions:{}
  83. // }
  84. // async function inportQuestionAndtion(){
  85. // // 导入科室数据
  86. // let questionslist = Questionslist
  87. // for (let index = 0; index < questionslist.length; index++) {
  88. // let questions = questionslist[index];
  89. // questions = await importObject("Questions",questions)
  90. // }
  91. // // 导入医生数据
  92. // let optionslist = optionlist
  93. // for (let index = 0; index < optionslist.length; index++) {
  94. // let options = optionslist[index];
  95. // options = await importObject("option",options)
  96. // }
  97. // //console.log(DataMap["option"])
  98. // }
  99. // async function importObject(className,data){
  100. // // 查重 srcId 数据源列表中的objectId并非数据库生成的唯一ID,因此需要有一个srcId字段进行记录,并查重
  101. // let query = new CloudQuery(className)
  102. // let srcId = data.objectId
  103. // query.equalTo("srcId",srcId)
  104. // let importObj = await query.first()
  105. // //console.log(importObj)
  106. // // 导入
  107. // // 导入前批量处理Pointer类型数据,进行重定向
  108. // Object.keys(data)?.forEach(key=>{
  109. // let field = data[key]
  110. // let srcId = field?.objectId
  111. // if(srcId){ // 是数组字段
  112. // if(key=="question"){
  113. // data[key] = DataMap?.["Questions"]?.[srcId]?.toPointer();
  114. // }
  115. // }
  116. // })
  117. // // 若未添加,则创建新对象并保存
  118. // if(!importObj?.id){
  119. // importObj = new CloudObject(className)
  120. // }
  121. // // 保存或更新数据
  122. // data.srcId = srcId;
  123. // importObj.set(data);
  124. // importObj = await importObj.save();
  125. // DataMap[className][srcId] = importObj
  126. // }
  127. // async function main(){
  128. // let questionsList = await getquestions();
  129. // console.log("questionsList",questionsList);
  130. // console.log("questionsList count",questionsList?.length);
  131. // let newQuestions = { "questionText":"S" }
  132. // let newSavedId = await creatquestions(newQuestions)
  133. // newQuestions.objectId = newQuestions
  134. // console.log("newQuestions",newQuestions)
  135. // // let updateQuestions = { id:"NYoCWM9cNV" , "questionsText":"St" }
  136. // // let updateSavedId = await updatequestions(updateQuestions)
  137. // // updateQuestions.objectId = updateQuestions
  138. // // console.log("updateQuestions",updateQuestions)
  139. // await deleteQuestions(newSavedId)
  140. // console.log("newSavedId 已删除",newSavedId)
  141. // }
  142. // main()
  143. // async function deleteQuestions(id){
  144. // let response = await fetch("http://dev.fmode.cn:1337/parse/classes/Questions/"+ id, {
  145. // "headers": {
  146. // "accept": "*/*",
  147. // "accept-language": "zh-CN,zh;q=0.9",
  148. // "x-parse-application-id": "dev"
  149. // },
  150. // "referrer": "http://127.0.0.1:4040/apps/DevServer/api_console/rest",
  151. // "referrerPolicy": "no-referrer-when-downgrade",
  152. // "body": null,
  153. // "method": "DELETE",
  154. // "mode": "cors",
  155. // "credentials": "omit"
  156. // });
  157. // return response?.json();
  158. // }
  159. // async function updatequestions(id,questionsInfo){
  160. // let body = JSON.stringify(questionsInfo)
  161. // let response = await fetch("http://dev.fmode.cn:1337/parse/classes/Questions/" + id, {
  162. // "headers": {
  163. // "accept": "*/*",
  164. // "accept-language": "zh-CN,zh;q=0.9",
  165. // "content-type": "text/plain;charset=UTF-8",
  166. // "x-parse-application-id": "dev"
  167. // },
  168. // "referrer": "http://127.0.0.1:4040/apps/DevServer/api_console/rest",
  169. // "referrerPolicy": "no-referrer-when-downgrade",
  170. // "body": body,
  171. // "method": "PUT",
  172. // "mode": "cors",
  173. // "credentials": "omit"
  174. // });
  175. // let result = await response?.json()
  176. // return result?.objectId
  177. // }
  178. // async function creatquestions(questionsInfo){
  179. // let body = JSON.stringify(questionsInfo)
  180. // let response = await fetch("http://dev.fmode.cn:1337/parse/classes/Questions", {
  181. // "headers": {
  182. // "accept": "*/*",
  183. // "accept-language": "zh-CN,zh;q=0.9",
  184. // "content-type": "text/plain;charset=UTF-8",
  185. // "x-parse-application-id": "dev"
  186. // },
  187. // "referrer": "http://127.0.0.1:4040/apps/DevServer/api_console/rest",
  188. // "referrerPolicy": "no-referrer-when-downgrade",
  189. // "body": body,
  190. // "method": "POST",
  191. // "mode": "cors",
  192. // "credentials": "omit"
  193. // });
  194. // let result = await response?.json()
  195. // return result?.objectId
  196. // }
  197. // async function getquestions(){
  198. // let response = await fetch("http://dev.fmode.cn:1337/parse/classes/Questions?", {
  199. // "headers": {
  200. // "accept": "*/*",
  201. // "accept-language": "zh-CN,zh;q=0.9",
  202. // "x-parse-application-id": "dev"
  203. // },
  204. // "referrer": "http://127.0.0.1:4040/apps/DevServer/api_console/rest",
  205. // "referrerPolicy": "no-referrer-when-downgrade",
  206. // "body": null,
  207. // "method": "GET",
  208. // "mode": "cors",
  209. // "credentials": "omit"
  210. // });
  211. // let json = await response?.json();
  212. // return json?.results || []
  213. // }