From 67e28e90712caec17bef26af4604ac497ed400c5 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sat, 14 Sep 2019 18:29:15 +0200 Subject: [PATCH] Initial commit --- .gitignore | 1 + README.md | 34 ++++++++++++++++++++++++++++++++++ example.js | 10 ++++++++++ index.js | 20 ++++++++++++++++++++ package.json | 11 +++++++++++ yarn.lock | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 110 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 example.js create mode 100644 index.js create mode 100644 package.json create mode 100644 yarn.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/README.md new file mode 100644 index 0000000..7929cb2 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# postgresql-socket-url + +Generates a well-formed URL for a given PostgreSQL domain socket path and database name. Useful when you want to use a database driver (like `pg`) to connect to PostgreSQL through a Unix domain socket. + +## Example + +```js +"use strict"; + +const postgresqlSocketUrl = require("postgresql-socket-url"); + +let url = postgresqlSocketUrl({ + socketPath: "/tmp", + database: "myproject" +}); + +console.log(url); // socket:/tmp?db=myproject +``` + +## API + +### postgresqlSocketUrl(options) + +Generates the URL to connect to. + +- __options:__ + - __socketPath:__ The path of the folder that __contains__ the PostgreSQL socket. *Not* the path of the socket itself. + - __database:__ The name of the database to connect to. + +## Changelog + +### 1.0.0 (September 14, 2019) + +Initial release. diff --git a/example.js b/example.js new file mode 100644 index 0000000..58319c4 --- /dev/null +++ b/example.js @@ -0,0 +1,10 @@ +"use strict"; + +const postgresqlSocketUrl = require("."); + +let url = postgresqlSocketUrl({ + socketPath: "/tmp", + database: "myproject" +}); + +console.log(url); // socket:/tmp?db=myproject diff --git a/index.js b/index.js new file mode 100644 index 0000000..be69d0f --- /dev/null +++ b/index.js @@ -0,0 +1,20 @@ +"use strict"; + +const url = require("url"); +const { validateOptions, required, isString } = require("validatem"); + +module.exports = function postgresqlSocketUrl({ socketPath, database } = {}) { + validateOptions(arguments, { + socketPath: [ required, isString ], + database: [ required, isString ] + }); + + return url.format({ + protocol: "socket", + slashes: false, + pathname: socketPath, + query: { + db: database + } + }); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..8df66bb --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "postgresql-socket-url", + "version": "1.0.0", + "main": "index.js", + "repository": "http://git.cryto.net/joepie91/postgresql-socket-url.git", + "author": "Sven Slootweg ", + "license": "WTFPL OR CC0-1.0", + "dependencies": { + "validatem": "^0.2.0" + } +} diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..cd40255 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,34 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +assure-array@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/assure-array/-/assure-array-1.0.0.tgz#4f4ad16a87659d6200a4fb7103462033d216ec1f" + integrity sha1-T0rRaodlnWIApPtxA0YgM9IW7B8= + +create-error@^0.3.1: + version "0.3.1" + resolved "https://registry.yarnpkg.com/create-error/-/create-error-0.3.1.tgz#69810245a629e654432bf04377360003a5351a23" + integrity sha1-aYECRaYp5lRDK/BDdzYAA6U1GiM= + +default-value@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/default-value/-/default-value-1.0.0.tgz#8c6f52a5a1193fe78fdc9f86eb71d16c9757c83a" + integrity sha1-jG9SpaEZP+eP3J+G63HRbJdXyDo= + dependencies: + es6-promise-try "0.0.1" + +es6-promise-try@0.0.1: + version "0.0.1" + resolved "https://registry.yarnpkg.com/es6-promise-try/-/es6-promise-try-0.0.1.tgz#10f140dad27459cef949973e5d21a087f7274b20" + integrity sha1-EPFA2tJ0Wc75SZc+XSGgh/cnSyA= + +validatem@^0.2.0: + version "0.2.0" + resolved "https://registry.yarnpkg.com/validatem/-/validatem-0.2.0.tgz#0cde815813f3c8a33615bf09370e88d511a65658" + integrity sha512-fSW8L19US3smdm/E5tw7zTR3WgC+yGiBf67zhxCgVJExujZui2t8gwVyX05u9tcIBu/90tzqsD9hfMR0VAGKUA== + dependencies: + assure-array "^1.0.0" + create-error "^0.3.1" + default-value "^1.0.0"