Sharing SQL queries between Server and Mobile databases

Overview

Sharing SQL queries between Server and Mobile databases

Overview

As we all know, code is expensive to maintain, and the more complex the code, the more cost associated with maintaining it. Since the beginning of time, software developers have worked very hard to achieve the goal of code reusability.

Couchbase Mobile 3.0 introduces support for SQL++ queries. As a developer, the first thought that came to mind was sharing queries used in projects targeting Couchbase Server and a mobile application. But, is this a good idea? Let’s explore this question using sample data and a proof of concept mobile application.

For full information please read the blog post here.

Prerequisites

  • This sample assumes familiarity with building SwiftUI apps with Xcode and with the basics of Couchbase Lite. If you are unfamiliar with the basics of Couchbase Lite, it is recommended that you walk through the Quickstart in Couchbase Lite with iOS, Swift, and UIKit on using Couchbase Lite as a standalone database

  • This sample assumes familiarity with running Couchbase Server either on your computer or inside of docker.

  • iOS (Xcode 12/13) - Download latest version from the Mac App Store or via Xcodes

Note: If you are using an older version of Xcode, which you need to retain for other development needs, make a copy of your existing version of Xcode and install the latest Xcode version. That way you can have multiple versions of Xcode on your Mac. More information can be found in Apple's Developer Documentation.

Installation

  • To clone the project from GitHub, type the following command in your terminal
git clone https://github.com/biozal/cblite-swiftui-icecream-locations.git

Try it Out

  • Open the IceCreamLocator.xcodeproj. The project would be located at /path/to/cblite-swiftui-icecream-locations/src
open IceCreamLocator.xcodeproj
  • Build and run the project using the simulator in Xcode. While you can run the app on a real device, we recommend the Simulator so you can see the debug logs in the output console.

Data Model

The sample data used for the following tests comes from the OpenStreetMap project and is licensed under the Open Data Commons Open Database License (ODbL) by the OpenStreetMap Foundation.

The data set contains all the shops that sell Ice Cream in the United States. An example JSON document is listed below:

{
  "type": "Feature",
  "id": "node/472242349",
  "properties": {
    "addrCity": "Austin",
    "addrHousenumber": "4477",
    "addrPostcode": "78745",
    "addrState": "TX",
    "addrStreet": "South Lamar Boulevard",
    "addrUnit": "#790",
    "amenity": "ice_cream",
    "cuisine": "ice_cream",
    "name": "Amy's Ice Creams",
    "phone": "+1-512-891-0573",
    "id": "node/472242349"
  },
  "geometry": {
    "type": "Point",
    "coordinates": [
      -97.7998856,
      30.230688
    ]
  }
}

Importing Data into a bucket in Couchbase Server

I assume you already have Couchbase Server running. If you do not, directions on how to get it setup can be found on the Couchbase Developer Portal.

From the Coucbhase Web Console create a bucket called icecream. Take the default stats as the documents you will import don't take a lot of space. If you don't know how to create a bucket, you can follow the documentation here.

The Couchbase documentation on importing documents can be used to walk you through importing the sample data found in the data folder (us-south-ice-cream-cbserver.json).

Once you have imported the data, you can use the SQL statements in the create-indexes-couchbase-server.sql file found in the data folder to create the indexes I used in the blog article. All the queries used in the blog article can be found in the article-cbserver-sql-statements.sql file.

SwiftUI Application

The application is based on SwiftUI which targets iOS, iPadOS, and MacOS. The application uses Apple's Combine framework for binding information between the View and the ViewModel. The application structure is:

  • Shared folder: All the shared code between iOS and MacOS so that the app can run on either platform
    • Data folder: This is the location of the Repository protocol and IceCreamLocationRepository class that is used to communicate with the Couchbase Lite database. All functions that open and retrieve information are located here.
    • prebuild folder: This is where the prebuild database icecream.cblite2 is located and used in the demo. This database has all the JSON documents pre-imported into it.
    • ViewModels folder: this is the location of where the IceCreamListViewModel is located. This communicates with the IceCreamLocationRepository and handles state of information that will be displayed in the application.
    • Views folder: The views folder is the location of the IceCreamLocationListView which contains the SwiftUI code that renders the UI on the screen in the mobile application.

More Information

For more information about Couchbase Mobile head over to the Couchbase Developer Portal.

