import-data.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // import-data.js
  2. const { CloudQuery, CloudObject } = require("../lib/ncloud");
  3. const { DepartList, DoctorList, HotDotList, DrugList,RealDoctorList } = require("./data");
  4. inportDapartAndDoctor();
  5. DataMap = {
  6. Doctor: {},
  7. Department: {},
  8. HotDot: {},
  9. Drug: {},
  10. RealDoctor: {},
  11. };
  12. async function inportDapartAndDoctor() {
  13. // 导入药品数据
  14. // let drugList = DrugList;
  15. // for (let index = 0; index < drugList.length; index++) {
  16. // let drug = drugList[index];
  17. // drug = await importObject("Drug", drug);
  18. // }
  19. // let realDoctorList = RealDoctorList;
  20. // for (let index = 0; index < realDoctorList.length; index++) {
  21. // let realDoctor = realDoctorList[index];
  22. // realDoctor = await importObject("RealDoctor", realDoctor);
  23. // }
  24. //如果需要导入其他数据(科室、医生、热点),可以取消注释以下代码
  25. // 导入科室数据
  26. // let departList = DepartList;
  27. // for (let index = 0; index < departList.length; index++) {
  28. // let depart = departList[index];
  29. // depart = await importObject("Department", depart);
  30. // }
  31. // 导入医生数据
  32. // let doctorList = DoctorList;
  33. // for (let index = 0; index < doctorList.length; index++) {
  34. // let doctor = doctorList[index];
  35. // doctor = await importObject("Doctor", doctor);
  36. // }
  37. // 导入热点数据
  38. let hotDotList = HotDotList;
  39. for (let index = 0; index < hotDotList.length; index++) {
  40. let hotDot = hotDotList[index];
  41. hotDot = await importObject("HotDot", hotDot);
  42. }
  43. // console.log("药品数据导入完成");
  44. }
  45. async function importObject(className, data) {
  46. // 查重 srcId 数据源列表中的objectId并非数据库生成的唯一ID,因此需要有一个srcId字段进行记录,并查重
  47. let query = new CloudQuery(className);
  48. let srcId = data.objectId;
  49. query.equalTo("srcId", srcId);
  50. let importObj = await query.first();
  51. console.log(importObj);
  52. // 导入前批量处理Pointer类型数据,进行重定向
  53. Object.keys(data)?.forEach(key => {
  54. let field = data[key];
  55. let srcId = field?.objectId;
  56. if (srcId) { // 是 Pointer 类型字段
  57. if (key === "depart") {
  58. data[key] = DataMap["Department"]?.[srcId]?.toPointer();
  59. }
  60. // 如果有其他 Pointer 类型的字段,可以在这里处理
  61. }
  62. });
  63. // 若未添加,则创建新对象并保存
  64. if (!importObj?.id) {
  65. importObj = new CloudObject(className);
  66. }
  67. // 保存或更新数据
  68. data.srcId = srcId;
  69. importObj.set(data);
  70. importObj = await importObj.save();
  71. DataMap[className][srcId] = importObj;
  72. }