Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

node.js - Postgres denormalize tables into json objects for node

Heres my problem. I have the following tables

CREATE TABLE inventory(
    id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
    owner text NOT NULL references profile(name)
);

CREATE TABLE inventory_item(
    id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
    description text,
    amount bigint,
    inventory uuid nOT NULL references table(id)
);

Now I would like to query for a single inventory

  • all items in that inventory
  • id and owner of that inventory

So I basically get a result of the following form, to send to the client:

{
   "id":"1234123-1234-1234",
   "owner":"tom",
   "items":[
      {
         "id":"2344-123412-34",
         "description":"sword",
         "amount":33
      },
      {
         "id":"6434-123412-34",
         "description":"apple",
         "amount":23
      },
      {
         "id":"45454-1123412-34",
         "description":"bow",
         "amount":32
      },
      {
         "id":"234234-1232412-34",
         "description":"arrow",
         "amount":1
      }
   ]
}

I was thinking of querying id and owner directly and then adding the items as a subquery using ARRAY(). Unfortunately, ARRAY() only works with columns of the same type.

So how can I denormalize my table structure into a JSON array for node using a postgres query?

question from:https://stackoverflow.com/questions/65830994/postgres-denormalize-tables-into-json-objects-for-node

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...