注意:SwiftData 需要 Xcode 15b+ 和 iOS 17/macOS 14。
Microsoft 的 Northwind 数据库的 SwiftData 版本,数据来自 NorthwindSQLite.swift,而 NorthwindSQLite.swift 是 @jpwhite3 的 Northwind-SQLite3 的打包版本。
来自 Northwind-SQLite3
Northwind 示例数据库随 Microsoft Access 一起提供,作为管理小型企业客户、订单、库存、采购、供应商、运输和员工的教程模式。Northwind 是一个优秀的小型企业 ERP 教程模式,包含客户、订单、库存、采购、供应商、运输、员工和单式记账。
在 SwiftData 上下文中,它可以用于学习 SwiftData,因为它提供了一个预填充的数据库和一组模型。 该数据库有点旧,但仍然可以用来开始使用实际数据集。
https://github.com/Northwind-swift/NorthwindSwiftData.git
@main
struct NorthwindApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(try! NorthwindStore.modelContainer())
}
}
就这样。 它会将包含的数据库复制到应用程序的支持目录,并设置其他所有内容。
另一种选择是使用 .readOnlyModelContainer()
,它将返回一个直接访问嵌入式数据库的容器。 这对于测试等情况很有用。
由 Northwind-SQLite3 提供的模型结构。
erDiagram
CustomerCustomerDemo }o--|| CustomerDemographics : have
CustomerCustomerDemo }o--|| Customers : through
Employees ||--|| Employees : "reports to"
Employees ||--o{ EmployeeTerritories : through
Orders }o--|| Shippers : "ships via"
"Order Details" }o--|| Orders : have
"Order Details" }o--|| Products : contain
Products }o--|| Categories : in
Products }o--|| Suppliers : "supplied by"
Territories ||--|| Regions : in
EmployeeTerritories }o--|| Territories : have
Orders }o--|| Customers : place
Orders }o--|| Employees : "sold by"
Categories {
int CategoryID PK
string CategoryName
string Description
blob Picture
}
CustomerCustomerDemo {
string CustomerID PK, FK
string CustomerTypeID PK, FK
}
CustomerDemographics {
string CustomerTypeID PK
string CustomerDesc
}
Customers {
string CustomerID PK
string CompanyName
string ContactName
string ContactTitle
string Address
string City
string Region
string PostalCode
string Country
string Phone
string Fax
}
Employees {
int EmployeeID PK
string LastName
string FirstName
string Title
string TitleOfCourtesy
date BirthDate
date HireDate
string Address
string City
string Region
string PostalCode
string Country
string HomePhone
string Extension
blob Photo
string Notes
int ReportsTo FK
string PhotoPath
}
EmployeeTerritories {
int EmployeeID PK, FK
int TerritoryID PK, FK
}
"Order Details" {
int OrderID PK, FK
int ProductID PK, FK
float UnitPrice
int Quantity
real Discount
}
Orders {
int OrderID PK
string CustomerID FK
int EmployeeID FK
datetime OrderDate
datetime RequiredDate
datetime ShippedDate
int ShipVia FK
numeric Freight
string ShipName
string ShipAddress
string ShipCity
string ShipRegion
string ShipPostalCode
string ShipCountry
}
Products {
int ProductID PK
string ProductName
int SupplierID FK
int CategoryID FK
int QuantityPerUnit
float UnitPrice
int UnitsInStock
int UnitsOnOrder
int ReorderLevel
string Discontinued
}
Regions {
int RegionID PK
string RegionDescription
}
Shippers {
int ShipperID PK
string CompanyName
string Phone
}
Suppliers {
int SupplierID PK
string CompanyName
string ContactName
string ContactTitle
string Address
string City
string Region
string PostalCode
string Country
string Phone
string Fax
string HomePage
}
Territories {
string TerritoryID PK
string TerritoryDescription
int RegionID FK
}
该软件包包含一个已填充的 Northwind 数据库,该数据库是从 Northwind-SQLite3 中提供的数据创建的。 它通过单独的 Importer Tool 使用 NorthwindSQLite.swift 导入。
注意:导入需要几个小时才能运行 ⏳
Northwind 的 SQLite 版本有一些缺陷,主要与可选性有关(即,许多列被标记为 NULLable,这可能不应该)。 目标是在 SwiftData 模型和导入中修复这些问题,欢迎提交 PR。 即使字段从未在数据库中设置为 NULL(例如,地址字段),也不应过度地使用非空字段。
https://github.com/Northwind-swift/NorthwindSwiftData.git
@Model
,用于 CoreData,向后兼容较早的 OS 版本。Northwind for SwiftData 由 Helge Heß / ZeeZide 提供。 我们喜欢反馈、GitHub 星星、酷的合同工作,大概是你能想到的任何形式的赞美。
想支持我的工作吗? 购买应用程序:Code for SQLite3, Past for iChat, SVG Shaper, HMScriptEditor。 你不必使用它! 😀