diff --git a/db/hasura_metadata.json b/db/hasura_metadata.json new file mode 100644 index 0000000..236b0e3 --- /dev/null +++ b/db/hasura_metadata.json @@ -0,0 +1,322 @@ +{ + "version": 2, + "tables": [ + { + "table": { + "schema": "accounts", + "name": "profile" + }, + "object_relationships": [ + { + "name": "user", + "using": { + "foreign_key_constraint_on": "userid" + } + } + ], + "select_permissions": [ + { + "role": "user", + "permission": { + "columns": [ + "userid", + "username", + "birthdate", + "height", + "displayunit" + ], + "filter": { + "userid": { + "_eq": "X-Hasura-User-Id" + } + }, + "limit": 1 + } + } + ], + "update_permissions": [ + { + "role": "user", + "permission": { + "columns": [ + "birthdate", + "displayunit", + "height", + "username" + ], + "filter": { + "userid": { + "_eq": "X-Hasura-User-Id" + } + }, + "check": null + } + } + ] + }, + { + "table": { + "schema": "accounts", + "name": "users" + }, + "array_relationships": [ + { + "name": "loggoals", + "using": { + "foreign_key_constraint_on": { + "column": "userid", + "table": { + "schema": "stats", + "name": "loggoal" + } + } + } + }, + { + "name": "logs", + "using": { + "foreign_key_constraint_on": { + "column": "userid", + "table": { + "schema": "stats", + "name": "log" + } + } + } + }, + { + "name": "profiles", + "using": { + "foreign_key_constraint_on": { + "column": "userid", + "table": { + "schema": "accounts", + "name": "profile" + } + } + } + }, + { + "name": "usersauths", + "using": { + "foreign_key_constraint_on": { + "column": "userid", + "table": { + "schema": "accounts", + "name": "usersauth" + } + } + } + } + ], + "select_permissions": [ + { + "role": "user", + "permission": { + "columns": [ + "email" + ], + "filter": { + "id": { + "_eq": "X-Hasura-User-Id" + } + }, + "limit": 1 + } + } + ] + }, + { + "table": { + "schema": "accounts", + "name": "usersauth" + }, + "object_relationships": [ + { + "name": "user", + "using": { + "foreign_key_constraint_on": "userid" + } + } + ], + "select_permissions": [ + { + "role": "user", + "permission": { + "columns": [ + "createdts", + "deviceid", + "sourceip", + "tokenhash" + ], + "filter": { + "userid": { + "_eq": "X-Hasura-User-Id" + } + } + } + } + ] + }, + { + "table": { + "schema": "stats", + "name": "log" + }, + "object_relationships": [ + { + "name": "user", + "using": { + "foreign_key_constraint_on": "userid" + } + } + ], + "insert_permissions": [ + { + "role": "user", + "permission": { + "check": { + "userid": { + "_eq": "X-Hasura-User-Id" + } + }, + "columns": [ + "logtype", + "recordedts", + "userid", + "value" + ], + "backend_only": false + } + } + ], + "select_permissions": [ + { + "role": "user", + "permission": { + "columns": [ + "id", + "userid", + "logtype", + "value", + "recordedts" + ], + "filter": { + "userid": { + "_eq": "X-Hasura-User-Id" + } + } + } + } + ] + }, + { + "table": { + "schema": "stats", + "name": "loggoal" + }, + "object_relationships": [ + { + "name": "user", + "using": { + "foreign_key_constraint_on": "userid" + } + } + ], + "insert_permissions": [ + { + "role": "user", + "permission": { + "check": { + "userid": { + "_eq": "X-Hasura-User-Id" + } + }, + "columns": [ + "endts", + "logtype", + "startts", + "userid", + "value" + ], + "backend_only": false + } + } + ], + "select_permissions": [ + { + "role": "user", + "permission": { + "columns": [ + "endts", + "logtype", + "startts", + "userid", + "value" + ], + "filter": { + "userid": { + "_eq": "X-Hasura-User-Id" + } + } + } + } + ], + "update_permissions": [ + { + "role": "user", + "permission": { + "columns": [ + "endts", + "logtype", + "startts", + "value" + ], + "filter": { + "userid": { + "_eq": "X-Hasura-User-Id" + } + }, + "check": null + } + } + ], + "delete_permissions": [ + { + "role": "user", + "permission": { + "filter": { + "userid": { + "_eq": "X-Hasura-User-Id" + } + } + } + } + ] + }, + { + "table": { + "schema": "workouts", + "name": "exercises" + }, + "select_permissions": [ + { + "role": "user", + "permission": { + "columns": [ + "id", + "name", + "muscletarget", + "forcetype", + "mechanicstype", + "instructions", + "videolink", + "tutoriallink" + ], + "filter": {} + } + } + ] + } + ] +} \ No newline at end of file diff --git a/db/makefile b/db/makefile index 2e0a8aa..fe513ef 100644 --- a/db/makefile +++ b/db/makefile @@ -3,7 +3,10 @@ default: start clean: @rm -rf .data -start: .data +start: start-db start-graphql +stop: stop-graphql stop-db + +start-db: .data @docker run -it -d --name=bodytrack-db \ -p 5432:5432/tcp \ -e POSTGRES_USER=postgres \ @@ -15,9 +18,21 @@ start: .data -u $$UID:$$UID \ postgres:13-alpine -stop: +stop-db: @docker rm -f -v bodytrack-db +start-graphql: stop-graphql + @docker run -it -d --name=bodytrack-graphql \ + -p 8080:8080 \ + -e HASURA_GRAPHQL_DATABASE_URL=postgres://hasura:hasura-user@bodytrack-db:5432/bodytrack \ + -e HASURA_GRAPHQL_ENABLE_CONSOLE=true \ + -e HASURA_GRAPHQL_ADMIN_SECRET=bodytrack-graphql \ + --link=bodytrack-db \ + hasura/graphql-engine + +stop-graphql: + @docker rm -f -v bodytrack-graphql + logs: docker logs bodytrack-db diff --git a/db/schema/0000_root.sql b/db/schema/0000_root.sql index 845eca8..51ed993 100644 --- a/db/schema/0000_root.sql +++ b/db/schema/0000_root.sql @@ -1,3 +1,6 @@ CREATE TYPE UNIT AS ENUM ('Metric', 'Imperial'); CREATE TYPE LogType AS ENUM ('Weight', 'Height', 'Calories'); +CREATE TYPE MuscleGroup AS ENUM ('Chest', 'Thighs', 'Back', 'Glutes', 'Calves'); +CREATE TYPE Force AS ENUM ('Push', 'Pull'); +CREATE TYPE Mechanic AS ENUM ('Compound', 'Isolation'); diff --git a/db/schema/0003_exercises.sql b/db/schema/0003_exercises.sql new file mode 100644 index 0000000..81e278a --- /dev/null +++ b/db/schema/0003_exercises.sql @@ -0,0 +1,82 @@ +CREATE SCHEMA Workouts; + + +CREATE TABLE Workouts.Exercises ( + id SERIAL PRIMARY KEY, + name VARCHAR(128) UNIQUE NOT NULL, + muscleTarget MuscleGroup NOT NULL, + forceType Force NOT NULL DEFAULT 'Push', + mechanicsType Mechanic NOT NULL DEFAULT 'Compound', + instructions VARCHAR(4096) NOT NULL DEFAULT '', + videoLink VARCHAR(1024), + tutorialLink VARCHAR(1024) +); + +CREATE INDEX exercises_forceType ON workouts.exercises USING btree (forceType); +CREATE INDEX exercises_mechanicsType ON workouts.exercises USING btree (mechanicsType); +CREATE INDEX exercises_muscleTarget ON workouts.exercises USING btree (muscleTarget); +CREATE INDEX exercises_name ON workouts.exercises USING btree (name); + +INSERT INTO Workouts.Exercises + (name, muscleTarget, forceType, mechanicsType, instructions, videoLink, tutorialLink) +VALUES ( + 'Barbell Bench Press', + 'Chest', + 'Push', + 'Compound', + 'Preparation\n\nLie supine on bench. Dismount barbell from rack over upper chest using wide oblique overhand grip.\n\nExecution\nLower weight to chest. Press bar upward until arms are extended. Repeat.', + 'https://exrx.glorb.com/api/video/51453f31e81987c67e79c3ec4000ba3e/10697', + 'https://exrx.net/WeightExercises/PectoralSternal/BBBenchPress' +); + + +INSERT INTO Workouts.Exercises + (name, muscleTarget, forceType, mechanicsType, instructions, videoLink, tutorialLink) +VALUES ( + 'Barbell Squat (High Bar)', + 'Thighs', + 'Push', + 'Compound', + 'Preparation\n\nFrom rack with barbell at upper chest height, position bar high on back of shoulders and grasp barbell to sides. Dismount bar from rack and stand with shoulder width stance.\nExecution\n\nSquat down by bending hips back while allowing knees to bend forward, keeping back straight and knees pointed same direction as feet. Descend until thighs are just past parallel to floor. Extend knees and hips until legs are straight. Return and repeat.', + 'https://exrx.glorb.com/api/video/3a149c0a8908503bce86276bcf74e547/10080', + 'https://exrx.net/WeightExercises/Quadriceps/BBSquat' +); + + +INSERT INTO Workouts.Exercises + (name, muscleTarget, forceType, mechanicsType, instructions, videoLink, tutorialLink) +VALUES ( + 'Barbell Deadlift', + 'Glutes', + 'Pull', + 'Compound', + 'Preparation\n\nWith feet flat beneath bar squat down and grasp bar with shoulder width or slightly wider overhand or mixed grip.\n\nExecution\n\nLift bar by extending hips and knees to full extension. Pull shoulders back at top of lift if rounded. Return weights to floor by bending hips back while allowing knees to bend forward, keeping back straight and knees pointed same direction as feet. Repeat.', + 'https://exrx.glorb.com/api/video/508d5717dd3dc74d685a11925aa7afe4/10458', + 'https://exrx.net/WeightExercises/GluteusMaximus/BBDeadlift' +); + + +INSERT INTO Workouts.Exercises + (name, muscleTarget, forceType, mechanicsType, instructions, videoLink, tutorialLink) +VALUES ( + 'Barbell Standing Leg Calf Raise', + 'Calves', + 'Push', + 'Isolation', + 'Preparation\n\nSet barbell on power rack upper chest height with calf block under barbell. Position back of shoulders under barbell with both hands to sides. Position toes and balls of feet on calf block with arches and heels extending off. Lean barbell against rack and raise from supports by extending knees and hips. Support barbell against verticals with both hands to sides.\n\nExecution\n\nRaise heels by extending ankles as high as possible. Lower heels by bending ankles until calves are stretched. Repeat.', + 'https://exrx.glorb.com/api/video/3f3d82424fce04b41e0764c3fdb461b5/9908', + 'https://exrx.net/WeightExercises/Gastrocnemius/BBStandingCalfRaise' +); + + +INSERT INTO Workouts.Exercises + (name, muscleTarget, forceType, mechanicsType, instructions, videoLink, tutorialLink) +VALUES ( + 'Barbell Bent-over Row', + 'Back', + 'Pull', + 'Compound', + 'Preparation\n\nBend knees slightly and bend over bar with back straight. Grasp bar with wide overhand grip.\n\nExecution\n\nPull bar to upper waist. Return until arms are extended and shoulders are stretched downward. Repeat.', + 'https://exrx.glorb.com/api/video/3a149c0a8908503bce86276bcf74e547/10080', + 'https://exrx.net/WeightExercises/Quadriceps/BBSquat' +); diff --git a/db/schema/9000_db_users.sql b/db/schema/9000_db_users.sql index 2fb46e3..53d8647 100644 --- a/db/schema/9000_db_users.sql +++ b/db/schema/9000_db_users.sql @@ -10,4 +10,29 @@ GRANT USAGE ON SCHEMA "stats" TO api; GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA "stats" TO api; GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA stats TO api; +GRANT SELECT, UPDATE, INSERT ON ALL TABLES IN SCHEMA "workouts" TO api; +GRANT USAGE ON SCHEMA "workouts" TO api; +GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA "workouts" TO api; +GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA workouts TO api; + + + +CREATE ROLE hasura LOGIN PASSWORD 'hasura-user'; + +GRANT SELECT, UPDATE, INSERT ON ALL TABLES IN SCHEMA "accounts" TO hasura; +GRANT USAGE ON SCHEMA "accounts" TO hasura; +GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA "accounts" TO hasura; +GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA accounts TO hasura; + +GRANT SELECT, UPDATE, INSERT ON ALL TABLES IN SCHEMA "stats" TO hasura; +GRANT USAGE ON SCHEMA "stats" TO hasura; +GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA "stats" TO hasura; +GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA stats TO hasura; + +GRANT SELECT, UPDATE, INSERT ON ALL TABLES IN SCHEMA "workouts" TO hasura; +GRANT USAGE ON SCHEMA "workouts" TO hasura; +GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA "workouts" TO hasura; +GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA workouts TO hasura; + +GRANT CREATE ON DATABASE bodytrack TO hasura;