-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/sql-tag/src/factories/createSqlTag.test/uuid.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { FragmentToken } from '../../tokens'; | ||
import { createSqlTag } from '../createSqlTag'; | ||
import test from 'ava'; | ||
|
||
const sql = createSqlTag(); | ||
|
||
test('binds a uuid', (t) => { | ||
const query = sql.fragment`SELECT ${sql.uuid( | ||
'00000000-0000-0000-0000-000000000000', | ||
)}`; | ||
|
||
t.deepEqual(query, { | ||
sql: 'SELECT $slonik_1::uuid', | ||
type: FragmentToken, | ||
values: ['00000000-0000-0000-0000-000000000000'], | ||
}); | ||
}); | ||
|
||
test('throws if not valid uuid', (t) => { | ||
const error = t.throws(() => { | ||
sql.fragment`SELECT ${sql.uuid('1')}`; | ||
}); | ||
|
||
t.is(error?.message, 'UUID parameter value must be a valid UUID.'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/sql-tag/src/sqlFragmentFactories/createUuidSqlFragment.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { FragmentToken } from '../tokens'; | ||
import { type SqlFragmentToken, type UuidSqlToken } from '../types'; | ||
import { formatSlonikPlaceholder } from '../utilities/formatSlonikPlaceholder'; | ||
import { InvalidInputError } from '@slonik/errors'; | ||
|
||
export type UUID = `${string}-${string}-${string}-${string}-${string}`; | ||
|
||
// eslint-disable-next-line func-style | ||
function isValidUuid(uuid: string): uuid is UUID { | ||
const uuidRegex = /^[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}$/iu; | ||
|
||
return uuidRegex.test(uuid); | ||
} | ||
|
||
export const createUuidSqlFragment = ( | ||
token: UuidSqlToken, | ||
greatestParameterPosition: number, | ||
): SqlFragmentToken => { | ||
if (!isValidUuid(token.uuid)) { | ||
throw new InvalidInputError('UUID parameter value must be a valid UUID.'); | ||
} | ||
|
||
return { | ||
sql: formatSlonikPlaceholder(greatestParameterPosition + 1) + '::uuid', | ||
type: FragmentToken, | ||
values: [token.uuid], | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters