https://github.com/marthym/entity
Entity is a small library that handles the notion of model entity.
https://github.com/marthym/entity
java java-17 library
Last synced: 10 months ago
JSON representation
Entity is a small library that handles the notion of model entity.
- Host: GitHub
- URL: https://github.com/marthym/entity
- Owner: Marthym
- License: mit
- Created: 2024-01-21T10:53:36.000Z (almost 2 years ago)
- Default Branch: develop
- Last Pushed: 2024-11-25T11:11:52.000Z (about 1 year ago)
- Last Synced: 2025-01-20T15:56:29.614Z (12 months ago)
- Topics: java, java-17, library
- Language: Java
- Homepage: https://marthym.github.io/entity/
- Size: 144 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# Entity [](https://GitHub.com/Marthym/entity/releases/) [](https://github.com/Marthym/entity/blob/master/LICENSE)
[](https://sonarcloud.io/dashboard?id=Marthym_entity)
[](https://sonarcloud.io/dashboard?id=Marthym_entity)
[](https://sonarcloud.io/dashboard?id=Marthym_entity)

**Entity** is a small library that handles the notion of model entity. **Entity** offers an elegant way of working with
business objects that may or may not have an identifier.
The entity can hold, or not, some metadata.
For example ID of the user who creates the entity, the date of creation, the last update, ...
This metadata was embedded through an EnumMap, the most efficient structure to store unknown number of key/value pairs.
The jackson package offers a module for serializing and deserializing these entities in a flat manner.
## Installation
Use the package manager [maven](https://maven.apache.org/) to install entity.
```xml
fr.ght1pc9kc
entity-api
VERSION
fr.ght1pc9kc
entity-jackson
VERSION
fr.ght1pc9kc
entity-graphql
VERSION
```
for gradle
```groovy
compile "fr.ght1pc9kc:entity-api:VERSION"
compile "fr.ght1pc9kc:entity-jackson:VERSION"
compile "fr.ght1pc9kc:entity-graphql:VERSION"
```
## Usage
### Create an entity
```java
import fr.ght1pc9kc.entity.api.Entity;
import java.time.Instant;
import static fr.ght1pc9kc.demo.DefaultMeta.createdBy;
import static fr.ght1pc9kc.demo.DefaultMeta.createdAt;
Entity basic = Entity.identify("May the force")
.withId("4TH");
Entity simple = Entity.identify("May the force")
.meta(createdBy, "Yoda")
.meta(createdAt, Instant.now())
.withId("4TH");
Entity verySimple = Entity.identify("May the force").withId("4TH");
Entity POLYMORPHIC_ENTITY = Entity
.identify(new LightSaber(Color.GREEN, 1))
.meta(createdAt, Instant.parse("2024-01-20T15:06:42.546Z"))
.meta(createdBy, "okenobi")
.withId("LIGHTSABER");
```
### Include Jackson module
```java
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import fr.ght1pc9kc.entity.jackson.EntityModule;
ObjectMapper mapper = new ObjectMapper()
.registerModule(new JavaTimeModule())
.registerModule(new EntityModule())
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
```
Using `JavaTimeModule` is mandatory to allow serialize `Instant`, `WRITE_DATES_AS_TIMESTAMPS` is optional but provide
more beautiful json.
```json
{
"_id": "LIGHTSABER",
"_createdAt": "2024-01-20T15:06:42.546Z",
"_createdBy": "okenobi",
"@type": "LIGHT",
"blade": 1,
"color": "GREEN"
}
```
### Use GrapQL DataFetcher module
In Spring Boot, create a configuration classe into your module
```java
@Slf4j
@Configuration
public class MyObjectEntityConfiguration {
@Bean
public RuntimeWiringConfigurer customRuntimeWiringConfigurer(ObjectMapper mapper) {
// Create the DataFetcher by passing the configured Jackson Object Mapper
DataFetcher dataFetcher = new EntityDataFetcher(mapper);
// Create a TypeResolver which detect Entity object
TypeResolver typeResolver = new EntityTypeResolver();
// customize WiringConfigurer to map possible Entity self object to use EntityDataFetcher
return wiringBuilder -> wiringBuilder
.type(MyObject.class.getSimpleName(), builder -> builder.defaultDataFetcher(dataFetcher).typeResolver(typeResolver));
}
}
```
## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
## License
[MIT](https://choosealicense.com/licenses/mit/)