Bookshelf.js FindOrCreate方法

时间:2016-02-05 17:48:15

标签: bluebird bookshelf.js

我在书架模型中有以下内容

    create (data, options = {}) {
        return this.forge(data)
            .save(null, options);
    },

    findOne (data, options = {}) {
        return this.forge(data).fetch(options);
    },

    findOrCreate (data, options = {}) {
        let createOpts = _.clone(data);
        let findOpts = _.pick(data, 'name', 'id');

        return this.findOne(findOpts, options)
            .then(model => {
                return model ? model : this.create(createOpts, options);
            });
    }

我有一个控制器,其方法可以调用findOrCreate方法,如下所示:

function insertRows(ScenesCollection, Location) {
    return config => {
        const payload = config.payload;
        const projectId = config.project.get('id');
        const userId = config.userId;
        let inserts = filterImportPayload(payload);
        let inserted = [];

        _.forEach(inserts, obj => {
            let opts = extractOptions(obj, projectId);
            let sceneOpts = _.omit(opts, 'location');

            _.extend(sceneOpts, {
                create_user: config.userId,
                update_user: config.userId
            });


            if (obj.location) {
                const locOpts = _.pick({
                    id: obj.location.value.id,
                    name: _.isObject(obj.location.value) ? undefined : obj.location.value,
                    project_id: projectId,
                    create_user: userId,
                    update_user: userId
                }, _.identity);

                inserted.push(Location.findOrCreate(locOpts).then(location => {
                    sceneOpts.location_id = location.get('id');
                }).thenReturn(sceneOpts));
            } else {
                inserted.push(sceneOpts);
            }
        });

        return Bluebird.all(inserted).then(inserts => {
            config.inserted = ScenesCollection.forge(inserts).invokeThen('save');
            return Bluebird.props(config);
        });
    }
}

来自POST请求的有效负载是

