// Adding the @export directive when defining a table creates a REST endpoint
type Owner @table @export {
name: String
age: Int
address: String
phone: String
email: String
dogId: ID @indexed # Foreign key referencing the Pet table (Dog ID)
dog: [Dog] @relationship(from: dogId) # Relationship to the Dog table, linked by dogId
}...
type Dog @table @export {
id: ID @primaryKey # The unique identifier for each Dog (Primary Key)
name: String
breed: String
age: Int
}
// The REST endpoint can be accessed using the following URL when running a HarperDB application component on your local machine:
curl 'http://localhost:9926/Owner/'
// The code below retrieves a dog's information from the database using the DogAge class, which extends the Dog model to provide functionality for converting a dog's age to human age.
// This class retrieves a dog's information from the database and calculates its equivalent age in human years.
const { Dog } = tables;
// Convert dog age to human age
export class DogAge extends Dog {
get() {
const dog = super.get(); // Get the dog from the database
const humanAge = dog.age * 7; // Convert dog age to human age
return { ...dog, humanAge }; // Return the dog with human age
}
}
// Add new data to the database
Request:
curl -X POST http://localhost:9926/Owner/ \
-H "Content-Type: application/json" \
-d '{
"id": 1,
"name": "harper",
"breed": "good boy",
"age": 5
}'