-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor to clean up packages and start on functions client
- Loading branch information
1 parent
f28bab6
commit 27105e1
Showing
14 changed files
with
276 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.