Skip to content

Commit f7d88e5

Browse files
author
Sylvestre Gug
committed
use modules
1 parent 3f52de5 commit f7d88e5

File tree

6 files changed

+37
-37
lines changed

6 files changed

+37
-37
lines changed

bin/observable-database-proxy renamed to bin/observable-database-proxy.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
#!/usr/bin/env node
22

3-
require = require("esm")(module);
4-
const exit = require("../lib/errors").exit;
5-
const {start, add, remove, reset, list} = require("../lib/commands");
3+
import {start, add, remove, reset, list} from "../lib/commands.js";
4+
import argv from "yargs";
65

7-
const name = yargs =>
6+
const name = (yargs) =>
87
yargs.positional("name", {
98
describe: "Database connector name",
10-
type: "string"
9+
type: "string",
1110
});
1211

13-
const argv = require("yargs")
12+
argv
1413
.usage(`Usage: $0 <command> <name> [options]`)
1514
.command(
1615
`start <name> [options]`,

lib/commands.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import {
66
readConfig,
77
readDecodedConfig,
88
writeConfig,
9-
decodeSecret
10-
} from "./config";
11-
import {server} from "./server";
12-
import {exit} from "./errors";
9+
decodeSecret,
10+
} from "./config.js";
11+
import {server} from "./server.js";
12+
import {exit} from "./errors.js";
1313

1414
export function start(argv) {
1515
const config = readDecodedConfig(argv.name);
@@ -25,9 +25,10 @@ export async function add(argv, reset = false) {
2525

2626
const rl = createInterface({
2727
input: process.stdin,
28-
output: process.stdout
28+
output: process.stdout,
2929
});
30-
const question = query => new Promise(resolve => rl.question(query, resolve));
30+
const question = (query) =>
31+
new Promise((resolve) => rl.question(query, resolve));
3132

3233
if (reset) {
3334
if (!config[name]) exit(`No configuration found for "${name}"`);
@@ -87,7 +88,9 @@ export function list() {
8788
if (!config) exit(`No database proxies configured`);
8889
console.log(
8990
config
90-
.map(c => `${c.name} (${c.type}) ${c.ssl === "required" ? `(SSL)` : ``}`)
91+
.map(
92+
(c) => `${c.name} (${c.type}) ${c.ssl === "required" ? `(SSL)` : ``}`
93+
)
9194
.join("\n")
9295
);
9396
}

lib/config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {existsSync, readFileSync, writeFileSync} from "fs";
22
import {homedir} from "os";
33
import {join} from "path";
4-
import {exit} from "./errors";
4+
import {exit} from "./errors.js";
55

66
const configFile = join(homedir(), ".observablehq");
77
const key = `database-proxy`;
@@ -23,9 +23,9 @@ export function readDecodedConfig(name) {
2323
if (!raw) exit(`No configuration found for "${name}"`);
2424
return {...decodeSecret(raw.secret), url: raw.url};
2525
} else {
26-
return Object.values(config).map(c => ({
26+
return Object.values(config).map((c) => ({
2727
...decodeSecret(c.secret),
28-
url: c.url
28+
url: c.url,
2929
}));
3030
}
3131
}

lib/mysql.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import {json} from "micro";
22
import {createPool} from "mysql";
3-
import {parseUrl} from "mysql/lib/ConnectionConfig";
4-
import types from "mysql/lib/protocol/constants/types";
3+
import connectionConfig from "mysql/lib/ConnectionConfig.js";
4+
import types from "mysql/lib/protocol/constants/types.js";
55
import JSONStream from "JSONStream";
66

7-
export default url => {
7+
const {parseUrl} = connectionConfig;
8+
9+
export default (url) => {
810
const pool = createPool(parseUrl(url));
911

1012
return async function query(req, res) {
@@ -14,18 +16,15 @@ export default url => {
1416
await new Promise((resolve, reject) => {
1517
const stream = pool
1618
.query({sql, timeout: 30e3}, params)
17-
.on("fields", f => (fields = f))
19+
.on("fields", (f) => (fields = f))
1820
.stream()
1921
.on("end", resolve)
20-
.on("error", error => {
22+
.on("error", (error) => {
2123
stream.destroy();
2224
reject(error);
2325
})
2426
.pipe(JSONStream.stringify(`{"data":[`, ",", "]"));
25-
stream.pipe(
26-
res,
27-
{end: false}
28-
);
27+
stream.pipe(res, {end: false});
2928
});
3029

3130
const schema = {
@@ -37,8 +36,8 @@ export default url => {
3736
(schema[name] = dataTypeSchema({type, charsetNr})), schema
3837
),
3938
{}
40-
)
41-
}
39+
),
40+
},
4241
};
4342
res.end(`,"schema":${JSON.stringify(schema)}}`);
4443
};

lib/postgres.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {json} from "micro";
2-
import {Pool} from "pg";
2+
import pg from "pg";
33
import QueryStream from "pg-query-stream";
44
import JSONStream from "JSONStream";
55

6-
export default url => {
6+
const {Pool} = pg;
7+
8+
export default (url) => {
79
const pool = new Pool({connectionString: url});
810

911
return async function query(req, res) {
@@ -19,10 +21,7 @@ export default url => {
1921
.on("end", resolve)
2022
.on("error", reject)
2123
.pipe(JSONStream.stringify(`{"data":[`, ",", "]"))
22-
.pipe(
23-
res,
24-
{end: false}
25-
);
24+
.pipe(res, {end: false});
2625
});
2726

2827
const schema = {
@@ -34,8 +33,8 @@ export default url => {
3433
(schema[name] = dataTypeSchema(dataTypeID)), schema
3534
),
3635
{}
37-
)
38-
}
36+
),
37+
},
3938
};
4039
res.end(`,"schema":${JSON.stringify(schema)}}`);
4140
} finally {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"./snowflake.js": "./lib/snowflake.js"
1313
},
1414
"bin": {
15-
"observable-database-proxy": "./bin/observable-database-proxy"
15+
"observable-database-proxy": "./bin/observable-database-proxy.js"
1616
},
1717
"dependencies": {
1818
"JSONStream": "^1.3.5",
@@ -30,7 +30,7 @@
3030
"nodemon": "^1.19.1"
3131
},
3232
"scripts": {
33-
"dev": "NODE_ENV=development nodemon bin/observable-database-proxy",
33+
"dev": "NODE_ENV=development nodemon bin/observable-database-proxy.js",
3434
"test": "echo \"Error: no test specified\" && exit 1"
3535
},
3636
"author": "Observable",

0 commit comments

Comments
 (0)