Skip to content

Instantly share code, notes, and snippets.

@paf31
Last active Jun 18, 2020
Embed
What would you like to do?
-- Example 1: Hoisting out frequently repeated constants
local XHasuraUserId = "X-Hasura-User-Id";
-- Example 2: More hoisting out of constants, but this time
-- complex objects.
local authorFilter =
{
"author_id": {
"_eq": XHasuraUserId
}
};
local reviewerFilter =
{
"reviewers": {
"reviewer_id": {
"_eq": XHasuraUserId
}
}
};
-- Example 3: Reusing constants to give more semantic meaning
-- to the data - author and reviewer column access is not random,
-- it's everything minus the one column they are restricted from
-- seeing, so let's make that explicit.
local allColumns =
[
"author_id",
"editor_rating",
"id",
"is_published",
"is_reviewed",
"review_comment",
"title"
];
local allColumnsExceptEditorRating =
std.setDiff(allColumns,
[
"editor_rating"
]);
-- Example 4: reducing lots of duplication by extracting
-- functions for object and array relationships.
local objectRel(name, foreignKey) =
{
"name": name,
"using": {
"foreign_key_constraint_on": foreignKey
}
};
local arrayRel(schema, name, column) =
{
"name": name,
"using": {
"foreign_key_constraint_on": {
"column": column,
"table": {
"schema": schema,
"name": name
}
}
}
};
{
"version": 2,
"tables": [
{
"table": {
"schema": "public",
"name": "articles"
},
"object_relationships": [
objectRel("user", "author_id")
],
"array_relationships": [
arrayRel("public", "reviewers", "article_id")
],
"insert_permissions": [
{
"role": "author",
"permission": {
"check": {},
"set": {
"author_id": XHasuraUserId
},
"columns": [
"title"
]
}
}
],
"select_permissions": [
{
"role": "author",
"permission": {
"columns": allColumnsExceptEditorRating,
"filter": authorFilter,
"allow_aggregations": true
}
},
{
"role": "editor",
"permission": {
"columns": allColumns,
"filter": {}
}
},
{
"role": "reviewer",
"permission": {
"columns": allColumnsExceptEditorRating,
"filter": reviewerFilter
}
}
],
"update_permissions": [
{
"role": "editor",
"permission": {
"columns": [
"editor_rating",
"is_published",
"is_reviewed",
"title"
],
"filter": {},
"check": null
}
},
{
"role": "reviewer",
"permission": {
"columns": [
"is_reviewed",
"review_comment",
"title"
],
"filter": reviewerFilter,
"check": null
}
}
]
},
{
"table": {
"schema": "public",
"name": "editors"
},
"object_relationships": [
objectRel("user", "editor_id")
]
},
{
"table": {
"schema": "public",
"name": "reviewers"
},
"object_relationships": [
objectRel("article", "article_id"),
objectRel("user", "reviewer_id")
]
},
{
"table": {
"schema": "public",
"name": "users"
},
"array_relationships": [
arrayRel("public", "articles", "author_id"),
arrayRel("public", "editors", "editor_id"),
arrayRel("public", "reviewers", "reviewer_id")
]
}
]
}
{
"tables": [
{
"array_relationships": [
{
"name": "reviewers",
"using": {
"foreign_key_constraint_on": {
"column": "article_id",
"table": {
"name": "reviewers",
"schema": "public"
}
}
}
}
],
"insert_permissions": [
{
"permission": {
"check": { },
"columns": [
"title"
],
"set": {
"author_id": "X-Hasura-User-Id"
}
},
"role": "author"
}
],
"object_relationships": [
{
"name": "user",
"using": {
"foreign_key_constraint_on": "author_id"
}
}
],
"select_permissions": [
{
"permission": {
"allow_aggregations": true,
"columns": [
"author_id",
"id",
"is_published",
"is_reviewed",
"review_comment",
"title"
],
"filter": {
"author_id": {
"_eq": "X-Hasura-User-Id"
}
}
},
"role": "author"
},
{
"permission": {
"columns": [
"author_id",
"editor_rating",
"id",
"is_published",
"is_reviewed",
"review_comment",
"title"
],
"filter": { }
},
"role": "editor"
},
{
"permission": {
"columns": [
"author_id",
"id",
"is_published",
"is_reviewed",
"review_comment",
"title"
],
"filter": {
"reviewers": {
"reviewer_id": {
"_eq": "X-Hasura-User-Id"
}
}
}
},
"role": "reviewer"
}
],
"table": {
"name": "articles",
"schema": "public"
},
"update_permissions": [
{
"permission": {
"check": null,
"columns": [
"editor_rating",
"is_published",
"is_reviewed",
"title"
],
"filter": { }
},
"role": "editor"
},
{
"permission": {
"check": null,
"columns": [
"is_reviewed",
"review_comment",
"title"
],
"filter": {
"reviewers": {
"reviewer_id": {
"_eq": "X-Hasura-User-Id"
}
}
}
},
"role": "reviewer"
}
]
},
{
"object_relationships": [
{
"name": "user",
"using": {
"foreign_key_constraint_on": "editor_id"
}
}
],
"table": {
"name": "editors",
"schema": "public"
}
},
{
"object_relationships": [
{
"name": "article",
"using": {
"foreign_key_constraint_on": "article_id"
}
},
{
"name": "user",
"using": {
"foreign_key_constraint_on": "reviewer_id"
}
}
],
"table": {
"name": "reviewers",
"schema": "public"
}
},
{
"array_relationships": [
{
"name": "articles",
"using": {
"foreign_key_constraint_on": {
"column": "author_id",
"table": {
"name": "articles",
"schema": "public"
}
}
}
},
{
"name": "editors",
"using": {
"foreign_key_constraint_on": {
"column": "editor_id",
"table": {
"name": "editors",
"schema": "public"
}
}
}
},
{
"name": "reviewers",
"using": {
"foreign_key_constraint_on": {
"column": "reviewer_id",
"table": {
"name": "reviewers",
"schema": "public"
}
}
}
}
],
"table": {
"name": "users",
"schema": "public"
}
}
],
"version": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment