GithubHelp home page GithubHelp logo

biggates / spring-data-mongodb-datatables Goto Github PK

View Code? Open in Web Editor NEW
6.0 4.0 3.0 188 KB

datatables binding for spring-data-mongodb

Java 82.51% HTML 7.09% JavaScript 10.40%
datatables mongodb spring-data-mongodb

spring-data-mongodb-datatables's Introduction

spring-data-mongodb-datatables

Datatables binding for spring-data-mongodb.

This is a sample project showing how it works.

This project is inspired from darrachequesne/spring-data-jpa-datatables, which works with spring-data-jpa.

Deprecation

Because Damien now has darrachequesne/spring-data-mongodb-datatables released, and this project has its own modification on DataTablesInput, I think it is time to mark it as @Deprecated now.

As Damien's implementation does not implement Aggregation, this repository will remain open , but will NOT be maintained anymore.

If you need similar features (customizable query with spring-data) but don't use DataTables, my new project eaphone-spring-data-query will be another choice.

Usage

Basic usage is the same with darrachequesne/spring-data-jpa-datatables

Introduce into project

TODO Not uploaded to any public Maven Repository yet.

<dependency>
    <groupId>com.eaphone</groupId>
    <artifactId>spring-data-mongodb-datatables</artifactId>
    <version>0.3.2-SNAPSHOT</version>
</dependency>

Initialization

In any @Configuration class, add:

@EnableMongoRepositories(repositoryFactoryBeanClass = DataTablesRepositoryFactoryBean.class)

Write new Repo

Just as spring-data-mongodb does:

@Repository
public interface UserRepository extends DataTablesRepository<Order, String> {
}

Note that DataTablesRepository extends PagingAndSortingRepository so it already contains functionalities like findAll(Pageable) and save().

Expose fields on view

@Data
@Document(collection = "order")
public class Order {

    @Id
    @JsonView(DataTablesOutput.View.class)
    private String id;

    @JsonFormat(pattern = "yyyy-MM-dd")
    @JsonView(DataTablesOutput.View.class)
    private Date date;

    @JsonView(DataTablesOutput.View.class)
    private String orderNumber;

    @JsonView(DataTablesOutput.View.class)
    private boolean isValid;

    @JsonView(DataTablesOutput.View.class)
    private int amount;

    @JsonView(DataTablesOutput.View.class)
    private double price;
}

On the browser side

Include jquery.spring-friendly.js so column[0][data] is changed to column[0].data and is correctly parsed by SpringMVC.

On the Server Side

The repository has the following methods:

  • Using Query
    • DataTablesOutput<T> findAll(DataTablesInput input);
    • DataTablesOutput<T> findAll(DataTablesInput input, Criteria additionalCriteria);
    • DataTablesOutput<T> findAll(DataTablesInput input, Criteria additionalCriteria, Criteria preFilteringCriteria);
  • Using Aggregation
    • <View> DataTablesOutput<View> findAll(Class<View> classOfView, DataTablesInput input, AggregationOperation... operations);
    • <View> DataTablesOutput<View> findAll(Class<View> classOfView, DataTablesInput input, Collection<? extends AggregationOperation> operations);

Examples

@GetMapping("/data/orders")
public DataTablesOutput<Order> getOrders(@Valid DataTablesInput input) {
    return repo.findAll(input);
}

Or:

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;

@GetMapping("/")
public DataTablesOutput<DataView> getAll(@Valid DataTablesInput input) {
    return repo.findAll(DataView.class,
        input,
        // just provide your aggregation pipeline here
        lookup("user", "userId", "id", "user"),
        unwind("user"),
        project()
            .and("user.sex").as("user.gender")
            .andInclude(
                "timestamp", "createTime", "sensorType",
                "batchId", "source", "beginTime",
                "endTime", "text", "url", "value")
            );
}

Filter

In addition to DataTables' columns[x].search parameters, columns[x].filter is a new way to define more complex queries.

A more detailed document in Simplified Chinese (zh_CN) is provided here.

Future Plans

In the near future:

  • In Criteria converting, more types (Date) must be handled, as currently only String and Boolean are handled
  • More tests (and verifications)

Known Issues

  • $match, $sum: 1, $limit and $skip are attached to given aggregation pipeline so in some cases the logic may be broken.
  • Text search is simply converted to Regular Expressions with Literal flag and may contain some logical flaws.
  • Global search is NOT implementd yet (as discussed in #1).
  • Querydsl support is REMOVED, as my own project does not use it.

spring-data-mongodb-datatables's People

Contributors

biggates avatar starboyate avatar windseeker2011 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.