sqlquerybuilder
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -64,14 +64,10 @@ var _ = require('lodash'); | ||
} else { | ||
if (self._options.entityToTableMap[table]) { | ||
element = self._options.entityToTableMap[table] + '.' + parts[1]; | ||
} else { | ||
alias = _.find(self._selectAliases, function (alias) { | ||
return alias.alias == element; | ||
}); | ||
alias = _.find(self._selectAliases, function (alias) { | ||
return alias.alias == element; | ||
}); | ||
if (alias) | ||
element = alias.selectProp; | ||
else | ||
element = self._checkTableName(table) + '.' + parts[1]; | ||
} | ||
if (alias) | ||
element = alias.selectProp; | ||
else | ||
element = self._checkTableName(table) + '.' + parts[1]; | ||
} | ||
@@ -78,0 +74,0 @@ } else if (!skipAlias) { |
{ | ||
"name": "sqlquerybuilder", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Highly opinionated Sql Server Query Writer, mostly for internal use.", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index", |
@@ -9,3 +9,4 @@ var sqlQueryBuilder = require('../../../lib/index'), | ||
entityToTableMap: { | ||
"CustomerType": "AccountingProcessTypes" | ||
"CustomerType": "AccountingProcessTypes", | ||
"OperationalCategory": "EquipmentTypes" | ||
}, | ||
@@ -202,2 +203,55 @@ enumerationSuffixes: ['DisplayName', 'Value'], | ||
}); | ||
it('should use aliases first before entityToTableMap names', function(done){ | ||
var reqQuery = { | ||
sidx: 'OperationalCategory.Name', | ||
sord: 'asc' | ||
}; | ||
var query = sqlBuilder() | ||
.select({"f.Id" : null,"AssetNumber":null,"ShortDescription":null,"glc.Name":"GLCategory.Name","ac.Number":"AccountCode", | ||
"oc.Name":"OperationalCategory.Name","f.InitialCost":null,"y.Name":"Yard.Name", | ||
"dep.Name":"Department.Name", "f.FixedAssetTypeDisplayName":"FixedAssetType.DisplayName","f.VIN":null, | ||
"po.Number":"PONumber", "f.IsActive":null}) | ||
.from("FixedAssets f") | ||
.join("AccountingProcessTypes glc", "glc.Id", "f.GLCategory_id") | ||
.join("AccountCodes ac","ac.Id","glc.AccountCode_id") | ||
.join("EquipmentTypes oc", "oc.Id", "f.OperationalCategory_id") | ||
.join("Yards y") | ||
.join("Departments dep") | ||
.leftJoin("Receivers r") | ||
.leftJoin("PurchaseOrders po", "po.Id", "r.PurchaseOrder_id") | ||
.processListRequest(reqQuery) | ||
.build(); | ||
query.trim().should.equal("WITH SelectedItems AS (SELECT f.Id, AssetNumber, ShortDescription, glc.Name AS 'GLCategory.Name', " + | ||
"ac.Number AS 'AccountCode', oc.Name AS 'OperationalCategory.Name', f.InitialCost, " + | ||
"y.Name AS 'Yard.Name', dep.Name AS 'Department.Name', f.FixedAssetTypeDisplayName AS 'FixedAssetType.DisplayName', " + | ||
"f.VIN, po.Number AS 'PONumber', f.IsActive, ROW_NUMBER() OVER (ORDER BY oc.Name ASC) AS Position FROM FixedAssets f " + | ||
"JOIN AccountingProcessTypes glc ON glc.Id = f.GLCategory_id JOIN AccountCodes ac ON ac.Id = glc.AccountCode_id " + | ||
"JOIN EquipmentTypes oc ON oc.Id = f.OperationalCategory_id JOIN Yards y ON f.Yard_id = y.Id " + | ||
"JOIN Departments dep ON f.Department_id = dep.Id LEFT JOIN Receivers r ON f.Receiver_id = r.Id " + | ||
"LEFT JOIN PurchaseOrders po ON po.Id = r.PurchaseOrder_id) SELECT *, " + | ||
"(Select MAX(Position) From SelectedItems) as 'TotalRows' FROM SelectedItems WHERE Position > 0 AND Position <= 100"); | ||
done(); | ||
}); | ||
it('should use entityToTableMap names when aliases werent found', function(done){ | ||
var reqQuery = { | ||
sidx: 'OperationalCategory.Name', | ||
sord: 'asc' | ||
}; | ||
var query = sqlBuilder() | ||
.select("EquipmentTypes.Name AS 'OperationalCategory.Name'") | ||
.from("FixedAssets f") | ||
.join("EquipmentTypes", "EquipmentTypes.Id", "f.OperationalCategory_id") | ||
.processListRequest(reqQuery) | ||
.build(); | ||
query.trim().should.equal("WITH SelectedItems AS (SELECT EquipmentTypes.Name AS 'OperationalCategory.Name', " + | ||
"ROW_NUMBER() OVER (ORDER BY EquipmentTypes.Name ASC) AS Position FROM FixedAssets f JOIN EquipmentTypes " + | ||
"ON EquipmentTypes.Id = f.OperationalCategory_id) SELECT *, (Select MAX(Position) From SelectedItems) as 'TotalRows' " + | ||
"FROM SelectedItems WHERE Position > 0 AND Position <= 100"); | ||
done(); | ||
}); | ||
}); |
240537
4845