Skip to content

Commit 9186470

Browse files
authored
Full example with Sping Boot + rewrittent parts + added tests (#1)
1 parent 98f0166 commit 9186470

File tree

59 files changed

+2255
-1097
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2255
-1097
lines changed

README.md

Lines changed: 176 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ This project is a [*jsonschema2pojo*](https://github.com/joelittlejohn/jsonschem
1616
At the schema of an object level, it is possible to define a POJO as being a Couchbase document using the custom JSON
1717
property `x-cb-document`.
1818

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

@@ -28,7 +29,6 @@ E.g., this schema:
2829
{
2930
"title": "Sample entity",
3031
"type": "object",
31-
"x-cb-document": true,
3232
"properties": {
3333
"..."
3434
}
@@ -67,6 +67,62 @@ public class Entity {
6767
}
6868
```
6969

70+
#### Composite indexes
71+
72+
A sub-property `compositeIndexes` is available to declare indexes that will can be auto-generated by Spring
73+
Data Couchbase via the [`CompositeQueryIndex`](https://docs.spring.io/spring-data/couchbase/docs/current/api/org/springframework/data/couchbase/core/index/CompositeQueryIndex.html)
74+
annotation and its parameters (detailed in the annotation's documentation).
75+
76+
E.g., this schema:
77+
```json
78+
{
79+
"title": "Sample entity",
80+
"type": "object",
81+
"x-cb-document": {
82+
"compositeIndexes": [
83+
{
84+
"fields": [
85+
"field1",
86+
"field12"
87+
]
88+
},
89+
{
90+
"fields": [
91+
"field2",
92+
"field3",
93+
"field4"
94+
],
95+
"name": "idx_fields"
96+
}
97+
]
98+
},
99+
"properties": {
100+
"..."
101+
}
102+
}
103+
```
104+
Will produce:
105+
```java
106+
@Document(expiry = 2, expiryUnit = TimeUnit.MINUTES, touchOnRead = true)
107+
@CompositeQueryIndex(fields = {
108+
"field1",
109+
"field2"
110+
})
111+
@CompositeQueryIndex(fields = {
112+
"field3",
113+
"field4",
114+
"field5"
115+
}, name = "idx_fields")
116+
public class Entity {
117+
...
118+
}
119+
```
120+
121+
#### Exclude a POJO from Couchbase related elements
122+
123+
If `x-cb-document` is `false`, Couchbase related elements will be skipped for the generated class, also for its fields
124+
and its sub-objects.
125+
70126
### Document id
71127

72128
At the property of an object level, it is possible to define a field as being a document id using the custom JSON
@@ -88,7 +144,6 @@ E.g., this schema:
88144
"id": {
89145
"title": "Entity id",
90146
"type": "string",
91-
"format": "uuid",
92147
"x-cb-id": true
93148
},
94149
"..."
@@ -98,9 +153,11 @@ E.g., this schema:
98153
Will produce:
99154
```java
100155
@Id
101-
private UUID id;
156+
private String id;
102157
```
103158

159+
#### Generated id
160+
104161
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)
105162
annotation and its parameters (detailed in the annotation's documentation).
106163

@@ -138,7 +195,6 @@ And this schema:
138195
"id": {
139196
"title": "Entity id",
140197
"type": "string",
141-
"format": "uuid",
142198
"x-cb-id": {
143199
"generated": {
144200
"delimiter": "::",
@@ -253,13 +309,10 @@ Will produce:
253309
private String field;
254310
```
255311

256-
Some sub-properties are available to manage the generating of the annotations targeted at building the document id using the field value, and their parameters (detailed in the annotation's documentation).
257-
* `idPrefix` for the [`IdPrefix`](https://docs.spring.io/spring-data/couchbase/docs/current/api/org/springframework/data/couchbase/core/mapping/id/IdPrefix.html)
258-
annotation.
259-
* `idAttribute` for the [`IdAttribute`](https://docs.spring.io/spring-data/couchbase/docs/current/api/org/springframework/data/couchbase/core/mapping/id/IdAttribute.html)
260-
annotation.
261-
* `idSuffix` for the [`IdSuffix`](https://docs.spring.io/spring-data/couchbase/docs/current/api/org/springframework/data/couchbase/core/mapping/id/IdSuffix.html)
262-
annotation.
312+
#### Use field for id generation
313+
314+
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)
315+
annotation and its parameters (detailed in the annotation's documentation).
263316

264317
E.g., this schema:
265318
```json
@@ -271,7 +324,7 @@ E.g., this schema:
271324
"title": "A field",
272325
"type": "string",
273326
"x-cb-field": {
274-
"idPrefix": true
327+
"idAttribute": true
275328
}
276329
},
277330
"..."
@@ -281,7 +334,7 @@ E.g., this schema:
281334
Will produce:
282335
```java
283336
@Field
284-
@IdPrefix
337+
@IdAttribute
285338
private String field;
286339
```
287340
And this schema:
@@ -310,6 +363,115 @@ Will produce:
310363
private String field;
311364
```
312365

366+
### Non persisted fields for key generation
367+
368+
At property level, two JSON properties are available to use fields in code for key generation without persisting them,
369+
via the following annotations and their parameters (detailed in the annotation's documentation).
370+
* `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)
371+
annotation.
372+
* `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)
373+
annotation.
374+
375+
E.g., this schema:
376+
```json
377+
{
378+
"..."
379+
"properties": {
380+
"..."
381+
"field": {
382+
"title": "A field",
383+
"type": "string",
384+
"x-cb-idPrefix": true
385+
},
386+
"..."
387+
}
388+
}
389+
```
390+
Will produce:
391+
```java
392+
@IdPrefix
393+
private String field;
394+
```
395+
And this schema:
396+
```json
397+
{
398+
"..."
399+
"properties": {
400+
"..."
401+
"field": {
402+
"title": "A field",
403+
"type": "string",
404+
"x-cb-idSuffix": {
405+
"order": 1
406+
}
407+
},
408+
"..."
409+
}
410+
}
411+
```
412+
Will produce:
413+
```java
414+
@Field
415+
@IdSuffix(order = 1)
416+
private String field;
417+
```
418+
419+
#### Indexes
420+
421+
A sub-property `index` is available to declare indexes that will can be auto-generated by Spring
422+
Data Couchbase via the [`QueryIndexed`](https://docs.spring.io/spring-data/couchbase/docs/current/api/org/springframework/data/couchbase/core/index/QueryIndexed.html)
423+
annotation and its parameters (detailed in the annotation's documentation).
424+
425+
E.g., this schema:
426+
```json
427+
{
428+
"..."
429+
"properties": {
430+
"..."
431+
"field": {
432+
"title": "A field",
433+
"type": "string",
434+
"x-cb-field": {
435+
"index": true
436+
}
437+
},
438+
"..."
439+
}
440+
}
441+
```
442+
Will produce:
443+
```java
444+
@Field
445+
@QueryIndexed
446+
private String field;
447+
```
448+
And this schema:
449+
```json
450+
{
451+
"..."
452+
"properties": {
453+
"..."
454+
"field": {
455+
"title": "A field",
456+
"type": "string",
457+
"x-cb-field": {
458+
"index": {
459+
"direction": "ASCENDING",
460+
"name": "idx_sample"
461+
}
462+
}
463+
},
464+
"..."
465+
}
466+
}
467+
```
468+
Will produce:
469+
```java
470+
@Field
471+
@QueryIndexed(direction = QueryIndexDirection.ASCENDING, name = "idx_sample")
472+
private String field;
473+
```
474+
313475
## Maven configuration
314476

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

example/docker/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
COMPOSE_PROJECT_NAME=js2p-spring-cb-example

example/docker/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM couchbase:community-6.6.0
2+
3+
COPY configure-node.sh /opt/couchbase
4+
5+
RUN ["chmod", "+x", "/opt/couchbase/configure-node.sh"]
6+
7+
CMD ["/opt/couchbase/configure-node.sh"]

example/docker/configure-node.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
3+
set -x
4+
set -m
5+
6+
/entrypoint.sh couchbase-server &
7+
8+
while [ "$(curl -Isw '%{http_code}' -o /dev/null http://localhost:8091/ui/index.html#/)" != 200 ]; do
9+
sleep 5
10+
done
11+
12+
# Setup cluster
13+
couchbase-cli cluster-init \
14+
--cluster 127.0.0.1:8091 \
15+
--services data,query,index \
16+
--cluster-ramsize 256 \
17+
--cluster-index-ramsize 256 \
18+
--cluster-username Administrator \
19+
--cluster-password password
20+
21+
# Setup bucket
22+
couchbase-cli bucket-create \
23+
--cluster 127.0.0.1:8091 \
24+
--username Administrator \
25+
--password password \
26+
--bucket example \
27+
--bucket-type couchbase \
28+
--bucket-ramsize 256
29+
30+
# Setup user
31+
couchbase-cli user-manage \
32+
--cluster 127.0.0.1:8091 \
33+
--username Administrator \
34+
--password password \
35+
--set \
36+
--rbac-username example \
37+
--rbac-password password \
38+
--roles bucket_full_access[example] \
39+
--auth-domain local
40+
41+
echo "Type: $TYPE"
42+
43+
if [ "$TYPE" = "WORKER" ]; then
44+
echo "Sleeping ..."
45+
sleep 15
46+
47+
#IP=`hostname -s`
48+
IP=$(hostname -I | cut -d ' ' -f1)
49+
echo "IP: " $IP
50+
51+
echo "Auto Rebalance: $AUTO_REBALANCE"
52+
if [ "$AUTO_REBALANCE" = "true" ]; then
53+
couchbase-cli rebalance --cluster=$COUCHBASE_MASTER:8091 --user=Administrator --password=password --server-add=$IP --server-add-username=Administrator --server-add-password=password
54+
else
55+
couchbase-cli server-add --cluster=$COUCHBASE_MASTER:8091 --user=Administrator --password=password --server-add=$IP --server-add-username=Administrator --server-add-password=password
56+
fi
57+
fi
58+
59+
fg 1

example/docker/docker-compose.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: '3.3'
2+
3+
services:
4+
couchbase:
5+
build: .
6+
ports:
7+
- "8091-8094:8091-8094"
8+
- "11207:11207"
9+
- "11210-11211:11210-11211"
10+
- "18091-18093:18091-18093"

0 commit comments

Comments
 (0)