Template Filters

Filters are essentially functions that can be applied to variables. They are called with a pipe operator (|) and can take arguments. nunjucks filters alter output of variables in place. You can use all available builtin nunjucks filters.

{{ table.name | lower }} -> Converts name to lower case. (i.e. Member -> member)

They can be chained:

{{ table.name | plural | camelCase }} -> Converts name to plural in camel case. (i.e. member_name -> memberNames)

pg-generator filters

pg-generator offfers additional filters suitable for database scaffolding. Some of them are based on highly popular inflection npm module.

Filter Name Description Before After
camelCase Converts string to camelcase. member_name memberName
pascalCase Converts string to pascal case. member_name MemberName
classCase Converts string to class case. member_name MemberName
snakeCase Converts string to snake case. memberName member_name
dashCase Converts string to separated by dash (-). member_Name member-name
singular Converts string to singular. member_names member_name
plural Converts string to plural. member_name member_names
clearDefault Clears PostgreSQL default values to be used in JS code. "O''Reilly" "O'Reilly"
quote Adds quotes to string using JSON.stringify. member_name "member_name"
singleQuote Adds single quotes to string. member_name 'member_name'
stringifyIfObject({nullToUndef:false, raw:false}) Converts object to string to use in template directly. { name: 'George' } { name: 'George' }
stripPrefix(arg1, arg2, ...) Strips given texts and object.names from beginning of string. cart_cart_id1 cart_id
stripSuffix(arg1, arg2, ...) Strips given texts and object.names from end of string. cart_id2 cart
strip(arg1, arg2, ...) Strips given texts and object.names from string. cart_product_id3 cart_id
padRight(length, [char]) Pads string with optional char (default space) until it's length equals to length. member4 member......
relationName Converts foreign key name to be used in a relationship. If string ends with '_id' or 'id', strips it (case insensitive). Otherwise adds given prefix at the beginning of the string. company_id, account company related_account
makeJsDoc(str) Converts given string to JSOC lines "some \n multi line" " _ some\n _ multi line"

* Footnotes

*1 {{ 'cart_cart_id'    | stripPrefix('cart') }}        -> cart_id
   {{ 'cart_cart_id'    | stripPrefix(cart_table) }}    -> cart_id (Assuming cart_table.name equals cart)
   {{ 'a_b_c_table'     | stripPrefix('a', 'b') }}      -> c_table
*2 {{ 'cart_id'         | stripSuffix('id') }}          -> cart
*3 {{ 'cart_product_id' | strip('product') }}           -> cart_id
*4 {{ 'member'          | padRight(10) }}               -> member    <- Space padded until here.
   {{ 'member'          | padRight(10, '_') }}          -> member____

API of Filter Functions

clearDefault(string) ⇒ string | boolean | undefined

Clears SQL type escapes (Two quote '' to one quote ') and strips beginning and trailing quotes around string. Also escapes result according to JSON standards.

Returns: string | boolean | undefined - - Default value to use in template.

Param Type Description
string string Default value returned from PostgreSQL.

relationName(str, [prefix]) ⇒ string

Converts foreign key name to be used in a relationship. If string ends with '_id' or 'id', strips it (case insensitive). Otherwise adds given prefix at the beginning of the string. company_id -> company, account -> related_account

Returns: string - - Name for the belongsTo relationship.

Param Type Default Description
str string Foreign key name.
[prefix] string "related" Prefix to add if no given string does not contain 'id'.

stripPrefix(source, arguments) ⇒ string

Variadic function which strips given list of strings or object's names from start of the source string.

Returns: string - - Cleaned string.

Param Type Description
source string Source string to be cleaned.
arguments string | Object List of strings or objects (object's names) to delete from source string.

stripSuffix(source, arguments) ⇒ string

Variadic function which strips given list of strings or object's names from end of the source string.

Returns: string - - Cleaned string.

Param Type Description
source string Source string to be cleaned.
arguments string | Object List of strings or objects (object's names) to delete from source string.

padRight(str, count, [char]) ⇒ string

Pads given string's right side with given character (default space) to complete its length to count.

Returns: string - - Result string with length of count.

Param Type Default Description
str string Source string.
count number Total length of the result string.
[char] string "space" Padding character