zdata-client
Version:
TypeScript client library for zdata backend API with authentication and full CRUD operations
129 lines • 3.76 kB
JavaScript
/**
* @fileoverview Base client for creating custom data sources
*/
import { ZDataClient } from "./client.js";
/**
* Abstract base class for creating custom data source clients
*
* This class extends ZDataClient and provides a foundation for creating
* type-safe data access layers for specific entities. It allows developers
* to create strongly-typed wrappers around the generic CRUD operations.
*
* @template T - The entity type this client manages
*
* @example
* ```typescript
* interface Payment {
* amount: number;
* description: string;
* userId: string;
* }
*
* class PaymentClient extends BaseDataSourceClient<Payment> {
* constructor(config: ApiConfig) {
* super(config, 'pagamentos');
* }
*
* // Optional: Add custom business logic methods
* async findPaymentsByUser(userId: string) {
* return this.findRecords({
* resourceName: this.resourceName,
* search: `user:${userId}`,
* });
* }
* }
* ```
*/
export class BaseDataSourceClient extends ZDataClient {
/**
* Create a new data source client instance
* @param config - API client configuration
* @param resourceName - Name of the resource this client manages
*/
constructor(config, resourceName) {
super(config);
this.resourceName = resourceName;
}
/**
* Create a new record for this resource
* @param data - Entity data to create (without base fields)
* @returns Promise resolving to the created entity with base fields
*/
async create(data) {
return this.createRecord(this.resourceName, data);
}
/**
* Update an existing record
* @param id - Record identifier
* @param data - Partial entity data to update
* @returns Promise resolving to the updated entity with base fields
*/
async update(id, data) {
return this.updateRecord(this.resourceName, id, data);
}
/**
* Delete a record by ID
* @param id - Record identifier
* @returns Promise that resolves when deletion is complete
*/
async delete(id) {
return this.deleteRecord(this.resourceName, id);
}
/**
* Find a specific record by ID
* @param id - Record identifier
* @returns Promise resolving to the found entity with base fields
*/
async findById(id) {
return this.findRecordById(this.resourceName, id);
}
/**
* Find records with pagination and optional search
* @param params - Query parameters (resourceName will be automatically set)
* @returns Promise resolving to paginated response with entities
*/
async find(params = {}) {
return this.findRecords({
resourceName: this.resourceName,
...params,
});
}
/**
* Get the resource name this client manages
* @returns The resource name
*/
getResourceName() {
return this.resourceName;
}
}
/**
* Concrete implementation of BaseDataSourceClient for easy instantiation
*
* This class can be used when you don't need custom business logic
* but want the benefits of type safety and simplified method names.
*
* @template T - The entity type this client manages
*
* @example
* ```typescript
* interface User {
* name: string;
* email: string;
* }
*
* const userClient = new DataSourceClient<User>(config, 'users');
*
* const newUser = await userClient.create({
* name: 'John Doe',
* email: 'john@example.com'
* });
*
* const users = await userClient.find({ page: 1, limit: 10 });
* ```
*/
export class DataSourceClient extends BaseDataSourceClient {
constructor(config, resourceName) {
super(config, resourceName);
}
}
//# sourceMappingURL=base-client.js.map