Keystone JS - 更新/播种/有关系

Keystone JS - Update / seeding / with Relationship

我对 KeystoneJS 还很陌生,很难通过种子/更新预填充数据库。我对独立属性没有问题,但对有关系的属性感到困惑。

例如,我有一个包含照片的位置模型。

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

Location.add({
  name: {
    type: Types.Text,
    required: true,
    initial: true
  },
  photos: {
    type: Types.Relationship,
    ref: 'Photo',
    many: true
  }
}

照片模特是这样的:

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

Photo.add({
    title: {
        type: Types.Text,
        initial: true,
        index: true
    },
    image: {
        type: Types.CloudinaryImage,
        required: true,
        initial: false
    }
});    

Photo.relationship({
    ref: 'Location',
    path: 'photos',
    refPath: 'photos'
});

在更新文件夹中,我正在尝试使用预加载的数据为数据库播种。 Location 和 Photo 模型都是单独填充的,但我无法在 Admin UI 中预先填充两者之间的关系,并且缺乏关于如何解决的知识。我做了很多研究,尝试了不同的方法,例如使用 __ref_ids 但无法使其工作。我也无法在 KeystoneJS 文档中找到答案。也许我确实缺少一些明显的东西。

exports.create = {
    Location: [
        {
            name: 'London',
            photos: [
                // <-- how to do it here? 
            ]
        },
        {
            name: 'New York',
            photos: [
                // <-- how to do it here? 
            ]
        }
    ]
};

有人知道预填充 KeystoneJs 数据库关系的正确方法吗?非常感谢。

我设法通过映射照片并替换为按标题找到的实际照片来解决。如果它可以帮助其他人,我是这样做的:

exports = module.exports = function (next) {
   Promise.all([
        {
            name: 'London',
            photos: ['london_1', 'london_2']
        },
        {
            name: 'New York',
            photos: ['new_york_1', 'new_york_2']
       }
    ].map(function (location) {
        var _photos = location.photos || [];
        location.photos = [];

        return Promise.all([
            _photos.map(function (title) {
                return Photo.model.findOne({ title: title })
                .then(function (photo) {
                     location.photos.push(photo);
                });
            })
        ])
        .then(function () {
            new Location.model(location).save();
        });
    }))
    .then(function () {
        next();
    })
    .catch(next);
};