From 61dc138903cd078934f446c207e0ce5bef0b7ca1 Mon Sep 17 00:00:00 2001 From: Christopher Saez Date: Wed, 8 Mar 2017 00:55:22 +0100 Subject: [PATCH 1/4] Create fixtures --- fluent/fixtures | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 fluent/fixtures diff --git a/fluent/fixtures b/fluent/fixtures new file mode 100644 index 00000000..d29af581 --- /dev/null +++ b/fluent/fixtures @@ -0,0 +1,40 @@ +--- +currentMenu: fluent-fixtures +--- + +# Fixtures +Sometimes, you may need to prepopulate your database with data. For exemples, you might need insert in your table 'Users' an admin account or a test account. + + +In a separate file (Database+Fixture.swift): +```swift +extension Database { + func insertFixtures(_ data: S) throws where S.Iterator.Element == T { + let context = DatabaseContext(self) + try data.forEach { model in + let query = Query(self) + query.action = .create + query.data = try model.makeNode(context: context) + try driver.query(query) + } + } +} +``` + +In your model User.swift file +```swift +static func prepare(_ database: Database) throws { + try database.create("users") { users in + users.id() + users.string("email") + users.string("password") + } + let seedData: [AppUser] = [ + try User(email: "admin@admin.com", rawPassword: "Def4ultPassword?!"), + try User(email: "test@test.com", rawPassword: "Def4ultPassword?!") + ] + + try database.insertFixtures(seedData) + } + ``` + From 45041150a356d1638583932e1b3bcab50cf2fb93 Mon Sep 17 00:00:00 2001 From: Christopher Saez Date: Wed, 8 Mar 2017 00:58:09 +0100 Subject: [PATCH 2/4] Rename fixtures to fixtures.md --- fluent/{fixtures => fixtures.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename fluent/{fixtures => fixtures.md} (100%) diff --git a/fluent/fixtures b/fluent/fixtures.md similarity index 100% rename from fluent/fixtures rename to fluent/fixtures.md From fd3d58ec9395caabe0ceb283424f66ea937bb64b Mon Sep 17 00:00:00 2001 From: Christopher Saez Date: Wed, 8 Mar 2017 00:59:05 +0100 Subject: [PATCH 3/4] Update fixtures.md --- fluent/fixtures.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fluent/fixtures.md b/fluent/fixtures.md index d29af581..a2303965 100644 --- a/fluent/fixtures.md +++ b/fluent/fixtures.md @@ -3,7 +3,7 @@ currentMenu: fluent-fixtures --- # Fixtures -Sometimes, you may need to prepopulate your database with data. For exemples, you might need insert in your table 'Users' an admin account or a test account. +Sometimes, you may need to prepopulate your database with existing datas. For exemple, you might need to insert in your table 'Users' an admin account or a test account. In a separate file (Database+Fixture.swift): From ed8f918940c287d7ae3c9b50cd2407dc39606051 Mon Sep 17 00:00:00 2001 From: Christopher Saez Date: Wed, 8 Mar 2017 23:23:06 +0100 Subject: [PATCH 4/4] Update fixtures.md --- fluent/fixtures.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/fluent/fixtures.md b/fluent/fixtures.md index a2303965..ea6b8a0e 100644 --- a/fluent/fixtures.md +++ b/fluent/fixtures.md @@ -3,8 +3,7 @@ currentMenu: fluent-fixtures --- # Fixtures -Sometimes, you may need to prepopulate your database with existing datas. For exemple, you might need to insert in your table 'Users' an admin account or a test account. - +When you want to prepopulate a database with existing data, you will need to create one or several fixtures. For example, in your table `users`, it's very common to create admin account and a test account just after the creation of the table. The following snippets of code show you how to deal with it. In a separate file (Database+Fixture.swift): ```swift @@ -21,7 +20,7 @@ extension Database { } ``` -In your model User.swift file +In your model User.swift file: ```swift static func prepare(_ database: Database) throws { try database.create("users") { users in