1.0.144 • Published 12 months ago

@brainstack/crud v1.0.144

Weekly downloads
-
License
MIT
Repository
-
Last release
12 months ago

@brainstack/crud

A TypeScript CRUD (Create, Read, Update, Delete) abstraction package that allows you to perform CRUD operations using different data sources.

Installation

Install the package using npm:

npm install @brainstack/crud

Usage

import { createCRUD, CRUDIntegration, CRUD } from '@brainstack/crud';

// Define CRUDIntegration implementation
class MyCRUDIntegration<T> implements CRUDIntegration<T> {
  // Implement CRUD methods here...
}

// Create CRUD instance
const myCRUD: CRUD<MyType> = createCRUD(new MyCRUDIntegration<MyType>());

// Perform CRUD operations
const newItem = await myCRUD.create({ id: '1', name: 'Item 1' });
const readItem = await myCRUD.read('1');
const updatedItem = await myCRUD.update('1', { id: '1', name: 'Updated Item' });
await myCRUD.delete('1');
const listItems = await myCRUD.list();
const searchResults = await myCRUD.search('term');
const filterResults = await myCRUD.filter({ key: 'value' });

API

createCRUD(integration: CRUDIntegration<T>): CRUD<T>

Creates a CRUD instance with the provided integration.

  • integration: An object implementing the CRUDIntegration interface.

CRUDIntegration<T>

An interface defining the integration methods for CRUD operations. Implement this interface to connect to your data source.

  • create(item: T): Promise<T>
  • read(id: string): Promise<T | undefined>
  • update(id: string, item: T): Promise<T | undefined>
  • delete(id: string): Promise<void>
  • list(options?: { page?: number; limit?: number }): Promise<T[]>
  • search(term: string): Promise<T[]>
  • filter(criterias: { [key: string]: any }): Promise<T[]>

Integration Examples

REST API Integration

import axios from 'axios';
import { CRUDIntegration } from '@brainstack/crud';

class RestAPIIntegration<T> implements CRUDIntegration<T> {
  constructor(private baseUrl: string) {}

  async create(item: T): Promise<T> {
    const response = await axios.post(this.baseUrl, item);
    return response.data;
  }

  // Implement other CRUD methods...
}

const restAPI = new RestAPIIntegration<MyType>('https://api.example.com/items');
const crud = createCRUD(restAPI);

Mock Integration

import { CRUDIntegration } from '@brainstack/crud';

class MockCRUDIntegration<T> implements CRUDIntegration<T> {
  private data: Record<string, T> = {};

  async create(item: T): Promise<T> {
    this.data[item.id] = item;
    return item;
  }

  async read(id: string): Promise<T | undefined> {
    return this.data[id];
  }

  // Implement other CRUD methods...
}

const mockIntegration = new MockCRUDIntegration<MyType>();
const crud = createCRUD(mockIntegration);

Contributing

Contributions are welcome! If you would like to contribute to this module, please follow these guidelines:

Fork the repository
Create a new branch for your changes
Make your changes and commit them with descriptive commit messages
Push your changes to your fork
Submit a pull request

License

This module is released under the MIT License.

1.0.143

12 months ago

1.0.142

12 months ago

1.0.144

12 months ago

1.0.141

1 year ago

1.0.140

1 year ago

1.0.139

1 year ago

1.0.132

1 year ago

1.0.134

1 year ago

1.0.133

1 year ago

1.0.136

1 year ago

1.0.135

1 year ago

1.0.138

1 year ago

1.0.137

1 year ago

1.0.131

1 year ago

1.0.130

1 year ago

1.0.129

1 year ago

1.0.128

1 year ago

1.0.127

1 year ago

1.0.126

1 year ago

1.0.125

1 year ago

1.0.121

1 year ago

1.0.120

1 year ago

1.0.123

1 year ago

1.0.122

1 year ago

1.0.124

1 year ago

1.0.119

1 year ago

1.0.118

1 year ago

1.0.117

1 year ago

1.0.116

1 year ago

1.0.109

1 year ago

1.0.108

1 year ago

1.0.110

1 year ago

1.0.112

1 year ago

1.0.111

1 year ago

1.0.114

1 year ago

1.0.113

1 year ago

1.0.115

1 year ago

1.0.107

1 year ago

1.0.101

1 year ago

1.0.100

1 year ago

1.0.106

1 year ago

1.0.103

1 year ago

1.0.102

1 year ago

1.0.105

1 year ago

1.0.104

1 year ago

1.0.99

1 year ago

1.0.98

1 year ago

1.0.97

1 year ago

1.0.96

1 year ago

1.0.95

1 year ago

1.0.94

1 year ago

1.0.93

1 year ago

1.0.92

1 year ago

1.0.85

1 year ago

1.0.73

2 years ago

1.0.72

2 years ago

1.0.71

2 years ago

1.0.70

2 years ago

1.0.77

2 years ago

1.0.76

2 years ago

1.0.75

2 years ago

1.0.74

2 years ago

1.0.79

2 years ago

1.0.78

2 years ago

1.0.80

2 years ago

1.0.83

2 years ago

1.0.82

2 years ago

1.0.81

2 years ago

1.0.62

2 years ago

1.0.61

2 years ago

1.0.60

2 years ago

1.0.66

2 years ago

1.0.65

2 years ago

1.0.64

2 years ago

1.0.63

2 years ago

1.0.69

2 years ago

1.0.68

2 years ago

1.0.67

2 years ago

1.0.40

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.48

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.49

2 years ago

1.0.51

2 years ago

1.0.50

2 years ago

1.0.55

2 years ago

1.0.54

2 years ago

1.0.53

2 years ago

1.0.52

2 years ago

1.0.59

2 years ago

1.0.58

2 years ago

1.0.57

2 years ago

1.0.56

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.28

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago