Skip to content

Commit

Permalink
feat(record): fetch all records
Browse files Browse the repository at this point in the history
  • Loading branch information
KutsenkoA committed Nov 20, 2018
1 parent e07fbcc commit 861fd5b
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 12 deletions.
36 changes: 36 additions & 0 deletions extra_docs/examples/javascript/node/rest_get_all_records.ts
@@ -0,0 +1,36 @@
// tslint:disable:no-console
// tslint:disable:interface-name
// Node application
import { dataOperations, jexiaClient } from "jexia-sdk-js/node";

// Optionally use interfaces to type your datasets,
// then you will have better auto complete and type cheeking
interface Post {
title: string;
published: boolean;
}

// Initialize DataOperationsModule
let dom = dataOperations();

// Initialize Client and pass DataOperationsModule to it.
jexiaClient().init({
projectID: "_projectId_",
key: "_key_",
secret: "_secret_"
}, dom);

// Use your data module with an optional generic type
dom.dataset<Post>("posts")
.select()
// .fields("title", "id", "created_at")
.execute()
.then((records) => {
console.log(records);
process.exit();
})
.catch((error) => {
// there was a problem retrieving the records
console.log(error);
process.exit();
});
4 changes: 2 additions & 2 deletions src/api/dataops/queries/baseQuery.spec.ts
Expand Up @@ -75,9 +75,9 @@ describe("fields method", () => {
});

describe("execute method", () => {
it("should call queryExecuter.executeRequest()", () => {
it("should call queryExecuter.executeRestRequest()", () => {
let { subject, requestExecuterMock } = createSubject({action: QueryAction.select});
subject.execute();
expect(requestExecuterMock.executeQueryRequest).toHaveBeenLastCalledWith({action: "select"});
expect(requestExecuterMock.executeRestRequest).toHaveBeenCalled();
});
});
15 changes: 13 additions & 2 deletions src/api/dataops/queries/baseQuery.ts
Expand Up @@ -54,10 +54,21 @@ export abstract class BaseQuery<T> {
* @returns Result of this operation with the affected data
*/
public execute(): Promise<T[]> {
return this.queryExecuter.executeQueryRequest(compileDataRequest({

/* Compile request */
const compiledRequest = compileDataRequest({
action: this.action,
query: this.query,
records: this.records,
}));
});

/* if it is a select request and it has no query conditions
call record (rest) API, otherwise use query API
*/
if (compiledRequest.action === QueryAction.select && !compiledRequest.params) {
return this.queryExecuter.executeRestRequest();
} else {
return this.queryExecuter.executeQueryRequest(compiledRequest);
}
}
}
3 changes: 2 additions & 1 deletion src/api/dataops/queries/insertQuery.spec.ts
@@ -1,5 +1,6 @@
// tslint:disable:no-string-literal
import { createRequestExecuterMock } from "../../../../spec/testUtils";
import { Methods } from "../../../internal/requestAdapter";
import { InsertQuery } from "./insertQuery";

describe("InsertQuery class", () => {
Expand Down Expand Up @@ -44,6 +45,6 @@ describe("InsertQuery class", () => {
let subject: any = new InsertQuery(qe, [{ title: "Another first post", user_id: 1 }], dataset);
spyOn(subject["queryExecuter"], "executeRestRequest");
subject.execute();
expect(subject["queryExecuter"].executeRestRequest).toHaveBeenLastCalledWith(subject["records"]);
expect(subject["queryExecuter"].executeRestRequest).toHaveBeenLastCalledWith(Methods.POST, subject["records"]);
});
});
3 changes: 2 additions & 1 deletion src/api/dataops/queries/insertQuery.ts
@@ -1,4 +1,5 @@
import { RequestExecuter } from "../../../internal/executer";
import { Methods } from "../../../internal/requestAdapter";
import { BaseQuery, QueryAction } from "./baseQuery";

/**
Expand Down Expand Up @@ -33,6 +34,6 @@ export class InsertQuery<T, D extends T> extends BaseQuery<T> {
* @returns {Promise<any>}
*/
public execute(): Promise<D[]> {
return this.queryExecuter.executeRestRequest<T, D>(this.records);
return this.queryExecuter.executeRestRequest<T, D>(Methods.POST, this.records);
}
}
11 changes: 10 additions & 1 deletion src/api/dataops/queries/selectQuery.spec.ts
Expand Up @@ -117,9 +117,18 @@ describe("SelectQuery class", () => {

});

it("should correct execute the query", () => {
it("without any params should execute the rest api", () => {
let qe = createRequestExecuterMock(projectID, dataset);
let subject: any = new SelectQuery(qe, dataset);
spyOn(subject["queryExecuter"], "executeRestRequest");
subject.execute();
expect(subject["queryExecuter"].executeRestRequest).toHaveBeenCalled();
});

it("should execute the rest api if there is any param", () => {
let qe = createRequestExecuterMock(projectID, dataset);
let subject: any = new SelectQuery(qe, dataset);
subject.fields("id");
let compiledRequest = compileDataRequest({
action: QueryAction.select,
query: subject["query"],
Expand Down
4 changes: 2 additions & 2 deletions src/internal/executer.ts
Expand Up @@ -16,12 +16,12 @@ export class RequestExecuter {
private tokenManager: TokenManager,
) { }

public async executeRestRequest<T, D>(records: T[]): Promise<D[]> {
public async executeRestRequest<T, D>(method = Methods.GET, records?: T[]): Promise<D[]> {
await this.systemInit;
const token = await this.tokenManager.token;
return this.requestAdapter.execute(
this.getUrl(),
{ headers: { Authorization: token }, body: records, method: Methods.POST });
{ headers: { Authorization: token }, method, ...(records ? { body: records } : {}) });
}

public async executeQueryRequest(request: ICompiledRequest): Promise<any> {
Expand Down
4 changes: 1 addition & 3 deletions src/internal/query.ts
Expand Up @@ -8,7 +8,6 @@ interface ISort {
}

export class Query<T = any> {
private dataSet: string;
private fields: string[];
private limit: number;
private offset: number;
Expand All @@ -17,8 +16,7 @@ export class Query<T = any> {
private relations: Query[];
private data: T;

constructor(dataSet: string) {
this.dataSet = dataSet;
constructor(private readonly dataSet: string) {
this.orders = [];
this.relations = [];
}
Expand Down

0 comments on commit 861fd5b

Please sign in to comment.