sqlquerybuilder
Advanced tools
Comparing version 0.0.9 to 0.0.10
@@ -10,3 +10,5 @@ var uuid = require('node-uuid'), pluralizer = require('pluralizer'); | ||
exports.init = function(opt) { | ||
options = opt || {}; | ||
options = { | ||
entityToTableMap: opt.entityToTableMap || {} | ||
}; | ||
}; | ||
@@ -36,7 +38,3 @@ | ||
function shouldMakeJoin(prop) { | ||
if (prop.toLowerCase().indexOf('count(') !== -1) { | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
return (prop.toLowerCase().indexOf('count(') === -1); | ||
} | ||
@@ -532,5 +530,11 @@ | ||
if(table.length > 2 && table[table.length -1].toLowerCase() !== 's'){ | ||
table = table.pluralize(); | ||
if(options.entityToTableMap[table]){ | ||
table = options.entityToTableMap[table]; | ||
}else{ | ||
table = table.pluralize(); | ||
} | ||
} | ||
orderByInput = table + '.' + parts[1]; | ||
}else{ | ||
orderByInput = _sqlObject.fromAlias + "." + orderByInput; | ||
} | ||
@@ -537,0 +541,0 @@ |
{ | ||
"name": "sqlquerybuilder", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"description": "Highly opinionated Sql Server Query Writer, mostly for internal use.", | ||
@@ -5,0 +5,0 @@ "main": "./lib/sqlQueryBuilder", |
@@ -17,5 +17,5 @@ /** | ||
.build()) | ||
.should.equal("SELECT DISTINCT Foo FROM Tickets ORDER BY Foo DESC "); | ||
.should.equal("SELECT DISTINCT Foo FROM Tickets ORDER BY Tickets.Foo DESC "); | ||
done(); | ||
}); | ||
}); |
@@ -5,6 +5,15 @@ /** | ||
var sqlBuilder = require('../../lib/sqlQueryBuilder').queryBuilder; | ||
var sqlQueryBuilder = require('../../lib/sqlQueryBuilder'), | ||
sqlBuilder = sqlQueryBuilder.queryBuilder; | ||
describe("when calling ORDER BY, valid sql should be produced when", function(){ | ||
before(function() { | ||
sqlQueryBuilder.init({ | ||
entityToTableMap: { | ||
"CustomerType" : "AccountingProcessTypes" | ||
} | ||
}); | ||
}); | ||
// ORDER BY | ||
@@ -17,3 +26,3 @@ it('it is called with 1 argument', function(done){ | ||
.build()) | ||
.should.equal("SELECT Foo FROM Tickets ORDER BY Foo ASC "); | ||
.should.equal("SELECT Foo FROM Tickets ORDER BY Tickets.Foo ASC "); | ||
done(); | ||
@@ -29,3 +38,3 @@ }); | ||
.build()) | ||
.should.equal("SELECT Foo FROM Tickets ORDER BY Foo DESC "); | ||
.should.equal("SELECT Foo FROM Tickets ORDER BY Tickets.Foo DESC "); | ||
done(); | ||
@@ -59,3 +68,3 @@ }); | ||
.build()) | ||
.should.equal("SELECT Foo FROM Tickets ORDER BY Foo DESC "); | ||
.should.equal("SELECT Foo FROM Tickets ORDER BY Tickets.Foo DESC "); | ||
done(); | ||
@@ -71,3 +80,15 @@ }); | ||
done(); | ||
}) | ||
}); | ||
it('it is called by the servergrid', function(done){ | ||
(sqlBuilder().select({"c.Name": "Name", "at.Name": "CustomerType.Name"}).from("Customers c").join("AccountingProcessTypes at", "c.CustomerType_id", "at.Id").orderBy('Name').build()) | ||
.should.equal("SELECT c.Name AS 'Name', at.Name AS 'CustomerType.Name' FROM Customers c JOIN AccountingProcessTypes at ON c.CustomerType_id = at.Id ORDER BY c.Name ASC "); | ||
done(); | ||
}); | ||
it('it is called by the servergrid with a nested prop', function(done){ | ||
(sqlBuilder().select({"c.Name": "Name", "at.Name": "CustomerType.Name"}).from("Customers c").join("AccountingProcessTypes at", "c.CustomerType_id", "at.Id").orderBy('CustomerType.Name').build()) | ||
.should.equal("SELECT c.Name AS 'Name', at.Name AS 'CustomerType.Name' FROM Customers c JOIN AccountingProcessTypes at ON c.CustomerType_id = at.Id ORDER BY AccountingProcessTypes.Name ASC "); | ||
done(); | ||
}); | ||
}); |
@@ -29,5 +29,5 @@ /** | ||
.build()) | ||
.should.equal("WITH SelectedItems AS (SELECT (City + ', ' + State) Description, Bar, Blah, Thing, ROW_NUMBER() OVER (ORDER BY Description DESC) AS Position FROM Tickets) SELECT *, (Select MAX(Position) From SelectedItems) as 'TotalRows' FROM SelectedItems WHERE Position > 0 AND Position <= 50 "); | ||
.should.equal("WITH SelectedItems AS (SELECT (City + ', ' + State) Description, Bar, Blah, Thing, ROW_NUMBER() OVER (ORDER BY Tickets.Description DESC) AS Position FROM Tickets) SELECT *, (Select MAX(Position) From SelectedItems) as 'TotalRows' FROM SelectedItems WHERE Position > 0 AND Position <= 50 "); | ||
done(); | ||
}); | ||
}); |
@@ -442,3 +442,3 @@ /** | ||
"SELECT Id, Name, DaysBeforeTicketAged, LeaseRequired, " + | ||
"ROW_NUMBER() OVER (ORDER BY Name ASC) AS Position " + | ||
"ROW_NUMBER() OVER (ORDER BY Phases.Name ASC) AS Position " + | ||
"FROM Phases " + | ||
@@ -445,0 +445,0 @@ "WHERE Phases.IsActive = 1" + |
146397
3175