Skip to content

Commit e89492a

Browse files
committed
Add documentation updates for the new XMLType support and the future object created in node-oracledb 6.3
1 parent 408e651 commit e89492a

File tree

4 files changed

+125
-50
lines changed

4 files changed

+125
-50
lines changed

doc/src/api_manual/oracledb.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3416,3 +3416,30 @@ Oracledb Methods
34163416
- Description
34173417
* - Error ``error``
34183418
- If ``startup()`` succeeds, ``error`` is NULL. If an error occurs, then ``error`` contains the :ref:`error message <errorobj>`.
3419+
3420+
.. _oracledbfuture:
3421+
3422+
Oracledb Future Object
3423+
======================
3424+
3425+
A special object that contains properties which control the behavior of
3426+
node-oracledb, allowing use of new features.
3427+
3428+
.. versionadded:: 6.3
3429+
3430+
.. attribute:: oracledb.future.oldJsonColumnAsObj
3431+
3432+
This property is a boolean which when set to *true* while using Oracle
3433+
Database 12c (or later), fetches VARCHAR2 and LOB columns that were
3434+
created with the ``IS JSON`` constraint in the same way that
3435+
:ref:`columns of type JSON <json21fetch>` are fetched when using
3436+
Oracle Database 21c (or later). The ``IS JSON`` constraint that is
3437+
specified when creating VARCHAR2 and LOB columns ensures that only JSON
3438+
data is stored in these columns.
3439+
3440+
The default value is *false*.
3441+
3442+
In a future version of node-oracledb, the setting of this attribute will
3443+
no longer be required since this will be the default behavior.
3444+
3445+
.. versionadded:: 6.3

doc/src/user_guide/appendix_a.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ node-oracledb Thin and Thick modes. For more details see :ref:`modediff`.
285285
- Yes
286286
- Yes
287287
* - XMLType data type (see :ref:`xmltype`)
288-
- Yes - May need to fetch as CLOB
288+
- Yes
289289
- Yes - May need to fetch as CLOB
290290
* - BFILE data type
291291
- No

doc/src/user_guide/json_data_type.rst

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,52 +15,29 @@ For more information about using JSON in Oracle Database see the
1515
`Database JSON Developer’s Guide <https://www.oracle.com/pls/topic/
1616
lookup?ctx=dblatest&id=ADJSN>`__.
1717

18-
**Oracle Database 12c JSON Data Type**
19-
20-
Prior to Oracle Database 21, JSON in relational tables is stored as
21-
BLOB, CLOB or VARCHAR2 data, allowing easy access with node-oracledb.
22-
All of these types can be used with node-oracledb in Thin or Thick mode.
23-
24-
The older syntax to create a table with a JSON column is like:
25-
26-
.. code-block:: sql
27-
28-
CREATE TABLE j_purchaseorder (po_document BLOB CHECK (po_document IS JSON));
29-
30-
The check constraint with the clause ``IS JSON`` ensures only JSON data
31-
is stored in that column.
32-
33-
The older syntax can still be used in Oracle Database 21, however the
34-
recommendation is to move to the new JSON type. With the old syntax, the
35-
storage can be BLOB, CLOB, or VARCHAR2. Of these, BLOB is preferred to
36-
avoid character set conversion overheads.
18+
.. _json21ctype:
3719

38-
**Oracle Database 21c JSON Data Type**
20+
Using the Oracle Database 21c JSON Type in node-oracledb
21+
========================================================
3922

4023
Oracle Database 21c introduced a dedicated JSON data type with a new
4124
`binary storage format <https://blogs.oracle.com/database/post/
4225
autonomous-json-database-under-the-covers-oson-format>`__
43-
that improves performance and functionality. To use the new dedicated
44-
JSON type, you can use node-oracledb 5.1 or later. The 21c JSON data
45-
type can be used in both node-oracledb Thin and Thick modes. With Thick mode,
46-
the Oracle Client libraries must be version 21, or later.
26+
that improves performance and functionality. To take advantage of the new
27+
dedicated JSON type in Oracle Database 21c and later versions, use
28+
node-oracledb 5.1 or later. For Thick mode, you must additionally use
29+
Oracle Client 21c (or later).
4730

