Convert Figma logo to code with AI

vapor logofluent

Vapor ORM (queries, models, and relations) for NoSQL and SQL databases

1,316
172
1,316
16

Top Related Projects

78,107

Laravel is a web application framework with expressive, elegant syntax. We’ve already laid the foundation for your next big idea — freeing you to create without sweating the small things.

9,900

Doctrine Object Relational Mapper (ORM)

29,432

Feature-rich ORM for modern Node.js and TypeScript, it supports PostgreSQL (with JSON and JSONB support), MySQL, MariaDB, SQLite, MS SQL Server, Snowflake, Oracle DB (v6), DB2 and DB2 for IBM i.

33,970

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.

38,831

Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB

Hibernate's core Object/Relational Mapping functionality

Quick Overview

Fluent is an ORM (Object-Relational Mapping) framework for Swift, designed to work seamlessly with the Vapor web framework. It provides a powerful and intuitive way to interact with databases, supporting multiple database backends while offering a consistent API for developers.

Pros

  • Supports multiple database backends (PostgreSQL, MySQL, SQLite, MongoDB)
  • Integrates seamlessly with Vapor, providing a cohesive development experience
  • Offers a type-safe, expressive query builder
  • Provides automatic migrations for easy database schema management

Cons

  • Learning curve for developers new to Swift or ORMs
  • Performance overhead compared to raw SQL queries
  • Limited support for complex database operations or optimizations
  • Dependency on Vapor ecosystem may limit flexibility in some scenarios

Code Examples

  1. Defining a model:
import Fluent
import Vapor

final class Todo: Model, Content {
    static let schema = "todos"
    
    @ID(key: .id)
    var id: UUID?
    
    @Field(key: "title")
    var title: String
    
    @Field(key: "completed")
    var completed: Bool
    
    init() { }
    
    init(id: UUID? = nil, title: String, completed: Bool = false) {
        self.id = id
        self.title = title
        self.completed = completed
    }
}
  1. Performing a query:
app.get("todos") { req -> EventLoopFuture<[Todo]> in
    return Todo.query(on: req.db).all()
}
  1. Creating a migration:
struct CreateTodo: Migration {
    func prepare(on database: Database) -> EventLoopFuture<Void> {
        return database.schema("todos")
            .id()
            .field("title", .string, .required)
            .field("completed", .bool, .required, .custom("DEFAULT FALSE"))
            .create()
    }

    func revert(on database: Database) -> EventLoopFuture<Void> {
        return database.schema("todos").delete()
    }
}

Getting Started

  1. Add Fluent and a database driver to your Package.swift:
dependencies: [
    .package(url: "https://github.com/vapor/fluent.git", from: "4.0.0"),
    .package(url: "https://github.com/vapor/fluent-postgres-driver.git", from: "2.0.0")
],
targets: [
    .target(name: "App", dependencies: [
        .product(name: "Fluent", package: "fluent"),
        .product(name: "FluentPostgresDriver", package: "fluent-postgres-driver"),
        .product(name: "Vapor", package: "vapor"),
    ])
]
  1. Configure Fluent in your configure.swift:
import Fluent
import FluentPostgresDriver

public func configure(_ app: Application) throws {
    app.databases.use(.postgres(
        hostname: Environment.get("DATABASE_HOST") ?? "localhost",
        port: Environment.get("DATABASE_PORT").flatMap(Int.init(_:)) ?? PostgresConfiguration.ianaPortNumber,
        username: Environment.get("DATABASE_USERNAME") ?? "vapor_username",
        password: Environment.get("DATABASE_PASSWORD") ?? "vapor_password",
        database: Environment.get("DATABASE_NAME") ?? "vapor_database"
    ), as: .psql)

    app.migrations.add(CreateTodo())
}

Competitor Comparisons

78,107

Laravel is a web application framework with expressive, elegant syntax. We’ve already laid the foundation for your next big idea — freeing you to create without sweating the small things.

