|
1 | 1 | import { Database } from '../drivers/database' |
2 | 2 | import { Fetch, fetchWithAuth } from './utils/fetch' |
3 | | -import { PubSubClient } from './pubsub/PubSubClient' |
| 3 | +import { PubSubClient } from './_pubsub/PubSubClient' |
4 | 4 | import { WebliteClient } from './weblite/WebliteClient' |
5 | | -import { StorageClient } from './storage/StorageClient' |
6 | | -import { SQLiteCloudCommand, SQLiteCloudError } from '../drivers/types' |
7 | | -import { cleanConnectionString, getDefaultDatabase } from './utils' |
| 5 | +import { StorageClient } from './_storage/StorageClient' |
| 6 | +import { SQLiteCloudDataTypes, SQLiteCloudError } from '../drivers/types' |
| 7 | +import { cleanConnectionString } from './utils' |
8 | 8 | import { FunctionsClient } from './_functions/FunctionsClient' |
9 | 9 | import { SQLiteCloudClientConfig } from './types' |
| 10 | +import { DEFAULT_HEADERS } from '../drivers/constants' |
| 11 | + |
| 12 | +const validateConfig = (config: SQLiteCloudClientConfig | string) => { |
| 13 | + if (!(config)) throw new SQLiteCloudError('No configuration provided') |
| 14 | + if (typeof config === 'string') { |
| 15 | + if (!config.includes('sqlitecloud://')) throw new SQLiteCloudError('Invalid connection string') |
| 16 | + } |
| 17 | + |
| 18 | + if (typeof config === 'object') { |
| 19 | + if (!config.connectionString) throw new SQLiteCloudError('No connection string provided') |
| 20 | + if (!config.connectionString.includes('sqlitecloud://')) throw new SQLiteCloudError('Invalid connection string') |
| 21 | + } |
| 22 | +} |
10 | 23 |
|
11 | 24 | export class SQLiteCloudClient { |
12 | 25 | protected connectionString: string |
13 | 26 | protected fetch: Fetch |
14 | | - protected globalHeaders: Record<string, string> |
15 | | - protected _defaultDb: string |
16 | | - protected _db: Database |
| 27 | + protected _globalHeaders: Record<string, string> |
| 28 | + protected _db: Database | null |
| 29 | + protected _pubSub: PubSubClient | null |
| 30 | + protected _weblite: WebliteClient | null |
17 | 31 |
|
18 | 32 | constructor(config: SQLiteCloudClientConfig | string) { |
19 | 33 | try { |
20 | | - if (!config) { |
21 | | - throw new SQLiteCloudError('Invalid connection string or config') |
22 | | - } |
| 34 | + validateConfig(config) |
23 | 35 | let connectionString: string |
24 | 36 | let customFetch: Fetch | undefined |
25 | 37 | let globalHeaders: Record<string, string> = {} |
26 | | - |
| 38 | + |
27 | 39 | if (typeof config === 'string') { |
28 | 40 | connectionString = cleanConnectionString(config) |
29 | | - globalHeaders = {} |
| 41 | + globalHeaders = DEFAULT_HEADERS |
30 | 42 | } else { |
31 | 43 | connectionString = config.connectionString |
32 | 44 | customFetch = config.global?.fetch |
33 | | - globalHeaders = config.global?.headers ?? {} |
| 45 | + globalHeaders = config.global?.headers ? { ...DEFAULT_HEADERS, ...config.global.headers } : DEFAULT_HEADERS |
34 | 46 | } |
35 | 47 |
|
36 | 48 | this.connectionString = connectionString |
37 | 49 | this.fetch = fetchWithAuth(this.connectionString, customFetch) |
38 | | - this.globalHeaders = globalHeaders |
39 | | - this._defaultDb = getDefaultDatabase(this.connectionString) ?? '' |
40 | | - this._db = new Database(this.connectionString) |
| 50 | + this._globalHeaders = globalHeaders |
| 51 | + this._db = null |
| 52 | + this._pubSub = null |
| 53 | + this._weblite = null |
41 | 54 |
|
42 | 55 | } catch (error) { |
43 | 56 | throw new SQLiteCloudError('failed to initialize SQLiteCloudClient') |
44 | 57 | } |
45 | 58 | } |
46 | | - |
47 | | - async sql(sql: TemplateStringsArray | string | SQLiteCloudCommand, ...values: any[]) { |
48 | | - this.db.exec(`USE DATABASE ${this._defaultDb}`) |
49 | | - try { |
50 | | - const result = await this.db.sql(sql, ...values) |
51 | | - return { data: result, error: null } |
52 | | - } catch (error) { |
53 | | - return { error, data: null } |
54 | | - } |
| 59 | + // Defaults to HTTP API |
| 60 | + async sql(sql: TemplateStringsArray | string, ...values: SQLiteCloudDataTypes[]) { |
| 61 | + return await this.weblite.sql(sql, ...values) |
55 | 62 | } |
56 | 63 |
|
57 | 64 | get pubSub() { |
58 | | - return new PubSubClient(this.db.getConfiguration()) |
| 65 | + if (!this._pubSub) { |
| 66 | + this._pubSub = new PubSubClient(this.db.getConfiguration()) |
| 67 | + } |
| 68 | + return this._pubSub |
59 | 69 | } |
60 | 70 |
|
61 | 71 | get db() { |
| 72 | + if (!this._db) { |
| 73 | + this._db = new Database(this.connectionString) |
| 74 | + } |
62 | 75 | return this._db |
63 | 76 | } |
64 | 77 |
|
65 | 78 | get weblite() { |
66 | | - return new WebliteClient(this.connectionString, { |
67 | | - customFetch: this.fetch, |
68 | | - headers: this.globalHeaders |
69 | | - }) |
| 79 | + if (!this._weblite) { |
| 80 | + this._weblite = new WebliteClient(this.connectionString, { |
| 81 | + fetch: this.fetch, |
| 82 | + headers: this._globalHeaders |
| 83 | + }) |
| 84 | + } |
| 85 | + return this._weblite |
70 | 86 | } |
71 | 87 |
|
72 | 88 | get files() { |
73 | 89 | return new StorageClient(this.connectionString, { |
74 | 90 | customFetch: this.fetch, |
75 | | - headers: this.globalHeaders |
| 91 | + headers: this._globalHeaders |
76 | 92 | }) |
77 | 93 | } |
78 | 94 |
|
79 | 95 | get functions() { |
80 | 96 | return new FunctionsClient(this.connectionString, { |
81 | 97 | customFetch: this.fetch, |
82 | | - headers: this.globalHeaders |
| 98 | + headers: this._globalHeaders |
83 | 99 | }) |
84 | 100 | } |
85 | | - |
86 | | - set defaultDb(dbName: string) { |
87 | | - this._defaultDb = dbName |
88 | | - } |
89 | | - |
90 | | - get defaultDb() { |
91 | | - return this._defaultDb |
92 | | - } |
93 | 101 | } |
94 | 102 |
|
95 | 103 | export function createClient(config: SQLiteCloudClientConfig | string): SQLiteCloudClient { |
|
0 commit comments