48-
In Oracle Database 21 or later, to create a table with a column called
31+
In Oracle Database 21c or later, to create a table with a column called
4932
``PO_DOCUMENT`` for JSON data:
5033

5134
.. code-block:: sql
5235
5336
CREATE TABLE j_purchaseorder (po_document JSON);
5437
55-
.. _json21ctype:
38+
**Inserting JSON Data**
5639

57-
Using the Oracle Database 21c JSON Type in node-oracledb
58-
========================================================
59-
60-
Using node-oracledb Thin mode with Oracle Database 21c or later, or using
61-
node-oracledb Thick mode or node-oracledb 5.1 (or later) with Oracle Database
62-
21c (or later) and Oracle Client 21c (or later), you can insert JavaScript
63-
objects directly by binding as ``oracledb.DB_TYPE_JSON``:
40+
To insert JavaScript objects directly by binding as ``oracledb.DB_TYPE_JSON``:
6441

6542
.. code-block:: javascript
6643
@@ -71,6 +48,10 @@ objects directly by binding as ``oracledb.DB_TYPE_JSON``:
7148
{ bv: {val: data, type: oracledb.DB_TYPE_JSON} }
7249
);
7350
51+
.. _json21fetch:
52+
53+
**Fetching JSON Data**
54+
7455
To query a JSON column, use:
7556

7657
.. code-block:: javascript
@@ -86,11 +67,11 @@ The output is::
8667
}
8768
]
8869

89-
Using Oracle Client Libraries 19 or Earlier
90-
-------------------------------------------
70+
Using Oracle Client Libraries 19c or Earlier
71+
--------------------------------------------
9172

92-
If node-oracledb Thick mode uses Oracle Client Libraries 19 (or earlier),
93-
querying an Oracle Database 21 (or later), then JSON column returns a
73+
If node-oracledb Thick mode uses Oracle Client Libraries 19c (or earlier),
74+
querying an Oracle Database 21c (or later), then JSON column returns a
9475
:ref:`Lob Class <lobclass>` BLOB. You can stream the Lob or use
9576
:meth:`lob.getData()`:
9677

@@ -117,6 +98,26 @@ shown above.
11798
Using the Oracle Database 12c JSON Type in node-oracledb
11899
========================================================
119100

101+
In Oracle Database versions 12c or later (prior to Oracle Database 21c), JSON
102+
in relational tables is stored as BLOB, CLOB, or VARCHAR2 data. All of these
103+
types can be used with node-oracledb in Thin or Thick mode.
104+
105+
The older syntax to create a table with a JSON column is like:
106+
107+
.. code-block:: sql
108+
109+
CREATE TABLE j_purchaseorder (po_document BLOB CHECK (po_document IS JSON));
110+
111+
The check constraint with the clause ``IS JSON`` ensures only JSON data
112+
is stored in that column.
113+
114+
The older syntax can still be used in Oracle Database 21c. However the
115+
recommendation is to move to the new JSON type. With the old syntax, the
116+
storage can be BLOB, CLOB, or VARCHAR2. Of these, BLOB is preferred to
117+
avoid character set conversion overheads.
118+
119+
**Inserting JSON Data**
120+
120121
When using Oracle Database 12c or later with JSON using BLOB storage, you can
121122
insert JSON strings:
122123

@@ -131,11 +132,27 @@ insert JSON strings:
131132
[b] // bind the JSON string
132133
);
133134
135+
**Fetching JSON Data**
136+
137+
With Oracle Database 12c (or later), you can fetch VARCHAR2 and LOB columns
138+
that contain JSON data in the same way that
139+
:ref:`JSON type columns <json21fetch>` are fetched when using Oracle
140+
Database 21c (or later). This can be done by setting
141+
:attr:`oracledb.future.oldJsonColumnAsObj` to the value *true* as shown below.
142+
If you are using node-oracledb Thick mode, you must use Oracle Client 19c
143+
(or later) for this setting to work. For example:
144+
145+
.. code-block:: javascript
146+
147+
oracledb.future.oldJsonColumnAsObj = true;
148+
const r = await conn.execute(`SELECT po_document FROM j_purchaseorder`);
149+
console.dir(r.rows, { depth: null });
150+
134151
IN Bind Type Mapping
135152
====================
136153

