surrealdb / surrealdb.java

SurrealDB SDK for Java
https://surrealdb.com
Apache License 2.0
67 stars 21 forks source link

Initial commit for SDK With Repository pattern #83

Closed Mukund2900 closed 6 months ago

Mukund2900 commented 6 months ago

82

I raised this feature request a couple of days back. As you guys mentioned that you are open to ideas, here is an approach which can help spring developers to easily integrate surrealdb with their existing system.

I have created an implementation which will expect the following to use repository pattern (The library still works as it is if this is not required) ->

  1. Configuration through application.properties (else it will take the default values, will add id and password as well) :

surrealdb.host=localhost surrealdb.port=8000 surrealdb.useSsl=false surrealdb.nameSpace=test surrealdb.database=test

  1. Entity Annotation with @SurrealTable and @SurrealId: Entities can now be annotated to define document/table mappings and identifiers, simplifying the mapping process. For example:
@SurrealTable("parent_route")
@AllArgsConstructor
@Data
@NoArgsConstructor
public class ParentRoute {

    @SurrealId
    private String id;
    private Long validFrom;
    private Long validTo;
    private String client;
    private String rId;

 } 
  1. CRUD Operations through SurrealCrudRepository: Entities can extend the SurrealCrudRepository interface to enable CRUD operations, streamlining data manipulation tasks:
@Repository
public interface ParentRouteRepository extends SurrealCrudRepository<ParentRoute,String> {
}
  1. Custom Queries with @SurrealQuery: Similar to JPA's @Query annotation, @SurrealQuery enables the execution of custom queries, providing flexibility in data retrieval:
@Repository
public interface ParentRouteRepository extends SurrealCrudRepository<ParentRoute,String> {

    @SurrealQuery("select * from parent_route where client = ?1 and rId = ?2 ;")
    ParentRoute findByRouteIdAndBuid(String client, String rId);

    @SurrealQuery("select *, ->hasData->(r_data where time = ?1 ).*->hasVehicle->vehicle_detail.* as vehicle from parent_route where client = ?2 and rId = ?3 ;")
    List<ParentRoutePrj> findShuttleRouteByStartTimeAndBuidAndRouteId(long time, String client, String rId);

}

Extra feature in first cut -> If we want a field to have a sequence we can use @SurrealSequenceId(sequenceName = "rIdSequence") : This will give that field a numeric sequence (with string-type fields being appropriately casted).

phughk commented 6 months ago

Closing this PR as we have agreed with @Mukund2900 it is better in a separate repository

Mukund2900 commented 6 months ago

I have created a seperate repo for this.

naisofly commented 6 months ago

Thanks @Mukund2900 ! Feel free to contribute to the Awesome Surreal repo, perhaps under Client Libraries ?