Slonik - add support for json token type (#37437)

* Add support for json token type

* Fix bugs

* Add tests to verify jeson objects
This commit is contained in:
Ivan Kerin
2019-08-09 07:27:19 -07:00
committed by Nathan Shively-Sanders
parent 99472198e6
commit 22093c2dc3
3 changed files with 54 additions and 1 deletions
+26 -1
View File
@@ -1,4 +1,4 @@
// Type definitions for slonik 18.2
// Type definitions for slonik 18.6
// Project: https://github.com/gajus/slonik#readme
// Definitions by: Sebastian Sebald <https://github.com/sebald>
// Misha Kaletsky <https://github.com/mmkal>
@@ -26,6 +26,22 @@ export type LogicalBooleanOperatorType = 'AND' | 'OR';
// EXPRESSIONS AND TOKENS
// ----------------------------------------------------------------------
export type SerializableValueType =
| string
| number
| boolean
| null
| object
| SerializableValueObject
| SerializableValueArray;
export interface SerializableValueObject {
[x: string]: SerializableValueType;
}
export interface SerializableValueArray
extends ReadonlyArray<SerializableValueType> {}
export interface IdentifierTokenType {
names: ReadonlyArray<string>;
type: typeof SlonikSymbol.IdentifierTokenSymbol;
@@ -60,6 +76,11 @@ export interface ArraySqlTokenType {
values: PrimitiveValueExpressionType[];
}
export interface JsonSqlTokenType {
value: SerializableValueType;
type: typeof SlonikSymbol.JsonTokenSymbol;
}
export interface TupleSqlTokenType {
values: PrimitiveValueExpressionType[];
type: typeof SlonikSymbol.TupleTokenSymbol;
@@ -101,6 +122,7 @@ export type SqlTokenType =
AssignmentListTokenType |
IdentifierTokenType |
IdentifierListTokenType |
JsonSqlTokenType |
RawSqlTokenType |
SqlSqlTokenType<any> |
TupleListSqlTokenType |
@@ -325,6 +347,9 @@ export interface SqlTaggedTemplateType {
identifierList: (
identifiers: IdentifierListMemberType[]
) => IdentifierListTokenType;
json: (
value: SerializableValueType
) => JsonSqlTokenType;
raw: (
rawSql: string,
values?: PrimitiveValueExpressionType[]
+27
View File
@@ -283,6 +283,33 @@ createTimestampWithTimeZoneTypeParser();
// ExpectType SqlSqlTokenType
const query1 = sql`SELECT ${'baz'} FROM (${query0})`;
await connection.query(sql`
SELECT (${sql.json([1, 2, { test: 12, other: 'test' }])})
`);
await connection.query(sql`
SELECT (${sql.json('test')})
`);
await connection.query(sql`
SELECT (${sql.json(null)})
`);
await connection.query(sql`
SELECT (${sql.json(123)})
`);
await connection.query(sql`
SELECT (${sql.json({
nested: {
object: { is: { this: 'test', other: 12, and: new Date('123') } },
},
})})
`);
// $ExpectError
sql`SELECT ${sql.json(undefined)}`;
await connection.query(sql`
SELECT (${sql.valueList([1, 2, 3])})
`);
+1
View File
@@ -4,6 +4,7 @@ export const BooleanExpressionTokenSymbol: unique symbol;
export const ComparisonPredicateTokenSymbol: unique symbol;
export const IdentifierListTokenSymbol: unique symbol;
export const IdentifierTokenSymbol: unique symbol;
export const JsonTokenSymbol: unique symbol;
export const RawSqlTokenSymbol: unique symbol;
export const SqlTokenSymbol: unique symbol;
export const ValueListTokenSymbol: unique symbol;