| 62 | } |
| 63 | |
| 64 | export class StarbaseQuery implements QueryableBaseDriver { |
| 65 | protected url: string; |
| 66 | protected headers: Record<string, string>; |
| 67 | |
| 68 | constructor( |
| 69 | protected _url: string, |
| 70 | protected token: string, |
| 71 | protected type: string = "internal" |
| 72 | ) { |
| 73 | this.url = `${_url.replace(/\/$/, "")}/query/raw`; |
| 74 | this.headers = { |
| 75 | Authorization: `Bearer ${this.token}`, |
| 76 | "Content-Type": "application/json", |
| 77 | }; |
| 78 | |
| 79 | if (type !== "internal") { |
| 80 | this.headers["X-Starbase-Source"] = type; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | async transaction(stmts: string[]): Promise<DatabaseResultSet[]> { |
| 85 | const r = await fetch(this.url, { |
| 86 | method: "POST", |
| 87 | headers: this.headers, |
| 88 | body: JSON.stringify({ |
| 89 | transaction: stmts.map((s) => ({ sql: s })), |
| 90 | }), |
| 91 | }); |
| 92 | |
| 93 | const json: StarbaseResponse = await r.json(); |
| 94 | return (Array.isArray(json.result) ? json.result : [json.result]).map( |
| 95 | transformRawResult |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | async query(stmt: string): Promise<DatabaseResultSet> { |
| 100 | const r = await fetch(this.url, { |
| 101 | method: "POST", |
| 102 | headers: this.headers, |
| 103 | body: JSON.stringify({ sql: stmt }), |
| 104 | }); |
| 105 | |
| 106 | const json: StarbaseResponse = await r.json(); |
| 107 | |
| 108 | return transformRawResult( |
| 109 | Array.isArray(json.result) ? json.result[0] : json.result |
| 110 | ); |
| 111 | } |
| 112 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…