Skip to content

feat: add typescript typegen endpoint #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 9, 2022
Merged

feat: add typescript typegen endpoint #269

merged 2 commits into from
Jun 9, 2022

Conversation

soedirgo
Copy link
Member

@soedirgo soedirgo commented Jun 8, 2022

e.g. for current test fixtures:

-- Tables for testing
CREATE TYPE public.user_status AS ENUM ('ACTIVE', 'INACTIVE');
CREATE TABLE public.users (
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name text,
status user_status DEFAULT 'ACTIVE'
);
INSERT INTO
public.users (name)
VALUES
('Joe Bloggs'),
('Jane Doe');
CREATE TABLE public.todos (
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
details text,
"user-id" bigint REFERENCES users NOT NULL
);
INSERT INTO
public.todos (details, "user-id")
VALUES
('Star the repo', 1),
('Watch the releases', 2);
CREATE FUNCTION add(integer, integer) RETURNS integer
AS 'select $1 + $2;'
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;
create table public.users_audit (
id BIGINT generated by DEFAULT as identity,
created_at timestamptz DEFAULT now(),
user_id bigint,
previous_value jsonb
);
create function public.audit_action()
returns trigger as $$
begin
insert into public.users_audit (user_id, previous_value)
values (old.id, row_to_json(old));
return new;
end;
$$ language plpgsql;
CREATE VIEW todos_view AS SELECT * FROM public.todos;

CREATE TABLE public.category (
id serial NOT NULL PRIMARY KEY,
name text NOT NULL
);
-- Fake policies
grant select, update(name)
on category
to postgres;
create policy categories_update_policy
on category for update
to postgres
using(current_setting('my.username') IN (name));
INSERT INTO public.category (id, name) VALUES
(1, 'Funny'),
(2, 'Weird'),
(3, 'Dumb'),
(4, 'Cute'),
(5, 'Interesting');
CREATE TYPE meme_status AS ENUM ('new', 'old', 'retired');
CREATE TABLE public.memes (
id serial NOT NULL PRIMARY KEY,
name text NOT NULL,
category INTEGER REFERENCES category(id),
metadata jsonb,
created_at TIMESTAMP NOT NULL,
status meme_status DEFAULT 'old'
);

Generated types:

export type Json =
  | string
  | number
  | boolean
  | null
  | { [key: string]: Json }
  | Json[];

export interface Database {
  public: {
    Tables: {
      users: {
        Row: {
          id: number;
          name: string | null;
          status: "ACTIVE" | "INACTIVE" | null;
        };
        Insert: {
          id?: number;
          name?: string | null;
          status?: "ACTIVE" | "INACTIVE" | null;
        };
        Update: {
          id?: number;
          name?: string | null;
          status?: "ACTIVE" | "INACTIVE" | null;
        };
      };
      todos: {
        Row: {
          id: number;
          details: string | null;
          "user-id": number;
        };
        Insert: {
          id?: number;
          details?: string | null;
          "user-id": number;
        };
        Update: {
          id?: number;
          details?: string | null;
          "user-id"?: number;
        };
      };
      users_audit: {
        Row: {
          id: number;
          user_id: number | null;
          previous_value: Json | null;
          created_at: string | null;
        };
        Insert: {
          id?: number;
          user_id?: number | null;
          previous_value?: Json | null;
          created_at?: string | null;
        };
        Update: {
          id?: number;
          user_id?: number | null;
          previous_value?: Json | null;
          created_at?: string | null;
        };
      };
      category: {
        Row: {
          id: number;
          name: string;
        };
        Insert: {
          id?: number;
          name: string;
        };
        Update: {
          id?: number;
          name?: string;
        };
      };
      memes: {
        Row: {
          name: string;
          category: number | null;
          metadata: Json | null;
          created_at: string;
          id: number;
          status: "new" | "old" | "retired" | null;
        };
        Insert: {
          name: string;
          category?: number | null;
          metadata?: Json | null;
          created_at: string;
          id?: number;
          status?: "new" | "old" | "retired" | null;
        };
        Update: {
          name?: string;
          category?: number | null;
          metadata?: Json | null;
          created_at?: string;
          id?: number;
          status?: "new" | "old" | "retired" | null;
        };
      };
    };
    Functions: {
      add: {
        Args: Record<string, unknown>;
        Returns: number;
      };
    };
  };
}

fastify.get<{
Headers: { pg: string }
Querystring: {
excluded_schemas?: string
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is for auth, storage, graphql, realtime etc. - we should only generate for user's schemas

output = prettier.format(output, {
parser: 'typescript',
})
return output
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Content-Type: text/plain; charset=utf-8 by default

(function_) => `${JSON.stringify(function_.name)}: {
Args: ${(() => {
if (function_.argument_types === '') {
return 'Record<PropertyKey, never>'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for functions w/o args

Copy link
Member

@kiwicopple kiwicopple left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great @soedirgo

Perhaps just move the templating logic out of the "routes" file 👍

@soedirgo
Copy link
Member Author

soedirgo commented Jun 9, 2022

Perhaps just move the templating logic out of the "routes" file 👍

Done!

@soedirgo soedirgo merged commit 61a59ce into master Jun 9, 2022
@soedirgo soedirgo deleted the feat/typegen branch June 9, 2022 08:00
@github-actions
Copy link

github-actions bot commented Jun 9, 2022

🎉 This PR is included in version 0.37.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants