Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hectorbst/jsonschema2pojo-spring-data-couchbase

jsonschema2pojo extension to generate Spring Data Couchbase specific POJOs
https://github.com/hectorbst/jsonschema2pojo-spring-data-couchbase

couchbase couchbase-document generator java json json-properties jsonschema jsonschema2pojo pojo schema spring spring-data spring-data-couchbase springdata

Last synced: about 1 month ago
JSON representation

jsonschema2pojo extension to generate Spring Data Couchbase specific POJOs

Awesome Lists containing this project

README

        

[![Build](https://img.shields.io/github/workflow/status/hectorbst/jsonschema2pojo-spring-data-couchbase/Build?label=Build)](https://github.com/HectorBst/jsonschema2pojo-spring-data-couchbase/actions?query=workflow%3ABuild)
[![Coverage](https://img.shields.io/sonar/coverage/HectorBst_jsonschema2pojo-spring-data-couchbase?server=https%3A%2F%2Fsonarcloud.io&label=Coverage)](https://sonarcloud.io/dashboard?id=HectorBst_jsonschema2pojo-spring-data-couchbase)
[![Violations](https://img.shields.io/sonar/violations/HectorBst_jsonschema2pojo-spring-data-couchbase?server=https%3A%2F%2Fsonarcloud.io&label=Violations)](https://sonarcloud.io/dashboard?id=HectorBst_jsonschema2pojo-spring-data-couchbase)
[![Maven Central](https://img.shields.io/maven-central/v/dev.hctbst/jsonschema2pojo-spring-data-couchbase?label=Maven%20Central)](https://search.maven.org/artifact/dev.hctbst/jsonschema2pojo-spring-data-couchbase)
[![License](https://img.shields.io/github/license/hectorbst/jsonschema2pojo-spring-data-couchbase?label=License)](LICENSE)

# jsonschema2pojo-spring-data-couchbase

This project is a [*jsonschema2pojo*](https://github.com/joelittlejohn/jsonschema2pojo) extension dedicated to
[*Spring Data Couchbase*](https://docs.spring.io/spring-data/couchbase/docs/current/reference/html) entities generation.

## Features

### Couchbase document

At the schema of an object level, it is possible to define a POJO as being a Couchbase document using the custom JSON
property `x-cb-document`.

* If missing, the value of this property is `true` if the schema is at root or if its parent is also a Couchbase
document.
* The `true` value is equivalent to `{}`.
* The schema of the content of this custom property is available [here](src/main/resources/schema/document.json).

This property is responsible for generating the [`Document`](https://docs.spring.io/spring-data/couchbase/docs/current/api/org/springframework/data/couchbase/core/mapping/Document.html)
annotation.

E.g., this schema:
```json
{
"title": "Sample entity",
"type": "object",
"properties": {
"..."
}
}
```
Will produce:
```java
@Document
public class Entity {
...
}
```

Some sub-properties are available to manage the annotation parameters (detailed in the annotation's documentation).

E.g., this schema:
```json
{
"title": "Sample entity",
"type": "object",
"x-cb-document": {
"expiry": 2,
"expiryUnit": "MINUTES",
"touchOnRead": true
},
"properties": {
"..."
}
}
```
Will produce:
```java
@Document(expiry = 2, expiryUnit = TimeUnit.MINUTES, touchOnRead = true)
public class Entity {
...
}
```

#### Composite indexes

A sub-property `compositeIndexes` is available to declare indexes that will can be auto-generated by Spring
Data Couchbase via the [`CompositeQueryIndex`](https://docs.spring.io/spring-data/couchbase/docs/current/api/org/springframework/data/couchbase/core/index/CompositeQueryIndex.html)
annotation and its parameters (detailed in the annotation's documentation).

E.g., this schema:
```json
{
"title": "Sample entity",
"type": "object",
"x-cb-document": {
"compositeIndexes": [
{
"fields": [
"field1",
"field12"
]
},
{
"fields": [
"field2",
"field3",
"field4"
],
"name": "idx_fields"
}
]
},
"properties": {
"..."
}
}
```
Will produce:
```java
@Document(expiry = 2, expiryUnit = TimeUnit.MINUTES, touchOnRead = true)
@CompositeQueryIndex(fields = {
"field1",
"field2"
})
@CompositeQueryIndex(fields = {
"field3",
"field4",
"field5"
}, name = "idx_fields")
public class Entity {
...
}
```

You can find more information on Spring Data Couchbase index creation and how to activate it here [here](https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.repository.indexing).

#### Exclude a POJO from Couchbase related elements

If `x-cb-document` is `false`, Couchbase related elements will be skipped for the generated class, also for its fields
and its sub-objects.

### Document id

At the property of an object level, it is possible to define a field as being a document id using the custom JSON
property `x-cb-id`.

* If missing, the value of this property is `false`.
* The `true` value is equivalent to `{}`.
* The schema of the content of this custom property is available [here](src/main/resources/schema/id.json).

This property is responsible for generating the [`Id`](https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/annotation/Id.html)
annotation.

E.g., this schema:
```json
{
"..."
"properties": {
"..."
"id": {
"title": "Entity id",
"type": "string",
"x-cb-id": true
},
"..."
}
}
```
Will produce:
```java
@Id
private String id;
```

#### Generated id

A sub-property `generated` is available to manage the generating of the [`GeneratedValue`](https://docs.spring.io/spring-data/couchbase/docs/current/api/org/springframework/data/couchbase/core/mapping/id/GeneratedValue.html)
annotation and its parameters (detailed in the annotation's documentation).

E.g., this schema:
```json
{
"..."
"properties": {
"..."
"id": {
"title": "Entity id",
"type": "string",
"format": "uuid",
"x-cb-id": {
"generated": true
}
},
"..."
}
}
```
Will produce:
```java
@Id
@GeneratedValue
private UUID id;
```

And this schema:
```json
{
"..."
"properties": {
"..."
"id": {
"title": "Entity id",
"type": "string",
"x-cb-id": {
"generated": {
"delimiter": "::",
"strategy": "USE_ATTRIBUTES"
}
}
},
"..."
}
}
```
Will produce:
```java
@Id
@GeneratedValue(delimiter = "::", strategy = GenerationStrategy.USE_ATTRIBUTES)
private String id;
```

You can find more information on Spring Data Couchbase key generation [here](https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.autokeygeneration).

### Document CAS

At the property of an object level, it is possible to define a field as being a document CAS (Compare And Swap) using
the custom JSON property `x-cb-cas`.

If missing, the value of this property is `false`.

This property is responsible for generating the [`Version`](https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/annotation/Version.html)
annotation.

Note that the type of a CAS field must be `Long` or `long`. This can be achieved through a `formatTypeMapping` or the
`useLongIntegers` option.

E.g., this schema:
```json
{
"..."
"properties": {
"..."
"cas": {
"title": "Couchbase CAS",
"type": "integer",
"format": "int64",
"x-cb-cas": true
},
"..."
}
}
```
Will produce:
```java
@Version
private Long cas;
```

### Document field

At the property of an object level, it is possible to define a field as being a document id using the custom JSON
property `x-cb-field`.

* If missing and if the field is not already marked as being an id, a cas or a join, the value of this property is
`true`.
* If missing and if the field is already marked as being an id, a cas or a join, the value of this property is
`false`.
* The `true` value is equivalent to `{}`.
* The schema of the content of this custom property is available [here](src/main/resources/schema/field.json).

This property is responsible for generating the [`Field`](https://docs.spring.io/spring-data/couchbase/docs/current/api/org/springframework/data/couchbase/core/mapping/Field.html)
annotation.

E.g., this schema:
```json
{
"..."
"properties": {
"..."
"field": {
"title": "A field",
"type": "string"
},
"..."
}
}
```
Will produce:
```java
@Field
private String field;
```

Some sub-properties are available to manage the annotation parameters (detailed in the annotation's documentation).

E.g., this schema:
```json
{
"..."
"properties": {
"..."
"field": {
"title": "A field",
"type": "string",
"x-cb-field": {
"name": "field_",
"order": 5
}
},
"..."
}
}
```
Will produce:
```java
@Field(name = "field_", order = 5)
private String field;
```

#### Use field for id generation

An `idPrefix` sub-property is available to manage the the [`IdAttribute`](https://docs.spring.io/spring-data/couchbase/docs/current/api/org/springframework/data/couchbase/core/mapping/id/IdAttribute.html)
annotation and its parameters (detailed in the annotation's documentation).

E.g., this schema:
```json
{
"..."
"properties": {
"..."
"field": {
"title": "A field",
"type": "string",
"x-cb-field": {
"idAttribute": true
}
},
"..."
}
}
```
Will produce:
```java
@Field
@IdAttribute
private String field;
```
And this schema:
```json
{
"..."
"properties": {
"..."
"field": {
"title": "A field",
"type": "string",
"x-cb-field": {
"idAttribute": {
"order": 2
}
}
},
"..."
}
}
```
Will produce:
```java
@Field
@IdAttribute(order = 2)
private String field;
```

You can find more information on Spring Data Couchbase key generation [here](https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.autokeygeneration).

#### Indexes

A JSON sub-property `index` is available to declare indexes that will can be auto-generated by Spring
Data Couchbase via the [`QueryIndexed`](https://docs.spring.io/spring-data/couchbase/docs/current/api/org/springframework/data/couchbase/core/index/QueryIndexed.html)
annotation and its parameters (detailed in the annotation's documentation).

E.g., this schema:
```json
{
"..."
"properties": {
"..."
"field": {
"title": "A field",
"type": "string",
"x-cb-field": {
"index": true
}
},
"..."
}
}
```
Will produce:
```java
@Field
@QueryIndexed
private String field;
```
And this schema:
```json
{
"..."
"properties": {
"..."
"field": {
"title": "A field",
"type": "string",
"x-cb-field": {
"index": {
"direction": "ASCENDING",
"name": "idx_sample"
}
}
},
"..."
}
}
```
Will produce:
```java
@Field
@QueryIndexed(direction = QueryIndexDirection.ASCENDING, name = "idx_sample")
private String field;
```

You can find more information on Spring Data Couchbase index creation and how to activate it here [here](https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.repository.indexing).

### Non persisted fields for key generation

At property level, two JSON properties are available to use fields in code for key generation without persisting them,
via the following annotations and their parameters (detailed in the annotation's documentation).
* `x-cb-idPrefix` for the [`IdPrefix`](https://docs.spring.io/spring-data/couchbase/docs/current/api/org/springframework/data/couchbase/core/mapping/id/IdPrefix.html)
annotation.
* `x-cb-id-suffix` for the [`IdSuffix`](https://docs.spring.io/spring-data/couchbase/docs/current/api/org/springframework/data/couchbase/core/mapping/id/IdSuffix.html)
annotation.

E.g., this schema:
```json
{
"..."
"properties": {
"..."
"prefix": {
"title": "A field",
"type": "string",
"x-cb-idPrefix": true
},
"..."
}
}
```
Will produce:
```java
@IdPrefix
private String prefix;
```
And this schema:
```json
{
"..."
"properties": {
"..."
"suffix": {
"title": "A field",
"type": "string",
"x-cb-idSuffix": {
"order": 1
}
},
"..."
}
}
```
Will produce:
```java
@Field
@IdSuffix(order = 1)
private String suffix;
```

You can find more information on Spring Data Couchbase key generation [here](https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.autokeygeneration).

## Maven configuration

Here is an example of how the extension can be added to the jsonschema2pojo Maven plugin.

```xml

org.jsonschema2pojo
jsonschema2pojo-maven-plugin
${jsonschema2pojo.version}



generate


...


dev.hctbst.jsonschema2pojo.springframework.data.couchbase.SpringDataCouchbaseRuleFactory







dev.hctbst
jsonschema2pojo-spring-data-couchbase
${jsonschema2pojo-spring-data-couchbase.version}



org.springframework.boot
spring-boot-starter-data-couchbase
${spring-boot.version}

```

A more complete example is available [here](example).

## License

This project is released under version 2.0 of the [Apache License](https://www.apache.org/licenses/LICENSE-2.0).