-
Notifications
You must be signed in to change notification settings - Fork 6
Schema
The heart of the Supplejack API is the Schema. This defines the fields for each record and how those fields should be stored and searched. It also defines the different user roles and any restrictions for those roles.
Fields can be one of four types: String, Integer, Boolean or Datetime. When defining your fields in your schema you must declare the type of the field first
# Comments show example data that would be stored
string :name # "Joe Smith"
integer :age # 32
datetime :birthdate # 2006-08-07T00:00:00.000Z
boolean :employed # trueFields can be defined as multi value. This means that data will be stored as an array of values
string :email, multi_value: true # e.g. ["joe@test.com", "joe.smith@example.com"]Defining a search boost will add a boost to relevancy of the field when searching for records in SOLR.
string :name, search_boost: 10
string :address, search_boost: 2In the above example a search that matches values in the name field would receive a higher ranking than ones which match the address field.
How the SOLR should search the field. Can either be filter, fulltext or both.
Fulltext:
Filter:
string :name, search_as: [:filter, :fulltext]
string :address, search_as: [:fulltext]Groups allow you to collect several fields together and reference them using a single value. Groups are used when determining which fields to return if none are specified in the request. This means that you must always define at least the 'default' group.
group :default do
fields [
:name,
:address
]
end
group :all_fields do
includes [:default] # NOTE: Groups can include other groups
fields [
:age,
:gender,
:email,
:phone
]
end// Sample requests with different groups
// api.supplejack.com/records/123.json?api_key=abc123
{
"record": {
name: "Joe Smith",
address: "123 Smith St, Wellington, New Zealand"
}
}
// api.supplejack.com/records/123.json?api_key=abc123&fields=all_fields
{
"record": {
name: "Joe Smith",
address: "123 Smith St, Wellington, New Zealand",
age: 32
gender: "male"
email: "joe@test.com"
phone: "07 123 5457"
}
}You are able to define several roles for users of the API. These can be used to expose different fields
class Schema < SupplejackApi::SupplejackSchema
# Fields
string :name, search_boost: 10, search_as: [:filter, :fulltext]
string :address, search_boost: 2, search_as: [:filter, :fulltext]
string :email, multi_value: true, search_as: [:filter]
string :children, multi_value: true
string :contact, multi_value: true
integer :age
datetime :birth_date
boolean :nz_citizen, search_as: [:filter]
# Groups
group :default do
fields [
:name,
:address
]
end
group :all do
includes [:default]
fields [
:email,
:children,
:nz_citizen,
:birth_date,
:age
]
end
# Roles
role :developer do
default true
end
role :admin
end