SlideShare a Scribd company logo
1 of 23
New NORM!
NoRM – No ORM Framework
Hettie Dombrovskaya
Braviant Holdings
Chicago IL USA
Response time matters!
2020 2
TIME = MONEY
Money by the numbers
2020 3
1 sec pageloadslowdown=> $1.6billion
lost sales/year
Slowing searchresultsby0.4 sec– lossof 8
million searchesperday
50% visitors abandonthe site, which is notloadedwithin3 sec
79%visitors will never return again
Please wait….
2020 4
Welcome to ORM!
ORM – Object-Relational Mapper
• solves a problem of abstraction from details of data storing
• does not provide effective means of manipulation with data sets
2020 5
Application Model Database Model
ORM
Flat Tables
JDBC
(Object-Relational)
6
(Object-Oriented)
2020
Our Solution - NORM
TransferMapping Mapping
Database Model
(Object-
Relational)
JDBC TextText
Transfer model (JSON)
Transfer
Application Model
(Object-Oriented)
8
CONTRACT
• Both sides (an application and a
database) convert internal
representations into complex
hierarchical object
• Contract establishes object structure
implemented on both sides
• Now, for any application endpoint it
takes one database call to transfer
data to and from database
App DB
2020
DB Schema
2020 9
Objects Mapping (Transport Object)
2020 10
account_record
account_id
username
first_name
last_name
dob
phones[]
email_addresses[]
phone_record
phone_id
phone_number
phone_type_id
phone_type
email_record
email_id
email
email_priority_id
email_priority
phoness[]
email_addresses[]
Result in JSON Format
{” account id”: ”1”,
”username”: ”aliceacct1”,
”last_name”: ”johns”,
“firs_name”:”alice”,
”dob” : ”1996−04−01” ”,
”phones” : [
{ ”phone_id”: 1,
”phone_number” : ”2021234567” ,
”phone_type_id”:”2” ”phone type”: ”cell” }
],
”email addresses”:
[
{”email_address_id”:1 ,
”email address”:alicejons@gmail.com,
“email_priority_id”: “1”,
“email_priority”:”primary” }
]}
112020
Building the Function Output
2020 12
Create a UDT with Nested Types
CREATE TYPE phone_record as
( phone_id bigint ,
phone_number text ,
phone_type_id int ,
phone type text );
CREATE TYPE email_record as
(email_id bigint,
email text,
email_priority_id int,
email_priority text);
CREATE TYPE account_record as
(account_id bigint,
last_name text,
first_name text,
dob date,
phones phone_record[],
email_addresses email_record[]);
2020 13
Why Not Storing JSON?!
• The power of relational search is lost
• By using JSON as a return type for functions we are loosing the strong type
dependencies
That’s why we organize funcitons in “packages”:
we want to ensure that all functions from one “package” return the same object
2020 14
Solution
SELECT ROW(account_id ,
username,
first_name,
last_name,
dob ,
(SELECT array_agg(row(phone_id,
phone,
p.phone_type_id,
phone_type )::phone_record)
FROM phone p
JOIN phone_type pt USING (phone_type_id) WHERE p.account_id=a.account_id),
(SELECT array_agg(row(email_id,
email,
e.email_priority_id,
email_priority )::email_record)
FROM email e
JOIN email_priority ep using(email_priority_id) WHERE e.account_id=a.account_id)
)::account_record AS single_item
FROM account a WHERE a.account_id=p_account_id
2020 15
Transforming to Text
CREATE OR REPLACE
FUNCTION array_transport (all_items anyarray) returns setof text
RETURNS NULL ON NULL INPUT
LANGUAGE plpgsql as
$body$
DECLARE
item record;
BEGIN
FOREACH item IN array all_items
LOOP
RETURN next( to_json(item))::text;
END LOOP;
END;
2020 16
Example: How to Build it
https://github.com/hettie-d/NORM
2020 17
Creating NORM schema
2020 18
Building the Package
2020 19
Account Search
2020 20
Account Update
2020 21
Examples
2020 22
Questions?
2020 23

More Related Content

What's hot

Research on vector spatial data storage scheme based
Research on vector spatial data storage scheme basedResearch on vector spatial data storage scheme based
Research on vector spatial data storage scheme based
Anant Kumar
 
