import-schemas.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const Parse = require("parse/node")
  2. Parse.Cloud.useMasterKey();
  3. import {EduSchemas} from "../index"
  4. export async function importAllSchemas(){
  5. try{
  6. Parse.initialize(global.config['PARSE_APPID'],null,global.config['PARSE_MASTERKEY']);
  7. Parse.serverURL = global.config['PARSE_SERVERURL'];
  8. if(global.config["LOCAL"]){
  9. Parse.serverURL = "http://127.0.0.1:61337/parse"
  10. }
  11. Parse.Cloud.useMasterKey()
  12. Parse.Cloud.useMasterKey(true)
  13. // Parse.masterKey = global.config['PARSE_MASTERKEY'];
  14. console.log(Parse.applicationId)
  15. console.log(Parse.serverURL)
  16. console.log(Parse.masterKey)
  17. SchemaMap = {}
  18. let SchemaList = await Parse.Schema.all()
  19. SchemaList?.forEach(item=>{
  20. SchemaMap[item?.className] = item
  21. })
  22. for (let index = 0; index < EduSchemas.length; index++) {
  23. let schemaJson = EduSchemas[index];
  24. // EduSchemas.forEach(async schemaJson=>{
  25. console.log(schemaJson?.className,"schema loaded.")
  26. let schema = new Parse.Schema(schemaJson?.className);
  27. schemaJson.fields["isDeleted"] = {
  28. "type": "Boolean",
  29. "required": false
  30. }
  31. schemaJson.fields["company"] = {
  32. "type": "Pointer",
  33. "targetClass":"Company",
  34. "required": false
  35. }
  36. Object.keys(schemaJson?.fields)?.forEach(key=>{
  37. if(key=="objectId"||key=="ACL"||key=="createdAt"||key=="updatedAt") return
  38. let field = schemaJson?.fields[key]
  39. let options = {}
  40. if(field?.targetClass) options.targetClass = field?.targetClass
  41. schema.addField(key,field?.type,options)
  42. })
  43. let clp = new Parse.CLP();
  44. clp.setPublicReadAccess(true);
  45. clp.setPublicWriteAccess(true);
  46. schema.setCLP(clp);
  47. if(SchemaMap[schemaJson?.className]){
  48. try{
  49. await schema.update({useMasterKey:true});
  50. }catch(err1){}
  51. }else{
  52. try{
  53. await schema.save({useMasterKey:true});
  54. }catch(err2){}
  55. }
  56. // })
  57. }
  58. }catch(ierr){
  59. console.error(ierr)
  60. }
  61. return
  62. }
  63. module.exports.importAllSchemas = importAllSchemas