Skip to content

Commit

Permalink
added collections.truncate method
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Sep 28, 2024
1 parent 375d6ba commit 5793c99
Show file tree
Hide file tree
Showing 19 changed files with 88 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@

- Added new `pb.collections.getScaffolds()` method to retrieve a type indexed map with the collection models (base, auth, view) loaded with their defaults.

- Added new `pb.collections.truncate(idOrName)` to delete all records associated with the specified collection.

- Added the submitted fetch options as 3rd last argument in the `pb.afterSend` hook.

- ⚠️ Admins are converted to `_superusers` auth collection and there is no longer `AdminService` and `AdminModel` types.
Expand Down
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -936,17 +936,20 @@ const result = await batch.send()
// Returns the first found collection matching the specified filter.
🔐 pb.collections.getFirstListItem(filter, options = {});

// Returns a single collection by its id.
🔐 pb.collections.getOne(id, options = {});
// Returns a single collection by its id or name.
🔐 pb.collections.getOne(idOrName, options = {});

// Creates (aka. register) a new collection.
🔐 pb.collections.create(bodyParams = {}, options = {});

// Updates an existing collection by its id.
🔐 pb.collections.update(id, bodyParams = {}, options = {});
// Updates an existing collection by its id or name.
🔐 pb.collections.update(idOrName, bodyParams = {}, options = {});

// Deletes a single collection by its id.
🔐 pb.collections.delete(id, options = {});
// Deletes a single collection by its id or name.
🔐 pb.collections.delete(idOrName, options = {});

// Deletes all records associated with the specified collection.
🔐 pb.collections.truncate(idOrName, options = {});

// Imports the provided collections.
🔐 pb.collections.import(collections, deleteMissing = false, options = {});
Expand Down
6 changes: 6 additions & 0 deletions dist/pocketbase.cjs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,12 @@ declare class CollectionService extends CrudService<CollectionModel> {
getScaffolds(options?: CommonOptions): Promise<{
[key: string]: CollectionModel;
}>;
/**
* Deletes all records associated with the specified collection.
*
* @throws {ClientResponseError}
*/
truncate(collectionIdOrName: string, options?: CommonOptions): Promise<true>;
}
interface HourlyStats {
total: number;
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.

6 changes: 6 additions & 0 deletions dist/pocketbase.es.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,12 @@ declare class CollectionService extends CrudService<CollectionModel> {
getScaffolds(options?: CommonOptions): Promise<{
[key: string]: CollectionModel;
}>;
/**
* Deletes all records associated with the specified collection.
*
* @throws {ClientResponseError}
*/
truncate(collectionIdOrName: string, options?: CommonOptions): Promise<true>;
}
interface HourlyStats {
total: number;
Expand Down
6 changes: 6 additions & 0 deletions dist/pocketbase.es.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,12 @@ declare class CollectionService extends CrudService<CollectionModel> {
getScaffolds(options?: CommonOptions): Promise<{
[key: string]: CollectionModel;
}>;
/**
* Deletes all records associated with the specified collection.
*
* @throws {ClientResponseError}
*/
truncate(collectionIdOrName: string, options?: CommonOptions): Promise<true>;
}
interface HourlyStats {
total: number;
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.

6 changes: 6 additions & 0 deletions dist/pocketbase.iife.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,12 @@ declare class CollectionService extends CrudService<CollectionModel> {
getScaffolds(options?: CommonOptions): Promise<{
[key: string]: CollectionModel;
}>;
/**
* Deletes all records associated with the specified collection.
*
* @throws {ClientResponseError}
*/
truncate(collectionIdOrName: string, options?: CommonOptions): Promise<true>;
}
interface HourlyStats {
total: number;
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.

6 changes: 6 additions & 0 deletions dist/pocketbase.umd.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,12 @@ declare class CollectionService extends CrudService<CollectionModel> {
getScaffolds(options?: CommonOptions): Promise<{
[key: string]: CollectionModel;
}>;
/**
* Deletes all records associated with the specified collection.
*
* @throws {ClientResponseError}
*/
truncate(collectionIdOrName: string, options?: CommonOptions): Promise<true>;
}
interface HourlyStats {
total: number;
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.

16 changes: 16 additions & 0 deletions src/services/CollectionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,20 @@ export class CollectionService extends CrudService<CollectionModel> {

return this.client.send(this.baseCrudPath + "/meta/scaffolds", options);
}

/**
* Deletes all records associated with the specified collection.
*
* @throws {ClientResponseError}
*/
async truncate(collectionIdOrName: string, options?: CommonOptions): Promise<true> {
options = Object.assign(
{
method: "DELETE",
},
options,
);

return this.client.send(this.baseCrudPath + "/" + encodeURIComponent(collectionIdOrName) +"/truncate", options).then(() => true);
}
}
21 changes: 21 additions & 0 deletions tests/services/CollectionService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,25 @@ describe("CollectionService", function () {
assert.deepEqual(result as any, true);
});
});

describe("truncate()", function () {
test("Should send truncate collection request", async function () {
fetchMock.on({
method: "DELETE",
url: service.client.buildURL("/api/collections/test%3D/truncate?q1=456"),
additionalMatcher: (_, config) => {
return config?.headers?.["x-test"] === "123";
},
replyCode: 204,
replyBody: true,
});

const result = await service.truncate("test=", {
q1: 456,
headers: { "x-test": "123" },
});

assert.deepEqual(result, true);
});
});
});

0 comments on commit 5793c99

Please sign in to comment.