Skip to content

Schemas And Types

Justin Meyer edited this page Sep 16, 2021 · 9 revisions

getSchema

Map types:

{
   type: "map",
   keys: {
     name: MaybeString
     id: MaybeNumber
   }
}

List Types

{
  type: "list",
  values: String
  keys: {
    count: Number
  }
}

Types

{

  // BASIC PROPERTIES
  // These are used a LOT of places in CanJS.

  [can.isMember](value){
     return // if `value` is an instance of the type
  }
  [can.new](value) {
     return // Make an instance of the type given value. This can return primitive types.
  }
  [can.serialize](value) {
     return // an JSON.stringify() representation of the instance
  }

  // SET ENHANCEMENTS
  // Properties on a type used to help set logic 

  // This type is used as the class when doing query logic comparisons.
  // For example, you might want {date: "10-22"} on your observables to remain very 
  // string-like but you need CQL to treat that as a date.  
  [can.SetType]: {
     // Takes the query value (Ex: "10-22")
     constructor(){
       
     }

     // Turn a value like `{$gt: "10-22"}` into `new GreaterThan(new DateSet("10-22"))`.
     // This seems to be only used by the MaybeType.  I think it might be replace-able. 
     hydrate(valueToHydrate, childrenHydrator) {
       return // hydrated value
     }
     
     // A Map of various set comparison operators involving the type
     // It's two-levels deep to account for directionality with difference.
     [can.setComparisons]: Map({
       [Type1]: 
         {
           [Type2]: {intersection(sA, sb), union(sA, sb), difference(sA, sb) } 
         }
     })

     // valueOf or toString are used to get a sortable representation used for things like GreaterThan()
     valueOf(){
       return // value used for comparison 
     }
     toString(){
       return // value used for comparison 
     }

     [can.serialize]() {
       return // turn this back into something that would be used by a query
     }
  }
}

"set" types vs normal types

Read the DateStringSet example

NOTE: Types like DateString need to be distinguished from SetTypes like DateStringSet because types like DateString have different values.

For example, a DateStringSet might have a value like "yesterday", but this would not be a valid DateString. In this case, you might want the query string to look like:

{
  filter: {date: {$gt: "yesterday"}}
}

But, the server would return data that looks like:

[
  {id: "55", date: "Wed Apr 04 2018 10:00:00 GMT-0500 (CDT)" }
]

You might want "Wed Apr 04 2018 10:00:00 GMT-0500 (CDT)" to be a valid member of DateString and DateStringSet.

BUT, you wouldn't "yesterday" to be a valid member of DateString.

Clone this wiki locally