{"primaryContentSections":[{"kind":"content","content":[{"anchor":"Overview","level":2,"type":"heading","text":"Overview"},{"type":"paragraph","inlineContent":[{"type":"text","text":"You can learn about the "},{"type":"reference","isActive":true,"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/StorageEngine"},{"type":"text","text":" protocol in "},{"type":"reference","isActive":true,"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/Using-StorageEngines"},{"type":"text","text":", but at it’s most basic a "},{"type":"reference","isActive":true,"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/StorageEngine"},{"type":"text","text":" defines functions such as "},{"type":"codeVoice","code":"read(key: CacheKey)"},{"type":"text","text":", "},{"type":"codeVoice","code":"write(data: Data, key: CacheKey)"},{"type":"text","text":", "},{"type":"codeVoice","code":"remove(key: CacheKey)"},{"type":"text","text":", to let you read, store, and delete "},{"type":"codeVoice","code":"Data"},{"type":"text","text":" from your data storage mechanism."}]},{"type":"paragraph","inlineContent":[{"type":"text","text":"As Swift developers we’re used to interacting with higher-level types rather than "},{"type":"codeVoice","code":"Data"},{"type":"text","text":". Our apps include types such as "},{"type":"codeVoice","code":"String"},{"type":"text","text":", "},{"type":"codeVoice","code":"URL"},{"type":"text","text":", or custom models such as "},{"type":"codeVoice","code":"Note"},{"type":"text","text":" or "},{"type":"codeVoice","code":"Article"},{"type":"text","text":". "},{"type":"reference","isActive":true,"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/ObjectStorage"},{"type":"text","text":" allows you to read, store, and delete Swift objects, that way you don’t have to think about the underlying data."}]},{"anchor":"Setting-Up-ObjectStorage","level":2,"type":"heading","text":"Setting Up ObjectStorage"},{"type":"paragraph","inlineContent":[{"type":"text","text":"The relationship between "},{"type":"reference","isActive":true,"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/ObjectStorage"},{"type":"text","text":" and "},{"type":"reference","isActive":true,"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/StorageEngine"},{"type":"text","text":" is that an "},{"type":"reference","isActive":true,"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/ObjectStorage"},{"type":"text","text":" is initialized with a "},{"type":"reference","isActive":true,"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/StorageEngine"},{"type":"text","text":", but the API you’ll be interacting does not change no matter which "},{"type":"reference","isActive":true,"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/StorageEngine"},{"type":"text","text":" you choose. This is very powerful because it gives us one API to work with no matter if we’re saving files to disk, reading objects from a database, or even downloading models from CloudKit. To achieve this we have only one requirement for our model, it must conform to "},{"type":"codeVoice","code":"Codable"},{"type":"text","text":". "},{"type":"reference","isActive":true,"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/ObjectStorage"},{"type":"text","text":" requires "},{"type":"codeVoice","code":"Codable"},{"type":"text","text":" conformance so we can serialize items to their data storage, or even send them over the network."}]},{"type":"codeListing","syntax":"swift","code":["\/\/ This could be any StorageEngine, so let's use the default SQLiteStorageEngine","let articlesStorageEngine = SQLiteStorageEngine.default(appendingPath: \"Articles\")","let articlesStorage = ObjectStorage(storage: articlesStorageEngine)","","\/\/ Or on one line","let articlesStorage = ObjectStorage
("," storage: SQLiteStorageEngine.default(appendingPath: \"Articles\")",")"]},{"type":"paragraph","inlineContent":[{"type":"text","text":"Now that "},{"type":"reference","isActive":true,"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/ObjectStorage"},{"type":"text","text":" is setup, using it couldn’t be simpler. The API is very similar to the "},{"type":"reference","isActive":true,"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/StorageEngine"},{"type":"text","text":" API, but with slight function name differences to make the API feel more natural when working with Swift types rather than raw "},{"type":"codeVoice","code":"Data"},{"type":"text","text":"."}]},{"type":"codeListing","syntax":"swift","code":["let article = Article(title: \"How To Learn Swift\", text: \"... and then you practice for years\")","","\/\/ Write an article to your ObjectStorage","try await articlesStorage.store(article, forKey: CacheKey(\"how-to-learn-swift\"))","","\/\/ Read the article from your ObjectStorage","try await storage.object(forKey: CacheKey(\"how-to-learn-swift\"))"]},{"type":"paragraph","inlineContent":[{"type":"text","text":"And that’s it! You now have a data storage layer that can work with your app’s Swift types in only a few lines of code."}]}]}],"schemaVersion":{"major":0,"minor":2,"patch":0},"sections":[],"variants":[{"paths":["\/documentation\/bodega\/using-objectstorage"],"traits":[{"interfaceLanguage":"swift"}]}],"identifier":{"url":"doc:\/\/Bodega\/documentation\/Bodega\/Using-ObjectStorage","interfaceLanguage":"swift"},"abstract":[{"type":"reference","isActive":true,"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/ObjectStorage"},{"type":"text","text":" serves as unified layer over "},{"type":"reference","isActive":true,"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/StorageEngine"},{"type":"text","text":", allowing you to work with type-safe Swift models rather than "},{"type":"codeVoice","code":"Data"},{"type":"text","text":"."}],"kind":"article","metadata":{"roleHeading":"Article","title":"Using ObjectStorage","role":"article","modules":[{"name":"Bodega"}]},"hierarchy":{"paths":[["doc:\/\/Bodega\/documentation\/Bodega"]]},"seeAlsoSections":[{"title":"Fundamentals","identifiers":["doc:\/\/Bodega\/documentation\/Bodega\/Using-StorageEngines"],"generated":true}],"references":{"doc://Bodega/documentation/Bodega/Using-StorageEngines":{"role":"article","title":"Using StorageEngines","abstract":[{"type":"text","text":"The "},{"type":"reference","isActive":true,"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/StorageEngine"},{"type":"text","text":" is at the heart of what makes Bodega, Bodega."}],"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/Using-StorageEngines","kind":"article","type":"topic","url":"\/documentation\/bodega\/using-storageengines"},"doc://Bodega/documentation/Bodega/ObjectStorage":{"role":"symbol","title":"ObjectStorage","fragments":[{"kind":"keyword","text":"class"},{"kind":"text","text":" "},{"kind":"identifier","text":"ObjectStorage"}],"abstract":[{"type":"text","text":"A unified layer over a "},{"type":"reference","isActive":true,"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/StorageEngine"},{"type":"text","text":" primitives, allowing you to read, write, and save Swift objects."}],"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/ObjectStorage","kind":"symbol","type":"topic","navigatorTitle":[{"kind":"identifier","text":"ObjectStorage"}],"url":"\/documentation\/bodega\/objectstorage"},"doc://Bodega/documentation/Bodega":{"role":"collection","title":"Bodega","abstract":[{"type":"text","text":"A simple store all your basic needs, and also so much more. 🐱"}],"identifier":"doc:\/\/Bodega\/documentation\/Bodega","kind":"symbol","type":"topic","url":"\/documentation\/bodega"},"doc://Bodega/documentation/Bodega/StorageEngine":{"role":"symbol","title":"StorageEngine","fragments":[{"kind":"keyword","text":"protocol"},{"kind":"text","text":" "},{"kind":"identifier","text":"StorageEngine"}],"abstract":[{"type":"text","text":"A "},{"type":"reference","isActive":true,"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/StorageEngine"},{"type":"text","text":" represents a data storage mechanism for saving and persisting data."}],"identifier":"doc:\/\/Bodega\/documentation\/Bodega\/StorageEngine","kind":"symbol","type":"topic","navigatorTitle":[{"kind":"identifier","text":"StorageEngine"}],"url":"\/documentation\/bodega\/storageengine"}}}