Skip to content

Commit

Permalink
feat(playground): add example of entity view
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasnobile committed Jun 11, 2024
1 parent 05d4512 commit 04c6d25
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/playground/admin/app/pages/grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ const CustomGridRow = Component(() => (
</div>
<div className="inline-flex gap-2">
<MessageSquareIcon className="w-4 h-4" />
<HasMany field="comments" listComponent={({ accessor }) => accessor.length} />
<Field field={'details.commentsCount'} />
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"formatVersion": 5,
"modifications": [
{
"modification": "createView",
"entity": {
"eventLog": {
"enabled": true
},
"name": "GridArticleDetail",
"primary": "id",
"primaryColumn": "id",
"tableName": "grid_article_detail",
"fields": {
"id": {
"name": "id",
"columnName": "id",
"columnType": "uuid",
"nullable": false,
"type": "Uuid"
},
"commentsCount": {
"name": "commentsCount",
"columnName": "comments_count",
"columnType": "integer",
"nullable": false,
"type": "Integer"
},
"article": {
"type": "OneHasOne",
"name": "article",
"target": "GridArticle",
"joiningColumn": {
"columnName": "article_id",
"onDelete": "restrict"
},
"nullable": false,
"inversedBy": "details"
}
},
"unique": [],
"indexes": [],
"view": {
"sql": "\n\tSELECT\n\t\tarticle.id AS id,\n\t\tarticle.id AS article_id,\n\t\t(SELECT COUNT(*)\n\t\t FROM \"grid_article_comment\" comment\n\t\t WHERE comment.article_id = article.id) AS comments_count\n\tFROM \"grid_article\" article;\n"
}
}
},
{
"modification": "createRelationInverseSide",
"entityName": "GridArticle",
"relation": {
"type": "OneHasOne",
"name": "details",
"target": "GridArticleDetail",
"ownedBy": "article",
"nullable": true
}
},
{
"modification": "patchAclSchema",
"patch": [
{
"op": "add",
"path": "/roles/admin/entities/GridArticleDetail",
"value": {
"predicates": {},
"operations": {
"read": {
"id": true,
"article": true,
"commentsCount": true
},
"create": {
"id": true,
"article": true,
"commentsCount": true
},
"update": {
"id": true,
"article": true,
"commentsCount": true
},
"delete": true,
"customPrimary": true
}
}
},
{
"op": "add",
"path": "/roles/admin/entities/GridArticle/operations/read/details",
"value": true
},
{
"op": "add",
"path": "/roles/admin/entities/GridArticle/operations/create/details",
"value": true
},
{
"op": "add",
"path": "/roles/admin/entities/GridArticle/operations/update/details",
"value": true
}
]
}
]
}
15 changes: 15 additions & 0 deletions packages/playground/api/model/Grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class GridArticle {
tags = c.manyHasMany(GridTag)
views = c.intColumn()
comments = c.oneHasMany(GridArticleComment, 'article')
details = c.oneHasOneInverse(GridArticleDetail, 'article')
}

export class GridTag {
Expand All @@ -37,3 +38,17 @@ export class GridArticleComment {
content = c.stringColumn()
createdAt = c.dateTimeColumn()
}

@c.View(`
SELECT
article.id AS id,
article.id AS article_id,
(SELECT COUNT(*)
FROM "grid_article_comment" comment
WHERE comment.article_id = article.id) AS comments_count
FROM "grid_article" article;
`)
export class GridArticleDetail {
article = c.oneHasOne(GridArticle, 'details').notNull()
commentsCount = c.intColumn().notNull()
}

0 comments on commit 04c6d25

Please sign in to comment.