-
-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add custom types for array, blob and json (#559)
### ArrayType In PostgreSQL and MongoDB, it uses native arrays, otherwise it concatenates the values into string separated by commas. This means that you can't use values that contain comma with the `ArrayType` (but you can create custom array type that will handle this case, e.g. by using different separator). By default array of strings is returned from the type. You can also have arrays of numbers or other data types - to do so, you will need to implement custom `hydrate` method that is used for converting the array values to the right type. ```typescript @Property({ type: new ArrayType(i => +i), nullable: true }) array?: number[]; ``` ### BlobType Blob type can be used to store binary data in the database. ```typescript @Property({ type: BlobType, nullable: true }) blob?: Buffer; ``` ### JsonType To store objects we can use `JsonType`. As some drivers are handling objects automatically and some don't, this type will handle the serialization in a driver independent way (calling `parse` and `stringify` only when needed). ```typescript @Property({ type: JsonType, nullable: true }) object?: { foo: string; bar: number }; ``` Related: #476
- Loading branch information
Showing
26 changed files
with
551 additions
and
53 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Type } from './Type'; | ||
import { Utils, ValidationError } from '../utils'; | ||
import { EntityProperty } from '../typings'; | ||
import { Platform } from '../platforms'; | ||
|
||
export class ArrayType<T extends string | number = string> extends Type<T[] | null, string | null> { | ||
|
||
constructor(private readonly hydrate: (i: string) => T = i => i as T) { | ||
super(); | ||
} | ||
|
||
convertToDatabaseValue(value: T[] | null, platform: Platform): string | null { | ||
if (!value) { | ||
return value as null; | ||
} | ||
|
||
if (Array.isArray(value)) { | ||
return platform.marshallArray(value as string[]); | ||
} | ||
|
||
throw ValidationError.invalidType(ArrayType, value, 'JS'); | ||
} | ||
|
||
convertToJSValue(value: T[] | string | null, platform: Platform): T[] | null { | ||
if (!value) { | ||
return value as null; | ||
} | ||
|
||
if (Utils.isString(value)) { | ||
value = platform.unmarshallArray(value) as T[]; | ||
} | ||
|
||
return value.map(i => this.hydrate(i as string)); | ||
} | ||
|
||
toJSON(value: T[]): T[] { | ||
return value; | ||
} | ||
|
||
getColumnType(prop: EntityProperty, platform: Platform): string { | ||
return platform.getArrayDeclarationSQL(); | ||
} | ||
|
||
} |
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,27 @@ | ||
import { Type } from './Type'; | ||
import { Platform } from '../platforms'; | ||
import { EntityProperty } from '../typings'; | ||
|
||
export class BlobType extends Type<Buffer | null> { | ||
|
||
convertToDatabaseValue(value: Buffer, platform: Platform): Buffer { | ||
return value; | ||
} | ||
|
||
convertToJSValue(value: Buffer, platform: Platform): Buffer | null { | ||
if (!value) { | ||
return value; | ||
} | ||
|
||
if (value.buffer instanceof Buffer) { | ||
return value.buffer; | ||
} | ||
|
||
return Buffer.from(value); | ||
} | ||
|
||
getColumnType(prop: EntityProperty, platform: Platform): string { | ||
return platform.getBlobDeclarationSQL(); | ||
} | ||
|
||
} |
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 { Type } from './Type'; | ||
import { Platform } from '../platforms'; | ||
import { EntityProperty } from '../typings'; | ||
import { Utils } from '../utils'; | ||
|
||
export class JsonType extends Type<object, string | null> { | ||
|
||
convertToDatabaseValue(value: object, platform: Platform): string | null { | ||
if (platform.convertsJsonAutomatically(true) || value === null) { | ||
return value as unknown as string; | ||
} | ||
|
||
return JSON.stringify(value); | ||
} | ||
|
||
convertToJSValue(value: string | object, platform: Platform): object { | ||
if (!platform.convertsJsonAutomatically() && Utils.isString(value)) { | ||
return JSON.parse(value); | ||
} | ||
|
||
return value as object; | ||
} | ||
|
||
getColumnType(prop: EntityProperty, platform: Platform): string { | ||
return platform.getJsonDeclarationSQL(); | ||
} | ||
|
||
} |
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
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
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
Oops, something went wrong.