Coder Social home page Coder Social logo

0xleif / datastore Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 26 KB

An extendable data storage solution with built-in caching capabilities.

Home Page: https://datastore.0xl.io/

License: MIT License

Swift 100.00%
cache datastore ios loading macos observableobject service storing swift tvos watchos

datastore's Introduction

DataStore

An extendable data storage solution with built-in caching capabilities.

What is DataStore?

DataStore is a generic data storage and caching library for Swift. It provides a convenient way to load, cache, and store data in your apps. The DataStore class, by default, utilizes a cache to improve data loading performance. However, it can be subclassed to support other data storage mechanisms while still benefiting from the caching capabilities.

Features

  • Data Loading: Easily load data from various sources, such as network requests or local databases, by providing a custom DataLoading object.
  • Caching: By default, DataStore integrates a caching mechanism to enhance data loading performance. DataStore uses Cache as its caching solution.
  • Flexible Data Retrieval: Access loaded data via the fetch() method or fetch data based on specific criteria using closure-based filtering.
  • Data Storage: Store new data objects into the DataStore for later retrieval.
  • Data Deletion: Remove previously stored data objects from the DataStore.

Installation

Swift Package Manager (SPM)

Add the following line to your Package.swift file in the dependencies array:

dependencies: [
    .package(url: "https://github.com/0xLeif/DataStore.git", from: "0.1.0")
]

Usage

Creating a DataStore

To create a DataStore, you need to provide a DataLoading object. The DataLoading object encapsulates the logic to load data from a specific source, such as a network request or local database. You can easily subclass DataStore to implement your own data storage mechanism, while still utilizing the caching capabilities.

let store = DataStore(loader: YourDataLoader())

Loading Data

You can load data into the DataStore using the load() method. This will asynchronously load the data using the provided data loader.

try await store.load()

You can also load data with a specific identifier using the load(id:) method:

let expectedID = "some-id"
try await store.load(id: expectedID)

Fetching Data

You can fetch the loaded data from the DataStore using the fetch() method. This will return an array of the loaded data objects.

let values = store.fetch()

You can also fetch data with a specific identifier using the fetch(id:) method:

let expectedID = "some-id"
let value = try store.fetch(id: expectedID)

Additionally, you can fetch data that matches specific conditions using the fetch(where:) method with a closure:

let values = store.fetch { data in
    // Condition to filter the data, e.g. data.color == .red
}

Storing Data

You can store new data in the DataStore using the store(data:) method. This will add the provided data objects to the store.

try store.store(
    data: [
        YourDataObject(
            // Provide values for your data object properties
        )
    ]
)

Deleting Data

You can delete previously stored data from the DataStore using the delete(data:) method. This will remove the data objects with the specified identifiers from the store.

Example

Here are some usage examples to demonstrate how to use the DataStore library:

// Example 1: Loading and fetching data
let store = DataStore(loader: YourDataLoader())
try await store.load()
let values = store.fetch()

// Example 2: Storing and fetching data
let expectedID = "some-id"
try store.store(
    data: [
        YourDataObject(
            id: expectedID,
            // Provide values for your data object properties
        )
    ]
)
let value = try store.fetch(id: expectedID)

// Example 3: Deleting data
try store.delete(data: [value.id])

Contributing

Contributions are welcome! If you encounter any issues, have feature requests, or want to contribute code improvements, please feel free to open an issue or submit a pull request.

License

DataStore is released under the MIT License. See LICENSE for details.

datastore's People

Contributors

0xleif avatar

Stargazers

 avatar  avatar

Watchers

 avatar

datastore's Issues

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.