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

时间:2018-04-13 14:26:42

标签: javascript node.js mongodb mongoose keystonejs

我对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'
});

在更新文件夹中,我尝试使用预加载的数据为数据库设定种子。位置和照片模型都会单独填充,但我无法预先填充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数据库关系的正确方法吗?非常感谢你。

1 个答案:

答案 0 :(得分:1)

我设法通过照片映射解决,并替换为标题找到的实际照片。以下是我可以帮助其他人的方式:

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);
};
相关问题