Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tokens: use Symbols instead of strings #595

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const interpolatePositionalParameterReferences = (

return {
sql: resultSql,
type: 'SLONIK_TOKEN_FRAGMENT',
type: Symbol.for('SLONIK_TOKEN_FRAGMENT'),
values: Object.freeze(resultValues),
};
} as FragmentSqlToken;
};
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ export const createArraySqlFragment = (

let sql = formatSlonikPlaceholder(placeholderIndex) + '::';

if (
isSqlToken(token.memberType) &&
token.memberType.type === 'SLONIK_TOKEN_FRAGMENT'
) {
if (isSqlToken(token.memberType) && token.memberType.type === FragmentToken) {
const sqlFragment = createSqlTokenSqlFragment(
token.memberType,
placeholderIndex,
Expand Down
29 changes: 15 additions & 14 deletions packages/sql-tag/src/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
export const ArrayToken = 'SLONIK_TOKEN_ARRAY' as const;
export const BinaryToken = 'SLONIK_TOKEN_BINARY' as const;
export const ComparisonPredicateToken =
'SLONIK_TOKEN_COMPARISON_PREDICATE' as const;
export const DateToken = 'SLONIK_TOKEN_DATE' as const;
export const FragmentToken = 'SLONIK_TOKEN_FRAGMENT' as const;
export const IdentifierToken = 'SLONIK_TOKEN_IDENTIFIER' as const;
export const IntervalToken = 'SLONIK_TOKEN_INTERVAL' as const;
export const JsonBinaryToken = 'SLONIK_TOKEN_JSON_BINARY' as const;
export const JsonToken = 'SLONIK_TOKEN_JSON' as const;
export const ListToken = 'SLONIK_TOKEN_LIST' as const;
export const QueryToken = 'SLONIK_TOKEN_QUERY' as const;
export const TimestampToken = 'SLONIK_TOKEN_TIMESTAMP' as const;
export const UnnestToken = 'SLONIK_TOKEN_UNNEST' as const;
export const ArrayToken = Symbol.for('SLONIK_TOKEN_ARRAY');
export const BinaryToken = Symbol.for('SLONIK_TOKEN_BINARY');
export const ComparisonPredicateToken = Symbol.for(
'SLONIK_TOKEN_COMPARISON_PREDICATE',
);
export const DateToken = Symbol.for('SLONIK_TOKEN_DATE');
export const FragmentToken = Symbol.for('SLONIK_TOKEN_FRAGMENT');
export const IdentifierToken = Symbol.for('SLONIK_TOKEN_IDENTIFIER');
export const IntervalToken = Symbol.for('SLONIK_TOKEN_INTERVAL');
export const JsonBinaryToken = Symbol.for('SLONIK_TOKEN_JSON_BINARY');
export const JsonToken = Symbol.for('SLONIK_TOKEN_JSON');
export const ListToken = Symbol.for('SLONIK_TOKEN_LIST');
export const QueryToken = Symbol.for('SLONIK_TOKEN_QUERY');
export const TimestampToken = Symbol.for('SLONIK_TOKEN_TIMESTAMP');
export const UnnestToken = Symbol.for('SLONIK_TOKEN_UNNEST');
6 changes: 3 additions & 3 deletions packages/sql-tag/src/utilities/isSqlToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export const isSqlToken = (subject: unknown): subject is SqlTokenType => {
);
}

if (typeof subject.type !== 'string') {
throw new UnexpectedStateError('Expected type to be string.');
if (typeof subject.type !== 'symbol') {
throw new UnexpectedStateError('Expected type to be symbol.');
}

return (Tokens as readonly string[]).includes(subject.type);
return (Tokens as readonly symbol[]).includes(subject.type);
};
Loading