ORDS Part-3: Enable ORDS for Custom Schema using PLSQL

ORDS Part-3: Enable ORDS for Custom Schema using PLSQL

In the Last post, we have seen how to download and install ORDS with XE database.

In this post, we will see how to enable ORDS for schema.

Step1: Create New User/New Custom schema.

Login to XE database with SYS user.

A screenshot of a computer

AI-generated content may be incorrect.

Check all pluggable databases.

  1. Show pdbs;
A screenshot of a computer program

AI-generated content may be incorrect.

Alter session to FREEPDB1:

  1. ALTER SESSION SET CONTAINER=FREEPDB1;
A screenshot of a computer

AI-generated content may be incorrect.

Create new User/schema with username as “DEMO_ORDS”

  1. CREATE USER DEMO_ORDS IDENTIFIED BY Welcome123;
A screenshot of a computer

AI-generated content may be incorrect.

Verify user using below SQL statement:

  1. select username,account_status from dba_users where username='DEMO_ORDS';
A screenshot of a computer

AI-generated content may be incorrect.

Please make sure, ACCOUNT_STATUS is OPEN.

Grant privilege to this new user.

  1. grant all privileges to DEMO_ORDS;
A screenshot of a computer

AI-generated content may be incorrect.

Connect to this new schema in SQL developer.

Navigate to Connection—>New Database Connection.

A screenshot of a computer

AI-generated content may be incorrect.

Provide connection details as shown below and click on Test.

A screenshot of a computer

AI-generated content may be incorrect.

Once it is successful, click on save and then click on connect.

Step2: Create Sample Table and insert sample data in custom schema.

Let us create a new EMPLOYEES table.

  1. CREATE TABLE employees (
  2. emp_id NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY,
  3. name VARCHAR2(100),
  4. department VARCHAR2(50),
  5. salary NUMBER
  6. );
A screenshot of a computer

AI-generated content may be incorrect.

Add some sample data into this table.

  1. INSERT INTO employees (name, department, salary)
  2. VALUES ('Alice', 'HR', 60000);
  3. INSERT INTO employees (name, department, salary)
  4. VALUES ('Bob', 'IT', 75000);
  5. COMMIT;
A screenshot of a computer

AI-generated content may be incorrect.

Step3: Enabling ORDS for schema.

We can enable ORDS for schema using ORDS Database API. ORDS Database API is a database management and monitoring REST API embedded into Oracle REST Data Services. By default, the ORDS database API feature is disabled when you install ORDS for the first time.

To enable ORDS Database API:

Navigate to path where ORDS is installed and open command prompt.

Run below java command:

java -jar ords.war set-property database.api.enabled true

A screenshot of a computer program

AI-generated content may be incorrect.

Connect to DEMO_ORDS schema.

Execute SQL statement in SQL developer.

  1. BEGIN
  2. ORDS.ENABLE_SCHEMA(
  3. p_enabled => TRUE,
  4. p_schema => 'DEMO_ORDS',
  5. p_url_mapping_type => 'BASE_PATH',
  6. p_url_mapping_pattern => 'hr',
  7. p_auto_rest_auth => FALSE
  8. );
  9. COMMIT;
  10. END;
  11. /

This will enable ORDS for DEMO_ORDS schema and we can use it for REST.

Our base REST URL for this scheme is:

http://localhost:8080/ords/hr/

Step4: Enable ORDS for DB Objects.

Let us now enable ORDS for employees table created earlier.

  1. BEGIN
  2. ORDS.enable_object (
  3. p_enabled => TRUE, -- Default { TRUE | FALSE }
  4. p_schema => 'DEMO_ORDS',
  5. p_object => 'EMPLOYEES',
  6. p_object_type => 'TABLE', -- Default { TABLE | VIEW }
  7. p_object_alias => 'EMP'
  8. );
A screenshot of a computer

AI-generated content may be incorrect.

Rest Endpoint URL for this DB object will be:

http://localhost:8080/ords/hr/EMP/

A screenshot of a computer

AI-generated content may be incorrect.

This will create below Endpoints:

Operation

Rest Endpoint URL

Purpose

GET

http://localhost:8080/ords/hr/EMP/

Get all Employees

GET

http://localhost:8080/ords/hr/EMP/:id

Get employee by ID

POST

http://localhost:8080/ords/hr/EMP/

Create Employee

PUT

http://localhost:8080/ords/hr/EMP/:id

Update Employee

DELETE

http://localhost:8080/ords/hr/EMP/:id

Delete Employee


Let us verify operations:

GET:

Endpoint URL: http://localhost:8080/ords/hr/EMP/

A screenshot of a computer

AI-generated content may be incorrect.

GET by Employee ID:

Endpoint URL: http://localhost:8080/ords/hr/EMP/:id

A screenshot of a computer

AI-generated content may be incorrect.

POST:

Endpoint URL: http://localhost:8080/ords/hr/EMP/

PUT:

Endpoint URL: http://localhost:8080/ords/hr/EMP/:id

Let us change salary of Jon from 7000 to 8000.

A screenshot of a computer

AI-generated content may be incorrect.

DELETE:

Let us delete Jon’s account.

A black screen with white text

AI-generated content may be incorrect.

In this post, we have enabled custom schema and DB objects for ORDS. We have also tested different Rest endpoints for DB objects from postman.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top