137154
When binding a JavaScript object as ``oracledb.DB_TYPE_JSON`` for
138-
``oracledb.BIND_IN`` or ``oracledb.BIND_INOUT`` in Oracle Database 21
155+
``oracledb.BIND_IN`` or ``oracledb.BIND_INOUT`` in Oracle Database 21c
139156
(or later), JavaScript values are converted to JSON attributes as shown
140157
in the following table. The ‘SQL Equivalent’ syntax can be used in SQL
141158
INSERT and UPDATE statements if specific attribute types are needed but
@@ -221,7 +238,7 @@ might be like::
221238
Query and OUT Bind Type Mapping
222239
===============================
223240

224-
When getting Oracle Database 21 or later JSON values from the database, the
241+
When getting Oracle Database 21c or later JSON values from the database, the
225242
following attribute mapping occurs:
226243

227244
.. list-table-with-summary::

doc/src/user_guide/xml_data_type.rst

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,26 @@
44
Using XMLType Data
55
******************
66

7-
``XMLType`` columns queried will return Strings by default, limited
8-
to the size of a VARCHAR2.
7+
Oracle XMLType columns are fetched as strings by default in node-oracledb Thin
8+
and Thick modes. Note that in Thick mode, you may need to use the
9+
``XMLTYPE.GETCLOBVAL()`` function as detailed below.
910

10-
However, if desired, the SQL query could be changed to return a CLOB,
11-
for example:
11+
The examples below demonstrate using ``XMLType`` data with node-oracledb. The
12+
following table will be used in these examples:
1213

1314
.. code-block:: sql
1415
15-
const sql = `SELECT XMLTYPE.GETCLOBVAL(res) FROM resource_view`;
16+
CREATE TABLE xwarehouses(
17+
warehouse_id NUMBER,
18+
warehouse_spec XMLTYPE
19+
);
1620
17-
The CLOB can be fetched in node-oracledb as a String or
18-
:ref:`Lob <lobclass>`.
21+
Inserting XML
22+
=============
1923

20-
To insert into an ``XMLType`` column, directly insert a string
21-
containing the XML, or use a temporary LOB, depending on the data
22-
length.
24+
To insert into an ``XMLType`` column, you can directly insert a string
25+
containing the XML or use a temporary LOB, depending on the data
26+
length. For example:
2327

2428
.. code-block:: javascript
2529
@@ -42,5 +46,32 @@ length.
4246
{ id: 1, bv: myxml }
4347
);
4448
49+
Fetching XML
50+
============
51+
52+
Fetching XML data can be done directly in node-oracledb Thin mode. This also
53+
works in Thick mode for values that are shorter than the `maximum allowed
54+
length of a VARCHAR2 column <https://docs.oracle.com/pls/topic/lookup?ctx=
55+
dblatest&id=GUID-D424D23B-0933-425F-BC69-9C0E6724693C>`__:
56+
57+
.. code-block:: javascript
58+
59+
myxmldata = await connection.execute(`SELECT warehouse_spec FROM xwarehouses
60+
WHERE warehouse_id = :id`, [1]);
61+
console.log(myxmldata);
62+
63+
In Thick mode, for values that exceed the `maximum allowed length of a
64+
VARCHAR2 column <https://docs.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID
65+
-D424D23B-0933-425F-BC69-9C0E6724693C>`__, a CLOB must be returned by using
66+
the ``XMLTYPE.GETCLOBVAL()`` function:
67+
68+
.. code-block:: javascript
69+
70+
myxmldata = connection.execute(`SELECT XMLTYPE.GETCLOBVAL(warehouse_spec)
71+
AS mycontent FROM xwarehouses
72+
WHERE warehouse_id = :id`, [1]);
73+
console.log(myxmldata);
74+
75+
The CLOB can be fetched in node-oracledb as a String or :ref:`Lob <lobclass>`.
4576
LOB handling is as discussed in the section :ref:`Working with CLOB, NCLOB and
4677
BLOB Data <lobhandling>`.

0 commit comments

Comments
 (0)