@random — Auto-Generated Values
Generates random field values on insert. Supports multiple character sets, lengths, and prefix/suffix options. If an explicit value is provided, the annotation is skipped.
Syntax
src/interfaces.ts
// Colon syntax:
// @random:<type>:<length>
id?: string; // @random:string:16
// Parenthesized syntax (for extra options):
// @random:<type>(<length>, <options>)
uid?: string; // @random:string(24)
// Number (returns integer, not string):
pin?: number; // @random:number:4Character Set Variants
src/interfaces.ts
// Alphanumeric with case control:
upperAlnum?: string; // @random:alnum(10, upper)
lowerAlnum?: string; // @random:alnum(10, lower)
// Letters only:
lowerLetters?: string; // @random:lower(12)
upperLetters?: string; // @random:upper(8)
// Hex:
hexStr?: string; // @random:hex(16)
upperHex?: string; // @random:hex(16, upper)Prefix / Suffix
src/interfaces.ts
tokenPrefixed?: string; // @random:alnum(8, pfx=TKN_)
invoiceNum?: string; // @random:number(6, pfx=INV-)
userCode?: string; // @random:alnum(12, upper, pfx=USR_, sfx=_END)Custom Charset
src/interfaces.ts
// @random:custom(<charset>, <length>)
customPin?: string; // @random:custom(ABCDEF123456, 6)Usage
src/auth.ts
// Insert — values auto-generated
const key = await RandomKeys.insert({
createdAt: new Date().toISOString(),
});
console.log(key.id); // "jyAlzGHSigYiqvee" (string:16)
console.log(key.pin); // 5757 (number)
console.log(key.tokenPrefixed);// "TKN_TCrIn7NZ" (prefixed)
// Provide explicit value — annotation skipped
const explicit = await RandomKeys.insert({
id: "custom-id-001",
pin: 9999,
});
console.log(explicit.id); // "custom-id-001"
console.log(explicit.pin); // 9999Errors
| Issue | Cause | Fix |
|---|---|---|
| Unknown charset type | Typo in type name | Use: string, number, alnum, alpha, lower, upper, hex, custom |
| Length not specified | Missing length parameter | Defaults to 32 for strings, 8 for numbers |
Conventions
@random:numberreturns a JavaScriptnumbertype; all others returnstring- Prefixes and suffixes are not counted in the length
- Explicit values always take priority over auto-generation
- Colon syntax (
@random:string:16) is simpler; parenthesized syntax (@random:string(16)) enables case control, prefix, suffix, and custom charset