Skip to content

Commit 27105e1

Browse files
committed
refactor to clean up packages and start on functions client
1 parent f28bab6 commit 27105e1

File tree

14 files changed

+276
-146
lines changed

14 files changed

+276
-146
lines changed

README_Refactor.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,43 @@ Refactor 12.23.24
77
- Added FileClient class and methods for file upload and download
88
- Added SQLiteCloudVectorClient class and methods for upsert and query
99

10-
Refactor 12.24.14
10+
Refactor Summary
11+
- The Plan:
12+
- Improve the usability of the SQLite Cloud platform by consolidating various features
13+
under a single client with one consistent design and interface
14+
The Objective:
15+
- Provide a streamlined and consistent api for discovering, learning and using features on SQLite Cloud
16+
- Improve the visibility of various features on the SQLite Cloud platform by providing explicit namespaces and methods for:
17+
- functions
18+
- file storage
19+
- Pub/Sub
20+
- Vector search
21+
- Weblite (platform-level database management)
22+
- database (core database connection)
23+
- Increase adoption of SQLite Cloud's JS SDK by expanding our documentation.
24+
- Provide a solid architecture for future SDK development.
25+
- Goals:
26+
- Streamline the onboarding and implementation process for building JS apps on SQLite Cloud
27+
28+
Guidelines:
29+
- Use consistent and scalable designs to improve readability, usability and maintainability.
30+
1131
Scope of work:
1232
- refactor new code to improve code smells and readability
1333
- Recap progress.
34+
- packages
35+
- functions:
36+
- Purpose: used to interact with edge functions deployed on the SQLite Cloud platform
37+
- Value: removes need for custom client
38+
- Objective: simplify the onboarding and use of edge functions to increase adoption
39+
- storage:
40+
- Purpose: used to store files, with an emphasis on images and photos
41+
- Value: unifies development experience of handling transactional and non-transactional data
42+
- Objective: simplify the development process
43+
- pubsub:
44+
- Purpose: used to interact with the SQLite Cloud pubsub platform
45+
- Value: removes need for custom client
46+
- Objective: simplify the onboarding and use of pubsub to increase adoption
1447
- write tests for each new class
1548
- Idenitfy protential issues
1649
- Plan refactor with psuedo code
@@ -21,4 +54,9 @@ TODO:
2154
- add error handling and logging
2255
- add tests
2356
- add comments
24-
- add documentation
57+
- add documentation
58+
59+
60+
Out of scope:
61+
- Auth module (awaiting auth.js merge)
62+
- Vector search module

demo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pubSub.notifyChannel('messages', 'my message')
4848
* In the refactor, Database still exists and works as before.
4949
*/
5050

51-
import { createClient } from './src/SQLiteCloudClient'
51+
import { createClient } from './src/packages/SQLiteCloudClient'
5252

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sqlitecloud/drivers",
3-
"version": "1.0.358",
3+
"version": "1.0.359",
44
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

src/SQLiteCloudClient.ts

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/drivers/constants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ if (typeof Deno !== 'undefined') {
1111
JS_ENV = 'node'
1212
}
1313

14-
export const DEFAULT_HEADERS = { 'X-Client-Info': `sqlitecloud-js-${JS_ENV}/${version}` }
14+
export const DEFAULT_HEADERS = {
15+
'X-Client-Info': `sqlitecloud-js-${JS_ENV}/${version}`,
16+
'Content-Type': 'application/octet-stream'
17+
}
1518
export const DEFAULT_GLOBAL_OPTIONS = {
1619
headers: DEFAULT_HEADERS
1720
}

