Skip to content

feat: add optional prefix parameter to nanoid() for typed ids #18#29

Open
nik-sta wants to merge 2 commits into
feat/20-alphabet-256from
feat/18-nanoid-prefix
Open

feat: add optional prefix parameter to nanoid() for typed ids #18#29
nik-sta wants to merge 2 commits into
feat/20-alphabet-256from
feat/18-nanoid-prefix

Conversation

@nik-sta

@nik-sta nik-sta commented Jul 13, 2026

Copy link
Copy Markdown
Member

Summary

Adds prefix text DEFAULT '' as the new last parameter of nanoid(), prepended to the generated id for Stripe-style typed ids (usr_, ord_, ...):

  • size keeps meaning the number of random symbols; the total id length is length(prefix) + size, so columns must be sized accordingly (e.g. char(25) for nanoid(prefix => 'usr_'))
  • A NULL prefix behaves like an empty prefix instead of producing a NULL id (coalesce(prefix, ''))
  • nanoid_optimized() stays unchanged; the prefix is concatenated in nanoid()
  • The old signature nanoid(int, text, float) is dropped on upgrade, as the install script already does today
  • README documents the parameter, the length semantics, and a CREATE TABLE example

Stacked on #24; the diff of this PR is the prefix commit only.

Verification

  • Unit tests: positional prefix, named notation nanoid(prefix => 'ord_'), and NULL prefix; SELECT nanoid() behavior is unchanged (21 characters, default alphabet)
  • Full multi-version suite passes on PostgreSQL 9.6 through 18
  • Upgrade path verified: installing the new nanoid.sql over an existing installation leaves exactly one nanoid(size, alphabet, additionalBytesFactor, prefix) signature

Credits

Idea by @abdirahmn1.

Closes #18

Summary by CodeRabbit

  • New Features

    • Added an optional prefix parameter to generated Nano IDs, supporting typed identifiers such as usr_ and ord_.
    • Prefixes are included in the final ID without reducing the requested random portion size.
    • Prefixes can be provided positionally or by name.
  • Bug Fixes

    • A NULL prefix now behaves like no prefix (no longer yields a NULL identifier).
  • Documentation

    • Expanded “Prefixed ids” with new SQL examples and clarifications on total ID length and NULL prefix behavior.
  • Tests

    • Added unit coverage for prefix/size interactions, pattern expectations, and edge cases.

@nik-sta
nik-sta force-pushed the feat/18-nanoid-prefix branch from 23364e1 to 080212a Compare July 13, 2026 20:35
@nik-sta
nik-sta force-pushed the feat/20-alphabet-256 branch from eae4459 to fd629d4 Compare July 13, 2026 20:35
@nik-sta

nik-sta commented Jul 13, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown
✅ Action performed

Full review finished.

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b4900f20-e529-40ce-8aee-9d16d9716c17

📥 Commits

Reviewing files that changed from the base of the PR and between 681163e and b9c3b58.

📒 Files selected for processing (3)
  • README.md
  • dev/test/unit_tests.sql
  • nanoid.sql

📝 Walkthrough

Walkthrough

The nanoid function now accepts an optional prefix, prepends it to generated IDs, treats NULL as empty, and documents and tests the resulting length and format behavior.

Changes

Nanoid prefix support

Layer / File(s) Summary
Prefix parameter and generation
nanoid.sql
The nanoid signature adds an optional prefix parameter, removes older overloads, and prepends the NULL-safe prefix to the generated identifier.
Prefix validation and documentation
dev/test/unit_tests.sql, README.md
Tests cover positional, named, mixed-notation, NULL, and multi-byte prefixes; README examples describe typed prefixes and total ID length.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: adding an optional prefix parameter to nanoid() for typed IDs.
Linked Issues check ✅ Passed The changes implement the optional prefix, preserve size semantics, handle NULL as empty, keep nanoid_optimized() unchanged, add docs/tests, and drop the old signature.
Out of Scope Changes check ✅ Passed The PR stays focused on the prefix feature, with only related docs, tests, and migration updates.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/18-nanoid-prefix

Comment @coderabbitai help to get the list of available commands.

nik-sta added 2 commits July 14, 2026 00:30
…rough #18

The signature change (new prefix parameter) means the old nanoid(int, text,
float) must be dropped; a dependent column default now causes an atomic
rollback with recovery steps in the README instead of leaving two ambiguous
nanoid functions behind. Adds tests for out-of-alphabet prefix characters
and mixed positional/named notation.
@nik-sta
nik-sta force-pushed the feat/18-nanoid-prefix branch from 080212a to b9c3b58 Compare July 13, 2026 22:38
@nik-sta
nik-sta force-pushed the feat/20-alphabet-256 branch from 6e7a3e8 to 681163e Compare July 13, 2026 22:38
@nik-sta nik-sta added enhancement New feature or request community-idea Idea sourced from a community fork or contribution labels Jul 14, 2026
@nik-sta

nik-sta commented Jul 14, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@nik-sta

nik-sta commented Jul 14, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown
✅ Action performed

Full review finished.


You're currently rate limited under our Fair Usage Limits Policy. Your recent PR review activity is in the 95th percentile or higher among CodeRabbit users, so adaptive limits apply. Your next review will be available in 8 minutes.

@nik-sta

nik-sta commented Jul 14, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown
✅ Action performed

Full review finished.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-idea Idea sourced from a community fork or contribution enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant