Giter VIP home page Giter VIP logo

datastorage's Introduction

DataStorage

This library is created with the intention of hiding and abstracting the database serialization to the rest of programmers to make them easier their job on the moment to use a database.

At the moment the library supports Mysql and MongoDB but is intended to add more possibilities in the future.

Entity

To work with the library the first step is to create an entity to define its fields, so then it can be parsed and saved properly. To do that you have to use annotations of package es.brouse.datastorage.annotations.

  • @Entity Define how the entity will be represented on the database with fields like:

    • Name Default class name.
  • @EntityField Define how each field will be represented on the database with fields like:

    • Name Default field name.
    • Size Size on the database.
    • NotNull Indicates if the field can be null
    • Unique Indicates if the field must be unique
  • @EntityIdentifier Defines which will be the identifier of the table, this will mean that the field associated to it will have the following properties:

    • Unique: true
    • NotNull: true

NOTE: The entity must have only one field annotated with @EntityIdentifier and this field will also have to contain the @EntityField annotation.

This is an example of a valid Entity:

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Entity(name = "TestEntity")
@AllArgsConstructor
public class TestEntity {

    @EntityIdentifier
    @EntityField(name = "identifier", size = 50, unique = true)
    @Getter
    public String identifier;

    @EntityField
    @Getter @Setter public int value;
}

Storage

The library has the possibility to be customized by the programmer adding new Storage methods. This is done creating implementing the Storage<T> interface. And the registering it on the library.

//Get the DataStorage singleton instance
DataStorage dataStorage = DataStorage.getInstance();

//Get the StorageManager to register the new Storage<T>
StorageSelector storageSelector = dataStorage.getStorageSelector();
storageSelector.register("custom_name", MyCustomStorage.class);

Otherwise, if you want to use the provided storage methods you will have to get a DataStorage instance and get the correct instance providing the correct serializable entity as an argument.

//Get the DataStorage singleton instance
DataStorage dataStorage = DataStorage.getInstance();

//Get the StorageManager to register the new Storage<T>
StorageSelector storageSelector = dataStorage.getStorageSelector();

//Specify mysql (Default) / mongodb
storageSelector.setActive("name");

//Use the serializer to store
Storage<TestEntity> activeStorage = storageSelector.getActiveStorage(TestEntity.class);

CRUD Operations

When you have the correct instance of the storage that you want to use. The only step left is to specify the operation to perform. NOTE: As arguments it needs a Collection<?> the best option is to use Sets to avoid duplicate keys.

//Insert
activeStorage.insert(Sets.newHashSet(new TestEntity("test", 10), new TestEntity("test1", 100)));

//Read
activeStorage.read(Sets.newHashSet("identifier1"));
activeStorage.read(1, 2);

//Update (You mustn't modify id of the entity)
activeStorage.update(Sets.newHashSet(new TestEntity("test", 100)));

//Delete
activeStorage.delete(Sets.newHashSet("identifier1", "identifier2"));

datastorage's People

Contributors

brouse13 avatar

Watchers

 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.