Git Product home page Git Product logo

crud-store's Introduction

CRUD Store

A collection of CRUD components implementing a clean interface

license

What is it?

I used to write these crud stores several times across use-cases, decided to create a simple interface, and some default abstractions of popular databases that implements the interface.
I've been able to add the abstraction for aerospike. Impls for other databases as contributions are welcome.

Features

The interface

The following are the CRUD ops in the Store interface (pretty self-explanatory)

    void create(final T item);
    void update(final T item);
    void delete(final String id);
    Optional<T> get(final String id);
    Map<String, T> get(final List<String> ids);
    List<T> list();

An extension to this has been added (since 1.2.5) ReferenceExtendedStore, to support fetch by references through indexes if a Store is able to support it.

    void create(final T item, final List<String> refIds);
    List<T> getByRefId(final String refId);

Cached Store

Wraps your store with a Caffine in-memory cache. Use this to quickly have a cache on top of your store
Usage:

Store<MyData> store=new CachingStore(
        myAerospikeStore,
        1000, // maxsize of cache
        10,   // expire after write in seconds
        5,    // refresh after write in seconds 
        );

Aerospike Store

This impl provides crud for your data by storing it in a default set (data), hides details of serialization/de-serserialization, provides hook for proper error handling.

It also supports indexing on references, as it implements a ReferenceExtendedStore. So if you have data that needs to be retrieved through some reference ids, you can do so in an optimal manner. Internally, a LIST s-index is created, and an Aerospike query on the index is fired during retrieval

Usage:

public class MyAerospikeStore extends AerospikeStore<TestData> {

    protected MyAerospikeStore(final IAerospikeClient client,
                               final NamespaceSet namespaceSet,
                               final ObjectMapper mapper,
                               final Class<TestData> clazz,
                               final ErrorHandler<TestData> errorHandler) {
        super(client, namespaceSet, mapper, clazz, errorHandler);
    }

    @Override
    protected boolean isValidDataItem(final TestData userData) {
        return true; // you may do additional checks here
    }
}

Your TestData class can look something like this

@Builder
public record TestData(
        String id, 
        String name, 
        int age) implements Id {
}

That is it, you should be good to go to do the following

public class Logic {
    private AerospikeStore<TestData> store
            = new MyAerospikeStore(
                        AerospikeClientHelpers.aerospikeClient(AerospikeConfiguration.builder()
                                                           .hosts("...") // set the rest
                                                           .build()), new NamespaceSet("test", "test-set"),
                        mapper, 
                        TestData.class, 
                        new DefaultErrorHandler<>());

    void myOperations() {
        store.create(new TestData("Id001", "Rick", 47));
        store.create(new TestData("Id001", "Rick", 47), List.of("animated"));
        store.update(new TestData("Id001", "Rick Sanchez", 48));
        Optional<TestData> result = store.get("Id001");
        List<TestData> result = store.getByRefId("animated");
        List<TestData> result = store.get(List.of("Id001", "Id002"));
        List<TestData> result = store.list();
        store.delete("Id001");
    }
}

How to use it

Maven Dependency

The following can be added in your dependencies, for the aerospike crud-store.

<dependency>
    <groupId>com.livetheoogway.crudstore</groupId>
    <artifactId>aerospike-crud-store</artifactId>
    <version>${crudstore.version}</version> <!--look for the latest version on top-->
</dependency>

Tech Dependencies

  • Java 17
  • Aerospike (any provided version should do)

Contributions

Please raise Issues, Bugs or any feature requests at Github Issues .
If you plan on contributing to the code, fork the repository and raise a Pull Request back here.

crud-store's People

Contributors

tushar-naik avatar tilaksn avatar github-actions[bot] 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.