Skip to content

Commit

Permalink
Merge pull request #9 from NeedleInAJayStack/feature/documentation
Browse files Browse the repository at this point in the history
Updates documentation & changes name to DataLoader
  • Loading branch information
NeedleInAJayStack authored Jun 24, 2021
2 parents a264c85 + 3b8dd9e commit 3c99c85
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 148 deletions.
50 changes: 34 additions & 16 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## Build generated
## User settings
xcuserdata/

## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout

## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/

## Various settings
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
Expand All @@ -15,15 +21,11 @@ DerivedData/
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/

## Other
*.moved-aside
*.xccheckout
*.xcscmblueprint

## Obj-C/Swift specific
*.hmap

## App packaging
*.ipa
*.dSYM.zip
*.dSYM
Expand All @@ -38,11 +40,13 @@ playground.xcworkspace
# Packages/
# Package.pins
# Package.resolved
.DS_Store
/.build
/Packages
/*.xcodeproj
# *.xcodeproj
#
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
.swiftpm

.build/

# CocoaPods
#
Expand All @@ -51,22 +55,36 @@ playground.xcworkspace
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/
#
# Add this line if you want to avoid checking in source code from the Xcode workspace
# *.xcworkspace

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build
Carthage/Build/

# Accio dependency management
Dependencies/
.accio/

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# It is recommended to not store the screenshots in the git repo.
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output

# Code Injection
#
# After new code Injection tools there's a generated folder /iOSInjectionProject
# https://github.com/johnno1962/injectionforxcode

iOSInjectionProject/
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import PackageDescription

let package = Package(
name: "SwiftDataLoader",
name: "DataLoader",
products: [
.library(name: "SwiftDataLoader", targets: ["SwiftDataLoader"]),
.library(name: "DataLoader", targets: ["DataLoader"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"),
],
targets: [
.target(name: "SwiftDataLoader", dependencies: ["NIO"]),
.testTarget(name: "SwiftDataLoaderTests", dependencies: ["SwiftDataLoader"]),
.target(name: "DataLoader", dependencies: ["NIO"]),
.testTarget(name: "DataLoaderTests", dependencies: ["DataLoader"]),
],
swiftLanguageVersions: [.v5]
)
71 changes: 40 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# SwiftDataLoader
SwiftDataLoader is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.
# DataLoader
DataLoader is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.

This is a Swift version of the Facebook [DataLoader](https://github.com/facebook/dataloader).

[![CircleCI](https://circleci.com/gh/kimdv/SwiftDataLoader.svg?style=svg)](https://circleci.com/gh/kimdv/SwiftDataLoader)
[![codecov](https://codecov.io/gh/kimdv/SwiftDataLoader/branch/master/graph/badge.svg)](https://codecov.io/gh/kimdv/SwiftDataLoader)
[![Swift][swift-badge]][swift-url]
[![License][mit-badge]][mit-url]

## Installation 💻

Update your `Package.swift` file.

```swift
.Package(url: "https://github.com/kimdv/SwiftDataLoader.git", majorVersion: 1)
.Package(url: "https://github.com/GraphQLSwift/DataLoader.git", .upToNextMajor(from: "1.1.0"))
```

## Gettings started 🚀
Expand All @@ -27,27 +27,27 @@ let userLoader = Dataloader<Int, User>(batchLoadFunction: { keys in
})
```
#### Load single key
```
let future1 = try userLoader.load(key: 1, on: req)
let future2 = try userLoader.load(key: 2, on: req)
let future3 = try userLoader.load(key: 1, on: req)
```swift
let future1 = try userLoader.load(key: 1, on: eventLoopGroup)
let future2 = try userLoader.load(key: 2, on: eventLoopGroup)
let future3 = try userLoader.load(key: 1, on: eventLoopGroup)
```

Now there is only one thing left and that is to dispathc it `try userLoader.dispatchQueue(on: req.eventLoop)`

The example above will only fetch two users, because the user with key `1` is present twice in the list.

#### Load multiple keys
There is also an API to load multiple keys at once
```
try userLoader.loadMany(keys: [1, 2, 3], on: req.eventLoop)
There is also a method to load multiple keys at once
```swift
try userLoader.loadMany(keys: [1, 2, 3], on: eventLoopGroup)
```

### Disable batching
It is also possible to disable batching `DataLoaderOptions(batchingEnabled: false)`
It will invoke `batchLoadFunction` with a single key
#### Disable batching
It is possible to disable batching `DataLoaderOptions(batchingEnabled: false)`
It will invoke `batchLoadFunction` immediately whenever any key is loaded

### Chaching
### Caching

DataLoader provides a memoization cache for all loads which occur in a single
request to your application. After `.load()` is called once with a given key,
Expand All @@ -58,8 +58,8 @@ also creates fewer objects which may relieve memory pressure on your application

```swift
let userLoader = DataLoader<Int, Int>(...)
let future1 = userLoader.load(1)
let future2 = userLoader.load(1)
let future1 = userLoader.load(key: 1, on: eventLoopGroup)
let future2 = userLoader.load(key: 1, on: eventLoopGroup)
assert(future1 === future2)
```

Expand Down Expand Up @@ -91,13 +91,13 @@ Here's a simple example using SQL UPDATE to illustrate.
let userLoader = DataLoader<Int, Int>(...)

// And a value happens to be loaded (and cached).
userLoader.load(4)
userLoader.load(key: 4, on: eventLoopGroup)

// A mutation occurs, invalidating what might be in cache.
sqlRun('UPDATE users WHERE id=4 SET username="zuck"').then { userLoader.clear(4) }

// Later the value load is loaded again so the mutated data appears.
userLoader.load(4)
userLoader.load(key: 4, on: eventLoopGroup)

// Request completes.
```
Expand All @@ -112,19 +112,19 @@ be cached to avoid frequently loading the same `Error`.
In some circumstances you may wish to clear the cache for these individual Errors:

```swift
userLoader.load(1).catch { error in {
userLoader.load(key: 1, on: eventLoopGroup).catch { error in {
if (/* determine if should clear error */) {
userLoader.clear(1);
}
throw error
userLoader.clear(key: 1);
}
throw error
}
```

#### Disabling Cache

In certain uncommon cases, a DataLoader which *does not* cache may be desirable.
Calling `DataLoader(options: DataLoaderOptions(cachingEnabled: false), batchLoadFunction: batchLoadFunction)` will ensure that every
call to `.load()` will produce a *new* Future, and requested keys will not be
call to `.load()` will produce a *new* Future, and previously requested keys will not be
saved in memory.

However, when the memoization cache is disabled, your batch function will
Expand All @@ -135,13 +135,16 @@ for each instance of the requested key.
For example:

```swift
let myLoader = DataLoader<String, String>(options: DataLoaderOptions(cachingEnabled: false), batchLoadFunction: { keys in
self.someBatchLoader(keys: keys).map { DataLoaderFutureValue.success($0) }
})
let myLoader = DataLoader<String, String>(
options: DataLoaderOptions(cachingEnabled: false),
batchLoadFunction: { keys in
self.someBatchLoader(keys: keys).map { DataLoaderFutureValue.success($0) }
}
)

myLoader.load("A")
myLoader.load("B")
myLoader.load("A")
myLoader.load(key: "A", on: eventLoopGroup)
myLoader.load(key: "B", on: eventLoopGroup)
myLoader.load(key: "A", on: eventLoopGroup)

// > [ "A", "B", "A" ]
```
Expand Down Expand Up @@ -171,3 +174,9 @@ When creating a pull request, please adhere to the current coding style where po

This library is entirely a Swift version of Facebooks [DataLoader](https://github.com/facebook/dataloader). Developed by [Lee Byron](https://github.com/leebyron) and
[Nicholas Schrock](https://github.com/schrockn) from [Facebook](https://www.facebook.com/).

[swift-badge]: https://img.shields.io/badge/Swift-5.2-orange.svg?style=flat
[swift-url]: https://swift.org

[mit-badge]: https://img.shields.io/badge/License-MIT-blue.svg?style=flat
[mit-url]: https://tldrlegal.com/license/mit-license
Loading

0 comments on commit 3c99c85

Please sign in to comment.