2

Suppose I have a class

class Dog {
    id: number
    legs: number
}

and a table with the same properties. I am using pg-promise to query from the database like so:

getDogs(): Promise<Dog[]> {
    return obj.any('SELECT id, legs FROM Dog')
}

and I want to call this function like this:

const dogs: Dog[] = await Database.getDogs()

What I want is the result to automatically map to an array of dogs. Right now I have to iterate over the result and manually map each result and then add it to the array and I don't want to do this for every object I query from the database.

Mirt Hlaj
  • 173
  • 10

1 Answers1

2

Use method Database.map<T=any> for that:

getDogs(): Promise<Dog[]> {
    return obj.map<Dog>('SELECT id, legs FROM Dog', [], a => new Dog(a));
}

Your class would be:

class Dog {
    constructor(row: any) {
        this.id = row.id;
        this.legs = row.legs;
    }
    id: number
    legs: number
}

Other useful links:

vitaly-t
  • 24,279
  • 15
  • 116
  • 138
  • Does this also work for jsonb objects. Suppose the dog had some subclass with properties that were stored as a jsonb object in the database? – Mirt Hlaj Jan 10 '18 at 10:48
  • 1
    Since you are passing in an abstract object into the constructor, it can handle any structure internally, including nested JSON objects. And if you want your query to return proper JSON objects like that, see this question: https://stackoverflow.com/questions/39805736/get-join-table-as-array-of-results-with-postgresql-nodejs, i.e. you can build any JSON object in SQL by using function `json_build_object` ;) – vitaly-t Jan 10 '18 at 10:52