> Portal Navigation: > > - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version. > - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages). > - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`). > - Top-level index of all portals: https://dev.wix.com/docs/llms.txt > - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt # CountDataItems # Package: externalDatabases # Namespace: ExternalDatabaseService # Method link: https://dev.wix.com/docs/api-reference/business-solutions/cms/external-databases/external-database-service-plugin/count-data-items.md ## Introduction Counts the number of items in the specified data collection that match the filtering preferences. --- ## REST API ### Schema ``` Method: countDataItems Description: Counts the number of items in the specified data collection that match the filtering preferences. URL: null Method: POST # Note: If the parameter `a.b` is listed under required parameters, `b` is only required if `a` is also present. Required parameters: collectionId, filter, consistentRead Method parameters: param name: collectionId | type: collectionId | description: GUID of the collection to query. | required: true param name: consistentRead | type: consistentRead | description: Whether to retrieve data from the primary database instance. This decreases performance but ensures data retrieved is up-to-date even immediately after an update. Applicable if the external database is eventually consistent. | required: true param name: filter | type: filter | description: Filter to specify which items to count. See [API Query Language](https://dev.wix.com/docs/rest/articles/getting-started/api-query-language.md#the-filter-section) for more information about handling data queries. | required: true Return type: CountDataItemsResponse - name: totalCount | type: integer | description: Number of items that match the query. ``` ### Examples ### Count items ```curl curl -X POST https://external-db.example.com/v3/items/count \ -H 'Content-Type: application/json;charset=UTF-8' \ -H 'Authorization: ' \ -d '{ "collectionId": "cities", "filter": { }, "consistentRead": false }' ``` ### Count items with a filter ```curl curl -X POST https://external-db.example.com/v3/items/count \ -H 'Content-Type: application/json;charset=UTF-8' \ -H 'Authorization: ' \ -d '{ "collectionId": "cities", "filter": { "country": "US" }, "consistentRead": false }' ``` --- ## JavaScript SDK ### Schema ``` Method: wixClientAdmin.externalDatabases.ExternalDatabaseService.countDataItems(request, metadata) Description: Counts the number of items in the specified data collection that match the filtering preferences. Method parameters: param name: metadata | type: Context | description: this message is not directly used by any service, it exists to describe the expected parameters that SHOULD be provided to invoked Velo methods as part of open-platform. e.g. SPIs, event-handlers, etc.. NOTE: this context object MUST be provided as the last argument in each Velo method signature. Example: ```typescript export function wixStores_onOrderCanceled({ event, metadata }: OrderCanceledEvent) { ... } ``` - name: requestId | type: string | description: A unique identifier of the request. You may print this GUID to your logs to help with future debugging and easier correlation with Wix's logs. - name: currency | type: string | description: [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) 3-letter currency code. - name: identity | type: IdentificationData | description: An object that describes the identity that triggered this request. - ONE-OF: - name: anonymousVisitorId | type: string | description: GUID of a site visitor that has not logged in to the site. - name: memberId | type: string | description: GUID of a site visitor that has logged in to the site. - name: wixUserId | type: string | description: GUID of a Wix user (site owner, contributor, etc.). - name: appId | type: string | description: GUID of an app. - name: languages | type: array | description: A string representing a language and region in the format of `"xx-XX"`. First 2 letters represent the language code according to ISO 639-1. This is followed by a dash "-", and then a by 2 capital letters representing the region according to ISO 3166-2. For example, `"en-US"`. - name: instanceId | type: string | description: The service provider app's instance GUID. param name: request | type: CountDataItemsRequest - name: collectionId | type: string | description: GUID of the collection to query. - name: filter | type: object | description: Filter to specify which items to count. See [API Query Language](https://dev.wix.com/docs/rest/articles/getting-started/api-query-language.md#the-filter-section) for more information about handling data queries. - name: consistentRead | type: boolean | description: Whether to retrieve data from the primary database instance. This decreases performance but ensures data retrieved is up-to-date even immediately after an update. Applicable if the external database is eventually consistent. Return type: PROMISE - name: totalCount | type: integer | description: Number of items that match the query. ``` ### Examples ### Example of a `totalCount` return value ```javascript import { externalDatabase } from '@wix/data/service-plugins'; externalDatabase.provideHandlers({ countDataItems: async ( payload ) => { const {request, metadata} = payload; // Use the `request` and `metadata` received from Wix and // apply custom logic. return { // Return your response exactly as documented to integrate with Wix. // Return value example: totalCount: 42 } } }); ``` ### countDataItems (self-hosted) Self-hosted SDK calls require you to [create a client](https://dev.wix.com/docs/sdk/articles/work-with-the-sdk/about-the-wix-client.md). ```javascript import { createClient } from '@wix/sdk'; import { externalDatabase } from '@wix/data/service-plugins'; // Import the auth strategy for the relevant access type // Import the relevant host module if needed const myWixClient = createClient ({ modules: { externalDatabase }, // Include the auth strategy and host as relevant }); async function countDataItems(request,metadata) { const response = await myWixClient.externalDatabase.countDataItems(request,metadata); }; ``` ---