Skip to content

Commit

Permalink
Initial implementation (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriomazzeo authored Dec 28, 2018
1 parent b68d9fd commit d00f8c6
Show file tree
Hide file tree
Showing 42 changed files with 2,401 additions and 5 deletions.
69 changes: 69 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## Build generated
*.xcodeproj
build/
DerivedData/

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/

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

## Obj-C/Swift specific
*.hmap
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
Package.pins
Package.resolved
.build/

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/

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

Carthage/Build

# 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.
# 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
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.5.1
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4.2
30 changes: 30 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
disabled_rules: # rule identifiers to exclude from running
- line_length
- identifier_name
- type_name
- large_tuple
function_body_length: 50
type_name:
max_length: 60
type_body_length: 400
file_length: 630
nesting:
type_level: 2
function_parameter_count: 6
cyclomatic_complexity: 20
included:
- Package.swift
- Sources
- Tests
custom_rules:
vertical_whitespace_return:
includes: "*.swift"
name: "Space above Return"
regex: '\S[^{\s:|in]\n[\t ]+return'
message: "Vertical whitespace required above return statement."
vertical_whitespace_closing_braces:
includes: "*.swift"
regex: '\n[ \t]*\n[ \t]*[)}\]]'
name: "Vertical Whitespace before Closing Braces"
message: "Don't include vertical whitespace (empty line) before closing braces."
severity: warning
57 changes: 57 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
os:
- linux
- osx
language: generic
sudo: required
dist: trusty
osx_image: xcode10.1
services: docker

before_install:
- if [ $TRAVIS_OS_NAME == "linux" ]; then
SWIFT_VERSION=$(<.swift-version);
docker pull swift:${SWIFT_VERSION};
docker pull mongo:3.6.8;
docker run -d -p 27017:27017 mongo:3.6.8;
elif [ $TRAVIS_OS_NAME == "osx" ]; then
rvm use 2.5.1 --install --binary --fuzzy;
ruby --version;
gem install bundler;
bundle --version;
brew update;
brew install mongo-c-driver;
brew install mongodb;
brew services start mongodb;
brew outdated swiftlint || brew upgrade swiftlint;
fi

install:
- if [ $TRAVIS_OS_NAME == "osx" ]; then
bundle install --jobs=3 --retry=3 --deployment;
fi

script:
- if [ $TRAVIS_OS_NAME == "osx" ]; then
swift --version;
swift build;
swift build -c release;
swift test;
swiftlint;
else
args="apt-get update
&& apt-get -y install cmake
&& git clone -b r1.13 https://github.com/mongodb/mongo-c-driver /tmp/libmongoc
&& pushd /tmp/libmongoc
&& cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr
&& make -j8 install
&& popd
&& swift build
&& swift build -c release
&& swift test";
docker run --rm --net=host -v $(pwd):/app --workdir /app swift:${SWIFT_VERSION} bash -c "${args}";
fi

notifications:
email:
on_success: never
on_failure: change
3 changes: 3 additions & 0 deletions GEMFILE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "https://rubygems.org"

gem 'bundler'
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
GEM
remote: https://rubygems.org/
specs:

PLATFORMS
ruby

DEPENDENCIES
bundler

BUNDLED WITH
1.17.1
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Asensei

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 4 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/vapor/core.git", .upToNextMajor(from: "3.1.0")),
.package(url: "https://github.com/vapor/fluent.git", .upToNextMajor(from: "3.1.2"))
.package(url: "https://github.com/vapor/fluent.git", .upToNextMajor(from: "3.1.2")),
.package(url: "https://github.com/mongodb/mongo-swift-driver.git", .revision("2610f57ac2c937a039b35e331086e225ea5f0cbf"))
],
targets: [
.target(name: "FluentMongo", dependencies: ["Async", "Fluent"]),
.testTarget(name: "FluentMongoTests", dependencies: ["FluentMongo"])
.target(name: "FluentMongo", dependencies: ["Async", "Fluent", "MongoSwift"]),
.testTarget(name: "FluentMongoTests", dependencies: ["FluentMongo", "FluentBenchmark"])
]
)
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# vapor-auth-jwt
# vapor-fluent-mongo

