{"id":18602619,"url":"https://github.com/ponzu-cms/live","last_synced_at":"2025-04-10T19:31:05.209Z","repository":{"id":57589385,"uuid":"148101577","full_name":"ponzu-cms/live","owner":"ponzu-cms","description":"Ponzu CMS package to add support for live queries. Subscribe to / emit from various hooks, i.e. AfterAPICreate, BeforeAdminDelete, etc.","archived":false,"fork":false,"pushed_at":"2018-09-10T05:51:24.000Z","size":5,"stargazers_count":5,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T03:33:46.794Z","etag":null,"topics":["database","golang","hook","ponzu-cms","reactive"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ponzu-cms.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-10T05:03:48.000Z","updated_at":"2020-02-15T11:01:05.000Z","dependencies_parsed_at":"2022-08-29T23:41:44.053Z","dependency_job_id":null,"html_url":"https://github.com/ponzu-cms/live","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ponzu-cms%2Flive","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ponzu-cms%2Flive/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ponzu-cms%2Flive/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ponzu-cms%2Flive/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ponzu-cms","download_url":"https://codeload.github.com/ponzu-cms/live/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248281395,"owners_count":21077423,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","golang","hook","ponzu-cms","reactive"],"created_at":"2024-11-07T02:11:57.392Z","updated_at":"2025-04-10T19:31:04.902Z","avatar_url":"https://github.com/ponzu-cms.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Live Queries\n\nThis package attempts to add a basic version of live queries to Ponzu. By \ninteracting with the database hooks provided with the `item.Hookable` interface, \nthe `live` package (`import \"github.com/ponzu-cms/live\"`) enables users to publish\nand subscribe to the hookable events throughout the content package.\n\nUsage:\n\n```go\npackage content\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/ponzu-cms/live\"\n\t\"github.com/ponzu-cms/ponzu/management/editor\"\n\t\"github.com/ponzu-cms/ponzu/system/item\"\n)\n\n// typical generated ponzu content type\ntype Customer struct {\n\titem.Item\n\n\tName  string `json:\"name\"`\n\tEmail string `json:\"email\"`\n}\n\n// declare a package-level subscriptions container (could be in it's own file \n// within the content package)\nvar subs live.Subscriptions\n\nfunc init() {\n    item.Types[\"Customer\"] = func() interface{} { return new(Customer) }\n\n    // create the new subscriptions container to Subscribe / Emit\n    subs = live.New()\n\n    // create a subscriber to handle AdminUpdate events on a Customer\n    go func() {\n        adminUpdates := subs.Subscribe(\"Customer\", live.AdminUpdate)\n        for event := range adminUpdates {\n            log.Println(\"\u003cg1\u003e UPDATE:\", event.Content().(*Customer))\n        }\n    }()\n\n    // NOTE: any number of subscriptions can be made to the same content type for\n    // any event type.\n\n    // create another subscriber for AdminDelete events on a Customer\n    go func() {\n        adminDeletes := subs.Subscribe(\"Customer\", live.AdminDelete)\n        for event := range adminDeletes {\n            log.Println(\"\u003cg2\u003e DELETE:\", event.Content().(*Customer))\n        }\n    }()\n}\n\n// emit content events from within the item.Hookable methods provided by Ponzu \n// to broadcast a change (hookable constants are defined in `live` for event types)\nfunc (c *Customer) AfterAdminUpdate(res http.ResponseWriter, req *http.Request) error {\n\terr := subs.Emit(req.Context(), \"Customer\", c, live.AdminUpdate)\n\tif err != nil {\n\t\tif liveErr, ok := err.(live.QueryError); ok {\n\t\t\t// handle live error in specific manner\n\t\t\tfmt.Println(liveErr)\n\t\t\treturn nil\n\t\t}\n\n\t\treturn err\n\t}\n\n\treturn nil\n}\n\nfunc (c *Customer) BeforeAdminDelete(res http.ResponseWriter, req *http.Request) error {\n\terr := subs.Emit(req.Context(), \"Customer\", c, live.AdminDelete)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\treturn nil\n}\n\n// MarshalEditor writes a buffer of html to edit a Customer within the CMS\n// and implements editor.Editable\nfunc (c *Customer) MarshalEditor() ([]byte, error) {\n\tview, err := editor.Form(c,\n\t\t// Take note that the first argument to these Input-like functions\n\t\t// is the string version of each Customer field, and must follow\n\t\t// this pattern for auto-decoding and auto-encoding reasons:\n\t\teditor.Field{\n\t\t\tView: editor.Input(\"Name\", c, map[string]string{\n\t\t\t\t\"label\":       \"Name\",\n\t\t\t\t\"type\":        \"text\",\n\t\t\t\t\"placeholder\": \"Enter the Name here\",\n\t\t\t}),\n\t\t},\n\t\teditor.Field{\n\t\t\tView: editor.Input(\"Email\", c, map[string]string{\n\t\t\t\t\"label\":       \"Email\",\n\t\t\t\t\"type\":        \"text\",\n\t\t\t\t\"placeholder\": \"Enter the Email here\",\n\t\t\t}),\n\t\t},\n\t)\n\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"Failed to render Customer editor view: %s\", err.Error())\n\t}\n\n\treturn view, nil\n}\n\n// String defines how a Customer is printed. Update it using more descriptive\n// fields from the Customer struct type\nfunc (c *Customer) String() string {\n\treturn fmt.Sprintf(\"Customer: %s\", c.UUID)\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fponzu-cms%2Flive","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fponzu-cms%2Flive","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fponzu-cms%2Flive/lists"}