openbase-js 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +27 -24
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
const axios = require('axios').default || require('axios');
|
|
2
|
-
|
|
3
1
|
// ─── Query Builder ────────────────────────────────────────────────────────────
|
|
4
2
|
|
|
5
3
|
class QueryBuilder {
|
|
@@ -17,8 +15,6 @@ class QueryBuilder {
|
|
|
17
15
|
this._updateData = null;
|
|
18
16
|
}
|
|
19
17
|
|
|
20
|
-
// ─── Read ──────────────────────────────────────────────────────────────────
|
|
21
|
-
|
|
22
18
|
select(columns = '*') {
|
|
23
19
|
this._columns = columns;
|
|
24
20
|
this._operation = 'select';
|
|
@@ -41,8 +37,6 @@ class QueryBuilder {
|
|
|
41
37
|
return this;
|
|
42
38
|
}
|
|
43
39
|
|
|
44
|
-
// ─── Write ─────────────────────────────────────────────────────────────────
|
|
45
|
-
|
|
46
40
|
insert(data) {
|
|
47
41
|
this._operation = 'insert';
|
|
48
42
|
this._insertData = data;
|
|
@@ -60,9 +54,7 @@ class QueryBuilder {
|
|
|
60
54
|
return this;
|
|
61
55
|
}
|
|
62
56
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
async _buildAndRun() {
|
|
57
|
+
_buildSQL() {
|
|
66
58
|
let sql = '';
|
|
67
59
|
|
|
68
60
|
if (this._operation === 'select') {
|
|
@@ -76,13 +68,13 @@ class QueryBuilder {
|
|
|
76
68
|
} else if (this._operation === 'insert') {
|
|
77
69
|
const cols = Object.keys(this._insertData).map(c => `"${c}"`).join(', ');
|
|
78
70
|
const vals = Object.values(this._insertData).map(v =>
|
|
79
|
-
typeof v === 'string' ? `'${v}'` : v
|
|
71
|
+
typeof v === 'string' ? `'${v.replace(/'/g, "''")}'` : v
|
|
80
72
|
).join(', ');
|
|
81
73
|
sql = `INSERT INTO "${this._table}" (${cols}) VALUES (${vals}) RETURNING *`;
|
|
82
74
|
|
|
83
75
|
} else if (this._operation === 'update') {
|
|
84
76
|
const setClauses = Object.entries(this._updateData).map(([col, val]) =>
|
|
85
|
-
typeof val === 'string' ? `"${col}" = '${val}'` : `"${col}" = ${val}`
|
|
77
|
+
typeof val === 'string' ? `"${col}" = '${val.replace(/'/g, "''")}'` : `"${col}" = ${val}`
|
|
86
78
|
).join(', ');
|
|
87
79
|
const whereParts = Object.entries(this._filters).map(([col, val]) =>
|
|
88
80
|
typeof val === 'string' ? `"${col}" = '${val}'` : `"${col}" = ${val}`
|
|
@@ -100,20 +92,25 @@ class QueryBuilder {
|
|
|
100
92
|
sql += ` RETURNING *`;
|
|
101
93
|
}
|
|
102
94
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
`${this._baseUrl}/query`,
|
|
106
|
-
{ sql, db_name: this._dbName },
|
|
107
|
-
{ headers: { Authorization: `Bearer ${this._apiKey}` } }
|
|
108
|
-
);
|
|
95
|
+
return sql;
|
|
96
|
+
}
|
|
109
97
|
|
|
110
|
-
|
|
98
|
+
async _buildAndRun() {
|
|
99
|
+
const sql = this._buildSQL();
|
|
100
|
+
try {
|
|
101
|
+
const res = await fetch(`${this._baseUrl}/query`, {
|
|
102
|
+
method: 'POST',
|
|
103
|
+
headers: {
|
|
104
|
+
'Content-Type': 'application/json',
|
|
105
|
+
'Authorization': `Bearer ${this._apiKey}`,
|
|
106
|
+
},
|
|
107
|
+
body: JSON.stringify({ sql, db_name: this._dbName }),
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const json = await res.json();
|
|
111
|
+
const { data, error } = json;
|
|
111
112
|
if (error) return { data: null, error };
|
|
112
|
-
|
|
113
|
-
if (this._single) {
|
|
114
|
-
return { data: data[0] || null, error: null };
|
|
115
|
-
}
|
|
116
|
-
|
|
113
|
+
if (this._single) return { data: data[0] || null, error: null };
|
|
117
114
|
return { data, error: null };
|
|
118
115
|
} catch (err) {
|
|
119
116
|
return { data: null, error: err.message };
|
|
@@ -145,4 +142,10 @@ function createClient(baseUrl, apiKey, dbName) {
|
|
|
145
142
|
return new OpenbaseClient(baseUrl, apiKey, dbName);
|
|
146
143
|
}
|
|
147
144
|
|
|
148
|
-
module
|
|
145
|
+
if (typeof module !== 'undefined') {
|
|
146
|
+
module.exports = { createClient };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (typeof window !== 'undefined') {
|
|
150
|
+
window.openbase = { createClient };
|
|
151
|
+
}
|