You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sources/Normalization/Documentation.docc/Normalization.md
+17-19Lines changed: 17 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,11 +6,9 @@ State management plays a crucial role in building efficient and maintainable app
6
6
7
7
Normalization is the process of structuring data in a way that eliminates redundancy and ensures data consistency. It is essential in state-management libraries because it significantly reduces the computational complexity of operations and makes it easier to manage the state.
Let's take a look at an example to illustrate the difference between normalized and denormalized data structures.
10
+
11
+
[Redux's normalization explanations](https://redux.js.org/usage/structuring-reducers/normalizing-state-shape) are also useful to understatnd the concept of normalization.
14
12
15
13
**Denormalized data structure:**
16
14
@@ -65,7 +63,7 @@ entities:
65
63
66
64
In the normalized structure, author data is stored separately from posts, eliminating data redundancy and ensuring data consistency. The relationship between posts and authors is represented by the `authorId` field in the posts.
67
65
68
-
VergeORM is designed to handle normalization in state-management libraries effectively. By leveraging VergeORM, you can simplify your state management, reduce the computational complexity of operations, and improve the overall performance and maintainability of your application.
66
+
Normalization is designed to handle normalization in state-management libraries effectively. By leveraging Normalization, you can simplify your state management, reduce the computational complexity of operations, and improve the overall performance and maintainability of your application.
69
67
70
68
**Defining Entities**
71
69
@@ -119,32 +117,32 @@ struct Database {
119
117
Embed the `Database` in your application's state:
120
118
121
119
```swift
122
-
struct RootState: StateType {
120
+
struct RootState {
123
121
var database: Database = .init()
124
122
}
125
123
```
126
124
127
125
**Storing and Querying Entities**
128
126
129
-
Here's an example of how to store and query entities using a `store` property
127
+
Writing
130
128
131
129
```swift
132
-
// Storing entities
133
-
store.commit {
134
-
$0.database.performBatchUpdates { context in
135
-
let authors = (0..<10).map { i in
136
-
Author(rawID: "\(i)")
137
-
}
138
-
let result = context.modifying.author.insert(authors)
130
+
var state: RootState
131
+
132
+
state.database.performBatchUpdates { context in
133
+
let authors = (0..<10).map { i in
134
+
Author(rawID: "\(i)")
139
135
}
136
+
let result = context.modifying.author.insert(authors)
140
137
}
141
-
142
-
// Querying entities
143
-
let book = store.state.database.db.book.find(by: .init("1"))
144
-
let author = store.state.database.db.author.find(by: .init("1"))
145
138
```
146
139
147
-
In this example, we use `store.commit` to perform batch updates on the database. We insert a new set of authors into the `author` entity table. Then, we use `store.state.database.db` to query the `book` and `author` entities by their identifiers.
140
+
Reading
141
+
142
+
```swift
143
+
let book = state.database.db.book.find(by: .init("1"))
144
+
let author = state.database.db.author.find(by: .init("1"))
145
+
```
148
146
149
147
By using VergeNormalization, you can efficiently manage your application state with a normalized data structure, which simplifies your state management, reduces the computational complexity of operations, and improves the overall performance and maintainability of your application.
0 commit comments