为 ManagedModels 准备的 Northwind 数据集

ManagedModels / CoreData 版本的 Microsoft Northwind 数据库,数据来源于 NorthwindSQLite.swift,而 NorthwindSQLite.swift 本身是 Northwind-SQLite3 的打包版本。

ManagedModels 是一个 Swift 包,它在纯 CoreData 之上提供了一个类似于 SwiftData 的 API。

数据来源于 Northwind-SQLite3

Northwind 示例数据库随 Microsoft Access 一起提供,作为管理小型企业客户、订单、库存、采购、供应商、运输和员工的教程模式。 Northwind 是一个优秀的小型企业 ERP 教程模式,包含客户、订单、库存、采购、供应商、运输、员工和单式记账。

使用 ManagedModels / CoreData 会很有帮助,因为它提供了一个预先填充的数据库和一组模型。 该数据库有点旧,但仍然可以用来开始使用实际数据集。

在 SwiftUI 应用程序中使用它

  1. 在 Xcode 中创建一个 SwiftUI 应用程序
  2. 添加 NorthwindManagedModels 作为依赖项,URL 是:https://github.com/Northwind-swift/NorthwindManagedModels
  3. 为 UI 设置模型容器
@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 中提供的数据创建的。 它是使用 NorthwindSQLite.swift 通过单独的 Importer Tool 导入的。

注意:导入需要几个小时才能运行 ⏳

错误

Northwind 的 SQLite 版本有一些错误,主要与可选性有关(即,很多列都标记为 NULLable,这可能不应该)。 目标是在 SwiftData 模型和导入中修复这些错误,欢迎提交 PR。 即使 DB 中的字段从未设置为 NULL(例如,地址字段),也不应过度地设为非空。

链接

作者

为 SwiftData 准备的 Northwind 数据集由 Helge Heß / ZeeZide 提供。 我们喜欢反馈、GitHub 星标、酷炫的合同工作,大概任何你能想到的赞扬形式。

想支持我的工作吗?购买一个应用程序:Code for SQLite3Past for iChatSVG ShaperShrugsHMScriptEditor。 您不必使用它! 😀