Skip to content

Commit d21dcc2

Browse files
committed
add convenience for psql db
1 parent aed43aa commit d21dcc2

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed
+19-22
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
//extension SchemaBuilder {
2-
// public func field<T>(for keyPath: KeyPath<Model, T>, type: PostgreSQLColumnType, primaryKey: Bool? = nil, generatedByDefaultAsIdentity: Bool = false, notNull: Bool = true, default: String? = nil) {
3-
// var attributes: [String] = []
4-
// if let primaryKey = primaryKey {
5-
// if primaryKey {
6-
// attributes.append("PRIMARY KEY")
7-
// }
8-
// } else if keyPath == Model.idKey {
9-
// attributes.append("PRIMARY KEY")
10-
// }
11-
// if generatedByDefaultAsIdentity {
12-
// attributes.append("GENERATED BY DEFAULT AS IDENTITY")
13-
// }
14-
// if notNull {
15-
// attributes.append("NOT NULL")
16-
// }
17-
// if let `default` = `default` {
18-
// attributes.append("DEFAULT " + `default`)
19-
// }
20-
// field(for: .fluentProperty(.keyPath(keyPath)), type: .init(name: type.name, parameters: type.parameters, attributes: attributes))
21-
// }
22-
//}
1+
extension SchemaBuilder where Model.Database == PostgreSQLDatabase {
2+
public func field<T>(
3+
for keyPath: KeyPath<Model, T>,
4+
type dataType: PostgreSQLQuery.DataType,
5+
isArray: Bool = false,
6+
collate: String? = nil,
7+
_ constraints: PostgreSQLQuery.ColumnConstraint...
8+
) {
9+
let property = FluentProperty.keyPath(keyPath)
10+
let columnDefinition = PostgreSQLQuery.ColumnDefinition.init(
11+
name: property.path[0],
12+
dataType: dataType,
13+
isArray: isArray,
14+
collate: collate,
15+
constraints: constraints
16+
)
17+
schema.createColumns.append(columnDefinition)
18+
}
19+
}

Diff for: Tests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class FluentPostgreSQLTests: XCTestCase {
175175

176176
static func prepare(on conn: PostgreSQLConnection) -> EventLoopFuture<Void> {
177177
return PostgreSQLDatabase.create(Pet.self, on: conn) { builder in
178-
builder.field(for: \.id, isIdentifier: true)
178+
builder.field(for: \.id, type: .bigint, .notNull, .primaryKey, .generated(.byDefault))
179179
builder.field(for: \.type, type: .bigint)
180180
builder.field(for: \.name, type: .text)
181181
}

0 commit comments

Comments
 (0)