Adding element name to relationship field in keystone.js file

Does anyone have a good example of adding a name to a relationship field in keystonejs? Right now, it just saves the identifier, so if I need to display this field name in a jade template, I need to also query this relationship model. Ideally, something like this:

var keystone = require('keystone'), Types = keystone.Field.Types; /** * Titles Model * ============= */ var Title = new keystone.List('Title'); Title.add({ name: { type: String, required: true, initial: true }, room: { type: Types.Relationship, initial: true, required: true, ref: 'Screening', addNew: false }, businessUnit: { type: Types.Relationship, initial: true, required: true, ref: 'BusinessUnit', addNew: false } }); Title.defaultSort = '-createdAt'; Title.defaultColumns = 'name, room'; Title.register(); 

It would be saved like this:

 title = { name: 'name', room: 3141234123442, businessUnit: { name: 'business name', _id: 123412341234 } } 

If there is no example, if someone could just guide me with the best practice when creating a field type of user relationships to store the value and identifier from the relationship selection menu, I could probably figure it out. This site will have 1000 documents in each collection, so I need to focus on productivity and best practices now.

+5
source share
1 answer

You cannot make it persist with Keystone, but you can make it pass the object according to your jade pattern.

You just need to “fill out” the relationship field according to the mongoose / mongodb functionality.

** So your businessUnit model might look like this:

 var keystone = require('keystone'); var Types = keystone.Field.Types; var BusinessUnit = new keystone.List('BusinessUnit', { autokey: { from: 'name', path: 'key', unique: true }, plural: 'BusinessUnits' }); BusinessUnit.add({ name: { type: String, required: true }, }); BusinessUnit.relationship({ ref: 'Title', path: 'title', refPath: 'businessUnit' }); BusinessUnit.register(); 

Your header model as above

 var keystone = require('keystone'), Types = keystone.Field.Types; var Title = new keystone.List('Title'); Title.add({ name: { type: String, required: true, initial: true }, room: { type: Types.Relationship, initial: true, required: true, ref: 'Screening', addNew: false }, businessUnit: { type: Types.Relationship, initial: true, required: true, ref: 'BusinessUnit', addNew: false } }); Title.defaultSort = '-createdAt'; Title.defaultColumns = 'name, room'; Title.register(); 

An important part of your controller might look like this:

 //Get all Titles with their nested businessUnits. view.on('init', function(next) { keystone.list('Title').model.find() .populate('businessUnit') .exec(function(err, results) { if(err) { console.log(err); return next(err); } else { locals.data.titlesWithNestedBusinessUnits = results; next(err); } }); }); 

As a side note, this will only work one level in accordance with the .populate capabilities. If you want to go through some deep (rather ineffective), you can use the deep filling of the mongoose . https://github.com/buunguyen/mongoose-deep-populate .

+3
source

Source: https://habr.com/ru/post/1200457/


All Articles