You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
refactor(api): run seed-admin through the shared script context - #733
seed-admin.ts kept its own copy of script-context.ts's Nest module: the same ConfigModule options and DatabaseModule, plus its own context lifecycle.
It read ADMIN_EMAIL and ADMIN_PASSWORD before anything loaded apps/api/.env. .env.example says to put them there, but that only worked through nx run, because Nx loads the project's .env itself. A direct tsx run reported them missing. ConfigService can't supply them either: validateEnv strips keys outside the app schema.
What changed
script-context.ts:withScriptDatabase also hands work the ConfigService. Existing callers pass one-argument callbacks and are unchanged.
seed-admin.ts:
It runs through withScriptDatabase, so its own module and context handling are gone.
It loads .env.local and .env with dotenv before reading the two credentials. They are still checked before any database connection.
Hashing is unchanged: BcryptPasswordHasher with BCRYPT_ROUNDS.
Verification
Every run used a direct tsx call against the local knowtis_test, with DATABASE_URL overridden.
Credentials only in .env.local: the admin was created, and the hash starts with $2b$12$. A second run printed "already an admin" and changed nothing.
ADMIN_EMAIL blank: the usage message and exit 1.
BCRYPT_ROUNDS=9: it aborts with BCRYPT_ROUNDS: Too small: expected number to be >=10.
The test users were deleted afterwards. The dev knowtis database has no users table, so nothing reached it.
nx run-many -t typecheck,lint -p api and Prettier: clean.
Summary by CodeRabbit
Chores
Admin account setup now reads local environment settings and trims the configured email address.
The setup process leaves an existing admin unchanged, promotes an existing non-admin account, or creates a new account with a hashed password.
Missing credentials and setup failures continue to report an error and exit unsuccessfully.
No actionable comments were generated in the recent review.
Walkthrough
The script database helper now provides ConfigService to its callback. The admin seed script loads credentials with dotenv and uses the helper to seed an account.
Changes
Admin seed script
Layer / File(s)
Summary
Pass configuration to script callbacks apps/api/src/scripts/script-context.ts
withScriptDatabase now passes both the database connection and ConfigService to its callback.
Load credentials and seed the admin apps/api/src/scripts/seed-admin.ts
The script loads .env.local and .env, trims ADMIN_EMAIL, and reads ADMIN_PASSWORD. It leaves existing admins unchanged, promotes other existing users, and creates missing users with a hashed password.
This change moves the admin seed script onto the shared script database helper and loads credentials from env files. No merge-blocking risk was found, and the reported manual checks cover the main paths.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title names the main change: seed-admin.ts now runs through the shared withScriptDatabase helper instead of its own Nest module and context lifecycle. The raw summary confirms this, and the object…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title names the main change: seed-admin.ts now runs through the shared withScriptDatabase helper instead of its own Nest module and context lifecycle. The raw summary confirms this, and the objectives describe the same refactor. The title is specific and matches the changeset.
Finishing Touches
Generate unit tests (beta)
Commit to this branch
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Warning
Some tools did not complete. Review the errors below.
ESLint
If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.
apps/api/src/scripts/script-context.ts
ESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox.
apps/api/src/scripts/seed-admin.ts
ESLint skipped: the matched ESLint configuration already failed (missing-dependency).
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Reviewing files that changed from the base of the PR and between 8f925c7 and 10cb7cb.
Files selected for processing: 2
apps/api/src/scripts/script-context.ts
apps/api/src/scripts/seed-admin.ts
Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.
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
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.
Why
Two leftovers from #731:
seed-admin.tskept its own copy ofscript-context.ts's Nest module: the sameConfigModuleoptions andDatabaseModule, plus its own context lifecycle.ADMIN_EMAILandADMIN_PASSWORDbefore anything loadedapps/api/.env..env.examplesays to put them there, but that only worked throughnx run, because Nx loads the project's.envitself. A directtsxrun reported them missing.ConfigServicecan't supply them either:validateEnvstrips keys outside the app schema.What changed
script-context.ts:withScriptDatabasealso handsworktheConfigService. Existing callers pass one-argument callbacks and are unchanged.seed-admin.ts:withScriptDatabase, so its own module and context handling are gone..env.localand.envwithdotenvbefore reading the two credentials. They are still checked before any database connection.BcryptPasswordHasherwithBCRYPT_ROUNDS.Verification
Every run used a direct
tsxcall against the localknowtis_test, withDATABASE_URLoverridden..env.local: the admin was created, and the hash starts with$2b$12$. A second run printed "already an admin" and changed nothing.ADMIN_EMAILblank: the usage message and exit 1.BCRYPT_ROUNDS=9: it aborts withBCRYPT_ROUNDS: Too small: expected number to be >=10.knowtisdatabase has nouserstable, so nothing reached it.nx run-many -t typecheck,lint -p apiand Prettier: clean.Summary by CodeRabbit