question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Array field type(s)

See original GitHub issue

All DB platforms we’re planning to support in the near-term (eg. Mongo, pgSQL) support storing an ordered series of values in a single field. In both cases, the values stored can be of (almost) any other type supported by the data layer.

There are two way we could map this of thought on how these values should be typed:

A) Multiple array types with a different one for each underlying type (“TextArray”, “NumberArray”, “FileArray”, … ), eg…

keystone.createList('Post', {
  fields: {
    // .. 
    tags: { type: TextArray },
  },
});

B) A single array type which is supplied an “inner type” when configured, eg…

keystone.createList('Post', {
  fields: {
    // .. 
    tags: { type: Array, ofType: Text },
  },
});

We also need to make some decisions about how these values are accessed via GraphQL. OpenCRUD has some filters defined (field_contains, field_contains_every, field_contains_some) but is missing others (field_not_contains, field_contains_none). Issues #87 and #217 have some relevant discussion.

@JedWatson leans fairly strongly towards building separate array types initially (option “A” above) with the focus on reusing components from the “inner” type where relevant. He’s suspects there are significant (enough) differences for how the interface will want to deal with a single value -vs- an array of values in the admin UI that going “fully generic” from the start won’t work well.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
jesstelfordcommented, May 26, 2019

The current workaround is to create a Relationship to another list:

keystone.createList('Post', {
  fields: {
    // .. 
    tags: { type: Relationship, ref: 'Tag.posts' },
  },
});

keystone.createList('Tag', {
  fields: {
    name: { type: Text },
    posts: { type: Relationship, ref: 'Post.tags' },
  },
});

Then the graphQL query is:

query posts {
  allPosts {
    tags {
      name
    }
  }
}

And you can also look up all posts for a given tag:

query tags {
  tag(where: { id: "..." }) {
    posts {
      id
    }
  }
}

Ideally, any nested ‘Array’ type would support a similar GraphQL API.

1reaction
jesstelfordcommented, May 18, 2020

You should be able to create an intermediate list which contains the sort index:

keystone.createList('Post', {
  fields: {
    tags: { type: Relationship, ref: 'TagToPost', many: true },
  },
});

keystone.createList('Tag', {
  fields: {
    name: { type: Text },
  },
});

keystone.createList('TagToPost', {
  fields: {
    tag: { type: Relationship, ref: 'Tag' },
    post: { type: Relationship, ref: 'Post' },
    sortOrderForPost: { type: Integer },
  }
});

Then you can retrieve an ordered list like so:

query {
  allPosts {
    tags(sortBy: sortOrderForPost_ASC) {
      tag {
        name
      }
    }
  }
}

Note that the .sortOrderForPost field needs to be managed manually by you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Array Field | Documentation - Payload CMS
The Array field type is used when you need to have a set of "repeating" fields. It stores an array of objects containing...
Read more >
Array Data Type - Trifacta Documentation
An array is a list of values grouped into a single value. An array may be of variable length; in one record the...
Read more >
Data type: Array - Micro Focus
The array data type is a compound data type represented by the number 8 in the database dictionary. Arrays store a list of...
Read more >
Documentation: 15: 8.15. Arrays - PostgreSQL
Arrays of any built-in or user-defined base type, enum type, composite type, range type, or domain can be created.
Read more >
Arrays | Elasticsearch Guide [8.5] | Elastic
In Elasticsearch, there is no dedicated array data type. Any field can contain zero or more values by default, however, all values in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found