Giter Club home page Giter Club logo

rapidrepo's Introduction

RapidRepo

RapidRepo is a Vapor framework that provides structure to assist users setting up a modified version of the repository pattern within their own apps.

Installation

Architecture

A set of DataSources are established by the app owner that all models submitted to the repository must adapt to. For example, if the app owner wishes to use a Redis cache, a Postgres database, and and use mock data for testing, each model will have to set up adaptors for each of those data sources.

Usage

You can set up your models just like you would normally.

import Vapor
import FluentSQLite

struct Dog: SQLiteModel, Content {
    var id: Int? 
    var name: String
    var age: Int
}

You can then establish your data sources by setting up an AdapterSet. The AdapterSet represents the set of Adapters that all models will need to have.

This AdapterSet establishes the requirement for database and mock adapters

struct AppAdapterSet<ModelType: SQLiteModel, DBAdapterType: SQLiteAdapter, MockAdapterType: MockAdapter>: AdapterSet {
    let dbAdapater: DBAdapterType
    let mockAdapter: MockAdapterType
}

You can set up a protocol with all the functionality you want all Dog adapters to have.

protocol DogAdapterProtocol: ModelDataSourceAdapter where ModelType == Dog {
    func get(id: ModelType.ID, on worker: Worker) throws -> Future<Dog?>
}

And then go ahead and set up your database and mock datasource adapters.

struct DogSQLiteAdapter: DogAdapterProtocol, SQLiteAdapter {
    func get(id: Dog.ID, on worker: Worker) throws -> EventLoopFuture<Dog?> {
        return try DataSourceType.DatabaseType().newConnection(on: worker).flatMap { (conn) in
            return ModelType.find(id, on: conn)
        }
    }
}
struct DogMockAdapter: DogAdapterProtocol, MockAdapter {
    func get(id: Dog.ID, on worker: Worker) throws -> EventLoopFuture<Dog?> {
        return worker.future(Dog(id: id, name: "Rex", age: 5))
    }
}

Now onto registering all that.

The AdapterSet types get pretty long so it'll be good to have a typealias

typealias DogAdapterSet = AppAdapterSet<Dog, DogSQLiteAdapter, DogMockAdapter>

Then within configure.swift

let repo = Repository()
let dbDogAdapter = DogSQLiteAdapter()
let mockDogAdapter = DogMockAdapter()

let adapterSet = DogAdapterSet(dbAdapater: dbDogAdapter, mockAdapter: mockDogAdapter)

repo.register(adapterSet: adapterSet)

services.register(repo, as: Repository.self)

and finally you can use your repository.

Within routes.swift:

router.get("repoTest") { req -> Future<Dog> in
    let dogMocks = try req.make(Repository.self).make(DogAdapterSet.self).mockAdapter

    return try dogMocks.get(id: 20, on: req).unwrap(or: Abort(.notFound))
}

rapidrepo's People

Contributors

twof avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 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.