From 38d0f9ce1569f2010c0002d78a4729d6eebc5778 Mon Sep 17 00:00:00 2001
From: Hugo Trentesaux <hugo@trentesaux.fr>
Date: Thu, 11 Apr 2024 10:23:39 +0200
Subject: [PATCH] init hasura from v2 datapods

---
 .env.example                                  |  4 ++
 .gitignore                                    | 17 +----
 .vscode/launch.json                           | 17 +++++
 docker-compose.yml                            | 36 ++++++++++
 hasura/metadata/actions.graphql               | 71 +++++++++++++++++++
 hasura/metadata/actions.yaml                  | 41 +++++++++++
 hasura/metadata/allow_list.yaml               |  1 +
 hasura/metadata/api_limits.yaml               |  1 +
 hasura/metadata/backend_configs.yaml          |  1 +
 hasura/metadata/cron_triggers.yaml            |  1 +
 hasura/metadata/databases/databases.yaml      | 14 ++++
 .../default/tables/public_profiles.yaml       | 25 +++++++
 .../databases/default/tables/tables.yaml      |  1 +
 .../graphql_schema_introspection.yaml         |  1 +
 hasura/metadata/inherited_roles.yaml          |  1 +
 hasura/metadata/metrics_config.yaml           |  1 +
 hasura/metadata/network.yaml                  |  1 +
 hasura/metadata/opentelemetry.yaml            |  1 +
 hasura/metadata/query_collections.yaml        |  1 +
 hasura/metadata/remote_schemas.yaml           |  1 +
 hasura/metadata/rest_endpoints.yaml           |  1 +
 hasura/metadata/version.yaml                  |  1 +
 .../default/1702976538494_init/up.sql         | 21 ++++++
 src/main.ts                                   |  2 +-
 src/types.ts                                  |  1 +
 25 files changed, 247 insertions(+), 16 deletions(-)
 create mode 100644 .env.example
 create mode 100644 .vscode/launch.json
 create mode 100644 docker-compose.yml
 create mode 100644 hasura/metadata/actions.graphql
 create mode 100644 hasura/metadata/actions.yaml
 create mode 100644 hasura/metadata/allow_list.yaml
 create mode 100644 hasura/metadata/api_limits.yaml
 create mode 100644 hasura/metadata/backend_configs.yaml
 create mode 100644 hasura/metadata/cron_triggers.yaml
 create mode 100644 hasura/metadata/databases/databases.yaml
 create mode 100644 hasura/metadata/databases/default/tables/public_profiles.yaml
 create mode 100644 hasura/metadata/databases/default/tables/tables.yaml
 create mode 100644 hasura/metadata/graphql_schema_introspection.yaml
 create mode 100644 hasura/metadata/inherited_roles.yaml
 create mode 100644 hasura/metadata/metrics_config.yaml
 create mode 100644 hasura/metadata/network.yaml
 create mode 100644 hasura/metadata/opentelemetry.yaml
 create mode 100644 hasura/metadata/query_collections.yaml
 create mode 100644 hasura/metadata/remote_schemas.yaml
 create mode 100644 hasura/metadata/rest_endpoints.yaml
 create mode 100644 hasura/metadata/version.yaml
 create mode 100644 hasura/migrations/default/1702976538494_init/up.sql

diff --git a/.env.example b/.env.example
new file mode 100644
index 0000000..7e38aab
--- /dev/null
+++ b/.env.example
@@ -0,0 +1,4 @@
+# === POSTGRES / HASURA ===
+DB_PASSWORD=my_db_password
+HASURA_GRAPHQL_ADMIN_SECRET=my_hasura_password
+HASURA_LISTEN_PORT=8765
diff --git a/.gitignore b/.gitignore
index a547bf3..118821d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,23 +2,10 @@
 logs
 *.log
 npm-debug.log*
-yarn-debug.log*
-yarn-error.log*
 pnpm-debug.log*
-lerna-debug.log*
 
 node_modules
-dist
-dist-ssr
 *.local
 
