Skip to content

A lightweight, type safe multiplatform library for CRUD operation on Google Sheets

License

Notifications You must be signed in to change notification settings

ayodelekehinde/sheets-db

Repository files navigation

Sheets-DB

Production actions Kotlin Maven Central

badge-jvm

A lightweight Kotlin multiplatform library that turns Google Sheets into a remote mini-database.

Features

  • Basic database functions: CRUD
  • Type safe
  • Fast and light
  • Multiplatform (soon)

Adding to your project

Sheets-DB is published on Maven Central

repositories { 
  mavenCentral()
}

Include the dependency. Latest version Maven Central

implementation("io.github.cherrio-llc:sheets-db:<version>")

Usage

Init Sheets DB

val sheetDb = SheetsDb {
    bearerToken = "your_token"
    sheetId = "google_sheet_id" //it is always embedded in the URL
}

Create a data class that will map to a sheet column names. This data class must be annotated with @Serializable from the kotlinx.serialization library. You can map the Google sheet column names to the created data class, or you can provide @SerialName()

@Serializable
data class User(
    @SerialName("firstName")
    val name: String,
    val id: Int,
    val email: String?
)

NOTE: The class name must match the sheet's name on the spreadsheet. In this example, the class User is a name of a sheet. Check the image below. Also your first column of a row should be an id for better queries.

alt text

Create a table

val table = sheetDb.getTable<User>()

This getTable function returns a SheetTable<T> that you will subsequently use for CRUD ops

Create Read Update and Delete ops

Read

Read your table

val table = sheetDb.get() //returns List<User>

Query your table

val result = table.find(User::id eq 1) //returns a List or empty list if no match

Write

Add data to your table.

val user = User("Guy Merve", 11, "guy@email.com")
val result = table.create(user)

Update

Update a specific row using its id you have to pass in the id. You can pass in the only fields that needs updating. Here we don't want to update the email

val user = User("Capt Marvel", 5, null)
val result = table.create(user)

RoadMap

There's a lot of updates to come.

  1. Delete api
  2. More platforms
  3. E.t.c

License

Copyright 2022 Ayodele Kehinde

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.