Pros of Laravel

  • More extensive ecosystem with a wide range of packages and tools
  • Larger community support and resources for learning and troubleshooting
  • Built-in features like authentication, queues, and caching out of the box

Cons of Laravel

  • Heavier framework with more overhead, potentially slower for simple applications
  • Steeper learning curve for beginners due to its extensive feature set
  • PHP-specific, limiting language choice for developers

Code Comparison

Laravel (routing example):

Route::get('/users', function () {
    return User::all();
});

Fluent (routing example):

app.get("users") { req in
    try await User.query(on: req.db).all()
}

Both frameworks offer elegant routing solutions, but Fluent's Swift syntax is more concise. Laravel's routing is more familiar to PHP developers, while Fluent caters to Swift programmers.

Laravel provides a full-stack framework with built-in features, making it suitable for complex applications. Fluent, being part of the Vapor ecosystem, focuses on database interactions and is more lightweight, allowing for greater flexibility in choosing other components.

The choice between these frameworks depends on factors such as the developer's preferred language (PHP vs. Swift), project requirements, and desired level of abstraction and built-in functionality.

9,900

Doctrine Object Relational Mapper (ORM)

Pros of Doctrine

  • More mature and widely adopted in the PHP ecosystem
  • Supports a broader range of databases and features
  • Extensive documentation and community support

Cons of Doctrine

  • Steeper learning curve due to its complexity
  • Can be overkill for smaller projects
  • Performance overhead in some scenarios

Code Comparison

Fluent (Swift):

final class User: Model {
    @ID(key: .id)
    var id: UUID?

    @Field(key: "name")
    var name: String

    init() { }
}

Doctrine (PHP):

/**
 * @Entity
 * @Table(name="users")
 */
class User
{
    /** @Id @Column(type="integer") @GeneratedValue */
    private $id;

    /** @Column(type="string") */
    private $name;
}

Fluent offers a more concise syntax with property wrappers, while Doctrine uses annotations for entity mapping. Fluent's approach is more Swift-like and integrates well with the language's features. Doctrine's syntax is more verbose but provides explicit control over entity configuration.

Both ORMs support basic CRUD operations, relationships, and migrations. However, Doctrine offers more advanced features like inheritance mapping and a powerful query builder. Fluent, being tailored for Swift and Vapor, provides a more streamlined experience for Swift developers working on server-side projects.

29,432

Feature-rich ORM for modern Node.js and TypeScript, it supports PostgreSQL (with JSON and JSONB support), MySQL, MariaDB, SQLite, MS SQL Server, Snowflake, Oracle DB (v6), DB2 and DB2 for IBM i.

Pros of Sequelize

  • Mature and widely adopted ORM with extensive documentation and community support
  • Supports multiple databases (MySQL, PostgreSQL, SQLite, etc.) with a unified API
  • Rich set of features including migrations, associations, and transactions

Cons of Sequelize

  • Steeper learning curve due to its extensive feature set
  • Can be slower for complex queries compared to raw SQL
  • JavaScript-specific, limiting its use to Node.js environments

Code Comparison

Sequelize:

const User = sequelize.define('User', {
  username: DataTypes.STRING,
  email: DataTypes.STRING
});

const users = await User.findAll();

Fluent:

struct User: Model {
    @ID var id: UUID?
    @Field(key: "username") var username: String
    @Field(key: "email") var email: String
}

let users = try await User.query(on: database).all()

Key Differences

  • Fluent is designed specifically for Swift and Vapor, while Sequelize is for JavaScript/Node.js
  • Fluent uses Swift's type system and property wrappers, making it more idiomatic for Swift developers
  • Sequelize offers more built-in features and database support out of the box
  • Fluent's API is more concise and leverages Swift's async/await syntax

Both ORMs provide similar core functionality, but their design philosophies and target ecosystems differ significantly. The choice between them largely depends on the programming language and framework being used for the project.

