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.

sortContext controls the global sort, and isn't per relationship

See original GitHub issue

When defining a sortable list of items, and a sortContext, it doesn’t work as expected. When you sort the items, it changes across all relationships, not just the one your viewing. For example:

/**
 * Work Item Model
 * ===============
 */

var WorkItem = new keystone.List('WorkItem', {
    sortable: true,
    sortContext: 'WorkCategory:items',
    map: { name: 'title' },
    autokey: { path: 'slug', from: 'title', unique: true }
});

WorkItem.add({
    title: { type: Types.Text, required: true, index: true, initial: true },
    description: { type: Types.Markdown, initial: true },
    slug: { type: Types.Text }
}, 'Assets', {
    image: { type: Types.CloudinaryImage, note: 'Must be > 2048px on the long edge'},
    fullImage: { type: Types.CloudinaryImage },
    thumbnail: { type: Types.CloudinaryImage, note: 'Must be > 800px square'},
    mediaUrl: { type: Types.Url, label: 'Media URL' },
    mediaInfo: { type: Types.Embedly, from: 'mediaUrl' }
}, 'Categorization', {
    category: { type: Types.Relationship, ref: 'WorkCategory', many: true },
    client: { type: Types.Relationship, ref: 'Client' }
});


/**
 * Work Category Model
 * ===================
 */

var WorkCategory = new keystone.List('WorkCategory', { 
    sortable: true,
    autokey: { path: 'slug', from: 'name', unique: true }
});

WorkCategory.add({
    name: { type: Types.Text, required: true, index: true, initial: true },
    description: { type: Types.Markdown, initial: true },
    slug: { type: Types.Text },
    isVisible: { type: Boolean, initial: true, index: true }
});

WorkCategory.relationship({ path: 'items', ref: 'WorkItem', refPath: 'category'});

Now when you add an item to the WorkItem list, and associate it with two WorkCategories—say one called “Digital” and one called “Print”—and then visit those categories in the admin, you see the items that are related, and they are orderable. However, if I change the order on one (“Digital”) it changes the order on the other (“Print”)! The UI implies that you’re changing the ordering for that category, but in fact you’re changing it globally.

Is this working as intended? The ability to order per-relationship is really what I’m looking for, but it seems that there’s one global ordering and that’s it.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JedWatsoncommented, Apr 7, 2019

This isn’t going to change in v4, will be different in v5 - see keystonejs/keystone-5#1002

0reactions
pr1ntrcommented, Aug 3, 2016

This has been perplexing to me. AFAIK The sorting of relationships systemwide has been broken since at least I have used keystone (end of 2014).

I think this call can be greatly simplified. If you want a list that is globally sorted then thats what the sortContext is for, the main list view of that Model then becomes sortable which I think is the current behavior. However this should not be confused with a relationship.

The biggest issue is that the react-select component that visualizes the many-to-many relationships. It allows no way to sort anything which borders on the insane at this point.

All we need is just to make some kind of sortable list in place of that or at the very least add Drag & Drop to that component.

Mongo has absolutely no concept of a relationship. Mongoose ODM just sticks an array of ObjectId()s where there is a relationship. The only thing that sorts these is insertion order (really). unless your front end api is performing some kind of sort the default will be the insertion order.

@wmertens, so the virtual populate api is a great feature of mongoose because it just goes in, reads the object id and inserts the matching record in its place; perfect.

The main issue here, still, is there is no working UI to do any sorting for the admin user.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Global search displays a page of matching results
Global search displays a page of results that match your search, grouped by table.
Read more >
Add sorting, filtering, and paging - ASP.NET MVC with EF Core
The default sort order is ascending. The first time the Index page is requested, there's no query string.
Read more >
4. Types of Navigation - Designing Web Navigation [Book]
Associative navigation links across levels of a hierarchy, creating semantic relationships between related pieces of content. Contextual navigation and quick ...
Read more >
How Queries Work – GROQ - Sanity.io
If you wanted to sort the movies by year, and then within each year we want them ... asc means “ascending” and desc...
Read more >
What To Do When Your Database Runs Out of Temp Space
When Oracle needs to store data in a global temporary table or build ... There typically is just one sort segment per temporary...
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