NgxMocks
This library generates fake information for a crud model schema. Fake information is gotten from Mockaroo Api and it's saved in the browser's localStorage like a objects list. NgxMocks allows pushing, editting and deleting objects from list.
Install and Setup
Use npm package manager and execute following command:
npm install ngx-crud-mocks --save-dev
First, import ngx-crud-mocks module in your main module. You must configure mockaroo api key in forRoot method.The forRoot static method is a convention that provides and configures services at the same time, in this case apiMockaroo value will be injected inside library. If you use a SharedModule that you import in multiple other feature modules, you can export the NgxCrudMocksModule to make sure you don't have to import it in every module.
Then, in your component configure a provider like this example:
;;
CrudService is an abstract injectable with the most common crud methods.
- save(model: any) - (Can create and update. If model has an id field this method update information)
- search(start: number, limit: number, searchParams?: any)
- getById(id: number)
- deleteById(id: number)
- getNumTotal(searchParams?: any)
If you have a same structure in your real service (Real means when a service consuming API REST) you can connect and disconnect changing useClass value between yourRealservice or NgxCrudMocksService.
Next, you must configure mockFormat and localstorage keyName using setConfigMock method in the component's constructor. Only mockFormat is mandatory, localstorage keyName is optional.
;;;
Finally, mockFormat is a json array of field specifications according crud method. For instance for Book model:
You can create this mockFormat:
For more details go to Mockaroo Api.
Thanks
2018