-
Notifications
You must be signed in to change notification settings - Fork 3
Cluster replication #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f13c4d4
Use more doxygen-style comments
swansontec 8a78830
Deprecate the legacy `autoReplicate` utility
swansontec af731b6
Improve documentation wording
swansontec 19f9e53
Add a dryRun feature to database setup
swansontec 40cbd3f
Add a syncOnly feature to database setup
swansontec 0ef6d21
Add a CouchPool class
swansontec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import { asEither, asObject, asOptional, asString } from 'cleaners' | ||
| import nano, { ServerScope } from 'nano' | ||
|
|
||
| /** | ||
| * A table of CouchDB connections, | ||
| * with one connection highlighted as the default. | ||
| */ | ||
| export interface CouchPool { | ||
| /** The default connection, to use for most queries. */ | ||
| default: ServerScope | ||
|
|
||
| /** The cluster name being used for the default connection. */ | ||
| defaultName: string | ||
|
|
||
| /** Lists the clusters that are available for connection. */ | ||
| clusterNames: string[] | ||
|
|
||
| /** | ||
| * Grab a connection to a specific cluster. | ||
| * Throws if the name is missing. | ||
| */ | ||
| connect: (name: string) => ServerScope | ||
|
|
||
| /** Grabs authentication information for a specific cluster. */ | ||
| getCredential: (name: string) => CouchCredential | undefined | ||
|
|
||
| /** Grab a connection, returning undefined if the cluster is missing. */ | ||
| maybeConnect: (name: string) => ServerScope | undefined | ||
| } | ||
|
|
||
| export interface CouchCredential { | ||
| url: string | ||
| username?: string | ||
| password?: string | ||
| } | ||
|
|
||
| export interface CouchCredentials { | ||
| [name: string]: | ||
| | string | ||
| | CouchCredential | ||
| // The `setupDatabase` function uses this, but it's deprecated: | ||
| | ServerScope | ||
| } | ||
|
|
||
| export const asCouchCredential = asObject<CouchCredential>({ | ||
| url: asString, | ||
| username: asOptional(asString), | ||
| password: asOptional(asString) | ||
| }) | ||
|
|
||
| export const asCouchCredentials = asObject( | ||
| asEither(asCouchCredential, asString) | ||
| ) | ||
|
|
||
| export function connectCouch(url: string): CouchPool | ||
| export function connectCouch( | ||
| defaultName: string, | ||
| credentials: CouchCredentials | ||
| ): CouchPool | ||
|
|
||
| /** | ||
| * Returns a CouchDB connection pool. | ||
| */ | ||
| export function connectCouch( | ||
| urlOrDefaultName: string, | ||
| maybeCreds?: CouchCredentials | ||
| ): CouchPool { | ||
| // Unpack the arguments: | ||
| const [defaultName, credentials] = | ||
| maybeCreds == null | ||
| ? // Just a URL: | ||
| ['default', { default: urlOrDefaultName }] | ||
| : // Full version: | ||
| [urlOrDefaultName, maybeCreds] | ||
|
|
||
| const cache = new Map<string, ServerScope>() | ||
|
|
||
| function maybeConnect(name: string): ServerScope | undefined { | ||
| // Use the cache, if we have it: | ||
| const cached = cache.get(name) | ||
| if (cached != null) return cached | ||
|
|
||
| // Find the credentials: | ||
| const row = credentials[name] | ||
| if (row == null) return | ||
|
|
||
| // Plain URL: | ||
| if (typeof row === 'string') { | ||
| const connection = nano(row) | ||
| cache.set(name, connection) | ||
| return connection | ||
| } | ||
|
|
||
| // Already-connected nano instance: | ||
| if ('relax' in row) { | ||
| cache.set(name, row) | ||
| return row | ||
| } | ||
|
|
||
| // A credentials object: | ||
| const { url, username, password } = row | ||
| if (username == null || password == null) { | ||
| const connection = nano(url) | ||
| cache.set(name, connection) | ||
| return connection | ||
| } | ||
| const connection = nano({ | ||
| url, | ||
| requestDefaults: { | ||
| auth: { username, password } | ||
| } | ||
| }) | ||
| cache.set(name, connection) | ||
| return connection | ||
| } | ||
|
|
||
| function connect(name: string): ServerScope { | ||
| const connection = maybeConnect(name) | ||
| if (connection == null) { | ||
| throw new Error(`Cannot find cluster '${name}'`) | ||
| } | ||
| return connection | ||
| } | ||
|
|
||
| function extractUrlCredentials(url: string): CouchCredential { | ||
| const parsed = new URL(url) | ||
| const { username, password } = parsed | ||
|
|
||
| // Remove credentials from the URL: | ||
| parsed.username = '' | ||
| parsed.password = '' | ||
| return { | ||
| url: parsed.toString(), | ||
| username, | ||
| password | ||
| } | ||
| } | ||
|
|
||
| function getCredential(name: string): CouchCredential | undefined { | ||
| const row = credentials[name] | ||
| if (row == null) return | ||
| if (typeof row === 'string') return extractUrlCredentials(row) | ||
| if ('relax' in row) return extractUrlCredentials(row.config.url) | ||
|
|
||
| const cleanUrl = extractUrlCredentials(row.url) | ||
| return { | ||
| url: cleanUrl.url, | ||
| username: row.username ?? cleanUrl.username, | ||
| password: row.password ?? cleanUrl.password | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| default: connect(defaultName), | ||
| defaultName, | ||
|
|
||
| clusterNames: Object.keys(credentials), | ||
|
|
||
| connect, | ||
| getCredential, | ||
| maybeConnect | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really good API. A part of me wonders if we can do without the
maybeConnectvia static type checking.connectcan still throw, but wouldn't ever be written to through if it constrained it's parameter. Inferring the name keys fromconnectCouchis where it gets tricky though because you inject'default'into the internal type, but I suppose the internal type doesn't need to be constrained by the name keys.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The input comes from cleaner-config in the login server, so there's no type information at that point. In other situations, we get the list of credentials from the info server, so it's even worse.