![Swift](https://img.shields.io/badge/swift-4.2-orange.svg)
[![Build Status](https://travis-ci.com/asensei/vapor-fluent-mongo.svg?token=eSrCssnzja3G3GciyhUB&branch=master)](https://travis-ci.com/asensei/vapor-fluent-mongo)

Mongo driver for Fluent `3.x`.

## Environment Variables

| Name | Required | Default | Value (e.g.) | Description |
| ------------- |:-------------:|:-------------:|:-------------:|:-------------|
| `FLUENT_MONGO_CONNECTION_URL` || `-` | `mongodb://127.0.0.1:27017/vapor` | Mongo connection string. |

## Getting Started

### Install the MongoDB C Driver
Expand All @@ -16,7 +22,7 @@ On a Mac, you can install both components at once using [Homebrew](https://brew.
On Linux: please follow the [instructions](http://mongoc.org/libmongoc/current/installing.html#building-on-unix) from `libmongoc`'s documentation. Note that the versions provided by your package manager may be too old, in which case you can follow the instructions for building and installing from source.


### Install MongoSwift
### Install FluentMongo

*Please follow the instructions in the previous section on installing the MongoDB C Driver before proceeding.*

Expand Down
17 changes: 17 additions & 0 deletions Sources/FluentMongo/BSONCoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// BSONCoder.swift
// FluentMongo
//
// Created by Valerio Mazzeo on 28/12/2018.
// Copyright © 2018 Asensei Inc. All rights reserved.
//

import Foundation
import MongoSwift

public protocol BSONCoder {

static var encoder: BSONEncoder { get }

static var decoder: BSONDecoder { get }
}
26 changes: 26 additions & 0 deletions Sources/FluentMongo/BSONEncoder+BSONValue.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// BSONEncoder+BSONValue.swift
// FluentMongo
//
// Created by Valerio Mazzeo on 11/12/2018.
// Copyright © 2018 Asensei Inc. All rights reserved.
//

import Foundation
import MongoSwift

public extension BSONEncoder {

public func encodeBSONValue<T: Encodable>(_ value: T) throws -> BSONValue {
switch value {
case let value as BSONValue:
return value
default:
// We can only use BSONEncoder to encode top-level data
let wrappedData = ["value": value]
let document: Document = try self.encode(wrappedData)

return document["value"] ?? BSONNull()
}
}
}
59 changes: 59 additions & 0 deletions Sources/FluentMongo/Document+Nested.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// Document+Nested.swift
// FluentMongo
//
// Created by Valerio Mazzeo on 06/12/2018.
// Copyright © 2018 Asensei Inc. All rights reserved.
//

import Foundation
import MongoSwift

// Review when https://jira.mongodb.org/browse/SWIFT-273 will be fixed.
public extension Document {
public subscript(keys: [String]) -> BSONValue? {
get {
guard !keys.isEmpty else {
return nil
}

var value: BSONValue? = self

for key in keys {
value = (value as? Document)?[key]
}

return value
}
set {
func setNewValue(for keys: [String], in document: inout Document) {
guard !keys.isEmpty else {
return
}

guard keys.count > 1 else {
document[keys[0]] = newValue

return
}

var path = keys
let component = path.removeFirst()
var next = document[component] as? Document ?? Document()
setNewValue(for: path, in: &next)
document[component] = next
}

setNewValue(for: keys, in: &self)
}
}

public subscript(keys: String...) -> BSONValue? {
get {
return self[keys]
}
set {
self[keys] = newValue
}
}
}
23 changes: 23 additions & 0 deletions Sources/FluentMongo/FluentMongoModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// FluentMongoModel.swift
// FluentMongo
//
// Created by Valerio Mazzeo on 06/12/2018.
// Copyright © 2018 Asensei Inc. All rights reserved.
//

import Foundation
import Fluent

public protocol FluentMongoModel {

associatedtype ID: Fluent.ID

var _id: ID? { get set }
}

public extension FluentMongoModel where Self: Model {
public static var idKey: WritableKeyPath<Self, ID?> {
return \._id
}
}
Loading

0 comments on commit d00f8c6

Please sign in to comment.