33,970

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.

Pros of TypeORM

  • Supports multiple databases (MySQL, PostgreSQL, MongoDB, etc.)
  • More mature and widely adopted in the JavaScript/TypeScript ecosystem
  • Extensive documentation and large community support

Cons of TypeORM

  • Steeper learning curve due to more complex features
  • Can be overkill for smaller projects
  • Performance may be slower for large-scale applications

Code Comparison

TypeORM:

@Entity()
class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;
}

Fluent:

final class User: Model {
    @ID(key: .id)
    var id: UUID?

    @Field(key: "name")
    var name: String
}

Both ORMs use decorators/property wrappers to define entity structures, but TypeORM's syntax is more verbose. Fluent's approach is more concise and Swift-like, while TypeORM follows a more traditional ORM pattern common in the JavaScript ecosystem.

TypeORM offers more flexibility with various database systems, making it suitable for diverse projects. Fluent, being part of the Vapor ecosystem, is optimized for Swift and integrates seamlessly with other Vapor components.

TypeORM's extensive feature set and larger community can be advantageous for complex projects, but may introduce unnecessary complexity for simpler applications. Fluent, on the other hand, provides a more streamlined experience within the Swift and Vapor ecosystem, potentially leading to faster development for Swift-based projects.

38,831

Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB

Pros of Prisma

  • More extensive database support, including PostgreSQL, MySQL, SQLite, and MongoDB
  • Powerful schema migration and introspection tools
  • Type-safe database queries with auto-generated client

Cons of Prisma

  • Steeper learning curve due to its unique query language
  • Less flexibility in raw SQL queries compared to Fluent
  • Potentially slower query performance in some complex scenarios

Code Comparison

Prisma query example:

const users = await prisma.user.findMany({
  where: { age: { gte: 18 } },
  include: { posts: true }
})

Fluent query example:

let users = try await User.query(on: database)
    .filter(\.$age >= 18)
    .with(\.$posts)
    .all()

Both Prisma and Fluent offer powerful ORM capabilities, but they cater to different ecosystems. Prisma is more popular in the JavaScript/TypeScript world, while Fluent is tailored for Swift developers using the Vapor framework. Prisma's schema-first approach and migration tools make it excellent for complex database structures, while Fluent's tight integration with Swift and Vapor provides a seamless experience for iOS and server-side Swift developers.

Hibernate's core Object/Relational Mapping functionality

Pros of Hibernate ORM

  • Mature and widely adopted ORM framework with extensive documentation and community support
  • Supports a wide range of databases and provides advanced features like caching and lazy loading
  • Offers powerful querying capabilities through HQL (Hibernate Query Language)

Cons of Hibernate ORM

  • Steeper learning curve and more complex configuration compared to Fluent
  • Can introduce performance overhead, especially for simple database operations
  • Potential for N+1 query problems if not carefully managed

Code Comparison

Hibernate ORM:

@Entity
public class User {
    @Id
    private Long id;
    private String name;
    // Getters and setters
}

Fluent:

final class User: Model {
    @ID var id: UUID?
    @Field(key: "name") var name: String
}

Summary

Hibernate ORM is a robust and feature-rich ORM solution for Java applications, offering extensive database support and advanced querying capabilities. However, it comes with a steeper learning curve and potential performance considerations. Fluent, on the other hand, provides a more lightweight and Swift-native approach to database interactions, with a focus on simplicity and ease of use. The choice between the two depends on the specific requirements of the project, the development ecosystem, and the desired balance between features and simplicity.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Fluent

Documentation Team Chat MIT License Continuous Integration Swift 5.8+


The Fluent package makes it easy to use FluentKit in Vapor applications by tying the FluentKit database abstraction layer into the Vapor application lifecycle. It also provides helpers for mapping FluentKit's models to Vapor's authentication APIs.

For more information, see the Fluent documentation.