-
Notifications
You must be signed in to change notification settings - Fork 8
feat: mle-ts-ords-backend template #301 #302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Database User | ||
DB_USER=<%= connectionUsername %> | ||
# Database User Password | ||
DB_PASSWORD=<%= connectionPassword %> | ||
# Connection string to your Autonomous Database/ | ||
# Oracle Database instance | ||
CONNECT_STRING=<%= connectionString %> | ||
# Oracle MLE Module name | ||
MLE_MODULE= | ||
# Optional HTTP Proxy Settings | ||
# HTTPS_PROXY= | ||
# HTTPS_PROXY_PORT= | ||
|
||
# Path to your local SQL Developer Command Line | ||
# installation | ||
SQL_CL_PATH=<%= sqlclPath %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Path to database wallet | ||
WALLET_PATH=<%= walletPath %> | ||
|
||
# Database User | ||
DB_USER=<%= connectionUsername %> | ||
# Database User Password | ||
DB_PASSWORD=<%= connectionPassword %> | ||
# Connection string to your Autonomous Database/ | ||
# Oracle Database instance | ||
CONNECT_STRING=<%= connectionString %> | ||
# Oracle MLE Module name | ||
MLE_MODULE= | ||
# Optional HTTP Proxy Settings | ||
# HTTPS_PROXY= | ||
# HTTPS_PROXY_PORT= | ||
|
||
# Path to your local SQL Developer Command Line | ||
# installation | ||
SQL_CL_PATH=<%= sqlclPath %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
node_modules | ||
dist | ||
dist-ssr | ||
*.local | ||
|
||
# Editor directories and files | ||
.DS_Store | ||
|
||
/.env | ||
/.env.* | ||
!/.env.example | ||
!/.env.*.example | ||
/server/utils/db/wallet |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<!-- markdownlint-disable MD013 --> | ||
|
||
# In-Database JavaScript with ORDS Backend Template | ||
|
||
An application template for Typescript and JavaScript developers showcasing REST API development using Oracle REST Data Services (ORDS) and Multilingual Engine (MLE). | ||
**Extends [`mle-ts-sample`](../mle-ts-sample/README.md); see that template's readme for foundational features and explanations.** | ||
|
||
## Description | ||
|
||
This project builds upon the [mle-ts-sample](../mle-ts-sample/README.md) template by demonstrating how to create REST APIs within Oracle Database using [Oracle REST Data Services (ORDS)](https://docs.oracle.com/en/database/oracle/oracle-rest-data-services/) and the [Multilingual Engine (MLE) JavaScript](https://docs.oracle.com/en/database/oracle/oracle-database/23/mlejs/introduction-to-mle.html). | ||
In addition to the TODO functionality of `mle-ts-sample`, this template shows how to: | ||
|
||
- Configure ORDS modules, templates, and handlers using SQL. | ||
- Export JavaScript functions as REST endpoints via ORDS, with handler logic in an MLE module. | ||
- Test REST endpoints with HTTP requests. | ||
- Set up a development environment using Docker Compose, with Oracle Database and an ORDS server. | ||
|
||
The basic application logic structure, build/deploy system, and database initialization approach remain as documented in the [mle-ts-sample README](../mle-ts-sample/README.md). | ||
|
||
### Project Structure | ||
|
||
Building on top of the standard [mle-ts-sample](../mle-ts-sample/README.md) layout, this template introduces several new files and conventions: | ||
|
||
| Source File | Used For | | ||
| ---------------------------------- | ------------------------------------------------------------------------------------- | | ||
| `src/ords.ts` | Contains handler functions for ORDS REST endpoints. | | ||
| `src/index.ts` | Exports the public API for the MLE module (see mle-ts-sample), plus ORDS handler implementations. | | ||
| `utils/database/ords.sql` | SQL script to configure ORDS: installs ORDS modules, templates, and handlers bound to the MLE JS module. | | ||
| `test/rest.test.js` | Automated tests for the REST API endpoints, using HTTP requests against the running ORDS backend. | | ||
| `docker-compose.yml` | Docker Compose file spinning up both Oracle Database 23 Free (`db23`) and an ORDS node (`ords-node1`). | | ||
|
||
All standard files and scripts from [`mle-ts-sample`](../mle-ts-sample/README.md) are also present and used as described in that README. | ||
|
||
### Requirements | ||
|
||
- [Oracle Database 23ai](https://www.oracle.com/database/) or Oracle Database Free (provided via Docker Compose). | ||
- [Oracle REST Data Services](https://www.oracle.com/database/technologies/appdev/rest.html) (ORDS, configured for database access). | ||
- [SQLcl](https://www.oracle.com/database/sqldeveloper/technologies/sqlcl) for deploying MLE modules. | ||
- [Node.js](https://nodejs.org/). | ||
- (Optional) [Docker](https://www.docker.com/) and [Docker Compose](https://docs.docker.com/compose/) for quick local setup. | ||
_Note: Any suitable container engine, such as [Podman](https://podman.io/), can be used in place of Docker._ | ||
|
||
### 1. Create the Application | ||
|
||
Follow the general steps in the [mle-ts-sample/README](../mle-ts-sample/README.md#getting-started) to create your project, but **choose `mle-ts-ords-backend` as the template** during `@create-database-app` initialization. | ||
|
||
### 2. (Optional) Start Development Environment with Docker Compose | ||
|
||
To quickly launch both Oracle Database and ORDS locally: | ||
|
||
```bash | ||
docker-compose up | ||
``` | ||
This starts: | ||
|
||
- `db23` - Oracle Database 23 Free | ||
- `ords-node1` - ORDS server configured to use `db23` | ||
|
||
See the `docker-compose.yml` file (and its comments) for configuration options. | ||
|
||
### 3. Install Project Dependencies | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
Installs all node modules, just like in mle-ts-sample. | ||
|
||
### 4. Initialize the Database | ||
|
||
Initialize the application data tables (as in mle-ts-sample): | ||
|
||
```bash | ||
npm run initdb | ||
``` | ||
|
||
### 5. Build and Deploy the MLE Module | ||
|
||
Build your application: | ||
|
||
```bash | ||
npm run build | ||
``` | ||
|
||
Deploy the transpiled JS module to the database: | ||
|
||
```bash | ||
npm run deploy | ||
``` | ||
|
||
Please check [mle-ts-sample/README](../mle-ts-sample/README.md) for more information about naming of the deployed modules. | ||
|
||
## REST Endpoint Development | ||
|
||
The main logic for REST endpoints is in [`src/ords.ts`](src/ords.ts). | ||
This file exports handler functions used by ORDS to process REST requests. These handlers are linked to ORDS modules and templates via SQL in [`utils/database/ords.sql`](utils/database/ords.sql). | ||
|
||
> **Before running any REST API tests, you must execute [`utils/database/ords.sql`](utils/database/ords.sql) to configure the ORDS modules, templates, and handlers.** | ||
> | ||
> **Important:** The MLE module name used in `ords.sql` is currently hardcoded. If you deployed your module under a different name, **edit `ords.sql` and replace the module name accordingly** to ensure ORDS invokes the correct module. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this step is important, could we have them as part of the other commands? like the build and deploy There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
||
- `src/index.ts` exports public functions and handlers for deployment as an MLE module. | ||
- ORDS is configured (via `ords.sql`) to call the correct JS handler for each endpoint. | ||
|
||
## Application Testing | ||
|
||
### Testing REST API Endpoints | ||
|
||
After deploying the MLE module and executing the ORDS configuration (`utils/database/ords.sql`), you can test the exposed REST API endpoints. | ||
|
||
#### Automated Testing | ||
|
||
Run the REST API test suite (see [`test/rest.test.js`](test/rest.test.js)): | ||
|
||
```bash | ||
npm run test | ||
``` | ||
|
||
This will send HTTP requests to the running ORDS server and verify the responses against expectations. | ||
|
||
You can also use `curl`, `Postman`, or any HTTP client to manually test the REST endpoints. | ||
Example (replace `[host]`, `[port]`, `<schema>`, and path as appropriate): | ||
|
||
```bash | ||
curl -X GET "http://localhost:8080/ords/<schema>/<ords_module_name>/<user_id>" | ||
``` | ||
Comment on lines
+124
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you add more examples? |
||
|
||
All endpoint routes, HTTP methods, and request/response formats are defined in the ORDS configuration and documented in `utils/database/ords.sql`. | ||
|
||
## Other Testing, Cleanup, and Usage | ||
|
||
You can use all application testing and database management approaches described in [mle-ts-sample/README](../mle-ts-sample/README.md) , including PLSQL call specifications, Oracle APEX, and other methods. | ||
|
||
## Links for Further Reading | ||
- [mle-ts-sample/README](../mle-ts-sample/README.md) - foundational documentation for this template. | ||
- [ORDS Documentation](https://docs.oracle.com/en/database/oracle/oracle-rest-data-services/index.html) | ||
- [Docker Compose](https://docs.docker.com/compose/) | ||
- [Podman Compose](https://docs.podman.io/en/v5.3.1/markdown/podman-compose.1.html) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
version: '2.4' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we use a more recent version instead? this one is from 2022 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while manually testing docker keeps throwing a deprecation error. |
||
services: | ||
db23: | ||
hostname: database | ||
image: container-registry.oracle.com/database/free:latest | ||
environment: | ||
- ORACLE_PDB=FREEPDB1 | ||
- ORACLE_PWD=oracle | ||
- DBHOST=database | ||
ports: | ||
- 1235:1521 | ||
mem_limit: 4096m | ||
cpus: 2 | ||
ords-node1: | ||
hostname: ords-node | ||
image: container-registry.oracle.com/database/ords:latest | ||
environment: | ||
- CONN_STRING=//database:1521/FREEPDB1 | ||
- ORACLE_PWD=oracle | ||
volumes: | ||
- ./ords/config:/etc/ords/config | ||
- ./apex:/opt/oracle/apex | ||
ports: | ||
- 8080:8080 | ||
healthcheck: | ||
test: curl --noproxy "localhost" -f -k http://localhost:8080/ords/ || exit 1 | ||
interval: 30s | ||
timeout: 10s | ||
retries: 150 | ||
depends_on: | ||
db23: | ||
condition: service_healthy |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { createUserHandler, getUserHandler, deleteUserHandler, updateUserHandler } from './ords'; | ||
//needed for basic tests of MLE module calls | ||
export { newUser, getUser, updateUser, deleteUser } from './todos'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { newUser, getUser, deleteUser, updateUser } from './todos'; | ||
|
||
export async function createUserHandler(req, resp) { | ||
const id = await newUser(req.query_parameters.name); | ||
resp.status(201); | ||
resp.content_type('application/json'); | ||
resp.json({ id }); | ||
} | ||
export async function getUserHandler(req, resp) { | ||
const userName = await getUser(parseInt(req.uri_parameters.id)); | ||
if(!userName) { | ||
resp.status(404); | ||
resp.content_type('application/json'); | ||
resp.json({msg: "User not found"}); | ||
} else { | ||
resp.status(200); | ||
resp.content_type('application/json'); | ||
resp.json(userName); | ||
} | ||
} | ||
export async function deleteUserHandler(req, resp) { | ||
const deleted = await deleteUser(parseInt(req.uri_parameters.id)); | ||
if (deleted > 0) { | ||
resp.status(200); | ||
} else { | ||
resp.status(404); | ||
} | ||
resp.content_type('application/json'); | ||
resp.json({ rowsDeleted: deleted }); | ||
} | ||
export async function updateUserHandler(req, resp) { | ||
const updated = await updateUser(parseInt(req.uri_parameters.id),req.query_parameters.name); | ||
if (updated > 0) { | ||
resp.status(200); | ||
} else { | ||
resp.status(404); | ||
} | ||
resp.content_type('application/json'); | ||
resp.json({ rowsUpdated: updated }); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a description on how to run this is missing, some new users might need it:
@/absolute/path/to/utils/database/ords.sql
or
sqlplus user/password@db @/absolute/path/to/utils/database/script.sql
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added separate command to run it
npm run ords