Skip to content

Commit

Permalink
Added GroupLimit attribute to GroupBy #15
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-markeev committed Jun 10, 2018
1 parent 3a589cb commit 7dd8ccf
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CamlJs.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>CamlJs</id>
<version>2.9.0</version>
<version>2.10.0</version>
<authors>Andrei Markeev</authors>
<owners>Andrei Markeev</owners>
<licenseUrl>https://github.com/andrei-markeev/camljs/blob/master/LICENSE</licenseUrl>
Expand Down
5 changes: 3 additions & 2 deletions dist/camljs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ declare module CamlBuilder {
}
interface IGroupable extends ISortable {
/** Adds GroupBy clause to the query.
@param collapse If true, only information about the groups is retrieved, otherwise items are also retrieved. */
GroupBy(fieldInternalName: any): IGroupedQuery;
@param collapse If true, only information about the groups is retrieved, otherwise items are also retrieved.
@param groupLimit Return only first N groups */
GroupBy(fieldInternalName: any, collapse?: boolean, groupLimit?: number): IGroupedQuery;
}
interface IExpression extends IGroupable {
/** Adds And clause to the query. */
Expand Down
24 changes: 14 additions & 10 deletions dist/camljs.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ var CamlBuilder = /** @class */ (function () {
return new FieldExpression(this.builder);
};
/** Adds GroupBy clause to the query.
@param collapse If true, only information about the groups is retrieved, otherwise items are also retrieved. */
QueryInternal.prototype.GroupBy = function (groupFieldName, collapse) {
this.builder.WriteStartGroupBy(groupFieldName, collapse);
@param collapse If true, only information about the groups is retrieved, otherwise items are also retrieved.
@param groupLimit Return only first N groups */
QueryInternal.prototype.GroupBy = function (groupFieldName, collapse, groupLimit) {
this.builder.WriteStartGroupBy(groupFieldName, collapse, groupLimit);
return new GroupedQuery(this.builder);
};
/** Adds OrderBy clause to the query
Expand Down Expand Up @@ -298,9 +299,10 @@ var CamlBuilder = /** @class */ (function () {
return new FieldExpression(this.builder);
};
/** Adds GroupBy clause to the query.
@param collapse If true, only information about the groups is retrieved, otherwise items are also retrieved. */
QueryToken.prototype.GroupBy = function (groupFieldName, collapse) {
this.builder.WriteStartGroupBy(groupFieldName, collapse);
@param collapse If true, only information about the groups is retrieved, otherwise items are also retrieved.
@param groupLimit Return only first N groups */
QueryToken.prototype.GroupBy = function (groupFieldName, collapse, groupLimit) {
this.builder.WriteStartGroupBy(groupFieldName, collapse, groupLimit);
return new GroupedQuery(this.builder);
};
/** Adds OrderBy clause to the query
Expand Down Expand Up @@ -925,7 +927,7 @@ var CamlBuilder = /** @class */ (function () {
this.WriteValueElement(valueType, value);
this.WriteEnd();
};
Builder.prototype.WriteStartGroupBy = function (groupFieldName, collapse) {
Builder.prototype.WriteStartGroupBy = function (groupFieldName, collapse, groupLimit) {
if (this.unclosedTags > 0) {
var tagsToClose = this.unclosedTags;
if (this.tree[0].Name == "Query")
Expand All @@ -936,10 +938,12 @@ var CamlBuilder = /** @class */ (function () {
this.tree.push({ Element: "End", Count: tagsToClose });
this.unclosedTags -= tagsToClose;
}
var elem = { Element: "Start", Name: "GroupBy", Attributes: [] };
if (collapse)
this.tree.push({ Element: "Start", Name: "GroupBy", Attributes: [{ Name: "Collapse", Value: "TRUE" }] });
else
this.tree.push({ Element: "Start", Name: "GroupBy" });
elem.Attributes.push({ Name: "Collapse", Value: "TRUE" });
if (groupLimit)
elem.Attributes.push({ Name: "GroupLimit", Value: "" + groupLimit });
this.tree.push(elem);
this.tree.push({ Element: "FieldRef", Name: groupFieldName });
this.WriteEnd();
};
Expand Down
30 changes: 18 additions & 12 deletions lib/camljs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ module CamlBuilder {
}
export interface IGroupable extends ISortable {
/** Adds GroupBy clause to the query.
@param collapse If true, only information about the groups is retrieved, otherwise items are also retrieved. */
GroupBy(fieldInternalName): IGroupedQuery;
@param collapse If true, only information about the groups is retrieved, otherwise items are also retrieved.
@param groupLimit Return only first N groups */
GroupBy(fieldInternalName, collapse?: boolean, groupLimit?: number): IGroupedQuery;
}

export interface IExpression extends IGroupable {
Expand Down Expand Up @@ -544,9 +545,10 @@ module CamlBuilder {
}

/** Adds GroupBy clause to the query.
@param collapse If true, only information about the groups is retrieved, otherwise items are also retrieved. */
GroupBy(groupFieldName: string, collapse?: boolean) {
this.builder.WriteStartGroupBy(groupFieldName, collapse);
@param collapse If true, only information about the groups is retrieved, otherwise items are also retrieved.
@param groupLimit Return only first N groups */
GroupBy(groupFieldName: string, collapse?: boolean, groupLimit?: number) {
this.builder.WriteStartGroupBy(groupFieldName, collapse, groupLimit);
return new GroupedQuery(this.builder);
}

Expand Down Expand Up @@ -675,9 +677,10 @@ module CamlBuilder {
}

/** Adds GroupBy clause to the query.
@param collapse If true, only information about the groups is retrieved, otherwise items are also retrieved. */
GroupBy(groupFieldName: string, collapse?: boolean) {
this.builder.WriteStartGroupBy(groupFieldName, collapse);
@param collapse If true, only information about the groups is retrieved, otherwise items are also retrieved.
@param groupLimit Return only first N groups */
GroupBy(groupFieldName: string, collapse?: boolean, groupLimit?: number) {
this.builder.WriteStartGroupBy(groupFieldName, collapse, groupLimit);
return new GroupedQuery(this.builder);
}

Expand Down Expand Up @@ -1370,7 +1373,7 @@ module CamlBuilder {
this.WriteValueElement(valueType, value);
this.WriteEnd();
}
WriteStartGroupBy(groupFieldName, collapse) {
WriteStartGroupBy(groupFieldName, collapse, groupLimit) {
if (this.unclosedTags > 0)
{
var tagsToClose = this.unclosedTags;
Expand All @@ -1382,10 +1385,13 @@ module CamlBuilder {
this.tree.push({ Element: "End", Count: tagsToClose });
this.unclosedTags -= tagsToClose;
}
let elem = { Element: "Start", Name: "GroupBy", Attributes: [] };
if (collapse)
this.tree.push({ Element: "Start", Name: "GroupBy", Attributes: [{ Name: "Collapse", Value: "TRUE" }] });
else
this.tree.push({ Element: "Start", Name: "GroupBy" });
elem.Attributes.push({ Name: "Collapse", Value: "TRUE" });
if (groupLimit)
elem.Attributes.push({ Name: "GroupLimit", Value: ""+groupLimit });

this.tree.push(elem);
this.tree.push({ Element: "FieldRef", Name: groupFieldName });
this.WriteEnd();
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "camljs",
"version": "2.9.1",
"version": "2.10.0",
"description": "Library for creating SharePoint CAML queries client-side. For JSOM, REST or SPServices.",
"main": "dist/camljs.js",
"types": "dist/camljs.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions tests/Tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ export default class Tests extends tsUnit.TestClass {
var query = new CamlBuilder()
.View(["Category", { count: "ID" }, { sum: "Amount" }])
.Query()
.GroupBy("Category")
.GroupBy("Category", true, 100)
.ToString();


Expand All @@ -607,7 +607,7 @@ export default class Tests extends tsUnit.TestClass {
<FieldRef Name="Amount" Type="SUM" />\
</Aggregations>\
<Query>\
<GroupBy>\
<GroupBy Collapse="TRUE" GroupLimit="100">\
<FieldRef Name="Category" />\
</GroupBy>\
</Query>\
Expand Down

0 comments on commit 7dd8ccf

Please sign in to comment.