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

#1 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,255
  • Joined: 08-June 10

DB Insert Cascade Ė Advice wanted

Posted 25 September 2013 - 01:07 AM

Hi,

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)
Spoiler

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