@herlinus/coloquent
Version:
Library for retrieving model objects from a JSON-API, with a fluent syntax inspired by Laravel Eloquent.
154 lines • 6.43 kB
JavaScript
import { FilterSpec } from "./FilterSpec";
import { SortSpec } from "./SortSpec";
import { PluralResponse } from "./response/PluralResponse";
import { SingularResponse } from "./response/SingularResponse";
import { Option } from "./Option";
import { PaginationStrategy } from "./PaginationStrategy";
import { OffsetBasedPaginationSpec } from "./paginationspec/OffsetBasedPaginationSpec";
import { PageBasedPaginationSpec } from "./paginationspec/PageBasedPaginationSpec";
import { Query } from "./Query";
import { SortDirection } from "./SortDirection";
export class Builder {
constructor(modelType, queriedRelationName = undefined, baseModelJsonApiType = undefined, baseModelJsonApiId = undefined, forceSingular = false) {
this.modelType = modelType;
let modelInstance = (new modelType());
baseModelJsonApiType = baseModelJsonApiType
? baseModelJsonApiType
: modelInstance.getJsonApiType();
this.query = new Query(baseModelJsonApiType, queriedRelationName, baseModelJsonApiId);
this.initPaginationSpec();
this.httpClient = modelType.getHttpClient();
this.forceSingular = forceSingular;
}
get(page = 0) {
const clone = this.clone();
clone.getQuery().getPaginationSpec().setPage(page);
if (this.forceSingular) {
return this.getHttpClient()
.get(clone.getQuery().toString())
.then((response) => {
return new SingularResponse(clone.getQuery(), response, this.modelType, response.getData());
}, function (response) {
throw new Error(response.message);
});
}
else {
return this.getHttpClient()
.get(clone.getQuery().toString())
.then((response) => {
return new PluralResponse(clone.getQuery(), response, this.modelType, response.getData(), page);
}, function (response) {
throw new Error(response.message);
});
}
}
first() {
const clone = this.clone();
clone.getQuery().getPaginationSpec().setPageLimit(1);
return this.getHttpClient()
.get(this.query.toString())
.then((response) => {
return new SingularResponse(this.query, response, this.modelType, response.getData());
}, function (response) {
throw new Error(response.message);
});
}
limit(limit) {
const clone = this.clone();
clone.getQuery().setLimit(limit);
return clone;
}
find(id) {
const clone = this.clone();
clone.query.setIdToFind(id);
return clone.getHttpClient()
.get(clone.getQuery().toString())
.then((response) => {
return new SingularResponse(clone.getQuery(), response, this.modelType, response.getData());
}, function (response) {
throw new Error(response.message);
});
}
where(attribute, value) {
const clone = this.clone();
clone.getQuery().addFilter(new FilterSpec(attribute, value));
return clone;
}
with(value) {
const clone = this.clone();
if (typeof value === 'string') {
clone.getQuery().addInclude(value);
}
else if (Array.isArray(value)) {
for (let v of value) {
clone.getQuery().addInclude(v);
}
}
else {
throw new Error("The argument for 'with' must be a string or an array of strings.");
}
return clone;
}
orderBy(attribute, direction) {
if (typeof direction === 'undefined' || direction === null) {
direction = SortDirection.ASC;
}
else if (typeof direction === 'string') {
if (direction === 'asc') {
direction = SortDirection.ASC;
}
else if (direction === 'desc') {
direction = SortDirection.DESC;
}
else {
throw new Error("The 'direction' parameter must be string of value 'asc' or 'desc', " +
"value '" + direction + "' invalid.");
}
}
const clone = this.clone();
clone.getQuery().addSort(new SortSpec(attribute, direction === SortDirection.ASC));
return clone;
}
option(queryParameter, value) {
const clone = this.clone();
clone.getQuery().addOption(new Option(queryParameter, value));
return clone;
}
clone() {
let clone = Object.create(this);
let query = new Query(this.query.getJsonApiType(), this.query.getQueriedRelationName(), this.query.getJsonApiId());
this.query.getFilters().forEach(filter => query.addFilter(filter));
this.query.getOptions().forEach(option => query.addOption(option));
this.query.getSort().forEach(sort => query.addSort(sort));
this.query.getInclude().forEach(include => query.addInclude(include));
query.setPaginationSpec(Object.create(this.query.getPaginationSpec()));
const limit = this.query.getLimit();
if (limit !== undefined) {
query.setLimit(limit);
}
clone.setQuery(query);
return clone;
}
getQuery() {
return this.query;
}
setQuery(query) {
this.query = query;
}
initPaginationSpec() {
let paginationStrategy = this.modelType.getPaginationStrategy();
if (paginationStrategy === PaginationStrategy.OffsetBased) {
this.query.setPaginationSpec(new OffsetBasedPaginationSpec(this.modelType.getPaginationOffsetParamName(), this.modelType.getPaginationLimitParamName(), this.modelType.getPageSize()));
}
else if (paginationStrategy === PaginationStrategy.PageBased) {
this.query.setPaginationSpec(new PageBasedPaginationSpec(this.modelType.getPaginationPageNumberParamName(), this.modelType.getPaginationPageSizeParamName(), this.modelType.getPageSize()));
}
else {
throw new Error('Illegal state: Pagination strategy is not set.');
}
}
getHttpClient() {
return this.httpClient;
}
}
//# sourceMappingURL=Builder.js.map