Skip to content

Commit

Permalink
refactor to clean up packages and start on functions client
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobprall committed Dec 25, 2024
1 parent f28bab6 commit 27105e1
Show file tree
Hide file tree
Showing 14 changed files with 276 additions and 146 deletions.
42 changes: 40 additions & 2 deletions README_Refactor.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,43 @@ Refactor 12.23.24
- Added FileClient class and methods for file upload and download
- Added SQLiteCloudVectorClient class and methods for upsert and query

Refactor 12.24.14
Refactor Summary
- The Plan:
- Improve the usability of the SQLite Cloud platform by consolidating various features
under a single client with one consistent design and interface
The Objective:
- Provide a streamlined and consistent api for discovering, learning and using features on SQLite Cloud
- Improve the visibility of various features on the SQLite Cloud platform by providing explicit namespaces and methods for:
- functions
- file storage
- Pub/Sub
- Vector search
- Weblite (platform-level database management)
- database (core database connection)
- Increase adoption of SQLite Cloud's JS SDK by expanding our documentation.
- Provide a solid architecture for future SDK development.
- Goals:
- Streamline the onboarding and implementation process for building JS apps on SQLite Cloud

Guidelines:
- Use consistent and scalable designs to improve readability, usability and maintainability.

Scope of work:
- refactor new code to improve code smells and readability
- Recap progress.
- packages
- functions:
- Purpose: used to interact with edge functions deployed on the SQLite Cloud platform
- Value: removes need for custom client
- Objective: simplify the onboarding and use of edge functions to increase adoption
- storage:
- Purpose: used to store files, with an emphasis on images and photos
- Value: unifies development experience of handling transactional and non-transactional data
- Objective: simplify the development process
- pubsub:
- Purpose: used to interact with the SQLite Cloud pubsub platform
- Value: removes need for custom client
- Objective: simplify the onboarding and use of pubsub to increase adoption
- write tests for each new class
- Idenitfy protential issues
- Plan refactor with psuedo code
Expand All @@ -21,4 +54,9 @@ TODO:
- add error handling and logging
- add tests
- add comments
- add documentation
- add documentation


Out of scope:
- Auth module (awaiting auth.js merge)
- Vector search module
2 changes: 1 addition & 1 deletion demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pubSub.notifyChannel('messages', 'my message')
* In the refactor, Database still exists and works as before.
*/

import { createClient } from './src/SQLiteCloudClient'
import { createClient } from './src/packages/SQLiteCloudClient'

const client = createClient('connection-string/chinook.db')

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sqlitecloud/drivers",
"version": "1.0.358",
"version": "1.0.359",
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
69 changes: 0 additions & 69 deletions src/SQLiteCloudClient.ts

This file was deleted.

5 changes: 4 additions & 1 deletion src/drivers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ if (typeof Deno !== 'undefined') {
JS_ENV = 'node'
}

export const DEFAULT_HEADERS = { 'X-Client-Info': `sqlitecloud-js-${JS_ENV}/${version}` }
export const DEFAULT_HEADERS = {
'X-Client-Info': `sqlitecloud-js-${JS_ENV}/${version}`,
'Content-Type': 'application/octet-stream'
}
export const DEFAULT_GLOBAL_OPTIONS = {
headers: DEFAULT_HEADERS
}
Expand Down
1 change: 1 addition & 0 deletions src/drivers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,5 @@ export enum SQLiteCloudArrayType {

export type UploadOptions = {
replace?: boolean
headers?: Record<string, string>
}
81 changes: 81 additions & 0 deletions src/packages/SQLiteCloudClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Database } from '../drivers/database'
import { Fetch, fetchWithAuth } from './utils/fetch'
import { PubSubClient } from './pubsub/PubSubClient'
import { SQLiteCloudWebliteClient } from './weblite/SQLiteCloudWebliteClient'
import { StorageClient } from './storage/SQLiteCloudStorageClient'
import { SQLiteCloudCommand, SQLiteCloudError } from '../drivers/types'
import { cleanConnectionString, getDefaultDatabase } from './utils'

interface SQLiteCloudClientConfig {
connectionString: string
fetch?: Fetch
}

export class SQLiteCloudClient {
protected connectionString: string
protected fetch: Fetch
protected _db: Database

constructor(config: SQLiteCloudClientConfig | string) {
try {
if (!config) {
throw new SQLiteCloudError('Invalid connection string or config')
}
let connectionString: string
let customFetch: Fetch | undefined

if (typeof config === 'string') {
connectionString = cleanConnectionString(config)
} else {
connectionString = config.connectionString
customFetch = config.fetch
}

this.connectionString = connectionString
this.fetch = fetchWithAuth(this.connectionString, customFetch)
this.defaultDb = getDefaultDatabase(this.connectionString) ?? ''
this._db = new Database(this.connectionString)
} catch (error) {
throw new SQLiteCloudError('failed to initialize SQLiteCloudClient')
}
}

async sql(sql: TemplateStringsArray | string | SQLiteCloudCommand, ...values: any[]) {
this.db.exec(`USE DATABASE ${this.defaultDb}`)
try {
const result = await this.db.sql(sql, ...values)
return { data: result, error: null }
} catch (error) {
return { error, data: null }
}
}

get pubSub() {
return new PubSubClient(this.db.getConfiguration())
}

get db() {
return this._db
}

get weblite() {
return new SQLiteCloudWebliteClient(this.connectionString, this.fetch)
}

get files() {
return new StorageClient(this.connectionString, this.fetch)
}

get functions() {
// return new SQLiteCloudFunctionsClient(this.connectionString, this.fetch)
return null
}

set defaultDb(dbName: string) {
this.defaultDb = dbName
}
}

export function createClient(config: SQLiteCloudClientConfig | string): SQLiteCloudClient {
return new SQLiteCloudClient(config)
}
2 changes: 2 additions & 0 deletions src/packages/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const FILES_DATABASE = 'files.sqlite'
export const FUNCTIONS_ROOT_PATH = 'functions'
36 changes: 36 additions & 0 deletions src/packages/functions/FunctionsClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { SQLiteCloudError } from '../../drivers/types'
import { FUNCTIONS_ROOT_PATH } from '../constants'
import { getAPIUrl } from '../utils'
import { Fetch, resolveFetch, resolveHeadersConstructor } from '../utils/fetch'

export class FunctionsClient {
protected url: string
protected fetch: Fetch
protected headers: Record<string, string>

constructor(
connectionString: string,
options: {
customFetch?: Fetch,
headers?: Record<string, string>
} = {}
) {
this.url = getAPIUrl(connectionString, FUNCTIONS_ROOT_PATH)
this.fetch = resolveFetch(options.customFetch)
this.headers = options.headers ?? {}
}
// auth token is the full connection string with apikey
setAuth(token: string) {
this.headers.Authorization = `Bearer ${token}`
}

async invoke(functionName: string, args: any[]) {
try {
// TODO IMPLEMENT
} catch (error) {
throw new SQLiteCloudError(`Failed to invoke function: ${error}`)
}
}


}
Loading

0 comments on commit 27105e1

Please sign in to comment.