-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
40 lines (36 loc) · 16.2 KB
/
Copy pathindex.html
File metadata and controls
40 lines (36 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html><html class="default" lang="en" data-base="./"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>@libreworks/db-provision-pgsql</title><meta name="description" content="Documentation for @libreworks/db-provision-pgsql"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="assets/style.css"/><link rel="stylesheet" href="assets/highlight.css"/><script defer src="assets/main.js"></script><script async src="assets/icons.js" id="tsd-icons-script"></script><script async src="assets/search.js" id="tsd-search-script"></script><script async src="assets/navigation.js" id="tsd-nav-script"></script><script async src="assets/hierarchy.js" id="tsd-hierarchy-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="index.html" class="title">@libreworks/db-provision-pgsql</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><h1>@libreworks/db-provision-pgsql</h1></div><div class="tsd-panel tsd-typography"><h1 id="libreworksdb-provision-pgsql" class="tsd-anchor-link">@libreworks/db-provision-pgsql<a href="#libreworksdb-provision-pgsql" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h1>
<p><a href="https://github.com/libreworks/db-provision-pgsql/blob/main/LICENSE"><img src="https://img.shields.io/github/license/libreworks/db-provision-pgsql" alt="MIT"></a>
<a href="https://www.npmjs.com/package/@libreworks/db-provision-pgsql"><img src="https://img.shields.io/npm/v/@libreworks/db-provision-pgsql" alt="npm"></a>
<a href="https://github.com/libreworks/db-provision-pgsql/actions/workflows/release.yml"><img src="https://img.shields.io/github/workflow/status/libreworks/db-provision-pgsql/release/main?label=release" alt="GitHub Workflow Status (branch)"></a>
<a href="https://github.com/libreworks/db-provision-pgsql/releases"><img src="https://img.shields.io/github/v/release/libreworks/db-provision-pgsql?sort=semver" alt="GitHub release (latest SemVer)"></a>
<a href="https://codecov.io/gh/libreworks/db-provision-pgsql"><img src="https://codecov.io/gh/libreworks/db-provision-pgsql/branch/main/graph/badge.svg?token=OHTRGNTSPO" alt="codecov"></a></p>
<p>Provision databases and schemas in PostgreSQL along with roles, logins, and grants.</p>
<h2 id="installation" class="tsd-anchor-link">Installation<a href="#installation" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
<pre><code class="shell"><span class="hl-0">npm</span><span class="hl-1"> </span><span class="hl-2">install</span><span class="hl-1"> </span><span class="hl-2">@libreworks/db-provision-pgsql</span>
</code><button type="button">Copy</button></pre>
<p>This library conforms to ECMAScript Modules (ESM). You can import this module using ESM or TypeScript syntax.</p>
<pre><code class="TypeScript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">Catalog</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">"@libreworks/db-provision-pgsql"</span><span class="hl-1">;</span>
</code><button type="button">Copy</button></pre>
<p>If you're using CommonJS, you must use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import">dynamic imports</a> instead.</p>
<h2 id="usage" class="tsd-anchor-link">Usage<a href="#usage" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
<p>You can use this library to perform initialization of a PostgreSQL database server (version 11 and later). For example, creating databases, schemas, roles, users, and grants.</p>
<p>Here is an example to provision several database objects.</p>
<pre><code class="typescript"><span class="hl-3">import</span><span class="hl-1"> { </span><span class="hl-4">Login</span><span class="hl-1">, </span><span class="hl-4">Role</span><span class="hl-1">, </span><span class="hl-4">Catalog</span><span class="hl-1"> } </span><span class="hl-3">from</span><span class="hl-1"> </span><span class="hl-2">"@libreworks/db-provision-pgsql"</span><span class="hl-1">;</span><br/><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">username</span><span class="hl-1"> = </span><span class="hl-2">"example_user"</span><span class="hl-1">;</span><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">password</span><span class="hl-1"> = </span><span class="hl-2">"🙈"</span><span class="hl-1">;</span><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">owner</span><span class="hl-1"> = </span><span class="hl-5">new</span><span class="hl-1"> </span><span class="hl-0">Login</span><span class="hl-1">(</span><span class="hl-4">username</span><span class="hl-1">, </span><span class="hl-4">password</span><span class="hl-1">);</span><br/><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">admin</span><span class="hl-1"> = </span><span class="hl-5">new</span><span class="hl-1"> </span><span class="hl-0">Role</span><span class="hl-1">(</span><span class="hl-2">"admin"</span><span class="hl-1">);</span><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">readers</span><span class="hl-1"> = </span><span class="hl-5">new</span><span class="hl-1"> </span><span class="hl-0">Role</span><span class="hl-1">(</span><span class="hl-2">"readers"</span><span class="hl-1">);</span><br/><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">grants</span><span class="hl-1"> = [</span><span class="hl-4">admin</span><span class="hl-1">.</span><span class="hl-0">assignTo</span><span class="hl-1">(</span><span class="hl-4">owner</span><span class="hl-1">)];</span><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">catalog</span><span class="hl-1"> = </span><span class="hl-5">new</span><span class="hl-1"> </span><span class="hl-0">Catalog</span><span class="hl-1">(</span><span class="hl-2">"my_database"</span><span class="hl-1">);</span><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">schema</span><span class="hl-1"> = </span><span class="hl-4">catalog</span><span class="hl-1">.</span><span class="hl-0">createSchema</span><span class="hl-1">(</span><span class="hl-4">username</span><span class="hl-1">, </span><span class="hl-4">owner</span><span class="hl-1">);</span><br/><span class="hl-4">grants</span><span class="hl-1">.</span><span class="hl-0">push</span><span class="hl-1">(</span><br/><span class="hl-1"> </span><span class="hl-4">catalog</span><span class="hl-1">.</span><span class="hl-0">grant</span><span class="hl-1">(</span><span class="hl-4">owner</span><span class="hl-1">, </span><span class="hl-2">"CONNECT"</span><span class="hl-1">, </span><span class="hl-2">"TEMP"</span><span class="hl-1">),</span><br/><span class="hl-1"> </span><span class="hl-4">catalog</span><span class="hl-1">.</span><span class="hl-0">grant</span><span class="hl-1">(</span><span class="hl-4">readers</span><span class="hl-1">, </span><span class="hl-2">"CONNECT"</span><span class="hl-1">, </span><span class="hl-2">"TEMP"</span><span class="hl-1">),</span><br/><span class="hl-1"> </span><span class="hl-4">schema</span><span class="hl-1">.</span><span class="hl-0">grant</span><span class="hl-1">(</span><span class="hl-4">readers</span><span class="hl-1">, </span><span class="hl-2">"USAGE"</span><span class="hl-1">),</span><br/><span class="hl-1"> </span><span class="hl-4">schema</span><span class="hl-1">.</span><span class="hl-0">allTables</span><span class="hl-1">().</span><span class="hl-0">grant</span><span class="hl-1">(</span><span class="hl-4">readers</span><span class="hl-1">, </span><span class="hl-2">"SELECT"</span><span class="hl-1">),</span><br/><span class="hl-1"> </span><span class="hl-4">schema</span><span class="hl-1">.</span><span class="hl-0">allSequences</span><span class="hl-1">().</span><span class="hl-0">grant</span><span class="hl-1">(</span><span class="hl-4">readers</span><span class="hl-1">, </span><span class="hl-2">"SELECT"</span><span class="hl-1">),</span><br/><span class="hl-1"> </span><span class="hl-4">schema</span><span class="hl-1">.</span><span class="hl-0">setDefaultTablePrivileges</span><span class="hl-1">(</span><span class="hl-4">readers</span><span class="hl-1">, </span><span class="hl-2">"SELECT"</span><span class="hl-1">).</span><span class="hl-0">forCreator</span><span class="hl-1">(</span><span class="hl-4">owner</span><span class="hl-1">),</span><br/><span class="hl-1"> </span><span class="hl-4">schema</span><span class="hl-1">.</span><span class="hl-0">setDefaultSequencePrivileges</span><span class="hl-1">(</span><span class="hl-4">readers</span><span class="hl-1">, </span><span class="hl-2">"SELECT"</span><span class="hl-1">).</span><span class="hl-0">forCreator</span><span class="hl-1">(</span><span class="hl-4">owner</span><span class="hl-1">)</span><br/><span class="hl-1">);</span><br/><br/><span class="hl-7">// Display the SQL</span><br/><span class="hl-5">const</span><span class="hl-1"> </span><span class="hl-6">statements</span><span class="hl-1"> = [</span><br/><span class="hl-1"> </span><span class="hl-4">owner</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">admin</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">readers</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">catalog</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">schema</span><span class="hl-1">,</span><br/><span class="hl-1"> ...</span><span class="hl-4">grants</span><span class="hl-1">,</span><br/><span class="hl-1">].</span><span class="hl-0">map</span><span class="hl-1">((</span><span class="hl-4">v</span><span class="hl-1">) </span><span class="hl-5">=></span><span class="hl-1"> </span><span class="hl-4">v</span><span class="hl-1">.</span><span class="hl-0">toSql</span><span class="hl-1">());</span><br/><span class="hl-4">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-4">statements</span><span class="hl-1">.</span><span class="hl-0">join</span><span class="hl-1">(</span><span class="hl-2">";</span><span class="hl-8">\n</span><span class="hl-2">"</span><span class="hl-1">) + </span><span class="hl-2">";</span><span class="hl-8">\n</span><span class="hl-2">"</span><span class="hl-1">);</span>
</code><button type="button">Copy</button></pre>
<p>The above example outputs the following SQL statements:</p>
<pre><code class="sql">CREATE USER "example_user" WITH PASSWORD '🙈';
CREATE ROLE "admin";
CREATE ROLE "readers";
CREATE DATABASE "my_database" ENCODING 'UTF8';
CREATE SCHEMA IF NOT EXISTS "example_user" AUTHORIZATION "example_user";
GRANT "admin" TO "example_user";
GRANT CONNECT, TEMP ON DATABASE "my_database" TO "example_user";
GRANT CONNECT, TEMP ON DATABASE "my_database" TO "readers";
GRANT USAGE ON SCHEMA "example_user" TO "readers";
GRANT SELECT ON ALL TABLES IN SCHEMA "example_user" TO "readers";
GRANT SELECT ON ALL SEQUENCES IN SCHEMA "example_user" TO "readers";
ALTER DEFAULT PRIVILEGES FOR USER "example_user" IN SCHEMA "example_user" GRANT SELECT ON TABLES TO "readers";
ALTER DEFAULT PRIVILEGES FOR USER "example_user" IN SCHEMA "example_user" GRANT SELECT ON SEQUENCES TO "readers";
</code><button type="button">Copy</button></pre>
<p>Because all identifiers are quoted, that means the objects will be created using the same character casing as provided. Without double quotes, PostgreSQL creates objects with lowercase identifiers.</p>
</div></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div><details open class="tsd-accordion tsd-page-navigation"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg><h3>On This Page</h3></summary><div class="tsd-accordion-details"><a href="#libreworksdb-provision-pgsql"><span>@libreworks/db-<wbr/>provision-<wbr/>pgsql</span></a><ul><li><a href="#installation"><span>Installation</span></a></li><li><a href="#usage"><span>Usage</span></a></li></ul></div></details></div><div class="site-menu"><nav class="tsd-navigation"><a href="modules.html">@libreworks/db-provision-pgsql</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>