ncloud.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. class CloudObject{
  2. id
  3. className
  4. data = {}
  5. constructor(className){
  6. this.className = className
  7. }
  8. toPointer(){
  9. return {"__type":"Pointer","className":this.className,"objectId":this.id}
  10. }
  11. set(json){
  12. Object.keys(json).forEach(key=>{
  13. if(["objectId","id","createdAt","updatedAt","ACL"].indexOf(key)>-1){
  14. return
  15. }
  16. this.data[key] = json[key]
  17. })
  18. }
  19. get(key){
  20. return this.data[key] || null
  21. }
  22. async save(){
  23. let method = "POST"
  24. let url = "http://dev.fmode.cn:1337/parse/classes/" + this.className
  25. // 更新
  26. if(this.id){
  27. url += "/"+this.id
  28. method = "PUT"
  29. }
  30. let body = JSON.stringify(this.data)
  31. let response = await fetch(url, {
  32. "headers": {
  33. "content-type": "application/json;charset=UTF-8",
  34. "x-parse-application-id": "dev"
  35. },
  36. "body": body,
  37. "method": method,
  38. "mode": "cors",
  39. "credentials": "omit"
  40. });
  41. let result = await response?.json();
  42. if(result?.error){
  43. console.error(result?.error)
  44. }
  45. if(result?.objectId){this.id = result?.objectId}
  46. return this
  47. }
  48. async destroy(){
  49. if(!this.id) return
  50. let response = await fetch("http://dev.fmode.cn:1337/parse/classes/"+this.className+"/"+this.id, {
  51. "headers": {
  52. "x-parse-application-id": "dev"
  53. },
  54. "body": null,
  55. "method": "DELETE",
  56. "mode": "cors",
  57. "credentials": "omit"
  58. });
  59. let result = await response?.json();
  60. if(result){
  61. this.id = null
  62. }
  63. return true
  64. }
  65. }
  66. class CloudQuery{
  67. className
  68. constructor(className){
  69. this.className = className
  70. }
  71. whereOptions = {}
  72. greaterThan(key,value){
  73. if(!this.whereOptions[key]) this.whereOptions[key] = {}
  74. this.whereOptions[key]["$gt"] = value
  75. }
  76. greaterThanAndEqualTo(key,value){
  77. if(!this.whereOptions[key]) this.whereOptions[key] = {}
  78. this.whereOptions[key]["$gte"] = value
  79. }
  80. lessThan(key,value){
  81. if(!this.whereOptions[key]) this.whereOptions[key] = {}
  82. this.whereOptions[key]["$lt"] = value
  83. }
  84. lessThanAndEqualTo(key,value){
  85. if(!this.whereOptions[key]) this.whereOptions[key] = {}
  86. this.whereOptions[key]["$lte"] = value
  87. }
  88. equalTo(key,value){
  89. this.whereOptions[key] = value
  90. }
  91. async get(id){
  92. let url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"/"+id+"?"
  93. let response = await fetch(url, {
  94. "headers": {
  95. "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
  96. "x-parse-application-id": "dev"
  97. },
  98. "body": null,
  99. "method": "GET",
  100. "mode": "cors",
  101. "credentials": "omit"
  102. });
  103. let json = await response?.json();
  104. return json || {}
  105. }
  106. async find(){
  107. let url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"?"
  108. if(Object.keys(this.whereOptions)?.length){
  109. let whereStr = JSON.stringify(this.whereOptions)
  110. url += `where=${whereStr}`
  111. }
  112. let response = await fetch(url, {
  113. "headers": {
  114. "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
  115. "x-parse-application-id": "dev"
  116. },
  117. "body": null,
  118. "method": "GET",
  119. "mode": "cors",
  120. "credentials": "omit"
  121. });
  122. let json = await response?.json();
  123. return json?.results || []
  124. }
  125. async first(){
  126. let url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"?"
  127. if(Object.keys(this.whereOptions)?.length){
  128. let whereStr = JSON.stringify(this.whereOptions)
  129. url += `where=${whereStr}`
  130. }
  131. let response = await fetch(url, {
  132. "headers": {
  133. "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
  134. "x-parse-application-id": "dev"
  135. },
  136. "body": null,
  137. "method": "GET",
  138. "mode": "cors",
  139. "credentials": "omit"
  140. });
  141. let json = await response?.json();
  142. let exists = json?.results?.[0] || null
  143. if(exists){
  144. let existsObject = new CloudObject(this.className)
  145. existsObject.set(exists)
  146. existsObject.id = exists.objectId
  147. existsObject.createdAt = exists.createdAt
  148. existsObject.updatedAt = exists.updatedAt
  149. return existsObject
  150. }
  151. }
  152. }
  153. module.exports.CloudObject = CloudObject
  154. module.exports.CloudQuery = CloudQuery