|
| 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 | +} |
0 commit comments