123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- class CloudObject{
- id
- className
- data = {}
- constructor(className){
- this.className = className
- }
- toPointer(){
- return {"__type":"Pointer","className":this.className,"objectId":this.id}
- }
- set(json){
- Object.keys(json).forEach(key=>{
- if(["objectId","id","createdAt","updatedAt","ACL"].indexOf(key)>-1){
- return
- }
- this.data[key] = json[key]
- })
- }
- get(key){
- return this.data[key] || null
- }
- async save(){
- let method = "POST"
- let url = "http://dev.fmode.cn:1337/parse/classes/" + this.className
- // 更新
- if(this.id){
- url += "/"+this.id
- method = "PUT"
- }
- let body = JSON.stringify(this.data)
- let response = await fetch(url, {
- "headers": {
- "content-type": "application/json;charset=UTF-8",
- "x-parse-application-id": "dev"
- },
- "body": body,
- "method": method,
- "mode": "cors",
- "credentials": "omit"
- });
- let result = await response?.json();
- if(result?.error){
- console.error(result?.error)
- }
- if(result?.objectId){this.id = result?.objectId}
- return this
- }
- async destroy(){
- if(!this.id) return
- let response = await fetch("http://dev.fmode.cn:1337/parse/classes/"+this.className+"/"+this.id, {
- "headers": {
- "x-parse-application-id": "dev"
- },
- "body": null,
- "method": "DELETE",
- "mode": "cors",
- "credentials": "omit"
- });
- let result = await response?.json();
- if(result){
- this.id = null
- }
- return true
- }
- }
- class CloudQuery{
- className
- constructor(className){
- this.className = className
- }
- whereOptions = {}
- greaterThan(key,value){
- if(!this.whereOptions[key]) this.whereOptions[key] = {}
- this.whereOptions[key]["$gt"] = value
- }
- greaterThanAndEqualTo(key,value){
- if(!this.whereOptions[key]) this.whereOptions[key] = {}
- this.whereOptions[key]["$gte"] = value
- }
- lessThan(key,value){
- if(!this.whereOptions[key]) this.whereOptions[key] = {}
- this.whereOptions[key]["$lt"] = value
- }
- lessThanAndEqualTo(key,value){
- if(!this.whereOptions[key]) this.whereOptions[key] = {}
- this.whereOptions[key]["$lte"] = value
- }
- equalTo(key,value){
- this.whereOptions[key] = value
- }
- async get(id){
- let url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"/"+id+"?"
- let response = await fetch(url, {
- "headers": {
- "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
- "x-parse-application-id": "dev"
- },
- "body": null,
- "method": "GET",
- "mode": "cors",
- "credentials": "omit"
- });
- let json = await response?.json();
- return json || {}
- }
- async find(){
- let url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"?"
-
- if(Object.keys(this.whereOptions)?.length){
- let whereStr = JSON.stringify(this.whereOptions)
- url += `where=${whereStr}`
- }
- let response = await fetch(url, {
- "headers": {
- "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
- "x-parse-application-id": "dev"
- },
- "body": null,
- "method": "GET",
- "mode": "cors",
- "credentials": "omit"
- });
- let json = await response?.json();
- return json?.results || []
- }
- async first(){
- let url = "http://dev.fmode.cn:1337/parse/classes/"+this.className+"?"
-
- if(Object.keys(this.whereOptions)?.length){
- let whereStr = JSON.stringify(this.whereOptions)
- url += `where=${whereStr}`
- }
- let response = await fetch(url, {
- "headers": {
- "if-none-match": "W/\"1f0-ghxH2EwTk6Blz0g89ivf2adBDKY\"",
- "x-parse-application-id": "dev"
- },
- "body": null,
- "method": "GET",
- "mode": "cors",
- "credentials": "omit"
- });
- let json = await response?.json();
- let exists = json?.results?.[0] || null
- if(exists){
- let existsObject = new CloudObject(this.className)
- existsObject.set(exists)
- existsObject.id = exists.objectId
- existsObject.createdAt = exists.createdAt
- existsObject.updatedAt = exists.updatedAt
- return existsObject
- }
- }
- }
- module.exports.CloudObject = CloudObject
- module.exports.CloudQuery = CloudQuery
|