pojo-repository
Repository interfaces for CRUD operations on plain-old-javascript/json-objects (pojos), and HttpRestRepositry provider for use with standard rest apis.
All methods are asynchonous using the Promise API.
Installation
Install with npm:
npm install pojo-repository
Or with yarn:
yarn add pojo-repository
Basic Usage
Import
;
Reading a model from its source
; ;
Saving changes to a model
; ;model.name = 'frog'; repository.savemodel.id, model;
Creating and saving new models
; ;model.name = 'frog'; await repository.savemodel.id, model, true /* Will perform POST instead of PUT for new records. */;
Removing models
; await repository.remove'my_id';
Readonly Repositories
A read-only APi for sources that cannot be saved to is available as the ReadRepository interface and HttpRestReadRepository class.
; ; // repository is readonly, save(), create(), remove() are not available.
Authorization or Custom Headers
HttpRestRespository and HttpRestReadRepository use ApiFetch from apifetch-json internally. You can pass an optional pre-configured ApiFetch object to the repository constructor to handle authorization or other special HTTP options.
; ; ;
For more examples of using ApiFetch check out the apifetch-json project.
Custom Repositories
You an also provide your own Repository and ReadRespository implementations by implementing the Repository or RepositoryRead interfaces.
;
Javascript Usage
pojo-repository works just as well with Javascript as Typescript. All you need to do is remove the type information from the above examples.
Here are the basic usage examples in plain Javascript:
Import
;
Reading a model from its source
var repository = 'https://...'; var model = repository;
Saving changes to a model
; ;model.name = 'frog'; repository.savemodel.id, model;
Creating and saving new models
var repository = 'https://...'; var model = await repository;modelname = 'frog'; await repository;
Removing models
; await repository.remove'my_id';
Readonly Repositories
A read-only APi for sources that cannot be saved to is available as the ReadRepository interface and HttpRestReadRepository class.
; ; // repository is readonly, save(), create(), remove() are not available.
Typescript
This project is written in typescript and comes with its own bindings.
License
Licensed under the MIT license.