A Neo4j user-defined function that provides Nano ID generation capabilities directly within Cypher queries. Nano ID is a tiny, secure, URL-safe, unique string ID generator that’s an excellent alternative to UUID.
-
🚀 Fast: High-performance ID generation
-
🔒 Secure: Cryptographically strong random generator using
java.util.Random
-
📏 Compact: Shorter than UUID (21 characters vs 36)
-
🌐 URL-safe: Uses URL-safe characters (A-Za-z0-9_-)
-
⚙️ Configurable: Custom alphabet and size support
-
🔌 Native: Direct integration with Neo4j Cypher queries
-
🛡️ Robust: Graceful fallback handling for edge cases
-
Neo4j 5.26.0 or later
-
Java 21+
Note
|
This project requires Java 21 or later and is tested with Neo4j 5.26.0. If you’re using an older Java version, you’ll see a build error. To check your Java version: java -version To install Java 21, consider using SDKMAN!: # Install SDKMAN
curl -s "https://get.sdkman.io" | bash
# Install Java 21
sdk install java 21.0.2-tem
# Use Java 21 for this project
sdk use java 21.0.2-tem |
-
Download the latest JAR from the releases page
-
Copy the JAR file to your Neo4j
plugins
directory:cp neo4j-nanoid-function-1.0.0.jar $NEO4J_HOME/plugins/
-
Restart your Neo4j instance
-
Verify installation:
RETURN nanoid.generate() AS id;
// Generate a standard Nano ID (21 characters)
RETURN nanoid.generate() AS id;
// Result: "V1StGXR8_Z5jdHi6B-myT"
// Create a node with Nano ID
CREATE (u:User {id: nanoid.generate(), name: 'Alice'});
// Use in MERGE operations
MERGE (p:Product {id: nanoid.generate()})
SET p.name = 'New Product';
// Generate shorter ID (10 characters)
RETURN nanoid.generateSized(10) AS id;
// Result: "IRFa-VaY2b"
// Generate longer ID (30 characters)
RETURN nanoid.generateSized(30) AS id;
// Result: "6a-9Znp_MRiuZ8sNFbJ1mxBEq2S3K"
// Use only uppercase letters and numbers
RETURN nanoid.generateCustomSized('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', 12) AS id;
// Result: "A1B2C3D4E5F6"
// Use only digits with default size (21 characters)
RETURN nanoid.generateCustom('0123456789') AS id;
// Result: "123456789012345678901"
// Use only digits with custom size
RETURN nanoid.generateCustomSized('0123456789', 8) AS id;
// Result: "12345678"
// Custom alphabet with specific length
RETURN nanoid.generateCustomSized('ABCDEF', 16) AS id;
// Result: "ABCDEFFEDCBABCDE"
Function | Description | Example | Default Size |
---|---|---|---|
|
Generate standard URL-safe Nano ID (A-Za-z0-9_-) |
|
21 chars |
|
Generate URL-safe Nano ID with custom size |
|
Custom |
|
Generate with custom alphabet (default size) |
|
21 chars |
|
Generate with custom alphabet and size |
|
Custom |
The functions include robust error handling:
-
Invalid size: Negative or zero size values fallback to the standard 21-character Nano ID
-
Empty alphabet: Empty or null alphabet values fallback to the standard URL-safe alphabet (A-Za-z0-9_-)
-
Whitespace-only alphabet: Alphabet strings containing only whitespace fallback to the default behavior
-
Thread safety: All functions are thread-safe and can be used concurrently
// Invalid sizes fallback to default 21 characters
RETURN nanoid.generateSized(0) AS id; // Returns 21-char Nano ID
RETURN nanoid.generateSized(-5) AS id; // Returns 21-char Nano ID
RETURN nanoid.generateSized(null) AS id; // Returns 21-char Nano ID
// Invalid alphabets fallback to default behavior
RETURN nanoid.generateCustom('') AS id; // Returns 21-char URL-safe Nano ID
RETURN nanoid.generateCustom(' ') AS id; // Returns 21-char URL-safe Nano ID
RETURN nanoid.generateCustom(null) AS id; // Returns 21-char URL-safe Nano ID
// Combination of invalid inputs
RETURN nanoid.generateCustomSized('', 0) AS id; // Returns 21-char URL-safe Nano ID
RETURN nanoid.generateCustomSized(null, -5) AS id; // Returns 21-char URL-safe Nano ID
Feature | Nano ID | UUID |
---|---|---|
Length |
21 characters |
36 characters |
Alphabet |
URL-safe (64 chars) |
Hex + hyphens |
Collision probability |
Same as UUID v4 |
2^122 |
Performance |
~60% faster |
Standard |
URL-friendly |
✅ Yes |
❌ No (hyphens) |
-
Primary Keys: Shorter than UUID, perfect for database IDs
-
URL Slugs: URL-safe characters, no encoding needed
-
API Keys: Secure random generation
-
Session IDs: Compact and secure
-
File Names: Safe for all file systems
# Ensure you're using Java 21+
java -version
# Clone the repository
git clone https://github.com/Abhid14/neo4j-nanoid-function.git
cd neo4j-nanoid-function
# Build the project
./mvnw clean package
# The JAR will be created in target/
ls target/*.jar
Tip
|
If you encounter a Java version error during build, make sure you’re using Java 21 or later. The build will fail with older Java versions. |
The function uses the standard Nano ID configuration:
-
Default size: 21 characters
-
Default alphabet:
_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
(64 characters) -
Collision probability: ~1% after generating 1 billion IDs
-
Edge case handling: Invalid inputs gracefully fall back to defaults
The functions are designed to be robust and always return valid IDs:
-
Size validation: Null, zero, or negative sizes default to 21 characters
-
Alphabet validation: Null, empty, or whitespace-only alphabets default to URL-safe characters
-
Graceful fallback: All edge cases result in valid Nano ID generation rather than errors
-
Standard alphabet: Contains 64 URL-safe characters:
A-Za-z0-9_-
-
Custom alphabets: Support any character set you provide
-
Character repetition: Custom alphabets can contain repeated characters for weighted randomness
The project includes comprehensive test coverage:
-
Standard generation: Validates 21-character URL-safe IDs
-
Custom sizing: Tests various size parameters including edge cases
-
Custom alphabets: Validates numeric-only, letter-only, and special character alphabets
-
Uniqueness testing: Ensures 100 generated IDs are all unique
-
Edge case handling: Tests null, zero, negative, and empty inputs
-
Fallback behavior: Verifies graceful degradation for invalid inputs
Benchmarks on standard hardware:
-
Generation rate: ~2M IDs/second
-
Memory usage: Minimal overhead
-
Thread safety: Fully thread-safe
-
jnanoid 2.0.0: Core Nano ID implementation
-
Neo4j 5.26.0: Function framework
-
JUnit Jupiter 5.11.0: Testing framework (test scope)
-
AssertJ 3.27.3: Assertion library (test scope)
-
Fork the repository
-
Create a feature branch (
git checkout -b feature/amazing-feature
) -
Commit your changes (
git commit -m 'Add amazing feature'
) -
Push to the branch (
git push origin feature/amazing-feature
) -
Open a Pull Request
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.