This is an old revision of the document!
Table of Contents
Calling OpenSRF from the Angular Client
The Angular client doesn't talk to the database directly, it instead communicates with OpenSRF to get the information and perform the actions it needs. To do this, it uses the NetService
, which is itself a wrapper around the OpenSRF javascript code.
The Angular client uses two general approaches to interacting with OpenSRF on database-related matters:
- Using the
NetService
directly to call a specific OpenSRF method. - Using the
PcrudService
, which provides a one-size-fits-all interface for most database operations.
Calling a specific OpenSRF method is preferred in cases in which:
- You need to make multiple database calls for a single action in the UI
- You are trying to do some complex slicing-and-dicing of data
- The UI action requires more work in the database than a single create, read, update, or delete (CRUD) action
- You need to interact with parts of the database that haven't been associated with permissions via the fieldmapper IDL XML file
The PcrudService is great for other cases, especially since it automatically checks the logged-in user's permissions and only provides the access that they are authorized for.
Since both approaches are used widely in the angular client, we will provide an example of each for each CRUD action below:
Creating a row in the database
PcrudService example
Let's start out with a simple PcrudService request:
// Create a new object in memory with the required information const note = this.idl.create('ahrn'); note.staff('t'); note.hold(12); note.title('My note'); note.body('Once upon a time, there was something interesting about this hold'); note.slip('t'); note.pub('f'); // Save the object to the database this.pcrud.create(note) // Pcrud calls are asynchronous, so that they don't // block other work that the browser needs to do in // the staff client. // // The following alert will happen once the PcrudService // receives a message back from OpenSRF .subscribe(() => { alert('You did it!') });
The above assumes that you have injected the IdlService
as this.idl
and the PcrudService
as this.pcrud
earlier in the file.
NetService example
Here's an example that uses the NetService
to create a database object instead, since the work we are doing is more complex and better suited to the Perl layer (reading from one database object, then creating a new database object in a totally different table with certain data taken from the first object):
this.net.request( 'open-ils.booking', 'open-ils.booking.resources.create_from_copies', // This OpenSRF method takes two parameters: the user's token and // an array of item IDs to make bookable this.auth.token(), [1, 2, 3]) // Like the PcrudService, the NetService is also asynchronous... .subscribe(() => { alert('Those items are bookable now')}); // Since the previous alert has to wait until a communication // comes back from OpenSRF, the following alert will almost // certainly appear to the user before the // 'Those items are bookable now' alert. alert('loading...');
The above assumes that you have injected the AuthService
as this.auth
and the NetService
as this.net
.
Reading data from the database
Examples coming soon
Updating data in the database
Examples coming soon
Deleting data in the database
Examples coming soon