bluebird - Bookshelf.js FindOrCreate method -
i have following in bookshelf model
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); }); }
i have controller method calls findorcreate
method so:
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); }); } }
and payload comes in post request is
[{ "name": { "value": "2a", "type": "data" }, "slugline": { "value": "the linten bakery.", "type": "data" }, "description": { "value": "mary hastily prepared 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. 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 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 remains of 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 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 radio @ 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 alice outside 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 , child 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 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 prepare 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 need 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 large explosion , people start running out of 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" }]
the problem having findorcreate
method creating multiple records of same location.
Comments
Post a Comment