Basic usage
This library abstracts the data access layer of your Angular project. You just need to provide some configuration thanks to typescript decorators to start using auto-generated repositories.
Resource
Start by creating a Typescript class for the type of resource you need to manipulate with ngx-repository
.
Once
this is
done, you can start annotating this class with the following decorators.
HttpResource and FirestoreResource
First, add a @HttpResource()
or @FirestoreResource()
decorator (depending on
which
protocol you wish to use)
on the resource class. The most basic configuration for this annotation consists in giving the HTTP or
Firestore
resource path of the resource.
import { FirestoreResource } from '@paddls/ngx-firestore-repository';
import { HttpResource } from '@paddls/ngx-http-repository';
// for Http
@HttpResource({
path: '/api/users'
})
// or for Firestore
@FirestoreResource({
path: '/users'
})
export class User {
// ...
}
Id and Column
Then, add an @Id()
decorator on the resource id and @Column()
decorators on
each
resource
field you want mapped with
ngx-repository
.
import { FirestoreResource } from '@paddls/ngx-firestore-repository';
import { HttpResource } from '@paddls/ngx-http-repository';
import { Id, Column } from '@paddls/ngx-repository';
// for Http
@HttpResource({
path: '/api/users'
})
// or for Firestore
@FirestoreResource({
path: '/users'
})
export class User {
@Id() // define the resource id
public id: number;
@Column() // define a column
public firstName: string;
@Column()
public lastName: string;
}
Repository
Right, now you have your resources. ngx-repository
will generate all repositories on
demand. You just
have to
inject it in your services using the
@InjectRepository()
decorator. You need to specify in the decorator context the type of
your
resource and the type of
your repository(HttpRepository
, FirestoreRepository
...).
The generic types of the generated repository type are the type of the resource and the type of the resource id.
import { InjectRepository, Page } from '@paddls/ngx-repository';
import { HttpRepository } from '@paddls/ngx-http-repository'
@Injectable()
export class BookService {
// repository is build with Http driver for User resource
@InjectRepository({resourceType: () => Book, repository: () => HttpRepository})
private readonly bookRepository: HttpRepository<Book, number>;
public findAll(): Observable<Page<Book>> {
return this.bookRepository.findAll();
}
// returns the id of created resource
public create(book: Book): Observable<number> {
return this.bookRepository.create(book);
}
// observable completes when update is effective
public update(book: Book): Observable<void> {
return this.bookRepository.update(book);
}
}
Each repository of a resource made from
FirestoreDriver
orHttpDriver
are singleton services stored in Angular Injector, so don't worry about injecting them on several services.
Last but not least : ngx-repository
automatically serializes and deserializes your
resources between JSON
and
strongly
typed TypeScript classes. Note that only fields with
@Id()
or @Column()
decorators are marked for serialization.