Skip to content

Commit eaa9950

Browse files
committed
Adds the ability to fetch types
1 parent 6ad4061 commit eaa9950

File tree

16 files changed

+291
-31
lines changed

16 files changed

+291
-31
lines changed

dist/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ router.use('/plugins', connectionMiddleware_1.addConnectionToRequest, require('.
1111
router.use('/query', connectionMiddleware_1.addConnectionToRequest, require('./api/query'));
1212
router.use('/schemas', connectionMiddleware_1.addConnectionToRequest, require('./api/schemas'));
1313
router.use('/tables', connectionMiddleware_1.addConnectionToRequest, require('./api/tables'));
14+
router.use('/types', connectionMiddleware_1.addConnectionToRequest, require('./api/types'));
1415
router.use('/users', connectionMiddleware_1.addConnectionToRequest, require('./api/users'));
1516
module.exports = router;

dist/api/schemas.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ router.get('/', function (req, res) { return __awaiter(void 0, void 0, void 0, f
6565
});
6666
}); });
6767
var removeSystemSchemas = function (data) {
68-
return data.filter(function (x) { return !schemas_1.DEFAULT_SYSTEM_SCHEMAS.includes(x.schema_name); });
68+
return data.filter(function (x) { return !schemas_1.DEFAULT_SYSTEM_SCHEMAS.includes(x.name); });
6969
};
7070
module.exports = router;

dist/api/types.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
var __generator = (this && this.__generator) || function (thisArg, body) {
12+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14+
function verb(n) { return function (v) { return step([n, v]); }; }
15+
function step(op) {
16+
if (f) throw new TypeError("Generator is already executing.");
17+
while (_) try {
18+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19+
if (y = 0, t) op = [op[0] & 2, t.value];
20+
switch (op[0]) {
21+
case 0: case 1: t = op; break;
22+
case 4: _.label++; return { value: op[1], done: false };
23+
case 5: _.label++; y = op[1]; op = [0]; continue;
24+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
25+
default:
26+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30+
if (t[2]) _.ops.pop();
31+
_.trys.pop(); continue;
32+
}
33+
op = body.call(thisArg, _);
34+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36+
}
37+
};
38+
Object.defineProperty(exports, "__esModule", { value: true });
39+
var Router = require('express').Router;
40+
var router = new Router();
41+
var types = require('../lib/sql').types;
42+
var RunQuery = require('../lib/connectionPool');
43+
var schemas_1 = require("../lib/constants/schemas");
44+
router.get('/', function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
45+
var data, query, payload, error_1;
46+
return __generator(this, function (_a) {
47+
switch (_a.label) {
48+
case 0:
49+
_a.trys.push([0, 2, , 3]);
50+
return [4 /*yield*/, RunQuery(req.headers.pg, types.list)];
51+
case 1:
52+
data = (_a.sent()).data;
53+
query = req.query;
54+
payload = data;
55+
if (!(query === null || query === void 0 ? void 0 : query.includeSystemSchemas))
56+
payload = removeSystemSchemas(data);
57+
return [2 /*return*/, res.status(200).json(payload)];
58+
case 2:
59+
error_1 = _a.sent();
60+
console.log('throwing error');
61+
res.status(500).json({ error: 'Database error', status: 500 });
62+
return [3 /*break*/, 3];
63+
case 3: return [2 /*return*/];
64+
}
65+
});
66+
}); });
67+
var removeSystemSchemas = function (data) {
68+
return data.filter(function (x) { return !schemas_1.DEFAULT_SYSTEM_SCHEMAS.includes(x.schema_name); });
69+
};
70+
module.exports = router;

dist/lib/interfaces/types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });

dist/lib/sql/schemas/list.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
SELECT
22
catalog_name,
3-
schema_name,
4-
schema_owner,
3+
schema_name as name,
4+
schema_owner as owner,
55
default_character_set_catalog,
66
default_character_set_schema,
77
default_character_set_name,