[{
    "name": {
        "value": "2a",
        "type": "data"
    },
    "slugline": {
        "value": "The Linten Bakery.",
        "type": "data"
    },
    "description": {
        "value": "Mary is a hastily prepared but surprisingly breakfast.   :)",
        "type": "data"
    },
    "story_day": {
        "value": 2,
        "type": "data"
    },
    "page_count": {
        "value": 1.5,
        "type": "data"
    },
    "location": {
        "type": "matched",
        "value": {
            "id": 149
        }
    },
    "day_night": {
        "type": "matched",
        "value": {
            "id": 10
        }
    },
    "int_ext": {
        "type": "matched",
        "value": {
            "id": 26
        }
    },
    "extras_count": {
        "value": 3,
        "type": "data"
    },
    "type": "insert"
}, {
    "name": {
        "value": "PU 11a",
        "type": "data"
    },
    "slugline": {
        "value": "Office.  Not good.  But worth it.asd",
        "type": "data"
    },
    "description": {
        "value": "asd",
        "type": "data"
    },
    "story_day": {
        "value": 1,
        "type": "data"
    },
    "page_count": {
        "value": 1,
        "type": "data"
    },
    "location": {
        "type": "matched",
        "value": {
            "id": 149
        }
    },
    "day_night": {
        "type": "matched",
        "value": {
            "id": 10
        }
    },
    "int_ext": {
        "value": null,
        "type": "ignore"
    },
    "extras_count": {
        "value": 16,
        "type": "data"
    },
    "type": "insert"
}, {
    "name": {
        "value": "PU 1",
        "type": "data"
    },
    "slugline": {
        "value": "Hallwayadqwe",
        "type": "data"
    },
    "description": {
        "value": "Mary goes to the kitchennettie.ads",
        "type": "data"
    },
    "story_day": {
        "value": 1,
        "type": "data"
    },
    "page_count": {
        "value": 1.75,
        "type": "data"
    },
    "location": {
        "type": "matched",
        "value": {
            "id": 149
        }
    },
    "day_night": {
        "type": "matched",
        "value": {
            "id": 10
        }
    },
    "int_ext": {
        "type": "matched",
        "value": {
            "id": 26
        }
    },
    "extras_count": {
        "value": 1,
        "type": "data"
    },
    "type": "insert"
}, {
    "name": {
        "value": "PU 11b",
        "type": "data"
    },
    "slugline": {
        "value": "Officeqe",
        "type": "data"
    },
    "description": {
        "value": "Extras enter office, causing commotionads",
        "type": "data"
    },
    "story_day": {
        "value": 1,
        "type": "data"
    },
    "page_count": {
        "value": 0.5,
        "type": "data"
    },
    "location": {
        "value": "The Cinema",
        "type": "insert"
    },
    "day_night": {
        "type": "matched",
        "value": {
            "id": 10
        }
    },
    "int_ext": {
        "type": "matched",
        "value": {
            "id": 26
        }
    },
    "extras_count": {
        "value": 15,
        "type": "data"
    },
    "type": "insert"
}, {
    "name": {
        "value": "443",
        "type": "data"
    },
    "slugline": {
        "value": "Office",
        "type": "data"
    },
    "description": {
        "value": "Wide of Mary standing in what remains of the officeasd",
        "type": "data"
    },
    "story_day": {
        "value": "",
        "type": "ignore"
    },
    "page_count": {
        "value": 1,
        "type": "data"
    },
    "location": {
        "value": "Milwaukee General Hospital",
        "type": "insert"
    },
    "day_night": {
        "type": "matched",
        "value": {
            "id": 10
        }
    },
    "int_ext": {
        "type": "matched",
        "value": {
            "id": 26
        }
    },
    "extras_count": {
        "value": 0,
        "type": "ignore"
    },
    "type": "insert"
}, {
    "name": {
        "value": "PU 6",
        "type": "data"
    },
    "slugline": {
        "value": "Office",
        "type": "data"
    },
    "description": {
        "value": "Mary walks up to office, out of breath. asd",
        "type": "data"
    },
    "story_day": {
        "value": 1,
        "type": "data"
    },
    "page_count": {
        "value": 0.5,
        "type": "data"
    },
    "location": {
        "value": "My Room",
        "type": "insert"
    },
    "day_night": {
        "type": "matched",
        "value": {
            "id": 10
        }
    },
    "int_ext": {
        "type": "matched",
        "value": {
            "id": 26
        }
    },
    "extras_count": {
        "value": "",
        "type": "ignore"
    },
    "type": "insert"
}, {
    "name": {
        "value": "PU 9",
        "type": "data"
    },
    "slugline": {
        "value": "Office/radio",
        "type": "data"
    },
    "description": {
        "value": "Mary enjoying listening to her radio at a reasonable volume.ads",
        "type": "data"
    },
    "story_day": {
        "value": 1,
        "type": "data"
    },
    "page_count": {
        "value": 0.5,
        "type": "data"
    },
    "location": {
        "value": "My Room",
        "type": "insert"
    },
    "day_night": {
        "type": "matched",
        "value": {
            "id": 10
        }
    },
    "int_ext": {
        "type": "matched",
        "value": {
            "id": 26
        }
    },
    "type": "insert"
}, {
    "name": {
        "value": "PU 8b",
        "type": "data"
    },
    "slugline": {
        "value": "Office",
        "type": "data"
    },
    "description": {
        "value": "Mary converses with Alice outside her officead",
        "type": "data"
    },
    "story_day": {
        "value": 1,
        "type": "data"
    },
    "page_count": {
        "value": 2.5,
        "type": "data"
    },
    "location": {
        "value": "The Cinema",
        "type": "insert"
    },
    "day_night": {
        "type": "matched",
        "value": {
            "id": 10
        }
    },
    "int_ext": {
        "type": "matched",
        "value": {
            "id": 26
        }
    },
    "type": "insert"
}, {
    "name": {
        "value": "PU 5a",
        "type": "data"
    },
    "slugline": {
        "value": "Street",
        "type": "data"
    },
    "description": {
        "value": "Wide of mary and child with ballasd",
        "type": "data"
    },
    "story_day": {
        "value": 1,
        "type": "data"
    },
    "page_count": {
        "value": 1,
        "type": "data"
    },
    "location": {
        "value": "My Room",
        "type": "insert"
    },
    "day_night": {
        "type": "matched",
        "value": {
            "id": 11
        }
    },
    "int_ext": {
        "type": "matched",
        "value": {
            "id": 26
        }
    },
    "extras_count": {
        "value": 1,
        "type": "data"
    },
    "type": "insert"
}, {
    "name": {
        "value": "3a",
        "type": "data"
    },
    "slugline": {
        "value": "Outside Door",
        "type": "data"
    },
    "description": {
        "value": "Mary exits the house",
        "type": "data"
    },
    "story_day": {
        "value": 1,
        "type": "data"
    },
    "page_count": {
        "value": 0.5,
        "type": "data"
    },
    "location": {
        "type": "matched",
        "value": {
            "id": 491
        }
    },
    "day_night": {
        "type": "matched",
        "value": {
            "id": 10
        }
    },
    "int_ext": {
        "type": "matched",
        "value": {
            "id": 26
        }
    },
    "extras_count": {
        "value": 0,
        "type": "ignore"
    },
    "type": "insert"
}, {
    "name": {
        "value": "2b",
        "type": "data"
    },
    "slugline": {
        "value": "Mary's Kitchen",
        "type": "data"
    },
    "description": {
        "value": "Mary begins to prepare a second breakfast.",
        "type": "data"
    },
    "story_day": {
        "value": 1,
        "type": "data"
    },
    "page_count": {
        "value": 1.125,
        "type": "data"
    },
    "day_night": {
        "type": "matched",
        "value": {
            "id": 10
        }
    },
    "int_ext": {
        "type": "matched",
        "value": {
            "id": 27
        }
    },
    "extras_count": {
        "value": 1,
        "type": "data"
    },
    "type": "insert"
}, {
    "name": {
        "value": "83",
        "type": "data"
    },
    "slugline": {
        "value": "Office",
        "type": "data"
    },
    "description": {
        "value": "Carsten enters Mary's office, informs her she will need to relocate downstairs.",
        "type": "data"
    },
    "story_day": {
        "value": 1,
        "type": "data"
    },
    "page_count": {
        "value": 0.5,
        "type": "data"
    },
    "location": {
        "value": "My Room",
        "type": "insert"
    },
    "day_night": {
        "type": "matched",
        "value": {
            "id": 10
        }
    },
    "int_ext": {
        "type": "matched",
        "value": {
            "id": 26
        }
    },
    "extras_count": {
        "value": 0,
        "type": "ignore"
    },
    "type": "insert"
}, {
    "name": {
        "value": "333",
        "type": "data"
    },
    "slugline": {
        "value": "COLLISIUM WEST",
        "type": "data"
    },
    "description": {
        "value": "There is a large explosion and then people start running out of the building",
        "type": "data"
    },
    "story_day": {
        "value": 10,
        "type": "data"
    },
    "page_count": {
        "value": 0,
        "type": "ignore"
    },
    "location": {
        "value": "Milwaukee General Hospital",
        "type": "insert"
    },
    "day_night": {
        "value": null,
        "type": "ignore"
    },
    "int_ext": {
        "type": "matched",
        "value": {
            "id": 27
        }
    },
    "extras_count": {
        "value": 12,
        "type": "data"
    },
    "type": "insert"
}, {
    "name": {
        "value": "43",
        "type": "data"
    },
    "slugline": {
        "value": "I love THAT",
        "type": "data"
    },
    "description": {
        "value": "bbg",
        "type": "data"
    },
    "story_day": {
        "value": 12,
        "type": "data"
    },
    "page_count": {
        "value": 0,
        "type": "ignore"
    },
    "location": {
        "value": "Milwaukee General Hospital",
        "type": "insert"
    },
    "day_night": {
        "type": "matched",
        "value": {
            "id": 11
        }
    },
    "int_ext": {
        "type": "matched",
        "value": {
            "id": 27
        }
    },
    "extras_count": {
        "value": 10,
        "type": "data"
    },
    "type": "insert"
}]

我遇到的问题是findOrCreate方法正在创建同一位置的多条记录。

0 个答案:

没有答案