mysql-relation-query
###resolves mysql relations
create a TableRelationManager, add internal and external columns and have it generate mysql queries, complete with joins and everything
###Imagine the following setup
Table posts
- id
- title
- text
- user_id (foreign key (or not, doesn't matter if it is an actual foreign) targeting users.id
Table users
###Then you'd create a relation like this
var TableRelationManager = require('mysql-relation-query');
var Post = new TableRelationManager('posts')
Post.addInternalColumn('id');
Post.addInternalColumn('title');
Post.addInternalColumn('text');
Post.addExternalColumn('user_id', 'users', 'id', ['id', 'username', 'email']);
Post.addForeignKey(
Post.foreignKey(user_id)
.references({table: users, column: id})
.returns(['id', 'username', 'email'])
);
var Configurer = require('../RelationsConfigurer');
var Post = new Configurer(require('../demoConfig.json'));
console.log(Post.generateQuery('select'));
would generate the following sql statement
select posts.id, posts.title, posts.text, users.username as usersusername, users.id as usersid from posts join users on (posts.user_id = users.id)