Saturday, March 14, 2015

mysql-js session.find composite key

[Initial stub post, clean up later]


Today while playing with mysql-js, Jeremy and I had a table that had a composite key (two columns for 1 key).  We tried a few ways to pass in the values to session.find, yet none of them worked.

After reviewing the code for mysql-js on Github, we determined that the two keys needed to be passed in via JSON format.

Find Command:
session.find(<tableObject>, <key[s]>, <callback method>);

Example with single column key:
session.find(Employee, '110183', onFind);

Example with Composite key:
To find employee 110183 who is the manager for the department d003 the following code works:

session.find(deptMan.DepartmentManager, {dept_no:'d003',emp_no:'110183'}, onFind);

The key is formatting the keys like this:  {dept_no:'d003',emp_no:'110183'}

No comments:

Post a Comment