简体   繁体   English

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

[英]Keystone JS - Update / seeding / with Relationship

I'm fairly new to KeystoneJS and struggle with pre-populating a database through seeds / updates. 我对KeystoneJS相当陌生,并努力通过种子/更新来预填充数据库。 I have no problem with independent properties but struggle with properties with relationships. 我对独立属性没有问题,但是对具有关系的属性却很挣扎。

I have for example a Location Model that includes photos. 例如,我有一个包含照片的位置模型。

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
  }
}

and the Photo Model is as such: 照片模型是这样的:

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

Within the update folder I'm trying to seed the database with pre-loaded data. 在更新文件夹中,我尝试使用预加载的数据为数据库播种。 Both Location and Photo models get populated individually but I am failing to pre-populate the relationship within both within the Admin UI and lacks knowledge on how to solve. 位置模型和照片模型都是分别填充的,但是我无法在Admin UI中预先填充两者之间的关系,并且缺乏如何求解的知识。 I did quite some research, tried different things such as using __ref and _ids but couldn't make it work. 我做了相当多的研究,尝试了诸如使用__ref_ids不同方法,但是无法使其正常工作。 I could not find the answer within the KeystoneJS documentation either. 我也无法在KeystoneJS文档中找到答案。 Maybe there is something obvious that I'm actually missing. 也许我确实很想念某些东西。

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

Would anyone know the right way to pre-populate KeystoneJs database relationships? 有人知道预填充KeystoneJs数据库关系的正确方法吗? Thank you very much. 非常感谢你。

I managed to solve by mapping through the photos and replace by the actual photo found by title. 我设法通过映射照片来解决,并替换为按标题找到的实际照片。 Here is how I did in case it can help someone else: 如果可以帮助其他人,请按以下步骤操作:

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);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM