github.com/nizigama/simple-note@v0.0.0-20210501235539-2cc762e06fe8/services/database/bolt.go (about) 1 package boltDB 2 3 import ( 4 "encoding/binary" 5 "encoding/json" 6 "fmt" 7 "log" 8 "time" 9 10 "github.com/boltdb/bolt" 11 ) 12 13 var ( 14 db *bolt.DB = nil 15 err error 16 ) 17 18 // InitDBConnection initiates the connection with a database 19 func InitDBConnection(logger *log.Logger, tables ...string) { 20 db, err = bolt.Open("simpnote.db", 0600, &bolt.Options{Timeout: 2 * time.Second}) 21 if err != nil { 22 logger.Fatalln("Initiate database connection:", err) 23 } 24 25 // checking if there are tables, and if there aren't create them 26 err = db.Update(func(tx *bolt.Tx) error { 27 28 for _, table := range tables { 29 if _, tErr := tx.CreateBucketIfNotExists([]byte(table)); tErr != nil { 30 return tErr 31 } 32 } 33 34 return nil 35 }) 36 37 if err != nil { 38 logger.Fatalln("Create database tables:", err) 39 } 40 } 41 42 func Store(item map[string]interface{}, tableName string) (uint64, error) { 43 44 var id []byte = make([]byte, 8) 45 xi, err := json.Marshal(item) 46 47 if err != nil { 48 return 0, err 49 } 50 51 var userID uint64 52 53 err = db.Update(func(tx *bolt.Tx) error { 54 55 b := tx.Bucket([]byte(tableName)) 56 57 if b == nil { 58 return fmt.Errorf("no such table found") 59 } 60 61 userID, err = b.NextSequence() 62 63 if err != nil { 64 return err 65 } 66 67 binary.LittleEndian.PutUint32(id, uint32(userID)) 68 69 return b.Put(id, xi) 70 }) 71 72 if err != nil { 73 return 0, err 74 } 75 76 return userID, nil 77 } 78 79 func Show(itemID uint64, tableName string) (map[string]interface{}, error) { 80 81 var itemData map[string]interface{} 82 result := []byte{} 83 84 err := db.View(func(tx *bolt.Tx) error { 85 86 b := tx.Bucket([]byte(tableName)) 87 88 if b == nil { 89 return fmt.Errorf("no such table found") 90 } 91 92 id := make([]byte, 8) 93 94 binary.LittleEndian.PutUint32(id, uint32(itemID)) 95 96 result = b.Get(id) 97 if result == nil { 98 return fmt.Errorf("no such item found") 99 } 100 101 return nil 102 }) 103 104 if err != nil { 105 return nil, err 106 } 107 108 err = json.Unmarshal(result, &itemData) 109 110 if err != nil { 111 return nil, err 112 } 113 114 return itemData, nil 115 } 116 117 func All(tableName string) ([]map[string]interface{}, error) { 118 119 var itemsData []map[string]interface{} 120 121 err := db.View(func(tx *bolt.Tx) error { 122 123 b := tx.Bucket([]byte(tableName)) 124 125 if b == nil { 126 return fmt.Errorf("no such table found") 127 } 128 129 err = b.ForEach(func(k []byte, v []byte) error { 130 131 itemData := map[string]interface{}{} 132 133 err = json.Unmarshal(v, &itemData) 134 135 if err != nil { 136 return err 137 } 138 itemData["itemID"] = binary.LittleEndian.Uint64(k) 139 140 itemsData = append(itemsData, itemData) 141 142 return nil 143 }) 144 if err != nil { 145 return fmt.Errorf("no such item found") 146 } 147 148 return nil 149 }) 150 151 if err != nil { 152 return nil, err 153 } 154 155 return itemsData, nil 156 } 157 158 func SingleByStringField(tableName, fieldName, fieldValue string) (map[string]interface{}, uint64, error) { 159 160 var matchingItemData map[string]interface{} 161 var key uint64 162 163 err := db.View(func(tx *bolt.Tx) error { 164 165 b := tx.Bucket([]byte(tableName)) 166 167 if b == nil { 168 return fmt.Errorf("no such table found") 169 } 170 171 err = b.ForEach(func(k []byte, v []byte) error { 172 173 itemData := map[string]interface{}{} 174 175 err = json.Unmarshal(v, &itemData) 176 177 if err != nil { 178 return err 179 } 180 181 if itemData[fieldName] == fieldValue { 182 key = binary.LittleEndian.Uint64(k) 183 matchingItemData = itemData 184 } 185 186 return nil 187 }) 188 if err != nil || key == 0 { 189 return fmt.Errorf("no such item found") 190 } 191 192 return nil 193 }) 194 195 if err != nil { 196 return nil, 0, err 197 } 198 199 return matchingItemData, key, nil 200 } 201 202 func ManyByStringField(tableName, fieldName, fieldValue string) ([]map[string]interface{}, error) { 203 204 var matchingItemsData []map[string]interface{} 205 206 err := db.View(func(tx *bolt.Tx) error { 207 208 b := tx.Bucket([]byte(tableName)) 209 210 if b == nil { 211 return fmt.Errorf("no such table found") 212 } 213 214 err = b.ForEach(func(k []byte, v []byte) error { 215 216 itemData := map[string]interface{}{} 217 218 err = json.Unmarshal(v, &itemData) 219 220 if err != nil { 221 return err 222 } 223 224 if itemData[fieldName] == fieldValue { 225 matchingItemsData = append(matchingItemsData, itemData) 226 } 227 228 return nil 229 }) 230 if err != nil || len(matchingItemsData) == 0 { 231 return fmt.Errorf("no such item found") 232 } 233 234 return nil 235 }) 236 237 if err != nil { 238 return nil, err 239 } 240 241 return matchingItemsData, nil 242 } 243 244 func ManyByIntField(tableName, fieldName string, fieldValue int) ([]map[string]interface{}, error) { 245 246 var matchingItemsData []map[string]interface{} 247 248 err := db.View(func(tx *bolt.Tx) error { 249 250 b := tx.Bucket([]byte(tableName)) 251 252 if b == nil { 253 return fmt.Errorf("no such table found") 254 } 255 256 err = b.ForEach(func(k []byte, v []byte) error { 257 258 itemData := map[string]interface{}{} 259 260 err = json.Unmarshal(v, &itemData) 261 262 if err != nil { 263 return err 264 } 265 266 itemData["itemID"] = binary.LittleEndian.Uint64(k) 267 268 if itemData[fieldName].(float64) == float64(fieldValue) { 269 matchingItemsData = append(matchingItemsData, itemData) 270 } 271 272 return nil 273 }) 274 275 if err != nil { 276 return fmt.Errorf("no such item found") 277 } 278 279 return nil 280 }) 281 282 if err != nil { 283 return nil, err 284 } 285 286 return matchingItemsData, nil 287 } 288 289 func Update(item map[string]interface{}, tableName string, itemID uint64) error { 290 291 xi, err := json.Marshal(item) 292 293 if err != nil { 294 return err 295 } 296 297 err = db.Update(func(tx *bolt.Tx) error { 298 299 b := tx.Bucket([]byte(tableName)) 300 301 if b == nil { 302 return fmt.Errorf("no such table found") 303 } 304 305 id := make([]byte, 8) 306 307 binary.LittleEndian.PutUint32(id, uint32(itemID)) 308 309 result := b.Get(id) 310 311 if result == nil { 312 return fmt.Errorf("no item found") 313 } 314 315 err = b.Delete(id) 316 317 if err != nil { 318 return fmt.Errorf("error deleting old item") 319 } 320 321 return nil 322 }) 323 324 if err != nil { 325 return err 326 } 327 328 err = db.Update(func(tx *bolt.Tx) error { 329 330 b := tx.Bucket([]byte(tableName)) 331 332 if b == nil { 333 return fmt.Errorf("no such table found") 334 } 335 336 id := make([]byte, 8) 337 338 binary.LittleEndian.PutUint32(id, uint32(itemID)) 339 340 return b.Put(id, xi) 341 }) 342 343 return nil 344 } 345 346 func Delete(tableName string, itemID uint64) error { 347 348 err = db.Update(func(tx *bolt.Tx) error { 349 350 b := tx.Bucket([]byte(tableName)) 351 352 if b == nil { 353 return fmt.Errorf("no such table found") 354 } 355 356 id := make([]byte, 8) 357 358 binary.LittleEndian.PutUint32(id, uint32(itemID)) 359 360 result := b.Get(id) 361 362 if result == nil { 363 return fmt.Errorf("no item found") 364 } 365 366 err = b.Delete(id) 367 368 if err != nil { 369 return fmt.Errorf("error deleting item") 370 } 371 372 return nil 373 }) 374 375 if err != nil { 376 return err 377 } 378 379 return nil 380 } 381 382 // CloseDBConnection closes the database connection and releases the db so that other apps can use it 383 func CloseDBConnection() { 384 db.Close() 385 }