You might also like...
PassValuesBetweenViewControllers - Pass Values Between ViewControllers
PassValuesBetweenViewControllers - Pass Values Between ViewControllers

PassValuesBetweenViewControllers Vista creada con: Programmatic + AutoLayout Bre

An efficient, small mobile key-value storage framework developed by WeChat. Works on Android, iOS, macOS, Windows, and POSIX.
An efficient, small mobile key-value storage framework developed by WeChat. Works on Android, iOS, macOS, Windows, and POSIX.

中文版本请参看这里 MMKV is an efficient, small, easy-to-use mobile key-value storage framework used in the WeChat application. It's currently available on Andr

Realm is a mobile database: a replacement for Core Data & SQLite

Realm is a mobile database that runs directly inside phones, tablets or wearables. This repository holds the source code for the iOS, macOS, tvOS & wa

A fast, pure swift MongoDB driver based on Swift NIO built for Server Side Swift
A fast, pure swift MongoDB driver based on Swift NIO built for Server Side Swift

A fast, pure swift MongoDB driver based on Swift NIO built for Server Side Swift. It features a great API and a battle-tested core. Supporting both MongoDB in server and embedded environments.

Web server serving local files

swift-web A web server serving local static files. Installation Using Mint The easiest way to install swift-web is via mint. mint install adam-fowler/

This Project domonstrate the latest Swift on Server to create RESTFul API's connected via Database: MongoDB NoSql

Swift is a general-purpose programming language built using a modern approach to safety & performance that make it specifically suitable for Server applications. Vapor is a web framework for Swift, allowing you to write backends, web apps APIs and HTTP servers in Swift

Server-driven SwiftUI - Maintain iOS apps without making app releases.

ServerDrivenSwiftUI Maintain ios apps without making app releases. Deploy changes to the server and users will receive changes within minutes. This pa

Prephirences is a Swift library that provides useful protocols and convenience methods to manage application preferences, configurations and app-state. UserDefaults
Prephirences is a Swift library that provides useful protocols and convenience methods to manage application preferences, configurations and app-state. UserDefaults

Prephirences - Preϕrences Prephirences is a Swift library that provides useful protocols and convenience methods to manage application preferences, co

Store and retrieve Codable objects to various persistence layers, in a couple lines of code!
Store and retrieve Codable objects to various persistence layers, in a couple lines of code!

tl;dr You love Swift's Codable protocol and use it everywhere, who doesn't! Here is an easy and very light way to store and retrieve Codable objects t

Owner
Aaron LaBeau
Senior Developer Advocate. Loves Visual Studio, Android Studio, XCode, .NET/Maui, Kotlin, Swift, building APIs, and IoT programming. Opinions are my own.
Aaron LaBeau
An alternative to Core Data for people who like having direct SQL access.

FCModel 2 An alternative to Core Data for people who like having direct SQL access. By Marco Arment. See the LICENSE file for license info (it's the M

null 1.7k Dec 28, 2022
High performance JSONPath queries for Swift

Sextant Sextant is a complete, high performance JSONPath implementation written in Swift. It was originally ported from SMJJSONPath, which in turn is

Rocco Bowling 30 Dec 27, 2022
Build declarative GraphQL queries in Swift.

SociableWeaver Swift meets GraphQL in this lightweight, easy to use framework. SociableWeaver uses a declarative style of programming and makes GraphQ

Nicholas Bellucci 71 Nov 20, 2022
Commands providing shortcuts to common Postgres introspection queries (Swift port of heroku-pg-extras)

Commands providing shortcuts to common Postgres introspection queries (Swift port of heroku-pg-extras)

Sven A. Schmidt 2 May 27, 2022
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
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
rTracker is a complete iOS application for creating local on-device databases ('trackers') to log data with timestamps.

rTracker is a complete iOS application for creating local on-device databases ('trackers') to log data with timestamps. Trackers can use a va

Rob Miller 40 Dec 10, 2022
Classes-and-structures-in-swift - This source files show what is the difference between class and structure

This source files show what is the difference between class and structure You ca

null 0 Jan 4, 2022
Elegant library to manage the interactions between view and model in Swift

An assistant to manage the interactions between view and model ModelAssistant is a mediator between the view and model. This framework is tailored to

Seyed Samad Gholamzadeh 28 Jan 29, 2022
Amplitude Analytics 0 Feb 1, 2022