-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create script to preemptively provision user DBs
- Loading branch information
1 parent
6b58269
commit 69380e1
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,54 @@ | ||
// export HASURA_ENDPOINT='http://localhost:8080' | ||
// export HASURA_ADMIN_SECRET='myadminsecretkey' | ||
// export PG_ADMIN_USER='postgres' | ||
// export PG_ADMIN_PASSWORD='postgrespassword' | ||
// export PG_ADMIN_DATABASE='postgres' | ||
// export PG_HOST='localhost' | ||
// export PG_PORT=5432 | ||
|
||
import { execSync } from 'child_process' | ||
import { providers } from 'near-api-js' | ||
|
||
import Provisioner from '../provisioner.js' | ||
import HasuraClient from '../hasura-client.js' | ||
|
||
const provisioner = new Provisioner(); | ||
|
||
const { rows } = await provisioner.pgClient.query('SELECT nspname AS name FROM pg_namespace;') | ||
|
||
const schemaNames = rows.map((row) => row.name); | ||
|
||
const accountIdsSet = schemaNames.reduce((accountIdsSet, schemaName) => { | ||
const parts = schemaName.split('_near_'); | ||
if (parts.length > 1) { | ||
accountIdsSet.add(`${parts[0]}_near`); | ||
} | ||
return accountIdsSet; | ||
}, new Set()); | ||
|
||
const accountIds = Array.from(accountIdsSet); | ||
|
||
console.log(`Creating datasources for accounts: ${accountIds.join(', ')}`) | ||
|
||
for (const accountId of accountIds) { | ||
console.log('---'); | ||
const sanitizedAccountId = provisioner.replaceSpecialChars(accountId); | ||
|
||
const databaseName = sanitizedAccountId; | ||
const userName = sanitizedAccountId; | ||
|
||
if (await provisioner.hasuraClient.doesSourceExist(databaseName)) { | ||
console.log(`Datasource ${databaseName} already exists, skipping.`) | ||
continue; | ||
} | ||
|
||
const password = provisioner.generatePassword() | ||
console.log(`Creating user: ${userName} and database: ${databaseName} with password: ${password}`); | ||
await provisioner.createUserDb(userName, password, databaseName); | ||
|
||
console.log(`Adding datasource ${databaseName} to Hasura`) | ||
await provisioner.addDatasource(userName, password, databaseName); | ||
} | ||
console.log('---'); | ||
|
||
console.log('Done'); |