|
| 1 | +# ht_data_postgres |
| 2 | + |
| 3 | + |
| 4 | +[](https://pub.dev/packages/very_good_analysis) |
| 5 | +[](https://polyformproject.org/licenses/free-trial/1.0.0) |
| 6 | + |
| 7 | +A generic PostgreSQL implementation of the `HtDataClient` interface. |
| 8 | + |
| 9 | +This package provides a concrete implementation of the `HtDataClient` |
| 10 | +for interacting with PostgreSQL databases. It translates generic CRUD and |
| 11 | +query operations into SQL commands, supporting various data models via |
| 12 | +JSON serialization and deserialization functions. |
| 13 | + |
| 14 | +## Getting Started |
| 15 | + |
| 16 | +Add the `ht_data_postgres` package to your `pubspec.yaml`: |
| 17 | + |
| 18 | +```yaml |
| 19 | +dependencies: |
| 20 | + ht_data_postgres: ^0.1.0 # Use the latest version |
| 21 | +``` |
| 22 | +
|
| 23 | +## Features |
| 24 | +
|
| 25 | +* **Generic Data Client:** Implements `HtDataClient<T>` for any data model. |
| 26 | +* **CRUD Operations:** Supports `create`, `read`, `update`, and `delete`. |
| 27 | +* **Querying:** `readAll` and `readAllByQuery` with pagination and sorting. |
| 28 | +* **User Scoping:** Operations can be scoped to a specific user via `userId`. |
| 29 | +* **Error Handling:** Maps PostgreSQL exceptions to `HtHttpException` subtypes. |
| 30 | + |
| 31 | +## Usage |
| 32 | + |
| 33 | +Initialize `HtDataPostgresClient` with a PostgreSQL `Connection`, table name, |
| 34 | +and JSON serialization functions: |
| 35 | + |
| 36 | +```dart |
| 37 | +import 'package:ht_data_client/ht_data_client.dart'; |
| 38 | +import 'package:ht_data_postgres/ht_data_postgres.dart'; |
| 39 | +import 'package:logging/logging.dart'; |
| 40 | +import 'package:postgres/postgres.dart'; |
| 41 | +
|
| 42 | +// Example data model (replace with your actual model) |
| 43 | +class MyItem { |
| 44 | + MyItem({required this.id, required this.name}); |
| 45 | + final String id; |
| 46 | + final String name; |
| 47 | +
|
| 48 | + Map<String, dynamic> toJson() => {'id': id, 'name': name}; |
| 49 | + static MyItem fromJson(Map<String, dynamic> json) => |
| 50 | + MyItem(id: json['id'] as String, name: json['name'] as String); |
| 51 | +} |
| 52 | +
|
| 53 | +Future<void> main() async { |
| 54 | + final log = Logger('PostgresClient'); |
| 55 | + final connection = await Connection.open( |
| 56 | + Endpoint( |
| 57 | + host: 'localhost', |
| 58 | + port: 5432, |
| 59 | + database: 'mydatabase', |
| 60 | + username: 'myuser', |
| 61 | + password: 'mypassword', |
| 62 | + ), |
| 63 | + ); |
| 64 | +
|
| 65 | + final client = HtDataPostgresClient<MyItem>( |
| 66 | + connection: connection, |
| 67 | + tableName: 'my_items', |
| 68 | + fromJson: MyItem.fromJson, |
| 69 | + toJson: (item) => item.toJson(), |
| 70 | + log: log, |
| 71 | + ); |
| 72 | +
|
| 73 | + try { |
| 74 | + // Create an item |
| 75 | + final newItem = MyItem(id: '123', name: 'Test Item'); |
| 76 | + final createdResponse = await client.create(item: newItem); |
| 77 | + print('Created: ${createdResponse.data.name}'); |
| 78 | +
|
| 79 | + // Read an item |
| 80 | + final readResponse = await client.read(id: '123'); |
| 81 | + print('Read: ${readResponse.data.name}'); |
| 82 | +
|
| 83 | + // Update an item |
| 84 | + final updatedItem = MyItem(id: '123', name: 'Updated Item'); |
| 85 | + final updatedResponse = await client.update(id: '123', item: updatedItem); |
| 86 | + print('Updated: ${updatedResponse.data.name}'); |
| 87 | +
|
| 88 | + // Read all items |
| 89 | + final allItemsResponse = await client.readAll(); |
| 90 | + print('All items: ${allItemsResponse.data.items.length}'); |
| 91 | +
|
| 92 | + // Delete an item |
| 93 | + await client.delete(id: '123'); |
| 94 | + print('Deleted item 123'); |
| 95 | + } catch (e) { |
| 96 | + print('Error: $e'); |
| 97 | + } finally { |
| 98 | + await connection.close(); |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +## License |
| 104 | + |
| 105 | +This package is licensed under the [PolyForm Free Trial](LICENSE). Please |
| 106 | +review the terms before use. |
0 commit comments