-# Editor directories and files
-.vscode/*
-!.vscode/extensions.json
-.idea
-.DS_Store
-*.suo
-*.ntvs*
-*.njsproj
-*.sln
-*.sw?
+# Env
+.env
\ No newline at end of file
diff --git a/.vscode/launch.json b/.vscode/launch.json
new file mode 100644
index 0000000..f5e7229
--- /dev/null
+++ b/.vscode/launch.json
@@ -0,0 +1,17 @@
+{
+  // Utilisez IntelliSense pour en savoir plus sur les attributs possibles.
+  // Pointez pour afficher la description des attributs existants.
+  // Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387
+  "version": "0.2.0",
+  "configurations": [
+    {
+      "name": "C+ import",
+      "type": "node",
+      "request": "launch",
+      "cwd": "${workspaceRoot}",
+      "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/tsx",
+      "program": "./src/scripts/cesium-plus-import.ts",
+      "console": "integratedTerminal"
+    }
+  ]
+}
\ No newline at end of file
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..0bce8ce
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,36 @@
+version: "3.6"
+services:
+  postgres:
+    image: postgres:16
+    restart: always
+    volumes:
+      - db_data:/var/lib/postgresql/data
+    environment:
+      POSTGRES_PASSWORD: ${DB_PASSWORD}
+    healthcheck:
+      test: ["CMD-SHELL", "pg_isready -U postgres -d postgres"]
+      interval: 2s
+      timeout: 2s
+      retries: 5
+
+  hasura:
+    image: hasura/graphql-engine:v2.38.1.cli-migrations-v3
+    depends_on:
+      postgres:
+        condition: service_healthy
+    restart: always
+    ports:
+      - "${HASURA_LISTEN_PORT}:8080"
+    environment:
+      HASURA_GRAPHQL_DATABASE_URL: postgres://postgres:${DB_PASSWORD}@postgres:5432/postgres
+      HASURA_GRAPHQL_ENABLE_CONSOLE: "true"
+      HASURA_GRAPHQL_DEV_MODE: "true"
+      HASURA_GRAPHQL_UNAUTHORIZED_ROLE: public
+      HASURA_GRAPHQL_ENABLE_TELEMETRY: "false"
+      HASURA_GRAPHQL_ADMIN_SECRET: ${HASURA_GRAPHQL_ADMIN_SECRET}
+    volumes: # for local developement, you want to record the database migrations in git
+      - ./hasura/migrations:/hasura-migrations
+      - ./hasura/metadata:/hasura-metadata
+
+volumes:
+  db_data:
diff --git a/hasura/metadata/actions.graphql b/hasura/metadata/actions.graphql
new file mode 100644
index 0000000..2d49d28
--- /dev/null
+++ b/hasura/metadata/actions.graphql
@@ -0,0 +1,71 @@
+type Mutation {
+  addTransaction(
+    id: String!
+    address: String!
+    hash: String!
+    signature: String!
+    comment: String!
+  ): AddTransactionResponse
+}
+
+type Mutation {
+  deleteProfile(
+    address: String!
+    hash: String!
+    signature: String!
+  ): DeleteProfileResponse
+}
+
+type Mutation {
+  migrateProfile(
+    addressOld: String!
+    addressNew: String!
+    hash: String!
+    signature: String!
+  ): MigrateProfileResponse
+}
+
+type Mutation {
+  updateProfile(
+    address: String!
+    hash: String!
+    signature: String!
+    avatarBase64: String
+    title: String
+    description: String
+    city: String
+    socials: [SocialInput!]
+    geoloc: GeolocInput
+  ): UpdateProfileResponse
+}
+
+input GeolocInput {
+  latitude: Float!
+  longitude: Float!
+}
+
+input SocialInput {
+  url: String!
+  type: String
+}
+
+type UpdateProfileResponse {
+  success: Boolean!
+  message: String!
+}
+
+type DeleteProfileResponse {
+  success: Boolean!
+  message: String!
+}
+
+type MigrateProfileResponse {
+  success: Boolean!
+  message: String!
+}
+
+type AddTransactionResponse {
+  success: Boolean!
+  message: String!
+}
+
diff --git a/hasura/metadata/actions.yaml b/hasura/metadata/actions.yaml
new file mode 100644
index 0000000..b03cfce
--- /dev/null
+++ b/hasura/metadata/actions.yaml
@@ -0,0 +1,41 @@
+actions:
+  - name: addTransaction
+    definition:
+      kind: synchronous
+      handler: http://host.docker.internal:3000/add-transaction
+    permissions:
+      - role: public
+    comment: addTransaction
+  - name: deleteProfile
+    definition:
+      kind: synchronous
+      handler: http://host.docker.internal:3000/delete-profile-data
+    permissions:
+      - role: public
+    comment: deleteProfile
+  - name: migrateProfile
+    definition:
+      kind: synchronous
+      handler: http://host.docker.internal:3000/migrate-profile-data
+    permissions:
+      - role: public
+    comment: migrateProfile
+  - name: updateProfile
+    definition:
+      kind: synchronous
+      handler: http://host.docker.internal:3000/update-profile-data
+      timeout: 15
+    permissions:
+      - role: public
+    comment: updateProfile
+custom_types:
+  enums: []
+  input_objects:
+    - name: GeolocInput
+    - name: SocialInput
+  objects:
+    - name: UpdateProfileResponse
+    - name: DeleteProfileResponse
+    - name: MigrateProfileResponse
+    - name: AddTransactionResponse
+  scalars: []
diff --git a/hasura/metadata/allow_list.yaml b/hasura/metadata/allow_list.yaml
new file mode 100644
index 0000000..fe51488
--- /dev/null
+++ b/hasura/metadata/allow_list.yaml
@@ -0,0 +1 @@
+[]
diff --git a/hasura/metadata/api_limits.yaml b/hasura/metadata/api_limits.yaml
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/hasura/metadata/api_limits.yaml
@@ -0,0 +1 @@
+{}
diff --git a/hasura/metadata/backend_configs.yaml b/hasura/metadata/backend_configs.yaml
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/hasura/metadata/backend_configs.yaml
@@ -0,0 +1 @@
+{}
diff --git a/hasura/metadata/cron_triggers.yaml b/hasura/metadata/cron_triggers.yaml
new file mode 100644
index 0000000..fe51488
--- /dev/null
+++ b/hasura/metadata/cron_triggers.yaml
@@ -0,0 +1 @@
+[]
diff --git a/hasura/metadata/databases/databases.yaml b/hasura/metadata/databases/databases.yaml
new file mode 100644
index 0000000..65a11b2
--- /dev/null
+++ b/hasura/metadata/databases/databases.yaml
@@ -0,0 +1,14 @@
+- name: default
+  kind: postgres
+  configuration:
+    connection_info:
+      database_url:
+        from_env: HASURA_GRAPHQL_DATABASE_URL
+      isolation_level: read-committed
+      pool_settings:
+        connection_lifetime: 600
+        idle_timeout: 180
+        max_connections: 50
+        retries: 1
+      use_prepared_statements: true
+  tables: "!include default/tables/tables.yaml"
diff --git a/hasura/metadata/databases/default/tables/public_profiles.yaml b/hasura/metadata/databases/default/tables/public_profiles.yaml
new file mode 100644
index 0000000..f94138f
--- /dev/null
+++ b/hasura/metadata/databases/default/tables/public_profiles.yaml
@@ -0,0 +1,25 @@
+table:
+  name: profiles
+  schema: public
+computed_fields:
+  - name: avatar64
+    definition:
+      function:
+        name: bytea_to_base64
+        schema: public
+    comment: convert avatar from bytea to base64
+select_permissions:
+  - role: public
+    permission:
+      columns:
+        - avatar
+        - address
+        - geoloc
+        - description
+        - created_at
+        - updated_at
+      computed_fields:
+        - avatar64
+      filter: {}
+      limit: 500
+    comment: ""
diff --git a/hasura/metadata/databases/default/tables/tables.yaml b/hasura/metadata/databases/default/tables/tables.yaml
new file mode 100644
index 0000000..6dc4150
--- /dev/null
+++ b/hasura/metadata/databases/default/tables/tables.yaml
@@ -0,0 +1 @@
+- "!include public_profiles.yaml"
diff --git a/hasura/metadata/graphql_schema_introspection.yaml b/hasura/metadata/graphql_schema_introspection.yaml
new file mode 100644
index 0000000..61a4dca
--- /dev/null
+++ b/hasura/metadata/graphql_schema_introspection.yaml
@@ -0,0 +1 @@
+disabled_for_roles: []
diff --git a/hasura/metadata/inherited_roles.yaml b/hasura/metadata/inherited_roles.yaml
new file mode 100644
index 0000000..fe51488
--- /dev/null
+++ b/hasura/metadata/inherited_roles.yaml
@@ -0,0 +1 @@
+[]
diff --git a/hasura/metadata/metrics_config.yaml b/hasura/metadata/metrics_config.yaml
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/hasura/metadata/metrics_config.yaml
@@ -0,0 +1 @@
+{}
diff --git a/hasura/metadata/network.yaml b/hasura/metadata/network.yaml
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/hasura/metadata/network.yaml
@@ -0,0 +1 @@
+{}
diff --git a/hasura/metadata/opentelemetry.yaml b/hasura/metadata/opentelemetry.yaml
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/hasura/metadata/opentelemetry.yaml
@@ -0,0 +1 @@
+{}
diff --git a/hasura/metadata/query_collections.yaml b/hasura/metadata/query_collections.yaml
new file mode 100644
index 0000000..fe51488
--- /dev/null
+++ b/hasura/metadata/query_collections.yaml
@@ -0,0 +1 @@
+[]
diff --git a/hasura/metadata/remote_schemas.yaml b/hasura/metadata/remote_schemas.yaml
new file mode 100644
index 0000000..fe51488
--- /dev/null
+++ b/hasura/metadata/remote_schemas.yaml
@@ -0,0 +1 @@
+[]
diff --git a/hasura/metadata/rest_endpoints.yaml b/hasura/metadata/rest_endpoints.yaml
new file mode 100644
index 0000000..fe51488
--- /dev/null
+++ b/hasura/metadata/rest_endpoints.yaml
@@ -0,0 +1 @@
+[]
diff --git a/hasura/metadata/version.yaml b/hasura/metadata/version.yaml
new file mode 100644
index 0000000..0a70aff
--- /dev/null
+++ b/hasura/metadata/version.yaml
@@ -0,0 +1 @@
+version: 3
diff --git a/hasura/migrations/default/1702976538494_init/up.sql b/hasura/migrations/default/1702976538494_init/up.sql
new file mode 100644
index 0000000..67c869e
--- /dev/null
+++ b/hasura/migrations/default/1702976538494_init/up.sql
@@ -0,0 +1,21 @@
+SET check_function_bodies = false;
+CREATE TABLE public.profiles (
+    address text NOT NULL,
+    avatar bytea,
+    description text,
+    geoloc point,
+    title text,
+    city text,
+    socials jsonb,
+    created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
+    updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP
+);
+CREATE FUNCTION public.bytea_to_base64(data_row public.profiles) RETURNS text
+    LANGUAGE plpgsql STABLE
+    AS $$
+BEGIN
+    RETURN ENCODE(data_row.avatar, 'base64');
+END;
+$$;
+ALTER TABLE ONLY public.profiles
+    ADD CONSTRAINT profiles_pkey PRIMARY KEY (address);
diff --git a/src/main.ts b/src/main.ts
index 5179324..f8cbf19 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -37,7 +37,7 @@ pubsub.addEventListener('message', (message) => {
       const isValid = isValidSignature(bytesPayload, dag.sig, dag.pubk)
       if (isValid) {
         // here we would do the processing
-        addToIndex(cid, dag)
+        // addToIndex(cid, dag)
       } else {
         feed.value.push('[invalid sig] ' + msg)
       }
diff --git a/src/types.ts b/src/types.ts
index 8942d05..1e0bffe 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -21,6 +21,7 @@ export interface IndexRequest {
   /// kind of the document
   // the CID points to something describing the kind of the document
   // this allows to filter documents based on kinds of interest
+  // TODO allow number for more compact known kinds
   kind: CID
   /// data coming with the index request
   // only nullable field, useful for deletion document for example
-- 
GitLab