Skip to content

Commit

Permalink
Merge pull request #4 from PoshAlpaca/vapor4
Browse files Browse the repository at this point in the history
Support for Vapor 4
  • Loading branch information
PoshAlpaca authored May 3, 2022
2 parents c13f032 + e910c6d commit ea14dfc
Show file tree
Hide file tree
Showing 50 changed files with 1,156 additions and 1,887 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Run tests
runs-on: ubuntu-latest
services:
postgres:
image: postgis/postgis
ports:
- "5432:5432"
env:
POSTGRES_USER: fluentpostgis
POSTGRES_PASSWORD: fluentpostgis
POSTGRES_DB: postgis_tests
steps:
- uses: actions/checkout@v3
- run: swift test
8 changes: 8 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--extensionacl on-declarations
--header strip
--maxwidth 100
--self insert
--swiftversion 5.5
--wraparguments before-first
--wrapcollections before-first
--wrapparameters before-first
44 changes: 0 additions & 44 deletions .travis.yml

This file was deleted.

2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test-db:
docker run --rm -e POSTGRES_PASSWORD=fluentpostgis -e POSTGRES_USER=fluentpostgis -e POSTGRES_DB=postgis_tests -p 5432:5432 odidev/postgis:11-2.5-alpine
35 changes: 21 additions & 14 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
// swift-tools-version:4.1
// swift-tools-version:5.5
import PackageDescription

let package = Package(
name: "FluentPostGIS",
name: "fluent-postgis",
platforms: [
.macOS(.v12),
],
products: [
// FluentPostgreSQL support for PostGIS
.library(
name: "FluentPostGIS",
targets: ["FluentPostGIS"]),
targets: ["FluentPostGIS"]
),
],
dependencies: [
// Swift ORM framework (queries, models, and relations) for building NoSQL and SQL database integrations.
.package(url: "https://github.com/vapor/fluent.git", from: "3.0.0"),

// 🐘 Non-blocking, event-driven Swift client for PostgreSQL.
.package(url: "https://github.com/vapor/fluent-postgresql.git", from: "1.0.0"),

// Well Known Binary Encoding and Decoding
.package(url: "https://github.com/plarson/WKCodable", from: "0.1.1"),
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.0.0"),
.package(url: "https://github.com/vapor/fluent-postgres-driver.git", from: "2.0.0"),
.package(url: "https://github.com/rabc/WKCodable.git", from: "0.1.0"),
],
targets: [
.target(
name: "FluentPostGIS",
dependencies: ["FluentPostgreSQL", "WKCodable"]),
dependencies: [
.product(name: "FluentKit", package: "fluent-kit"),
.product(name: "FluentPostgresDriver", package: "fluent-postgres-driver"),
.product(name: "WKCodable", package: "WKCodable"),
]
),
.testTarget(
name: "FluentPostGISTests",
dependencies: ["FluentBenchmark", "FluentPostGIS"]),
dependencies: [
.target(name: "FluentPostGIS"),
.product(name: "FluentBenchmark", package: "fluent-kit"),
]
),
]
)
73 changes: 58 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,73 @@
# FluentPostGIS

[![Build Status](https://travis-ci.org/plarson/fluent-postgis.svg?branch=master)](https://travis-ci.org/plarson/fluent-postgis)
![Platforms](https://img.shields.io/badge/platforms-Linux%20%7C%20OS%20X-blue.svg)
![Package Managers](https://img.shields.io/badge/package%20managers-SwiftPM-yellow.svg)
[![Twitter dizm](https://img.shields.io/badge/twitter-dizm-green.svg)](http://twitter.com/dizm)

PostGIS support for [FluentPostgreSQL](https://github.com/vapor/fluent-postgresql) and [Vapor](https://github.com/vapor/vapor)
A fork of the [FluentPostGIS](https://github.com/plarson/fluent-postgis) package which adds support for geographic queries. FluentPostGIS provides PostGIS support for [fluent-postgres-driver](https://github.com/vapor/fluent-postgres-driver) and [Vapor 4](https://github.com/vapor/vapor).

# Installation

## Swift Package Manager

Add this line to your dependencies in `Package.swift`:

```swift
.package(url: "https://github.com/brokenhandsio/fluent-postgis.git", from: "0.3.0")
```

Then add this line to a target's dependencies:

```swift
.package(url: "https://github.com/plarson/fluent-postgis.git", .branch("master"))
.product(name: "FluentPostGIS", package: "fluent-postgis"),
```

# Setup

Import module

```swift
import FluentPostGIS
```

Add to ```configure.swift```
Optionally, you can add a `Migration` to enable PostGIS:

```swift
try services.register(FluentPostGISProvider())
app.migrations.add(EnablePostGISMigration())

```

# Models
Add ```GISGeographicPoint2D``` to your models

Add a type to your model

```swift
final class User: PostgreSQLModel {
var id: Int?
var name: String
var location: GISGeographicPoint2D?
final class User: Model {
static let schema = "user"

@ID(key: .id)
var id: UUID?

@Field(key: "location")
var location: GeometricPoint2D
}
```

Then use its data type in the `Migration`:

```swift
struct UserMigration: AsyncMigration {
func prepare(on database: Database) async throws -> {
try await database.schema(User.schema)
.id()
.field("location", .geometricPoint2D)
.create()
}
func revert(on database: Database) async throws -> {
try await database.schema(User.schema).delete()
}
}
```

| Geometric Types | Geographic Types |
|---|---|
|GeometricPoint2D|GeographicPoint2D|
Expand All @@ -44,10 +79,14 @@ final class User: PostgreSQLModel {
|GeometricGeometryCollection2D|GeographicGeometryCollection2D|

# Queries
Query locations using ```ST_DWithin```
```swift
let searchLocation = GISGeographicPoint2D(longitude: -71.060316, latitude: 48.432044)
try User.query(on: conn).filterGeometryDistanceWithin(\User.location, searchLocation, 1000).all().wait()

Query using any of the filter functions:

```swift
let eiffelTower = GeographicPoint2D(longitude: 2.2945, latitude: 48.858222)
try await User.query(on: database)
.filterGeographyDistanceWithin(\.$location, eiffelTower, 1000)
.all()
```

| Queries |
Expand All @@ -57,11 +96,13 @@ try User.query(on: conn).filterGeometryDistanceWithin(\User.location, searchLoca
|filterGeometryDisjoint|
|filterGeometryDistance|
|filterGeometryDistanceWithin|
|filterGeographyDistanceWithin|
|filterGeometryEquals|
|filterGeometryIntersects|
|filterGeometryOverlaps|
|filterGeometryTouches|
|filterGeometryWithin|
|sortByDistance|

:gift_heart: Contributing
------------
Expand All @@ -73,4 +114,6 @@ MIT

:alien: Author
------
BrokenHands, Tim Condon, Nikolai Guyot - https://www.brokenhands.io/
Ricardo Carvalho - https://rabc.github.io/
Phil Larson - http://dizm.com
42 changes: 0 additions & 42 deletions Sources/FluentPostGIS/Geographic/GeographicLineString2D.swift

This file was deleted.

42 changes: 0 additions & 42 deletions Sources/FluentPostGIS/Geographic/GeographicMultiPoint2D.swift

This file was deleted.

44 changes: 0 additions & 44 deletions Sources/FluentPostGIS/Geographic/GeographicMultiPolygon2D.swift

This file was deleted.

Loading

0 comments on commit ea14dfc

Please sign in to comment.