dist/lib/sql/types/list.sql

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
SELECT
2-
pg_catalog.Format_type ( t.oid, NULL ) AS name,
3-
t.typname AS internal_name,
2+
t.typname AS name,
3+
pg_catalog.Format_type ( t.oid, NULL ) AS format,
4+
n.nspname as schema_name,
5+
pg_catalog.obj_description ( t.oid, 'pg_type' ) AS description,
46
CASE
57
WHEN t.typrelid != 0 THEN Cast ( 'tuple' AS pg_catalog.TEXT )
68
WHEN t.typlen < 0 THEN Cast ( 'var' AS pg_catalog.TEXT )
@@ -11,8 +13,7 @@ array (
1113
FROM pg_catalog.pg_enum e
1214
WHERE e.enumtypid = t.oid
1315
ORDER BY e.oid
14-
) AS enums,
15-
pg_catalog.obj_description ( t.oid, 'pg_type' ) AS description
16+
) AS enums
1617
FROM pg_catalog.pg_type t LEFT JOIN pg_catalog.pg_namespace n
1718
ON n.oid = t.typnamespace
1819
WHERE (
@@ -24,7 +25,5 @@ and NOT EXISTS (
2425
FROM pg_catalog.pg_type el
2526
WHERE el.oid = t.typelem AND el.typarray = t.oid
2627
)
27-
AND n.nspname = ?
28-
AND n.nspname not in ('information_schema', 'pg_catalog')
2928
AND pg_catalog.pg_type_is_visible ( t.oid )
3029
ORDER BY 1, 2;

docs/index.html

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@
146146

147147
</li>
148148

149+
<li>
150+
<a href="#pg-api-schemas" class="toc-h1 toc-link" data-title="Types">Types</a>
151+
152+
<ul class="toc-list-h2">
153+
154+
<li>
155+
<a href="#gettypes" class="toc-h2 toc-link" data-title="">getTypes</a>
156+
157+
</li>
158+
159+
</ul>
160+
161+
</li>
162+
149163
<li>
150164
<a href="#pg-api-plugins" class="toc-h1 toc-link" data-title="Plugins">Plugins</a>
151165

@@ -186,6 +200,10 @@
186200

187201
<li>© <a href="https://supabase.io">Supabase</a> 2020</li>
188202

203+
<li><a href="https://supabase.io">Visit supabase.io</a></li>
204+
205+
<li><a href="https://github.com/supabase/pg-api">View on GitHub</a></li>
206+
189207
</ul>
190208

191209
</div>
@@ -305,7 +323,7 @@ <h2 id="getschemas">getSchemas</h2>
305323
})
306324
</code></pre>
307325
<p><code>GET /schemas</code></p>
308-
<p><em>Get all tables</em></p>
326+
<p><em>Get all schemas</em></p>
309327
<blockquote>
310328
<p>Parameters:</p>
311329
</blockquote>
@@ -314,12 +332,12 @@ <h2 id="getschemas">getSchemas</h2>
314332
*/</span>
315333
</code></pre>
316334
<blockquote>
317-
<p>Returns: Scehemas.Schema[]</p>
335+
<p>Returns: Schemas.Schema[]</p>
318336
</blockquote>
319337
<pre class="highlight tab tab-javascript"><code>{
320338
<span class="hljs-attr">catalog_name</span>: string
321-
<span class="hljs-attr">schema_name</span>: string
322-
<span class="hljs-attr">schema_owner</span>: string
339+
<span class="hljs-attr">name</span>: string
340+
<span class="hljs-attr">owner</span>: string
323341
<span class="hljs-attr">default_character_set_catalog</span>: string
324342
<span class="hljs-attr">default_character_set_schema</span>: string
325343
<span class="hljs-attr">default_character_set_name</span>: string
@@ -346,6 +364,45 @@ <h2 id="gettables">getTables</h2>
346364
</code></pre>
347365
<p><code>GET /tables</code></p>
348366
<p><em>Get all tables</em></p>
367+
<h1 id="pg-api-schemas">Types</h1>
368+
<p>View and manage your Postgres types.</p>
369+
<h2 id="gettypes">getTypes</h2>
370+
<p><a id="get-types"></a></p>
371+
<blockquote>
372+
<p>GET /types</p>
373+
</blockquote>
374+
<pre class="highlight tab tab-sh"><code>curl -X GET https://pg-api.fly.dev/types \
375+
-H <span class="hljs-string">'Content-Type: application/json'</span> \
376+
-H <span class="hljs-string">'pg: { "host": "DB_HOST", "password": "DB_PASSWORD" }'</span>
377+
</code></pre>
378+
<pre class="highlight tab tab-js"><code><span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> fetch(<span class="hljs-string">'https://pg-api.fly.dev/types'</span>, {
379+
<span class="hljs-attr">method</span>: <span class="hljs-string">'GET'</span>,
380+
<span class="hljs-attr">headers</span>: {
381+
<span class="hljs-string">'pg'</span>: { <span class="hljs-string">"host"</span>: <span class="hljs-string">"DB_HOST"</span>, <span class="hljs-string">"password"</span>: <span class="hljs-string">"DB_PASSWORD"</span> }
382+
}
383+
})
384+
</code></pre>
385+
<p><code>GET /types</code></p>
386+
<p><em>Get all Types</em></p>
387+
<blockquote>
388+
<p>Parameters:</p>
389+
</blockquote>
390+
<pre class="highlight tab tab-javascript"><code><span class="hljs-comment">/**
391+
* <span class="hljs-doctag">@param <span class="hljs-type">{boolean}</span> </span>[includeSystemSchemas=false] - Return system schemas as well as user schemas
392+
*/</span>
393+
</code></pre>
394+
<blockquote>
395+
<p>Returns: Types.Type[]</p>
396+
</blockquote>
397+
<pre class="highlight tab tab-javascript"><code>{
398+
<span class="hljs-attr">name</span>: string
399+
<span class="hljs-attr">format</span>: string
400+
<span class="hljs-attr">schema_name</span>: string
401+
<span class="hljs-attr">description</span>: string
402+
<span class="hljs-attr">size</span>: string
403+
<span class="hljs-attr">enums</span>: string
404+
}
405+
</code></pre>
349406
<h1 id="pg-api-plugins">Plugins</h1>
350407
<p>View and manage your Postgres plugins.</p>
351408
<h2 id="getplugins">getPlugins</h2>

docs/source/index.html.md

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ language_tabs:
55
- js: JavaScript
66
toc_footers:
77
- © <a href="https://supabase.io">Supabase</a> 2020
8+
- <a href="https://supabase.io">Visit supabase.io</a>
9+
- <a href="https://github.com/supabase/pg-api">View on GitHub</a>
810
includes: []
911
search: true
1012
highlight_theme: darkula
@@ -156,20 +158,20 @@ const data = await fetch('https://pg-api.fly.dev/schemas', {
156158

157159
`GET /schemas`
158160

159-
*Get all tables*
161+
*Get all schemas*
160162

161163
> Parameters:
162164
```javascript
163165
/**
164166
* @param {boolean} [includeSystemSchemas=false] - Return system schemas as well as user schemas
165167
*/
166168
```
167-
> Returns: Scehemas.Schema[]
169+
> Returns: Schemas.Schema[]
168170
```javascript
169171
{
170172
catalog_name: string
171-
schema_name: string
172-
schema_owner: string
173+
name: string
174+
owner: string
173175
default_character_set_catalog: string
174176
default_character_set_schema: string
175177
default_character_set_name: string
@@ -216,6 +218,59 @@ const data = await fetch('https://pg-api.fly.dev/tables', {
216218

217219

218220

221+
222+
<h1 id="pg-api-schemas">Types</h1>
223+
224+
View and manage your Postgres types.
225+
226+
## getTypes
227+
228+
<a id="get-types"></a>
229+
230+
> GET /types
231+
232+
```sh
233+
curl -X GET https://pg-api.fly.dev/types \
234+
-H 'Content-Type: application/json' \
235+
-H 'pg: { "host": "DB_HOST", "password": "DB_PASSWORD" }'
236+
```
237+
```js
238+
const data = await fetch('https://pg-api.fly.dev/types', {
239+
method: 'GET',
240+
headers: {
241+
'pg': { "host": "DB_HOST", "password": "DB_PASSWORD" }
242+
}
243+
})
244+
```
245+
246+
`GET /types`
247+
248+
*Get all Types*
249+
250+
> Parameters:
251+
```javascript
252+
/**
253+
* @param {boolean} [includeSystemSchemas=false] - Return system schemas as well as user schemas
254+
*/
255+
```
256+
> Returns: Types.Type[]
257+
```javascript
258+
{
259+
name: string
260+
format: string
261+
schema_name: string
262+
description: string
263+
size: string
264+
enums: string
265+
}
266+
```
267+
268+
269+
270+
271+
272+
273+
219274
<h1 id="pg-api-plugins">Plugins</h1>
220275

221276
View and manage your Postgres plugins.

src/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ router.use('/plugins', addConnectionToRequest, require('./api/plugins'))
1010
router.use('/query', addConnectionToRequest, require('./api/query'))
1111
router.use('/schemas', addConnectionToRequest, require('./api/schemas'))
1212
router.use('/tables', addConnectionToRequest, require('./api/tables'))
13+
router.use('/types', addConnectionToRequest, require('./api/types'))
1314
router.use('/users', addConnectionToRequest, require('./api/users'))
1415

1516
module.exports = router

src/api/schemas.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ router.get('/', async (req, res) => {
2727

2828

2929
const removeSystemSchemas = (data: Schemas.Schema[]) => {
30-
return data.filter((x) => !DEFAULT_SYSTEM_SCHEMAS.includes(x.schema_name))
30+
return data.filter((x) => !DEFAULT_SYSTEM_SCHEMAS.includes(x.name))
3131
}
3232

3333

0 commit comments

Comments
 (0)