Postgres Types Mapped to JavaScript via GraphQL with Hasura
Tags: database, datamodelling, datatypes
Published: 2021-04-30 10:16:02

---
title: Postgres Types Mapped to JavaScript via GraphQL with Hasura
published: true
date: 2021-04-27 13:19:20 UTC
tags: Database,DataModelling,DataTypes
canonical_url: https://hasura.io/blog/postgres-types-mapped-to-javascript-via-graphql-with-hasura/
---

![Postgres Types Mapped to JavaScript via GraphQL with Hasura](https://hasura.io/blog/content/images/2021/04/js_postgres_map.png)

Hello all, [Adron](https://twitter.com/adron) here, your host on [Hasura Coding](https://www.twitch.tv/hasurahq/schedule). Today I've collected together a number of video shorts, extra details, and information about primary keys, primary key data types, and data types in general. In this post I've included links to mapping from Postgres types to JavaScript types via GraphQL and Hasura.

## Primary Keys

> Primary Key: A primary key in a relational database uniquely identifies the record among many records stored in a table.

Primary keys can be made up of one or more columns, of existing data, or made up data that serves only the purpose to identify the record itself. The following are the kinds of primary keys a table can have.

- **Super Key** - A super key is a single or multiple keys that make up a group of keys which identifies a row within a table.
- **Candidate Key** - A candidate key are attributes that uniquely identify rows in a table. A candidate key is a super key with none of the attributes repeated, thus a natural key within the existing stored data.
- **Alternate Key** - An alternate key is a column or group of columns that uniquely identify every row in the table. It is a key that isn't particularly selected as a primary key, but can be used as one if the primary key weren't available for some reason.
- **Compound Key** - A compound key has two or more columns included as the primary key of a table to uniquely identify each row of data.
- **Composite Key** - An artificial key (think of a generated autonumber, short ID, CUID, or UUID) that uniquely idenfies each record. Also referred to as a surrogate key, these keys are used when a natural key doesn't exist in the schema of the existing table.
- **Surrogate Key** - See _Composite Key_.

I put together two videos related to specific primary keys: UUIDs & creating Short URL Safe UUIDs. Check those out on Youtube.

{% youtube DdycuIVb2vI %}

The short ID function I add in this short, is embedded below and provided in [this gist](https://gist.github.com/Adron/f37ae1f28d2f87153d617183a9c4b43c) for easy and quick access!

<!--kg-card-begin: markdown-->
```
CREATE OR REPLACE FUNCTION gen_unique_short_id() returns text
    language plpgsql
as $$
DECLARE
    id text;
BEGIN
    id := encode(gen_random_bytes(6), 'base64');
    id := replace(id, '/', '_');
    id := replace(id, '+', '_');
    RETURN id;
END;
$$;

alter function gen_unique_short_id() owner to postgres;

```
<!--kg-card-end: markdown-->

{% youtube tQ8AaQyKKFk %}

The above definitions and details I discuss in ["A Hasura Bit - What is the UUID column type good for and what exactly is a UUID?"](https://youtu.be/tQ8AaQyKKFk) leave some additional details that should be added. UUIDs are great as surrogate keys for many situations, but there are some situations where they can cause significant problems.

For example. Some of the characteristics of UUIDs can become significant problems in higher scale scenarios or in certain hardware situations. Being the UUID is a largely random value and numbered value, the database engine attempts to order these during writes, which if there are too many writes that then require reordering immediately come in, it can cause disk thrashing. Something that in the end can become a significant performance issue! At the same time, 128-bit can be excessively large with large volumes of data just for the key that may not be needed.

These problems, as with any trade off, just emphasize the fact that for each and every scenario the pluses and minuses need to be measured carefully for the intended purpose and outcome for the mission at hand.

Other considerations with both the UUID and short ID above include the issue with writes incurring a read/write and possible disk thrashing if the insert volume gets too high too fast. Another issue for the short IDs is that there is a slightly higher possibility of duplicates being generated. However, in the sense of a high possibility it's still absurdly low and statistically almost nonexistent. However, it doesn't hurt to have a check and write a new key if a duplicate does occur to prevent that error. But the investment in that level vs. just dealing with the error might not be worth it, every project might have a different tolerance for a possible insert error of that sort. Maybe a retry is good enough?

## Character Data & Numeric Data Types

In the next two shorts I tackle a few details about character data and numeric data in GraphQL, how it maps back against Postgres types and reference the key grid that maps Postgres types to their respective API types one would get in Hasura served GraphQL JSON result objects [here](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html), and below the videos for quick reference in this article.

{% youtube oLNjr7j8tOs %}

{% youtube 4FJ0QgzmptU %}

<!--kg-card-begin: markdown-->

| Name | Aliases | Description | Hasura Type |
| --- | --- | --- | --- |
| bigint | int8 | signed eight-byte integer | [String](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#string) |
| bigserial | serial8 | autoincrementing eight-byte integer | [String](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#string) |
| bit [(n)] | | fixed-length bit string | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| bit varying [(n)] | varbit [(n)] | variable-length bit string | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| boolean | bool | logical Boolean (true/false) | [Bool](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#bool) |
| box | | rectangular box on a plane | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| bytea | | binary data (“byte array”) | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| character [(n)] | char [(n)] | fixed-length character string | [Char](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#char) |
| character varying [(n)] | varchar [(n)] | variable-length character string | [String](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#string) |
| cidr | | IPv4 or IPv6 network address | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| circle | | circle on a plane | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| date | | calendar date (year | month | day) | [Date](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#date) |
| double precision | float8 | double precision floating-point number (8 bytes) | [Float](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#float) |
| inet | | IPv4 or IPv6 host address | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| integer | int | int4 | signed four-byte integer | [Int](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#int) |
| interval [fields] [(p)] | | time span | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| json | | textual JSON data | [JSON](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#json) |
| jsonb | | binary JSON data | decomposed | [JSONB](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#jsonb) |
| line | | infinite line on a plane | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| lseg | | line segment on a plane | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| ltree | | labels of data stored in a hierarchical tree-like structure | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| geometry | | PostGIS Geometry type | [Geometry](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#geometry) |
| geography | | PostGIS Geography type | [Geography](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#geography) |
| macaddr | | MAC (Media Access Control) address | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| macaddr8 | | MAC (Media Access Control) address (EUI-64 format) | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| money | | currency amount | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| numeric [(p | s)] | decimal [(p | s)] | exact numeric of selectable precision | [Numeric](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#numeric) |
| path | | geometric path on a plane | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| pg\_lsn | | PostgreSQL Log Sequence Number | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| point | | geometric point on a plane | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| polygon | | closed geometric path on a plane | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| real | float4 | single precision floating-point number (4 bytes) | [Float](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#float) |
| smallint | int2 | signed two-byte integer | [Int](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#int) |
| smallserial | serial2 | autoincrementing two-byte integer | [Int](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#int) |
| serial | serial4 | autoincrementing four-byte integer | [Int](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#int) |
| text | | variable-length character string | [String](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#string) |
| time [(p)] (without time zone) | | time of day (no time zone) | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| time [(p)] (with time zone) | timetz | time of day | including time zone | [Timetz](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#timetz) |
| timestamp [(p)] (without time zone) | | date and time (no time zone) | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| timestamp [(p)] (with time zone) | timestamptz | date and time | including time zone | [Timestamptz](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#timestamptz) |
| tsquery | | text search query | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| tsvector | | text search document | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| txid\_snapshot | | user-level transaction ID snapshot | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| uuid | | universally unique identifier | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |
| xml | | XML data | [Implicit](https://hasura.io/docs/latest/graphql/core/databases/postgres/postgresql-types.html#implicit) |

<!--kg-card-end: markdown-->