Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mysql-relation-query

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mysql-relation-query - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

demoConfig.json

2

package.json
{
"name": "mysql-relation-query",
"description": "module modelling mysql relations",
"version": "0.0.1",
"version": "0.1.0",
"main": "TableRelationManager",

@@ -6,0 +6,0 @@ "keywords": [

@@ -30,12 +30,26 @@ mysql-relation-query

// external column relations
// name of column where key (usually primary key, usually id) of reference table is stored
// referenced table
// column (usually primary key, usually id) that is used as reference
// columns I want given when querying that table
Post.addExternalColumn('user_id', 'users', 'id', ['username', 'id']);
/*
* external column relations
* @param sourceColumn the column in the baseTable where a reference column_value (usually id) is stored
* @param targetTable table that I want referenced by the sourceColumn's value
* @param targetReferenceColumn the column (usually id) that is used as reference value
* @param interestingColumns the columns you'll want returned when querying the foreign table
*/
Post.addExternalColumn('user_id', 'users', 'id', ['id', 'username', 'email']);
// or
Post.addForeignKey(
Post.foreignKey(user_id)
.references({table: users, column: id})
.returns(['id', 'username', 'email'])
);
// or simply generate the whole thing using a json config file
// check demoConfig.json for a demo that would result in the same Relation as the two examples above
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)
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)
/**
* @author TPei
* Skyfillers GmbH
* @author Thomas Peikert
* created: 15/08/14.

@@ -21,4 +22,31 @@ */

this.interestingColumns = interestingColumns;
/**
* sets targetTable and targetReferenceColumn of TableRelation
* @param {Object} tableAndColumn containing table and column to be set
* @returns {TableRelation} updated TableRelation
*/
this.references = function (tableAndColumn) {
this.targetTable = tableAndColumn.table;
this.targetReferenceColumn = tableAndColumn.column;
return this;
}
/**
* sets interestingColumns of TableRelation
* @param fields array of columns
* @returns {TableRelation} updated TableRelation
*/
this.returns = function (fields) {
this.interestingColumns = fields;
return this;
}
}
/*
TableRelation.prototype.foreignKey = function (sourceColumn) {
this.sourceColumn = sourceColumn;
return this;
}*/
module.exports = TableRelation;
/**
* @author TPei
* Skyfillers GmbH
* @author Thomas Peikert
* created: 15/08/14.

@@ -10,3 +11,2 @@ */

* easily manage TableRelations
* @param queryType {string} ['select', 'delete'] ...
* @param tableOfOrigin {string} baseTable

@@ -38,3 +38,3 @@ * @constructor

* adds an internal column
* @param column
* @param column to be returned when querying the table
*/

@@ -50,4 +50,4 @@ this.addInternalColumn = function(column) {

* @param targetTable table that I want referenced by the sourceColumn's value
* @param targetColumn column I want to get when pulling the referenced table
* @param refColumn the column (usually id) that is used as reference value
* @param targetReferenceColumn the column (usually id) that is used as reference value
* @param interestingColumns the columns you'll want returned when querying the foreign table
*/

@@ -59,2 +59,36 @@ this.addExternalColumn = function(sourceColumn, targetTable, targetReferenceColumn, interestingColumns) {

/**
* creates a new TableRelation using the baseTable
* and adds it to the external columns array
* @param object with the following parts:
* - sourceColumn the column in the baseTable where a reference column_value (usually id) is stored
* - targetTable table that I want referenced by the sourceColumn's value
* - targetColumn column I want to get when pulling the referenced table
* - refColumn the column (usually id) that is used as reference value
* @param interestingColumns the columns you'll want returned when querying the foreign table
*/
this.addRelation = function(object, interestingColumns) {
this.externalColumns.push(new TableRelation(self.table, object.sourceColumn, object.targetTable, object.targetReferenceColumn, interestingColumns))
}
/**
* add a TableRelation to externalColumns array
* @param {TableRelation} tableRelation to be added
*/
this.addForeignKey = function (tableRelation) {
this.externalColumns.push(tableRelation);
}
/**
* create and return a tableRelation with sourceColumn
* @param sourceColumn sourceColumn to be set in new TableRelation
* @returns {TableRelation} a new TableRelation with set sourceColumn
*/
this.foreignKey = function (sourceColumn) {
var tableRelation = new TableRelation(self.table);
tableRelation.sourceColumn = sourceColumn;
return tableRelation;
}
/**
* generate querystring from all given columns and relations

@@ -61,0 +95,0 @@ * @param queryType {String} type of query ['select'|'delete'] etc

@@ -16,2 +16,55 @@ /**

describe('TableRelationsManager', function(){
describe('#constructor', function() {
it('should return a TableRelationsManager', function(){
var TableRelationManager = require('../TableRelationManager');
var manager = new TableRelationManager('sourceTable');
assert.equal(true, manager.internalColumns instanceof Array);
assert.equal(true, manager.externalColumns instanceof Array);
});
it('should return a TableRelationsManager having a set table attribute', function(){
var TableRelationManager = require('../TableRelationManager');
var manager = new TableRelationManager('sourceTable');
assert.equal(manager.table, 'sourceTable');
});
});
describe('#addInternalColumn', function() {
it('should add internal columns appropriately', function(){
var TableRelationManager = require('../TableRelationManager');
var manager = new TableRelationManager('sourceTable');
manager.addInternalColumn('name');
assert.equal(manager.internalColumns[manager.internalColumns.length-1], 'sourceTable.name');
});
});
describe('#addForeignKey', function() {
it('should add an external relation accordingly', function(){
var TableRelationManager = require('../TableRelationManager');
var manager = new TableRelationManager('sourceTable');
manager.addForeignKey(
manager.foreignKey('sourceColumn')
);
assert.equal(manager.externalColumns[manager.externalColumns.length-1].sourceTable, 'sourceTable');
assert.equal(manager.externalColumns[manager.externalColumns.length-1].sourceColumn, 'sourceColumn');
});
});
describe('#foreignkey', function(){
it('should return a TableRelation', function(){
var TableRelationManager = require('../TableRelationManager');
var manager = new TableRelationManager('sourceTable');
var tableRelation = manager.foreignKey('sourceColumn');
assert.equal('sourceTable', tableRelation.sourceTable);
assert.equal('sourceColumn', tableRelation.sourceColumn);
assert.equal(undefined, tableRelation.targetReferenceColumn);
});
});
describe('#generateQuery', function(){

@@ -35,4 +88,9 @@ it('should be able to handle internal columns and solve multiple relations', function(){

})
var Configurer = require('../RelationsConfigurer');
var post = new Configurer(require('../demoConfig.json'));
var query = post.generateQuery('select');
assert.equal(query, 'select posts.id, posts.title, posts.text, users.id as usersid, users.username as usersusername, users.email as usersemail from posts join users on (posts.user_id = users.id) ');
});
it('should work without any external relations', function(){

@@ -39,0 +97,0 @@ var TableRelationManager = require('../TableRelationManager');

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc