Collection
Update: Collection is the library for work with crud against an Angular 12 app.
- Collection class contain all method for work with list of items (load, create, update, delete, deleteMany).
- Item class contain all method for work with single item (load, create, update, delete).
Installation
npm i collection
API
import { Collection } from 'collection'
Collection Methods
Type | Required Params | Description | |
---|---|---|---|
reload() | method | no params | send GET request and return response |
search() | method | Optional, default: 300 | send GET request and cancel previous request |
createItem({ data }) | method | Optional, default: {} | send POST request with data for save |
updateItem() | method | Optional, default: 'query' | a string value which is used a query parameter in the url. Ex: http://localhost:3000/countries?query='c |
deleteItem() | method | Optional, default: 'get' | the http/jsonp method to be used. |
selectAll() | method | Optional, default: 'http' | http or jsonp method types. |
unSelectAll() | method | Optional | a string value for the callback query parameter. |
selectItem() | boolean | Optional, default: true | if true, it allows empty strings to pass and invoke search |
setParams() | boolean | Optional, default: true | if true, it allows empty strings to pass and invoke search |
getParams() | boolean | Optional, default: true | if true, it allows empty strings to pass and invoke search |
clearParams() | boolean | Optional, default: true | if true, it allows empty strings to pass and invoke search |
getRouteParams() | boolean | Optional, default: true | if true, it allows empty strings to pass and invoke search |
setRouteParams() | boolean | Optional, default: true | if true, it allows empty strings to pass and invoke search |
clearRouteParams() | boolean | Optional, default: true | if true, it allows empty strings to pass and invoke search |
clear() | boolean | Optional, default: true | if true, it allows empty strings to pass and invoke search |
import { Item } from 'collection'
Item Methods
Type | Required | Description | |
---|---|---|---|
load() | string | YES | the url of a remote server that supports http/jsonp calls. |
createItem() | object | Optional, default: {} | { key: string, value: any} object as additional parameters |
updateItem() | string | Optional, default: 'query' | a string value which is used a query parameter in the url. Ex: http://localhost:3000/countries?query='c |
deleteItem() | string | Optional, default: 'get' | the http/jsonp method to be used. |
setParams() | boolean | Optional, default: true | if true, it allows empty strings to pass and invoke search |
getParams() | boolean | Optional, default: true | if true, it allows empty strings to pass and invoke search |
clearParams() | boolean | Optional, default: true | if true, it allows empty strings to pass and invoke search |
getRouteParams() | boolean | Optional, default: true | if true, it allows empty strings to pass and invoke search |
setRouteParams() | boolean | Optional, default: true | if true, it allows empty strings to pass and invoke search |
clearRouteParams() | boolean | Optional, default: true | if true, it allows empty strings to pass and invoke search |
clear() | boolean | Optional, default: true | if true, it allows empty strings to pass and invoke search |
import { CrudService } from 'collection'
CrudService Methods
Type | Required | Description | |
---|---|---|---|
createEntity() | string | YES | the url of a remote server that supports http/jsonp calls. |
createGetEntity() | object | Optional, default: {} | { key: string, value: any} object as additional parameters |
createPostEntity() | string | Optional, default: 'query' | a string value which is used a query parameter in the url. Ex: http://localhost:3000/countries?query='c |
createUpdateEntity() | string | Optional, default: 'get' | the http/jsonp method to be used. |
createDeleteEntity() | string | Optional, default: 'get' | the http/jsonp method to be used. |
createDeleteManyEntity() | string | Optional, default: 'get' | the http/jsonp method to be used. |
Usage
@Component({
selector: 'app-component',
templateUrl: ['./app.component.html'],
styleUrls: ['./app.component.scss']
})
export class AppComponent {
constructor(private _crud: CrudService) {
// Set your backend api url
this._crud.apiUrl = 'apiUrl';
}
}
- Use the
Collection
in your component.
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { AppService } from './app.service';
import {
CollectionModel,
CrudService,
Collection
} from 'collection';
@Component({
selector: 'projects-list',
templateUrl: ['./projects-list.component.html'],
styleUrls: ['./projects-list.component.css']
})
export class ProjectsListComponent implements OnInit {
collection: CollectionModel;
getAll() {
this.collection.reload().subscribe( res => {
// Send GET request into path '/projects' with default query params
// per_page, page, search_query
})
/*
For set query params use collection.setParams(key, value),
this.collection
.setParams('page', 2)
.setParams('search_query', '1234')
.search();
*/
}
create(data) {
this.collection.createItem({ data }).subscribe( res => {
// Send POST request into path '/projects' with data
this.collection.search();
})
}
update(data) {
this.collection.updateItem({ id: data.id, data }).subscribe( res => {
// Send PUT request into path '/projects/:id' with data
this.collection.search();
})
}
delete(id) {
this.collection.deleteItem({ id }).subscribe( res => {
// Send DELETE request into path '/projects/:id'
this.collection.search();
})
}
deleteMany(idsArray) {
this.collection.deleteMany({ ids: idsArray }).subscribe( res => {
// Send DELETE request into path '/projects' with body data ids: [...idsArray]
this.collection.search();
})
}
ngOnInit() {
this.collection.search();
}
constructor(private _crud: CrudService) {
this.collection = new Collection({
api: this._crud.createEntity({ name: 'projects' }),
params: {
page: 1,
per_page: 15,
search_quesry: 'Project Name'
}
});
}
}
// projects-list.component.html
<section class="projects-wrapper">
<ng-container *ngFor="let project of collection.items$ | async">
<div class="project-item">
{{ project?.name }}
</div>
</ng-container>
<ng-container *ngIf="collection.loading$ | async">
<span>...Loading</span>
</ng-container>
</section>