17.0.4 • Published 4 months ago

angular-base-dao v17.0.4

Weekly downloads
36
License
-
Repository
github
Last release
4 months ago

IMPORANT: This package is moved into 'angular-base-lib'

AngularBaseDao

This library helps you to create dao-services that include all CURDL methods out of the box without needing to implement them by your own. Complex models that are received from the REST requests can simply be converted/mapped using the provided HttpBaseDao and IConverter.

CURLD => Create Read Update Delete List

Basic Usage

To create a service that provides all CRUDL functions out of the box follow these steps:

Step 1: Create an entity-class which implements the IIdentifiable interface

export interface UserModel implements IIdentifiable {
  id: string;
  name: string;
}

Step 2: Create an entity-dao-service, which extends from the NoConversionHttpBaseDao-class

By default, the service will set withCredentials to true, to make use of http-only cookie authentication. If this is not wanted, you can specify it in the super-constructor call.

If you prefer a different kind of authentication method, you can pass custom http-headers to the affected CRUDL method. (this.dao.list(false, { ...httpOptions }))

@Injectable({ providedIn: 'root' })
export class UserDaoService extends NoConversionHttpBaseDao<UserModel> {
  constructor() {
    super('https://api.net/user');
  }
}

Step 3: Use the service

@Component(...)
export class AppComponent {
  private readonly userDao = inject(UserDaoService);

  constructor() {
    this.userDao.create({ name: 'Max' }); // returns Promise<UserModel>
    this.userDao.read('my-id'); // returns Promise<UserModel>
    this.userDao.update({ id: 'max-id', name: 'Max' }); // returns Promise<UserModel>
    this.userDao.delete({ id: 'max-id', name: 'Max' }); // returns Promise<void>
    this.userDao.list(); // returns Promise<UserModel[]>
  }
}

Using converters to convert http body to models (optional)

Step 1: Create the request interface that the server sends back to the client

export interface UserResponse implements IIdentifiable {
  id: string;
  creationDateString: string; // ISO string
}

Step 2: Create the interface that the dao-service should return

export interface UserModel implements IIdentifiable {
  id: string;
  creationDate: Date; // Real date object
}

Step 3: Create a mapper-service that maps between those two models

@Injectable({ providedIn: 'root' })
export class PersonConverterService implements IConverter<UserResponse, UserModel> {
  // Converts the model that was returned by the server to the model that should be used in the applciation
  fromJson(response: UserResponse): UserModel {
    return {
      id: response.id,
      creationDate: new Date(response.creationDateString), // Converts the date-string to a date
    };
  }

  // Prepares the object that will be sent to the server for CREATE or UPDATE
  toJson(model: UserModel): unknown {
    return {
      id: model.id,
      creationDate: model.creationDate.toISOString(),
    };
  }
}

Step 4: Create the entity-dao-service and provide the created converter-service

@Injectable({ providedIn: 'root' })
export class UserDaoService extends HttpBaseDao<UserResponse, PersonModel> {
  constructor() {
    super('https://api.net/user', inject(HttpClient), inject(PersonConverterService));
  }
}

Server Requirements

17.0.4

4 months ago

17.0.3

5 months ago

17.0.2

5 months ago

17.0.1

5 months ago

16.1.1

7 months ago

16.0.2

10 months ago

16.1.0

7 months ago

16.0.4

7 months ago

16.1.2

7 months ago

16.0.3

8 months ago

16.0.1

11 months ago

15.0.2

1 year ago

15.0.0

1 year ago

15.0.1

1 year ago

13.0.2

2 years ago

14.0.0

2 years ago

1.13.2

2 years ago

1.13.1

2 years ago

1.13.0

2 years ago

13.0.1

2 years ago

0.12.0

3 years ago

0.11.0

3 years ago

0.11.1

3 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.3

4 years ago

0.0.4

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago