MagicData - A replacement of SQlite, CoreData or Realm.

Related tags

Database MagicData
Overview

MagicData

A replacement of SQlite, CoreData or Realm. It is very easy to use and is a light version.

Guides

MagicData

We use MagicData manage all the magic objects, which means MagicData can add, update or delete the objects. All the MagicDatas are in the some actor thread. In this way, we keep the thread-safe.

Here are two ways to create MagicData.

let magic = try awiat MagicData() // This will create a database at the app's document path
let magic try await MagicData(path: URL(fileURLWithPath: "").path) // This will create a database at your custom path
let magic = try await MagicData(type: .temporary) // This will create a auto-delete database
let magic = try await MagicData(type: .memory) // This will create a database in the memory

MagicObject

MagicObject is like the menaing of the table in sqlite, but it is more powerful. MagicObject is supposed to be a struct. But if you want to sync the non-magical value in the instance you can choose the class.

struct TestModel: MagicObject {
@PrimaryMagicValue var id: String

@MagicValue var name: String
@MagicValue var age: Int

@OptionMagicValue var school: Data?
@OptionMagicValue var petName: String?
@OptionMagicValue var hight: Double?

var customString: String {
"My ID: \(id), name: \(name)"
}

init() {}

init(name: String) {
self.name = name
}
}

All MagicObject need a line init() {}. If you want to use primary value to query the data or update the data you need to set the @PrimaryMagicValue. All the PrimaryMagicValues have a unique defualt value. MagicValue can save all the Magical value which it isn't option. It just for the init() {} line.. As you see, you can create a object without setting the value, but if you access the value, a crash will happen. OptionMagicValue like the MagicValue, but it can store the option value. It has a defualt value nil.

Although TestModel is a sturct but if you copy it, and change it, the value will change in the original instance too.

let test = TestModel(name: "hi")
let test2 = test
test2.name = "hello"
print(test.name) // Hello

And also if you just want to change the value of MagicValue, you don't need to set the struct as var.

If you gain a new instance form database, and change a value that you have had with the same primary value, the data will not sync between them.

let test = TestModel(name: "hi")
let id = test.id
magic.update(test)
let test2 = magic.object(of: TestModel.self).where { $0.id == id }
test2.name = "hello"

print(test.name) // "hi"

Magical

Magicals are kinds of values that can be stored in the database.

Now we support theses:

  • String will be stored as Text in the database.
  • UUID will be stored as Text in the database.
  • Int will be stored as Int in the database.
  • Double will be stored as Real in the database.
  • Data will be stored as Blob in the database.
  • Codable will be stored as Blob in the database.
  • Arrary will be stored as Blob in the database.
  • Dictionary will be stored as Blob in the database.

Points of Codable

First of all, we cannot store Codable, but it can be stored as MagicalCodable. MagicalCodable is a variant of Codable.

struct Job: MagicalCodable {
let title: String
let salary: Int
}

@OptionMagicValue var job: Job?

Ponints of Arrary & Dictionary

We only support the Arrary or Dictionary which conforms to the Codable.

Primary

Some value can be used in the @PrimaryMagicValue:

  • String has a defualt UUID String value.
  • UUID has a default UUID value.
  • Int has auto increase ability.

Add/Update

try await magic.update(object)

If object don't have primary value, every update is like add. If object has primary, update or add will base on whether it has been stored in the database.

Query All

try await magic.object(of: TestModel.self)

This will give back all the values.

Query by primary value

try await magic.object(of: TestModel.self, primary: AnyPrimaryValue)

This will throw a error if the primary value isn't in the database.

Know whethere the object exits

try await magic.has(of: TestModel.self, primary: instance1.uuid)

Requirement: MagicalObject has a primary value

Relationship

MagicData support relationship like core data.

One to One

struct TestModel: MagicObject {
    @PrimaryMagicValue var uuid: UUID

    @MagicValue var sub: Sub

    init() {}

    init(_ sub: Sub) {
        self.sub = sub
    }
}

struct Sub: MagicObject {
    @PrimaryMagicValue var uuid: UUID

    @MagicValue var text: String

    init() {}

    init(_ text: String) {
        self.text = text
    }
}

You could use @OptionMagicValue as well. This kind of relation is a little different of coredata's. Because it isn't a lazy value. That means, it will fetch the sub in the database, when you fetch the TestModel.

You might also like...
Swift APIs for SQLite: Type-safe down to the schema. Very, very, fast. Dependency free.
Swift APIs for SQLite: Type-safe down to the schema. Very, very, fast. Dependency free.

Lighter Lighter is a set of technologies applying code generation to access SQLite3 databases from Swift, e.g. in iOS applications or on the server. L

A library that provides the ability to import/export Realm files from a variety of data container formats.

Realm Converter Realm Converter is an open source software utility framework to make it easier to get data both in and out of Realm. It has been built

Sync Realm Database with CloudKit
Sync Realm Database with CloudKit

IceCream helps you sync Realm Database with CloudKit. It works like magic! Features Realm Database Off-line First Thread Safety Reactive Programming O

Unrealm is an extension on RealmCocoa, which enables Swift native types to be saved in Realm.
Unrealm is an extension on RealmCocoa, which enables Swift native types to be saved in Realm.

Unrealm enables you to easily store Swift native Classes, Structs and Enums into Realm . Stop inheriting from Object! Go for Protocol-Oriented program

Realm GeoQueries made easy
Realm GeoQueries made easy

RealmGeoQueries simplifies spatial queries with Realm Cocoa. In the absence of and official functions, this library provide the possibility to do prox

A simple order manager, created in order to try Realm database
A simple order manager, created in order to try Realm database

Overview A simple order manager, created in order to get acquainted with the features and limitations of the local Realm database. The project is writ

Creating a Todo app using Realm and SwiftUI
Creating a Todo app using Realm and SwiftUI

Realmで作るTodoアプリ note記事「【SwiftUI】Realmを使ってTodoアプリを作る」のソースです。

Realm-powered Core Data persistent store
Realm-powered Core Data persistent store

RealmIncrementalStore Realm-powered Core Data persistent store Wait, what? I like Realm. Realm's memory-mapped DB blows other databases out of the wat

Realm Manager for iOS
Realm Manager for iOS

Microservices for RealmSwift. Split realms for easier management and migration

Owner
Underthestars-zhy
Apple platform software developer. UI/UX Designer.
Underthestars-zhy
CoreData/Realm sweet wrapper written in Swift

What is SugarRecord? SugarRecord is a persistence wrapper designed to make working with persistence solutions like CoreData in a much easier way. Than

Modo 2.1k Dec 9, 2022
SQLite.swift - A type-safe, Swift-language layer over SQLite3.

SQLite.swift provides compile-time confidence in SQL statement syntax and intent.

Stephen Celis 8.7k Jan 3, 2023
A toolkit for SQLite databases, with a focus on application development

A toolkit for SQLite databases, with a focus on application development

Gwendal Roué 5.6k Jan 8, 2023
YapDB is a collection/key/value store with a plugin architecture. It's built atop sqlite, for Swift & objective-c developers.

YapDatabase is a collection/key/value store and so much more. It's built atop sqlite, for Swift & Objective-C developers, targeting macOS, iOS, tvOS &

Yap Studios 3.3k Dec 29, 2022
A Cocoa / Objective-C wrapper around SQLite

FMDB v2.7 This is an Objective-C wrapper around SQLite. The FMDB Mailing List: https://groups.google.com/group/fmdb Read the SQLite FAQ: https://www.s

August 13.7k Dec 28, 2022
Implement Student Admission System using SQlite

StudentAdmissionSQLiteApp Implement Student Admission System using SQlite. #Func

Hardik 2 Apr 27, 2022
BucketServer - Small API with SQLite database that saves notes for an iOS appliction called Bucket list

BucketList Server-Side Small API with SQLite database that saves notes for an iO

null 0 Dec 30, 2021
macOS Sqlite tableView 샘플 - objective c

목적 Objective C언어를 이용하여 macOS를 개발해본다. Sqlite를 이용하여 데이터를 저장하고, 불러와본다. FMDB를 이용한다. 데이터를 NSTableView를 이용하여 불러와본다. 추가적으로 NSOutlineView 구현해본다. 추가적으로 KVOCont

HyunSu Park 0 Jan 10, 2022
A Swift wrapper for SQLite databases

Squeal, a Swift interface to SQLite Squeal provides access to SQLite databases in Swift. Its goal is to provide a simple and straight-forward base API

Christian Niles 297 Aug 6, 2022
A stand-alone Swift wrapper around the SQLite 3 client library.

Perfect - SQLite Connector This project provides a Swift wrapper around the SQLite 3 library. This package builds with Swift Package Manager and is pa

PerfectlySoft Inc. 47 Nov 19, 2022