Skip to content

Commit

Permalink
added download and skipTotal query params
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Jul 22, 2023
1 parent 283499d commit 8a5091c
Show file tree
Hide file tree
Showing 20 changed files with 163 additions and 53 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.16.0-WIP

- Added `skipTotal=1` query parameter by default for the `getFirstListItem()` and `getFullList()` requests.
Note that this have performance boost only with PocketBase v0.17+.

- Added optional `download=1` query parameter to force file urls with `Content-Disposition: attachment` (_supported with PocketBase v0.17+_).


## 0.15.3

- Automatically resolve pending realtime connect `Promise`s in case `unsubscribe` is called before
Expand Down
12 changes: 7 additions & 5 deletions dist/pocketbase.cjs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ interface ListQueryParams extends BaseQueryParams {
perPage?: number;
sort?: string;
filter?: string;
skipTotal?: boolean;
}
interface FullListQueryParams extends ListQueryParams {
batch?: number;
Expand All @@ -196,6 +197,7 @@ interface LogStatsQueryParams extends BaseQueryParams {
}
interface FileQueryParams extends BaseQueryParams {
thumb?: string;
download?: boolean;
}
interface appleClientSecret {
secret: string;
Expand Down Expand Up @@ -264,8 +266,8 @@ declare abstract class BaseCrudService<M extends BaseModel> extends BaseService
/**
* Returns the first found item by a list filter.
*
* Internally it calls `_getList(basePath, 1, 1, { filter })` and returns its
* first item.
* Internally it calls `_getList(basePath, 1, 1, { filter, skipTotal })`
* and returns its first item.
*
* For consistency with `_getOne`, this method will throw a 404
* ClientResponseError if no item was found.
Expand All @@ -291,7 +293,7 @@ declare abstract class CrudService<M extends BaseModel> extends BaseCrudService<
abstract get baseCrudPath(): string;
/**
* Returns a promise with all list items batch fetched at once
* (by default 200 items per request; to change it set the `batch` query param).
* (by default 500 items per request; to change it set the `batch` query param).
*
* You can use the generic T to supply a wrapper type of the crud model.
*/
Expand All @@ -309,8 +311,8 @@ declare abstract class CrudService<M extends BaseModel> extends BaseCrudService<
/**
* Returns the first found item by the specified filter.
*
* Internally it calls `getList(1, 1, { filter })` and returns the
* first found item.
* Internally it calls `getList(1, 1, { filter, skipTotal })` and
* returns the first found item.
*
* You can use the generic T to supply a wrapper type of the crud model.
*
Expand Down
2 changes: 1 addition & 1 deletion dist/pocketbase.cjs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.cjs.js.map

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions dist/pocketbase.es.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ interface ListQueryParams extends BaseQueryParams {
perPage?: number;
sort?: string;
filter?: string;
skipTotal?: boolean;
}
interface FullListQueryParams extends ListQueryParams {
batch?: number;
Expand All @@ -196,6 +197,7 @@ interface LogStatsQueryParams extends BaseQueryParams {
}
interface FileQueryParams extends BaseQueryParams {
thumb?: string;
download?: boolean;
}
interface appleClientSecret {
secret: string;
Expand Down Expand Up @@ -264,8 +266,8 @@ declare abstract class BaseCrudService<M extends BaseModel> extends BaseService
/**
* Returns the first found item by a list filter.
*
* Internally it calls `_getList(basePath, 1, 1, { filter })` and returns its
* first item.
* Internally it calls `_getList(basePath, 1, 1, { filter, skipTotal })`
* and returns its first item.
*
* For consistency with `_getOne`, this method will throw a 404
* ClientResponseError if no item was found.
Expand All @@ -291,7 +293,7 @@ declare abstract class CrudService<M extends BaseModel> extends BaseCrudService<
abstract get baseCrudPath(): string;
/**
* Returns a promise with all list items batch fetched at once
* (by default 200 items per request; to change it set the `batch` query param).
* (by default 500 items per request; to change it set the `batch` query param).
*
* You can use the generic T to supply a wrapper type of the crud model.
*/
Expand All @@ -309,8 +311,8 @@ declare abstract class CrudService<M extends BaseModel> extends BaseCrudService<
/**
* Returns the first found item by the specified filter.
*
* Internally it calls `getList(1, 1, { filter })` and returns the
* first found item.
* Internally it calls `getList(1, 1, { filter, skipTotal })` and
* returns the first found item.
*
* You can use the generic T to supply a wrapper type of the crud model.
*
Expand Down
2 changes: 1 addition & 1 deletion dist/pocketbase.es.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.es.mjs.map

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions dist/pocketbase.iife.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ interface ListQueryParams extends BaseQueryParams {
perPage?: number;
sort?: string;
filter?: string;
skipTotal?: boolean;
}
interface FullListQueryParams extends ListQueryParams {
batch?: number;
Expand All @@ -196,6 +197,7 @@ interface LogStatsQueryParams extends BaseQueryParams {
}
interface FileQueryParams extends BaseQueryParams {
thumb?: string;
download?: boolean;
}
interface appleClientSecret {
secret: string;
Expand Down Expand Up @@ -264,8 +266,8 @@ declare abstract class BaseCrudService<M extends BaseModel> extends BaseService
/**
* Returns the first found item by a list filter.
*
* Internally it calls `_getList(basePath, 1, 1, { filter })` and returns its
* first item.
* Internally it calls `_getList(basePath, 1, 1, { filter, skipTotal })`
* and returns its first item.
*
* For consistency with `_getOne`, this method will throw a 404
* ClientResponseError if no item was found.
Expand All @@ -291,7 +293,7 @@ declare abstract class CrudService<M extends BaseModel> extends BaseCrudService<
abstract get baseCrudPath(): string;
/**
* Returns a promise with all list items batch fetched at once
* (by default 200 items per request; to change it set the `batch` query param).
* (by default 500 items per request; to change it set the `batch` query param).
*
* You can use the generic T to supply a wrapper type of the crud model.
*/
Expand All @@ -309,8 +311,8 @@ declare abstract class CrudService<M extends BaseModel> extends BaseCrudService<
/**
* Returns the first found item by the specified filter.
*
* Internally it calls `getList(1, 1, { filter })` and returns the
* first found item.
* Internally it calls `getList(1, 1, { filter, skipTotal })` and
* returns the first found item.
*
* You can use the generic T to supply a wrapper type of the crud model.
*
Expand Down
2 changes: 1 addition & 1 deletion dist/pocketbase.iife.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.iife.js.map

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions dist/pocketbase.umd.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ interface ListQueryParams extends BaseQueryParams {
perPage?: number;
sort?: string;
filter?: string;
skipTotal?: boolean;
}
interface FullListQueryParams extends ListQueryParams {
batch?: number;
Expand All @@ -196,6 +197,7 @@ interface LogStatsQueryParams extends BaseQueryParams {
}
interface FileQueryParams extends BaseQueryParams {
thumb?: string;
download?: boolean;
}
interface appleClientSecret {
secret: string;
Expand Down Expand Up @@ -264,8 +266,8 @@ declare abstract class BaseCrudService<M extends BaseModel> extends BaseService
/**
* Returns the first found item by a list filter.
*
* Internally it calls `_getList(basePath, 1, 1, { filter })` and returns its
* first item.
* Internally it calls `_getList(basePath, 1, 1, { filter, skipTotal })`
* and returns its first item.
*
* For consistency with `_getOne`, this method will throw a 404
* ClientResponseError if no item was found.
Expand All @@ -291,7 +293,7 @@ declare abstract class CrudService<M extends BaseModel> extends BaseCrudService<
abstract get baseCrudPath(): string;
/**
* Returns a promise with all list items batch fetched at once
* (by default 200 items per request; to change it set the `batch` query param).
* (by default 500 items per request; to change it set the `batch` query param).
*
* You can use the generic T to supply a wrapper type of the crud model.
*/
Expand All @@ -309,8 +311,8 @@ declare abstract class CrudService<M extends BaseModel> extends BaseCrudService<
/**
* Returns the first found item by the specified filter.
*
* Internally it calls `getList(1, 1, { filter })` and returns the
* first found item.
* Internally it calls `getList(1, 1, { filter, skipTotal })` and
* returns the first found item.
*
* You can use the generic T to supply a wrapper type of the crud model.
*
Expand Down
2 changes: 1 addition & 1 deletion dist/pocketbase.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pocketbase.umd.js.map

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/services/FileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ export default class FileService extends BaseService {
let result = this.client.buildUrl(parts.join('/'));

if (Object.keys(queryParams).length) {
// normalize the download query param for consistency with the Dart sdk
if (queryParams.download === false) {
delete(queryParams.download);
}

const params = new URLSearchParams(queryParams);

result += (result.includes("?") ? "&" : "?") + params;
}

Expand Down
20 changes: 12 additions & 8 deletions src/services/utils/BaseCrudService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ export default abstract class BaseCrudService<M extends BaseModel> extends BaseS
/**
* Returns a promise with all list items batch fetched at once.
*/
protected _getFullList<T = M>(basePath: string, batchSize = 200, queryParams: ListQueryParams = {}): Promise<Array<T>> {
var result: Array<T> = [];
protected _getFullList<T = M>(basePath: string, batchSize = 500, queryParams: ListQueryParams = {}): Promise<Array<T>> {
queryParams = Object.assign({
'skipTotal': 1,
}, queryParams);

let result: Array<T> = [];

let request = async (page: number): Promise<Array<any>> => {
return this._getList(basePath, page, batchSize || 200, queryParams).then((list) => {
return this._getList(basePath, page, batchSize || 500, queryParams).then((list) => {
const castedList = (list as any as ListResult<T>);
const items = castedList.items;
const totalItems = castedList.totalItems;
const items = castedList.items;

result = result.concat(items);

if (items.length && totalItems > result.length) {
if (items.length == list.perPage) {
return request(page + 1);
}

Expand Down Expand Up @@ -83,15 +86,16 @@ export default abstract class BaseCrudService<M extends BaseModel> extends BaseS
/**
* Returns the first found item by a list filter.
*
* Internally it calls `_getList(basePath, 1, 1, { filter })` and returns its
* first item.
* Internally it calls `_getList(basePath, 1, 1, { filter, skipTotal })`
* and returns its first item.
*
* For consistency with `_getOne`, this method will throw a 404
* ClientResponseError if no item was found.
*/
protected _getFirstListItem<T = M>(basePath: string, filter: string, queryParams: BaseQueryParams = {}): Promise<T> {
queryParams = Object.assign({
'filter': filter,
'skipTotal': 1,
'$cancelKey': 'one_by_filter_' + basePath + "_" + filter,
}, queryParams);

Expand Down
14 changes: 10 additions & 4 deletions src/services/utils/CrudService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default abstract class CrudService<M extends BaseModel> extends BaseCrudS

/**
* Returns a promise with all list items batch fetched at once
* (by default 200 items per request; to change it set the `batch` query param).
* (by default 500 items per request; to change it set the `batch` query param).
*
* You can use the generic T to supply a wrapper type of the crud model.
*/
Expand All @@ -33,7 +33,13 @@ export default abstract class CrudService<M extends BaseModel> extends BaseCrudS

const params = Object.assign({}, batchOrqueryParams, queryParams);

return this._getFullList<T>(this.baseCrudPath, params.batch || 200, params);
let batch = 500;
if (params.batch) {
batch = params.batch;
delete params.batch;
}

return this._getFullList<T>(this.baseCrudPath, batch, params);
}

/**
Expand All @@ -48,8 +54,8 @@ export default abstract class CrudService<M extends BaseModel> extends BaseCrudS
/**
* Returns the first found item by the specified filter.
*
* Internally it calls `getList(1, 1, { filter })` and returns the
* first found item.
* Internally it calls `getList(1, 1, { filter, skipTotal })` and
* returns the first found item.
*
* You can use the generic T to supply a wrapper type of the crud model.
*
Expand Down
10 changes: 6 additions & 4 deletions src/services/utils/QueryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ export interface BaseQueryParams {
}

export interface ListQueryParams extends BaseQueryParams {
page?: number;
perPage?: number;
sort?: string;
filter?: string;
page?: number;
perPage?: number;
sort?: string;
filter?: string;
skipTotal?: boolean;
}

export interface FullListQueryParams extends ListQueryParams {
Expand All @@ -33,4 +34,5 @@ export interface LogStatsQueryParams extends BaseQueryParams {

export interface FileQueryParams extends BaseQueryParams {
thumb?: string;
download?: boolean;
}
Loading

0 comments on commit 8a5091c

Please sign in to comment.