5 Data Modeling for NoSQL 1/2
5 Data Modeling for NoSQL 1/25 Data Modeling for NoSQL 1/2
5 Data Modeling for NoSQL 1/2
Fabio Fumarola
 

What's hot (20)

PDI data vault framework #pcmams 2012
PDI data vault framework #pcmams 2012PDI data vault framework #pcmams 2012
PDI data vault framework #pcmams 2012
 
True Postgres High Availability Architecture with Industry Standard Open-Sou...
 True Postgres High Availability Architecture with Industry Standard Open-Sou... True Postgres High Availability Architecture with Industry Standard Open-Sou...
True Postgres High Availability Architecture with Industry Standard Open-Sou...
 
Szabaduljon ki az Oracle szorításából
Szabaduljon ki az Oracle szorításábólSzabaduljon ki az Oracle szorításából
Szabaduljon ki az Oracle szorításából
 
Power JSON with PostgreSQL
Power JSON with PostgreSQLPower JSON with PostgreSQL
Power JSON with PostgreSQL
 
Agile Data Warehousing: Using SDDM to Build a Virtualized ODS
Agile Data Warehousing: Using SDDM to Build a Virtualized ODSAgile Data Warehousing: Using SDDM to Build a Virtualized ODS
Agile Data Warehousing: Using SDDM to Build a Virtualized ODS
 
NoSQL and Spatial Database Capabilities using PostgreSQL
NoSQL and Spatial Database Capabilities using PostgreSQLNoSQL and Spatial Database Capabilities using PostgreSQL
NoSQL and Spatial Database Capabilities using PostgreSQL
 
Jumpstart: MongoDB BI Connector & Tableau
Jumpstart: MongoDB BI Connector & TableauJumpstart: MongoDB BI Connector & Tableau
Jumpstart: MongoDB BI Connector & Tableau
 
NoSQL databases and managing big data
NoSQL databases and managing big dataNoSQL databases and managing big data
NoSQL databases and managing big data
 
Non Relational Databases
Non Relational DatabasesNon Relational Databases
Non Relational Databases
 
Keynote: The Postgres Ecosystem
Keynote: The Postgres EcosystemKeynote: The Postgres Ecosystem
Keynote: The Postgres Ecosystem
 
EDB 13 - New Enhancements for Security and Usability - APJ
EDB 13 - New Enhancements for Security and Usability - APJEDB 13 - New Enhancements for Security and Usability - APJ
EDB 13 - New Enhancements for Security and Usability - APJ
 
New enhancements for security and usability in EDB 13
New enhancements for security and usability in EDB 13New enhancements for security and usability in EDB 13
New enhancements for security and usability in EDB 13
 
PostgreSQL 13 is Coming - Find Out What's New!
PostgreSQL 13 is Coming - Find Out What's New!PostgreSQL 13 is Coming - Find Out What's New!
PostgreSQL 13 is Coming - Find Out What's New!
 
Research on vector spatial data storage scheme based
Research on vector spatial data storage scheme basedResearch on vector spatial data storage scheme based
Research on vector spatial data storage scheme based
 
Couch db
Couch dbCouch db
Couch db
 
24 Hour of PASS: Taking SQL Server into the Beyond Relational Realm
24 Hour of PASS: Taking SQL Server into the Beyond Relational Realm24 Hour of PASS: Taking SQL Server into the Beyond Relational Realm
24 Hour of PASS: Taking SQL Server into the Beyond Relational Realm
 
Why Care Risk Choose PostgreSQL
Why Care Risk Choose PostgreSQLWhy Care Risk Choose PostgreSQL
Why Care Risk Choose PostgreSQL
 
Making Postgres Central in Your Data Center
Making Postgres Central in Your Data CenterMaking Postgres Central in Your Data Center
Making Postgres Central in Your Data Center
 
Using PEM to understand and improve performance in Postgres: Postgres Tuning ...
Using PEM to understand and improve performance in Postgres: Postgres Tuning ...Using PEM to understand and improve performance in Postgres: Postgres Tuning ...
Using PEM to understand and improve performance in Postgres: Postgres Tuning ...
 
5 Data Modeling for NoSQL 1/2
5 Data Modeling for NoSQL 1/25 Data Modeling for NoSQL 1/2
5 Data Modeling for NoSQL 1/2
 

Similar to NORM: No ORM Framework

