# @openharbor/vue-data
A Vue 3 Composition API library for simplified data management with CQRS backends, powered by a flexible builder pattern. This library helps you effortlessly connect your Vue components to backend services using a POST-centric interaction model, providing reactive data, loading states, and robust error handling.
## Features
* **Vue 3 Composition API:** Fully integrated with Vue's reactivity system.
* **CQRS-Oriented:** Designed for backends that use POST requests for both queries and commands.
* **Flexible Data Source Builders:** Configure data fetching and command execution with a fluent API.
* **Reactive State Management:** Automatically tracks `data`/`items`, `total`, `loading` status, and `error` states.
* **Pluggable Commands:** Easily define domain-specific commands (e.g., `AddProduct`, `RemoveProduct`, `UpdateProductDetails`, `PlaceOrder`).
* **Standardized Error Handling:** Consistent approach to backend error messages and validation errors.
---
## Installation
To use this library in your Vue 3 project, you need to install it via npm or Yarn. Remember that `vue` is a peer dependency, so ensure you have Vue 3 installed in your project.
```bash
# Using npm
npm install @openharbor/vue-data
# Using Yarn
yarn add @openharbor/vue-data
```
## Usage
The library provides three main composables: `useHttpDataSource`, `useSingleDataSource`, and `useListDataSource`, each tailored for different data interaction patterns.
### Core Concepts
* **Query Criteria (`IQueryCriteria`):** An object defining the parameters for your data requests (e.g., filters, pagination, sorting).
* **Models (`TModel`):** The shape of the data objects you are fetching or sending as commands.
* **Commands (`ICommand`):** The data payload sent to your backend for actions (e.g., `AddProductCommand`, `RemoveProductCommand`, `UpdateProductDetailsCommand`).
* **`keyResolver`:** A function `(model: TModel) => TModel[keyof TModel]` that extracts a unique identifier (like an `id`) from your model. This is crucial for `useSingleDataSource` and for certain command resolution scenarios.
* **`autoFetch`:** A boolean option to automatically trigger a `read` operation when the component mounts or when `criteria` changes.
### `useHttpDataSource`
This is the most generic data source composable. It's suitable for a wide range of querying and commanding needs where the structure isn't strictly a single item or a traditional list.
```typescript
// Example: src/views/MyGenericDataComponent.vue
Products
Loading products...
Error: {{ error.message }}
{{ product.name }} - ${{ product.price }}
Total products: {{ total }}
```
### `useSingleDataSource`
Best for managing a single entity (e.g., editing a product details page, displaying a user profile). It requires a `keyResolver` and can easily set up common CQRS commands for a single entity.
```typescript
// Example: src/views/ProductDetailComponent.vue
Loading Product Details...
Error: {{ error.message }}
Edit Product: {{ editedProduct.name }}
```
### `useListDataSource`
Ideal for displaying collections of data, such as tables, grids, or paginated lists. It functions as a semantic wrapper around `useHttpDataSource` for clarity.
```typescript
// Example: src/views/ProductListingComponent.vue
Product Catalog
Loading products...
Error: {{ error.message }}
{{ product.name }} ({{ product.category }})
Page {{ criteria.pageNumber }} of {{ total ? Math.ceil(total / criteria.pageSize) : 1 }}