8 Schema Flexibility with JSON Columns in Duality Views
Including columns of JSON
data type in tables that underlie
a duality view lets applications add and delete fields, and change the types of field
values, in the documents supported by the view. The stored JSON data can be schemaless or
JSON Schema-based (to enforce particular types of values).
When schemaless, the values such fields can be of any JSON-language type (scalar, object, array). This is in contrast to the fields generated from scalar SQL columns, which are always of a predefined type (and are always present in the documents).
When you define a duality view, you can declaratively choose the kind and degree of schema flexibility you want, for particular document parts or whole documents.
The values of a JSON column can either be embedded in documents supported by a duality view, as the values of fields declared in the view definition, or merged into an existing document object by simply including them in the object when a document is inserted or updated.
Embedding values from a JSON
-type column into a
document is the same as embedding values from a column of another type, except that
there's no conversion from SQL type to JSON. The value of a field embedded from a
JSON
-type column can be of any JSON-language type, and its type can
be constrained to conform to a JSON schema.
Fields that are merged into a document aren't mapped to individual
columns. Instead, for a given table they're all implicitly mapped to the same
JSON
-type object column, called a flex
column. A flex column thus has data type JSON
(OBJECT)
, and no field is mapped to it in the view definition.
A table underlying a duality view can have both a flex column and nonflex
JSON
-type columns. Fields stored in the flex column are merged into
the document object produced by the table, and fields stored in the nonflex columns are
embedded into that object.
_________________________________________________________
- Embedding Values from JSON Columns into Documents
The value of a field mapped to aJSON
-type column underlying a duality view is embedded, as is, in documents supported by the view. There's no conversion from a SQL value — it's already a JSON value (of any JSON-language type: object, array, string, number,…, by default). - Merging Fields from JSON Flex Columns into Documents
A duality-view flex column stores (in an underlying table) JSON objects whose fields aren't predefined: they're not mapped individually to specific underlying columns. Unrecognized fields of an object in a document you insert or update are automatically added to the flex column for that object's underlying table. - When To Use JSON-Type Columns for a Duality View
Whether to store some of the data underlying a duality view asJSON
data type and, if so, whether to enforce its structure and typing, are design choices to consider when defining a JSON-relational duality view. - Flex Columns, Beyond the Basics
All about duality-view flex columns: rules of the road; when, where, and why to use them; field-name conflicts; gotchas.
Related Topics