src/drivers/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,5 @@ export enum SQLiteCloudArrayType {
163163

164164
export type UploadOptions = {
165165
replace?: boolean
166+
headers?: Record<string, string>
166167
}

src/packages/SQLiteCloudClient.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { Database } from '../drivers/database'
2+
import { Fetch, fetchWithAuth } from './utils/fetch'
3+
import { PubSubClient } from './pubsub/PubSubClient'
4+
import { SQLiteCloudWebliteClient } from './weblite/SQLiteCloudWebliteClient'
5+
import { StorageClient } from './storage/SQLiteCloudStorageClient'
6+
import { SQLiteCloudCommand, SQLiteCloudError } from '../drivers/types'
7+
import { cleanConnectionString, getDefaultDatabase } from './utils'
8+
9+
interface SQLiteCloudClientConfig {
10+
connectionString: string
11+
fetch?: Fetch
12+
}
13+
14+
export class SQLiteCloudClient {
15+
protected connectionString: string
16+
protected fetch: Fetch
17+
protected _db: Database
18+
19+
constructor(config: SQLiteCloudClientConfig | string) {
20+
try {
21+
if (!config) {
22+
throw new SQLiteCloudError('Invalid connection string or config')
23+
}
24+
let connectionString: string
25+
let customFetch: Fetch | undefined
26+
27+
if (typeof config === 'string') {
28+
connectionString = cleanConnectionString(config)
29+
} else {
30+
connectionString = config.connectionString
31+
customFetch = config.fetch
32+
}
33+
34+
this.connectionString = connectionString
35+
this.fetch = fetchWithAuth(this.connectionString, customFetch)
36+
this.defaultDb = getDefaultDatabase(this.connectionString) ?? ''
37+
this._db = new Database(this.connectionString)
38+
} catch (error) {
39+
throw new SQLiteCloudError('failed to initialize SQLiteCloudClient')
40+
}
41+
}
42+
43+
async sql(sql: TemplateStringsArray | string | SQLiteCloudCommand, ...values: any[]) {
44+
this.db.exec(`USE DATABASE ${this.defaultDb}`)
45+
try {
46+
const result = await this.db.sql(sql, ...values)
47+
return { data: result, error: null }
48+
} catch (error) {
49+
return { error, data: null }
50+
}
51+
}
52+
53+
get pubSub() {
54+
return new PubSubClient(this.db.getConfiguration())
55+
}
56+
57+
get db() {
58+
return this._db
59+
}
60+
61+
get weblite() {
62+
return new SQLiteCloudWebliteClient(this.connectionString, this.fetch)
63+
}
64+
65+
get files() {
66+
return new StorageClient(this.connectionString, this.fetch)
67+
}
68+
69+
get functions() {
70+
// return new SQLiteCloudFunctionsClient(this.connectionString, this.fetch)
71+
return null
72+
}
73+
74+
set defaultDb(dbName: string) {
75+
this.defaultDb = dbName
76+
}
77+
}
78+
79+
export function createClient(config: SQLiteCloudClientConfig | string): SQLiteCloudClient {
80+
return new SQLiteCloudClient(config)
81+
}

src/packages/constants/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const FILES_DATABASE = 'files.sqlite'
2+
export const FUNCTIONS_ROOT_PATH = 'functions'
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { SQLiteCloudError } from '../../drivers/types'
2+
import { FUNCTIONS_ROOT_PATH } from '../constants'
3+
import { getAPIUrl } from '../utils'
4+
import { Fetch, resolveFetch, resolveHeadersConstructor } from '../utils/fetch'
5+
6+
export class FunctionsClient {
7+
protected url: string
8+
protected fetch: Fetch
9+
protected headers: Record<string, string>
10+
11+
constructor(
12+
connectionString: string,
13+
options: {
14+
customFetch?: Fetch,
15+
headers?: Record<string, string>
16+
} = {}
17+
) {
18+
this.url = getAPIUrl(connectionString, FUNCTIONS_ROOT_PATH)
19+
this.fetch = resolveFetch(options.customFetch)
20+
this.headers = options.headers ?? {}
21+
}
22+
// auth token is the full connection string with apikey
23+
setAuth(token: string) {
24+
this.headers.Authorization = `Bearer ${token}`
25+
}
26+
27+
async invoke(functionName: string, args: any[]) {
28+
try {
29+
// TODO IMPLEMENT
30+
} catch (error) {
31+
throw new SQLiteCloudError(`Failed to invoke function: ${error}`)
32+
}
33+
}
34+
35+
36+
}

0 commit comments

Comments
 (0)