Reputation: 7196
I have this Knex
update:
NoteTag.createNoteTag = async (noteId, tagId, db) => (
db.sequelize.knex('note_tags')
.returning([
'id',
'note_id',
'tag_id',
])
.insert({
note_id: noteId,
tag_id: tagId,
})
);
I'd like to be able to pass tagId
as an array so that multiple tags could be added to a note.
Can I edit the function to do this and if so, how?
Upvotes: 1
Views: 950
Reputation: 35503
Knex's insert supports an array of object as well, so you can iterate over passed tagIds and map them to the insert object.
NoteTag.createNoteTag = async (noteId, tagIds, db) =>
db.sequelize
.knex('note_tags')
.returning(['id', 'note_id', 'tag_id'])
.insert(tagIds.map(tagId => ({ node_id: nodeId, tag_id: tagId })));
Upvotes: 1