Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ♻️ query function accepts queryObj #110

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/lib/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ export class Entries extends EntryQueryable {
* const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.contentType("contentTypeUid").entry().query();
*/
query() {
query(queryObj?: { [key: string]: any }) {
if (queryObj) return new Query(this._client, this._contentTypeUid, queryObj);

return new Query(this._client, this._contentTypeUid);
}
}
27 changes: 5 additions & 22 deletions src/lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import { BaseQueryParameters, QueryOperation, QueryOperator } from './types';
export class Query extends BaseQuery {
private _contentTypeUid?: string;

constructor(client: AxiosInstance, uid: string) {
constructor(client: AxiosInstance, uid: string, queryObj?: { [key: string]: any }) {
super();
this._client = client;
this._contentTypeUid = uid;
this._urlPath = `/content_types/${this._contentTypeUid}/entries`;

if (queryObj) {
this._parameters = { ...this._parameters, ...queryObj };
}
}

/**
Expand Down Expand Up @@ -137,27 +141,6 @@ export class Query extends BaseQuery {
return this;
}

/**
* @method query
* @memberof Query
* @description Adds multiple query parameters to the query.
* @example
* import contentstack from '@contentstack/typescript'
*
* const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const query = stack.contentType("contentTypeUid").entry().query();
* const result = await query.query({'brand': {'$nin_query': {'title': 'Apple Inc.'}}}).find()
* // OR
* const asset = await stack.asset().query({'brand': {'$nin_query': {'title': 'Apple Inc.'}}}).find()
*
* @returns {Query}
*/
query(queryObj: { [key: string]: any }): Query {
this._parameters = { ...this._parameters, ...queryObj };

return this;
}

/**
* @method getQuery
* @memberof Query
Expand Down
11 changes: 8 additions & 3 deletions test/api/query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ describe('Query API tests', () => {
const query = await makeQuery('blog_post').where('_version', QueryOperation.IS_LESS_THAN, 3).find<TEntries>();
expect(query.entries[0].title).toEqual('The future of business with AI');
});
it('should add a where filter to the query parameters when object is passed to query method', async () => {
const query = await makeQuery('blog_post', {'_version': { '$lt': 3 }}).find<TEntries>();
expect(query.entries[0].title).toEqual('The future of business with AI');
});
it('should add a where-in filter to the query parameters', async () => {
const query = await makeQuery('blog_post')
.whereIn('author', makeQuery('author').where('uid', QueryOperation.EQUALS, 'blt09f7d2d46afe6dc6'))
Expand Down Expand Up @@ -46,8 +50,9 @@ describe('Query API tests', () => {
expect(query.entries[0].publish_details).not.toEqual(undefined);
});
});
function makeQuery(ctUid: string) {
const query = stack.ContentType(ctUid).Entry().query();
function makeQuery(ctUid: string, queryObj?: { [key: string]: any }) {
const entryInstance = stack.ContentType(ctUid).Entry();

return query;
if (queryObj) return entryInstance.query(queryObj);
return entryInstance.query();
}
11 changes: 5 additions & 6 deletions test/unit/query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ describe('Query class', () => {
});

it('should set a parameter correctly', () => {
query.query({ key1: 'value1' });
expect(query._parameters).toEqual({ key1: 'value1' });

query.query({ key2: 'value2' });
expect(query._parameters).toEqual({ key1: 'value1', key2: 'value2' });
const _query = getQueryObject(client, 'contentTypeUid', { key1: 'value1' })
expect(_query._parameters).toEqual({ key1: 'value1' });
});

it('should add an equality parameter to _parameters when queryOperation is EQUALS', () => {
Expand Down Expand Up @@ -92,6 +89,8 @@ describe('Query class', () => {
});
});

function getQueryObject(client: AxiosInstance, uid: string) {
function getQueryObject(client: AxiosInstance, uid: string, queryObj?: { [key: string]: any }) {
if (queryObj) return new Query(client, uid, queryObj);

return new Query(client, uid);
}
Loading