1
- /* A simple example of using Fraci without any ORM. We recommend using an ORM integration , as querying fractional indices manually can be error-prone. */
1
+ /* A simple example of using Fraci without any ORM. We strongly recommend using our ORM integrations , as querying fractional indices manually can be error-prone. */
2
2
3
3
import { zValidator } from "@hono/zod-validator" ;
4
4
import { BASE62 , fraci , type FractionalIndex } from "fraci" ;
5
5
import { Hono } from "hono" ;
6
6
import * as z from "zod" ;
7
7
8
8
// Define the type for our fractional index
9
- type ExampleItemFI = FractionalIndex <
10
- typeof BASE62 ,
11
- typeof BASE62 ,
12
- "core.exampleItem.fi"
13
- > ;
9
+ type FI = FractionalIndex < typeof BASE62 , typeof BASE62 , "core.exampleItem.fi" > ;
14
10
15
11
// Define the structure of our example item
16
12
interface ExampleItem {
17
- id : number ;
18
- name : string ;
19
- fi : ExampleItemFI ;
20
- groupId : number ;
13
+ readonly id : number ;
14
+ readonly name : string ;
15
+ readonly fi : FI ;
16
+ readonly groupId : number ;
21
17
}
22
18
23
19
// In-memory database tables
@@ -119,9 +115,11 @@ const queryUtils = {
119
115
} ;
120
116
121
117
// Fraci utility for the example items
118
+ // Implementing this manually is strongly discouraged in a real-world application, as it can be error-prone
119
+ // Instead, use our built-in ORM integrations for your database
122
120
const xfi = {
123
121
// Generate a key between two existing keys
124
- generateKeyBetween : ( a : ExampleItemFI | null , b : ExampleItemFI | null ) => {
122
+ generateKeyBetween : ( a : FI | null , b : FI | null ) => {
125
123
return fraciForExampleItem . generateKeyBetween ( a , b ) ;
126
124
} ,
127
125
@@ -131,13 +129,12 @@ const xfi = {
131
129
where : ( item ) => item . groupId === where . groupId ,
132
130
orderBy : { field : "fi" , direction : "asc" } ,
133
131
} ) ;
134
-
135
132
if ( items . length === 0 ) {
136
- return [ null , null ] as [ ExampleItemFI | null , ExampleItemFI | null ] ;
133
+ return [ null , null ] as [ FI | null , FI | null ] ;
137
134
}
138
135
139
136
const lastItem = items [ items . length - 1 ] as ExampleItem ;
140
- return [ lastItem . fi , null ] as [ ExampleItemFI | null , ExampleItemFI | null ] ;
137
+ return [ lastItem . fi , null ] as [ FI | null , FI | null ] ;
141
138
} ,
142
139
143
140
// Get indices for an item before a reference item
@@ -146,7 +143,6 @@ const xfi = {
146
143
where : ( item ) => item . groupId === where . groupId ,
147
144
orderBy : { field : "fi" , direction : "asc" } ,
148
145
} ) ;
149
-
150
146
if ( items . length === 0 ) {
151
147
return null ;
152
148
}
@@ -158,8 +154,7 @@ const xfi = {
158
154
159
155
const prev = refIndex > 0 ? items [ refIndex - 1 ] . fi : null ;
160
156
const curr = items [ refIndex ] . fi ;
161
-
162
- return [ prev , curr ] as [ ExampleItemFI | null , ExampleItemFI | null ] ;
157
+ return [ prev , curr ] as [ FI | null , FI | null ] ;
163
158
} ,
164
159
165
160
// Get indices for an item after a reference item
@@ -168,7 +163,6 @@ const xfi = {
168
163
where : ( item ) => item . groupId === where . groupId ,
169
164
orderBy : { field : "fi" , direction : "asc" } ,
170
165
} ) ;
171
-
172
166
if ( items . length === 0 ) {
173
167
return null ;
174
168
}
@@ -180,8 +174,7 @@ const xfi = {
180
174
181
175
const curr = items [ refIndex ] . fi ;
182
176
const next = refIndex < items . length - 1 ? items [ refIndex + 1 ] . fi : null ;
183
-
184
- return [ curr , next ] as [ ExampleItemFI | null , ExampleItemFI | null ] ;
177
+ return [ curr , next ] as [ FI | null , FI | null ] ;
185
178
} ,
186
179
187
180
// Check if an error is an index conflict error
@@ -194,7 +187,9 @@ const xfi = {
194
187
} ;
195
188
196
189
// Function to check for index conflicts
197
- function checkIndexConflict ( groupId : number , fi : ExampleItemFI ) : boolean {
190
+ // Implementing this manually is strongly discouraged in a real-world application, as it can be error-prone
191
+ // Instead, use the built-in unique constraint handling provided by your database or ORM
192
+ function checkIndexConflict ( groupId : number , fi : FI ) : boolean {
198
193
return exampleItems . some (
199
194
( item ) => item . groupId === groupId && item . fi === fi
200
195
) ;
0 commit comments