Microsoft Analysis Services Physical Design
Microsoft Analysis Services Physical DesignMicrosoft Analysis Services Physical Design
Microsoft Analysis Services Physical Design
jamessnape
 
Dimensional Modelling Session 2
Dimensional Modelling Session 2Dimensional Modelling Session 2
Dimensional Modelling Session 2
akitda
 

Similar to NORM: No ORM Framework (20)

swiss_pgday_NORM_GEN_2022. NORM: No ORM Framework
swiss_pgday_NORM_GEN_2022. NORM: No ORM Frameworkswiss_pgday_NORM_GEN_2022. NORM: No ORM Framework
swiss_pgday_NORM_GEN_2022. NORM: No ORM Framework
 
MondCloud Semantic Data Hub for Insurance
MondCloud Semantic Data Hub for InsuranceMondCloud Semantic Data Hub for Insurance
MondCloud Semantic Data Hub for Insurance
 
Benefits of Using MongoDB Over RDBMSs
Benefits of Using MongoDB Over RDBMSsBenefits of Using MongoDB Over RDBMSs
Benefits of Using MongoDB Over RDBMSs
 
Introduction to MongoDB at IGDTUW
Introduction to MongoDB at IGDTUWIntroduction to MongoDB at IGDTUW
Introduction to MongoDB at IGDTUW
 
