Json collection?

Is it possible to make a collection that outputs json like this one? Want to make a calendar events editing a bit more easier.

json
{ 
   "data":[
  {
      "id":"1",
      "start_date":"2019-12-02 00:00:00",
      "end_date":"2019-12-04 00:00:00",
      "text":"dblclick me!",
		  "color":"red",
      "type":"1"
  },
  {
      "id":"2",
      "start_date":"2019-12-09 00:00:00",
      "end_date":"2019-12-11 00:00:00",
      "text":"and me!",
      "type":"2"
  },
  {
      "id":"3",
      "start_date":"2019-12-16 00:00:00",
      "end_date":"2019-12-18 00:00:00",
      "text":"and me too!",
      "type":"3"
  },
  { 
      "id":"4",
      "start_date":"2019-12-02 08:00:00",
      "end_date":"2019-12-02 14:10:00",
      "text":"Type 2 event",
      "type":"2"
  }
   ], 
   "collections": {
  "sections":[
     {"value":"1","label":"Simple"},
     {"value":"2","label":"Complex"},
     {"value":"3","label":"Unknown"}
  ]
   }
}

Hello? Does anyone even care?

Hello, we are still a small community and we contribute and help in our spare time - and it was Christmas.

Your question:

It is possible, but you have to do multiple steps.

Create a collection with your fields.

If you don’t need numeric ids, you can use the default _id field with mongodb id style.

For start_date and end_date, you can use the date field. If you need the part 00:00:00 in your output, you can copy and paste the date field component file into path/to/cockpit/config/tags/field-date.tag and modifiy it to produce your exact date format. This way, the core component will be overwritten.

Use a select field for type.

You can create your own field components, if you want to. I needed a custom date range field in the past and published it for inspiration: https://github.com/raffaelj/cockpit-scripts/blob/master/custom-fields/field-date-range.tag

Custom rest api output

You can create your own api endpoint, that produces the format, you want. Create a file /cockpit/config/api/myCustomEndpoint.php and return your format.

<?php

$data = cockpit('collections')->find('calendar');

$collections = ['sections' => ['...' => '...']];

return compact('data', 'collections');