Swift Package Index


The easiest way to seed a database using Fluent.


  • The latest stable release is 0.1.5. Released over 1 year ago.
  • The last commit to master was over 1 year ago.

Swift Version Compatibility

  • 0.1.5 and master
    5.3
    5.2
    5.1
    5.0
    4.2
Full build results

Platform Compatibility

  • 0.1.5 and master
    iOS
    macOS(Intel)
    macOS(ARM)
    Linux
    tvOS
    watchOS
Full build results

FluentSeeder

Swift Vapor Swift Package Manager License

FluentSeeder makes it easier to seed Fluent models. For testing purposes, it can create psuedo realistic random data for your models based on their property names and types using RandomFactory.

Installation

FluentSeeder is available through Swift Package Manager. To install, add the following to your Package.swift file.

let package = Package(
    name: "YourProject",
    dependencies: [
        ...
        .package(url: "https://github.com/Appsaurus/FluentSeeder", from: "0.1.0"),
    ],
    targets: [
      //If just using for test purposes (recommended use case)
      .testTarget(name: "YourAppTests", dependencies: ["FluentSeeder", ... ])
      //Not recommended but still possible to use to seed your actual app.
      .target(name: "YourApp", dependencies: ["FluentSeeder", ... ])
    ]
)
        

Usage

1. Import the library

import FluentSeeder

2. Implement Seeder

Registering and configuration of services, databases, and migrations can be done via overriding register(services:), configure(databases:) and configure(migrations:) respectively.


public class ExampleSeeder: Seeder{
	public typealias Database = SQLiteDatabase
	open static func seeds() -> [SeedProtocol]{
		return [
			//Seed models first
			Seed<ExampleModel>(count: 50),
			Seed<ExampleSiblingModel>(count: 25),
			Seed<ExampleChildModel>(count: 10),

			//Then relationships that depend on those models existing
			SiblingSeed<ExampleModelSiblingPivot>(count: 10),
			
			//You can seed parents for each child
			ParentSeed<ExampleModel, ExampleChildModel>(at: \.optionalParentModelId)
			
			//Or if you prefer to seed the relationship in the other direction (possibly for one-to-many relationship)
			ChildSeed<ExampleModel, ExampleChildModel>.init(count: 3, at:  \.optionalParentModelId)
		]
	}
}

3. Add your seeder's migration to the database

//Don't forget to add your model mirations first
migrations.add(model: ExampleModel.self, database: .sqlite)
migrations.add(model: ExampleSiblingModel.self, database: .sqlite)
migrations.add(model: ExampleChildModel.self, database: .sqlite)
migrations.add(model: ExampleModelSiblingPivot.self, database: .sqlite)

migrations.add(migration: ExampleSeeder.self, database: .sqlite)

Custom seeds

By supplying a ModelFactory to the Seed initializer, you can customize how your model is initialized and override the default randmized data. For relationship seeds, you can supply querys to filter which models are included when creating relationships are formed. For SiblingSeed you can optionally supply a leftQuery and rightQuery, and for ParentSeed and ChildSeed you can optionally supply parentQuery and childQuery. If no queries are provided, random samples are taken from each seeded model, or all models are iterated depending upon the direction of the seeded relationship.

Contributing

We would love you to contribute to FluentSeeder, check the CONTRIBUTING file for more info.

License

FluentSeeder is available under the MIT license. See the LICENSE file for more info.