Benefits of Using MongoDB Over RDBMS (At An Evening with MongoDB Minneapolis ...
Benefits of Using MongoDB Over RDBMS (At An Evening with MongoDB Minneapolis ...Benefits of Using MongoDB Over RDBMS (At An Evening with MongoDB Minneapolis ...
Benefits of Using MongoDB Over RDBMS (At An Evening with MongoDB Minneapolis ...
 
Microsoft Analysis Services Physical Design
Microsoft Analysis Services Physical DesignMicrosoft Analysis Services Physical Design
Microsoft Analysis Services Physical Design
 
Dimensional Modelling Session 2
Dimensional Modelling Session 2Dimensional Modelling Session 2
Dimensional Modelling Session 2
 
SA Chapter 10
SA Chapter 10SA Chapter 10
SA Chapter 10
 
OrientDB - The 2nd generation of (multi-model) NoSQL
OrientDB - The 2nd generation of  (multi-model) NoSQLOrientDB - The 2nd generation of  (multi-model) NoSQL
OrientDB - The 2nd generation of (multi-model) NoSQL
 
MongoDB Days Silicon Valley: Jumpstart: The Right and Wrong Use Cases for Mon...
MongoDB Days Silicon Valley: Jumpstart: The Right and Wrong Use Cases for Mon...MongoDB Days Silicon Valley: Jumpstart: The Right and Wrong Use Cases for Mon...
MongoDB Days Silicon Valley: Jumpstart: The Right and Wrong Use Cases for Mon...
 
Transitioning from SQL to MongoDB
Transitioning from SQL to MongoDBTransitioning from SQL to MongoDB
Transitioning from SQL to MongoDB
 
ADO.NET Entity Framework
ADO.NET Entity FrameworkADO.NET Entity Framework
ADO.NET Entity Framework
 
Lecture 13
Lecture 13Lecture 13
Lecture 13
 
Reducing Development Time with MongoDB vs. SQL
Reducing Development Time with MongoDB vs. SQLReducing Development Time with MongoDB vs. SQL
Reducing Development Time with MongoDB vs. SQL
 
Dialog Engine for Product Information
Dialog Engine for Product InformationDialog Engine for Product Information
Dialog Engine for Product Information
 
IRJET- Structuring Mobile Application for Retrieving Book Data Utilizing Opti...
IRJET- Structuring Mobile Application for Retrieving Book Data Utilizing Opti...IRJET- Structuring Mobile Application for Retrieving Book Data Utilizing Opti...
IRJET- Structuring Mobile Application for Retrieving Book Data Utilizing Opti...
 
BSSML17 - Basic Data Transformations
BSSML17 - Basic Data TransformationsBSSML17 - Basic Data Transformations
BSSML17 - Basic Data Transformations
 
RedisConf18 - Redis Memory Optimization
RedisConf18 - Redis Memory OptimizationRedisConf18 - Redis Memory Optimization
RedisConf18 - Redis Memory Optimization
 
VSSML17 L5. Basic Data Transformations and Feature Engineering
VSSML17 L5. Basic Data Transformations and Feature EngineeringVSSML17 L5. Basic Data Transformations and Feature Engineering
VSSML17 L5. Basic Data Transformations and Feature Engineering
 
Building complex UI on Android
Building complex UI on AndroidBuilding complex UI on Android
Building complex UI on Android
 

More from EDB

EFM Office Hours - APJ - July 29, 2021
EFM Office Hours - APJ - July 29, 2021EFM Office Hours - APJ - July 29, 2021
EFM Office Hours - APJ - July 29, 2021
EDB
 
Is There Anything PgBouncer Can’t Do?
Is There Anything PgBouncer Can’t Do?Is There Anything PgBouncer Can’t Do?
Is There Anything PgBouncer Can’t Do?
EDB
 
A Deeper Dive into EXPLAIN
A Deeper Dive into EXPLAINA Deeper Dive into EXPLAIN
A Deeper Dive into EXPLAIN
EDB
 

More from EDB (20)

Cloud Migration Paths: Kubernetes, IaaS, or DBaaS
Cloud Migration Paths: Kubernetes, IaaS, or DBaaSCloud Migration Paths: Kubernetes, IaaS, or DBaaS
Cloud Migration Paths: Kubernetes, IaaS, or DBaaS
 
Die 10 besten PostgreSQL-Replikationsstrategien für Ihr Unternehmen
Die 10 besten PostgreSQL-Replikationsstrategien für Ihr UnternehmenDie 10 besten PostgreSQL-Replikationsstrategien für Ihr Unternehmen
Die 10 besten PostgreSQL-Replikationsstrategien für Ihr Unternehmen
 
Migre sus bases de datos Oracle a la nube
Migre sus bases de datos Oracle a la nube Migre sus bases de datos Oracle a la nube
Migre sus bases de datos Oracle a la nube
 
EFM Office Hours - APJ - July 29, 2021
EFM Office Hours - APJ - July 29, 2021EFM Office Hours - APJ - July 29, 2021
EFM Office Hours - APJ - July 29, 2021
 
Benchmarking Cloud Native PostgreSQL
Benchmarking Cloud Native PostgreSQLBenchmarking Cloud Native PostgreSQL
Benchmarking Cloud Native PostgreSQL
 
Las Variaciones de la Replicación de PostgreSQL
Las Variaciones de la Replicación de PostgreSQLLas Variaciones de la Replicación de PostgreSQL
Las Variaciones de la Replicación de PostgreSQL
 
Is There Anything PgBouncer Can’t Do?
Is There Anything PgBouncer Can’t Do?Is There Anything PgBouncer Can’t Do?
Is There Anything PgBouncer Can’t Do?
 
Data Analysis with TensorFlow in PostgreSQL
Data Analysis with TensorFlow in PostgreSQLData Analysis with TensorFlow in PostgreSQL
Data Analysis with TensorFlow in PostgreSQL
 
Practical Partitioning in Production with Postgres
Practical Partitioning in Production with PostgresPractical Partitioning in Production with Postgres
Practical Partitioning in Production with Postgres
 
A Deeper Dive into EXPLAIN
A Deeper Dive into EXPLAINA Deeper Dive into EXPLAIN
A Deeper Dive into EXPLAIN
 
IOT with PostgreSQL
IOT with PostgreSQLIOT with PostgreSQL
IOT with PostgreSQL
 
Psql is awesome!
Psql is awesome!Psql is awesome!
Psql is awesome!
 
Comment sauvegarder correctement vos données
Comment sauvegarder correctement vos donnéesComment sauvegarder correctement vos données
Comment sauvegarder correctement vos données
 
Cloud Native PostgreSQL - Italiano
Cloud Native PostgreSQL - ItalianoCloud Native PostgreSQL - Italiano
Cloud Native PostgreSQL - Italiano
 
Cloud Native PostgreSQL - APJ
Cloud Native PostgreSQL - APJCloud Native PostgreSQL - APJ
Cloud Native PostgreSQL - APJ
 
Best Practices in Security with PostgreSQL
Best Practices in Security with PostgreSQLBest Practices in Security with PostgreSQL
Best Practices in Security with PostgreSQL
 
EDB Postgres & Tools in a Smart City Project
EDB Postgres & Tools in a Smart City ProjectEDB Postgres & Tools in a Smart City Project
EDB Postgres & Tools in a Smart City Project
 
Migrate Today: Proactive Steps to Unhook from Oracle
Migrate Today: Proactive Steps to Unhook from OracleMigrate Today: Proactive Steps to Unhook from Oracle
Migrate Today: Proactive Steps to Unhook from Oracle
 
All you need to know about CREATE STATISTICS
All you need to know about CREATE STATISTICSAll you need to know about CREATE STATISTICS
All you need to know about CREATE STATISTICS
 
Cloud Native PostgreSQL
Cloud Native PostgreSQLCloud Native PostgreSQL
Cloud Native PostgreSQL
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

NORM: No ORM Framework

  • 1. New NORM! NoRM – No ORM Framework Hettie Dombrovskaya Braviant Holdings Chicago IL USA
  • 3. Money by the numbers 2020 3 1 sec pageloadslowdown=> $1.6billion lost sales/year Slowing searchresultsby0.4 sec– lossof 8 million searchesperday 50% visitors abandonthe site, which is notloadedwithin3 sec 79%visitors will never return again
  • 5. Welcome to ORM! ORM – Object-Relational Mapper • solves a problem of abstraction from details of data storing • does not provide effective means of manipulation with data sets 2020 5
  • 6. Application Model Database Model ORM Flat Tables JDBC (Object-Relational) 6 (Object-Oriented) 2020
  • 7. Our Solution - NORM TransferMapping Mapping Database Model (Object- Relational) JDBC TextText Transfer model (JSON) Transfer Application Model (Object-Oriented)
  • 8. 8 CONTRACT • Both sides (an application and a database) convert internal representations into complex hierarchical object • Contract establishes object structure implemented on both sides • Now, for any application endpoint it takes one database call to transfer data to and from database App DB 2020
  • 10. Objects Mapping (Transport Object) 2020 10 account_record account_id username first_name last_name dob phones[] email_addresses[] phone_record phone_id phone_number phone_type_id phone_type email_record email_id email email_priority_id email_priority phoness[] email_addresses[]
  • 11. Result in JSON Format {” account id”: ”1”, ”username”: ”aliceacct1”, ”last_name”: ”johns”, “firs_name”:”alice”, ”dob” : ”1996−04−01” ”, ”phones” : [ { ”phone_id”: 1, ”phone_number” : ”2021234567” , ”phone_type_id”:”2” ”phone type”: ”cell” } ], ”email addresses”: [ {”email_address_id”:1 , ”email address”:alicejons@gmail.com, “email_priority_id”: “1”, “email_priority”:”primary” } ]} 112020
  • 12. Building the Function Output 2020 12
  • 13. Create a UDT with Nested Types CREATE TYPE phone_record as ( phone_id bigint , phone_number text , phone_type_id int , phone type text ); CREATE TYPE email_record as (email_id bigint, email text, email_priority_id int, email_priority text); CREATE TYPE account_record as (account_id bigint, last_name text, first_name text, dob date, phones phone_record[], email_addresses email_record[]); 2020 13
  • 14. Why Not Storing JSON?! • The power of relational search is lost • By using JSON as a return type for functions we are loosing the strong type dependencies That’s why we organize funcitons in “packages”: we want to ensure that all functions from one “package” return the same object 2020 14
  • 15. Solution SELECT ROW(account_id , username, first_name, last_name, dob , (SELECT array_agg(row(phone_id, phone, p.phone_type_id, phone_type )::phone_record) FROM phone p JOIN phone_type pt USING (phone_type_id) WHERE p.account_id=a.account_id), (SELECT array_agg(row(email_id, email, e.email_priority_id, email_priority )::email_record) FROM email e JOIN email_priority ep using(email_priority_id) WHERE e.account_id=a.account_id) )::account_record AS single_item FROM account a WHERE a.account_id=p_account_id 2020 15
  • 16. Transforming to Text CREATE OR REPLACE FUNCTION array_transport (all_items anyarray) returns setof text RETURNS NULL ON NULL INPUT LANGUAGE plpgsql as $body$ DECLARE item record; BEGIN FOREACH item IN array all_items LOOP RETURN next( to_json(item))::text; END LOOP; END; 2020 16
  • 17. Example: How to Build it https://github.com/hettie-d/NORM 2020 17

Editor's Notes

  1. . Imagine tha you have a shopping list, and you would go to the store for each of your items individually
  2. We utilize a well-known concept of API contract
  3. Let’s look again at the earlier slides