0 Replies - 612 Views - Last Post: 25 September 2013 - 01:07 AM Rate Topic: -----

#1 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 4191
  • View blog
  • Posts: 13,241
  • Joined: 08-June 10

DB Insert Cascade – Advice wanted

Posted 25 September 2013 - 01:07 AM


it’s me again. I’m having in my DB (MongoDB) a cascade of inserts to do, but I’m unsure how to tacle it.

first, there are two or three tables involved, depending how far I want to push the cascade.
  • an Ability table listing all possibly Abilities [table #1]
  • a Character table having the Character’s data (I only need the PK from that) [table #2]
  • a (link) table where the concrete Ability values (and some more non-fixed data) per Character are saved [table #3]

the scenario is as follows (note, for an RPG):
  • insert a new Character into the DB (think of user data)
  • create a data table entry (referencing the Character’s ID of the other table) that assigns some default values (Abilities and XP)
  • some of the default values (Abilities) are stored in another table

next, the table definition (only the relevant parts)

now the problem part: all DB queries are async*. so if I would just operate on the callback of each operation (1 - insert Char, 2 - fetch ability category #1, 3 - fetch ability category #2, ... , 6 - insert abilities, XP and Char ID (combined) into the table.) I’d land in callback hell.

possible solutions I thought of:
  • skip abilities, just insert Char ID and XP => only 2 DB operations
  • use Promises, but no idea how that should work with this problem (in Mongoose (v3.6), all DB fetch queries are Promises, but I need an aggregated result, not a sequential one)
  • use the async library

so, what path should I go?

* - in pseudo-code:
Schema.operation(<options>, function(error, result) {
	// handle error, if any

	// do something with result(s)

Is This A Good Question/Topic? 0
  • +

Page 1 of 1