ncloud.js 4.9 KB

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