decap-cms icon indicating copy to clipboard operation
decap-cms copied to clipboard

Feature: edit data files with a list/array at root level

Open rriemann opened this issue 8 years ago • 23 comments

- Do you want to request a feature or report a bug?

Feature

- What is the current behavior?

it seems to be impossible to create data files with a list at root level:

- nav_object_1
- nav_object_2

The current work-around is to use

navigation:
  - nav_object_1
  - nav_object_2

However, this may require a change of the template that may be part of libraries (jekyll theme gems).

- What is the expected behavior?

I can just create lists also on root level.

rriemann avatar Aug 16 '17 09:08 rriemann

Thanks for opening this - it's definitely something folks have needed.

I think the most straightforward approach would be to accept a boolean root config value on the list widget (this doesn't make much sense for any other widget type). If root is true, the resulting data is a list rather than an object. We would need validation to ensure that no other fields are defined if a "root" field is provided, and we would need to start handling frontmatter and data that are lists and not just objects as we do currently.

This probably conflicts with https://github.com/netlify/netlify-cms/pull/468 as well, where I believe we're outmoding the list widget in favor of a "repeatable" flag for all fields, in which case any "repeatable" field could have a "root" field (but still only one field defined if there's a root).

Would you be up for taking a swing at this?

erquhart avatar Aug 17 '17 15:08 erquhart

Would you be up for taking a swing at this?

Unfortunately I'm super busy with university. :(

rriemann avatar Aug 17 '17 15:08 rriemann

No problem! I'm sure someone will take it on.

erquhart avatar Aug 18 '17 13:08 erquhart

I would be happy to implement this - currently it is not possible to use gatsby-transformer-yaml with netlify since the transformer only supports lists.

Any pointers on where to look in the code to get started? I'm pretty new to netlify.

r1b avatar Sep 22 '17 19:09 r1b

I have solved this on the gatsby side with a custom yaml transformer. I would still like to support it here for maximum flexibility :)

r1b avatar Sep 28 '17 04:09 r1b

Forget the boolean thing I said before, we should apply the field/fields approach from the list widget to the collection config.

fields vs. field

Here's the list widget using fields:

# list field config using `fields`
- name: my-list
  label: My List
  widget: list
  fields: [{ name: title, label: title, widget: string }]

# output:
# list: [{ title: 'a title' }, { title: 'another title' }]

Here's the same widget config using field:

# list field config using `field`
- name: my-list
  label: My List
  widget: list
  field: [{ name: title, label: title, widget: string }]

# output
# list: ['a title', 'another title']

field on collections

Note: this is a proposal, doesn't actually work!

- name: posts
  label: Posts
  file: data/list.json # this only makes sense for file collections
  field: [{ name: name, label: Name, widget: string }]

# output
['item', 'another item']

The UI and certain config options (like allowAdd) from the list widget would be reused for at the collection level for this.

I can't think of any downsides or issues with this approach, personally. Thoughts?

erquhart avatar Feb 05 '19 18:02 erquhart

Are there any updates regarding the proposals mentioned above? This is a deal-breaker for Gatsby users, please see #1282.

kripod avatar May 02 '19 08:05 kripod

@kripod I believe this is a feature the CMS should support, Until then, it is NOT a deal-breaker for Gatsby users when it is still able to map to a structure the CMS does support at the moment.

talves avatar May 02 '19 14:05 talves

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 29 '19 09:10 stale[bot]

This is still happening.

erquhart avatar Nov 04 '19 17:11 erquhart

I've just patched this locally in netlify-cms-core/src/backend.ts functions entryToRaw and entryWithFormat. Around the calls to either format.toFile or format.fromFile I'm just doing a wrap/unwrap with a special key rootArray.

  entryWithFormat(collectionOrEntity: unknown) {
    return (entry: EntryValue): EntryValue => {
      const format = resolveFormat(collectionOrEntity, entry);
      if (entry && entry.raw !== undefined) {
        let data = (format && attempt(format.fromFile.bind(format, entry.raw))) || {};
        let data = Array.isArray(data) ? { rootArray: data } : data; //Added wrapper for root arrays
        if (isError(data)) console.error(data);
        return Object.assign(entry, { data: isError(data) ? {} : data });
      }
      let data = format.fromFile(entry);
      return Array.isArray(data) ? { rootArray: data } : data; //Added wrapper for root arrays
    };
  }

  entryToRaw(collection: Collection, entry: EntryMap): string {
    const format = resolveFormat(collection, entry.toJS());
    const fieldsOrder = this.fieldsOrder(collection, entry);
    let data = entry.get('data').toJS();
    let data = data.rootArray || data; // Unwrap root array (this should probably check the fields.length == 1 too)
    return format && format.toFile(data, fieldsOrder);
  }

Not as clever as the approach mentioned above - but if you think of it as normalising files to always be a map instead of sometimes passing arrays around then it feels a bit less hacky.

bharrisau avatar Feb 20 '20 15:02 bharrisau

No fix on this since 2017? )=

cyonder avatar May 06 '20 20:05 cyonder

Hi @cyonder, we try to get to the most up voted features first. You can see the sorted list here https://github.com/netlify/netlify-cms/issues?q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc. Regardless, we would love a contribution for this.

erezrokah avatar May 07 '20 06:05 erezrokah

I just tried to see if I could get the ball rolling on this issue.

  1. Parse the single entry file to get array of content
  2. Based on that content, generate new "real" entries
  3. Populate the collection with previously generated entries

Doing edits / adding new content would be similar to above but in reversed order.

But the assumptions of content being in separate files made it very complicated straight away but maybe my idea was overly complicated?

Would love to get some help / discussion with main contributors to the project. <3

reimertz avatar May 24 '20 22:05 reimertz

Hi @reimertz, thanks for pushing this forward. What do you think about putting a draft PR for it? We can use it for the discussion.

Another option is our #contributing channel on Slack https://www.netlifycms.org/chat.

erezrokah avatar May 25 '20 06:05 erezrokah

Hi everyone, any update on this ? This seems blocking for Eleventy global data files too, if anyone has a workaround for this I'm interested :sweat_smile:

vnourdin avatar Jan 19 '21 12:01 vnourdin

Hi @vnourdin this would make a great contribution to the CMS if anyone would like to pick it up

erezrokah avatar Jan 19 '21 13:01 erezrokah

Just to said this is also an issue for @nuxt/content with array at the root level of a json. Like here.

ManUtopiK avatar Feb 04 '21 21:02 ManUtopiK

+1 would love to see support for .json files with array at root level to mimic a MongoDB collection

hayden2114 avatar Dec 17 '21 04:12 hayden2114

would love to see support for this

astrawnuts avatar Feb 22 '22 14:02 astrawnuts

Still needed :)

izanagi1995 avatar Mar 26 '22 21:03 izanagi1995

NEEEEED

TheReyzer avatar Oct 13 '22 14:10 TheReyzer

Need it....

CKLFish avatar Feb 24 '23 18:02 CKLFish