Skip to content

Commit

Permalink
Work on #179
Browse files Browse the repository at this point in the history
  • Loading branch information
noam-honig committed Apr 14, 2023
1 parent 8f2127d commit e3f0f78
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions core-api/src/valueConverters.d.ts
Expand Up @@ -5,6 +5,7 @@ export declare class ValueConverters {
static readonly DateOnlyString: ValueConverter<Date>;
static readonly Boolean: ValueConverter<Boolean>;
static readonly Number: ValueConverter<number>;
static readonly String: ValueConverter<String>;
static readonly Integer: ValueConverter<number>;
static readonly Default: Required<ValueConverter<any>>;
static readonly JsonString: ValueConverter<any>;
Expand Down
5 changes: 1 addition & 4 deletions projects/core/src/remult3/RepositoryImplementation.ts
Expand Up @@ -1957,10 +1957,7 @@ export function decorateColumnSettings<valueType>(settings: FieldOptions<any, va
if (settings.valueType == String) {
let x = settings as unknown as FieldOptions<any, String>;
if (!settings.valueConverter)
x.valueConverter = {
toJson: x => x,
fromJson: x => x
};
x.valueConverter = ValueConverters.String;
}

if (settings.valueType == Number) {
Expand Down
9 changes: 9 additions & 0 deletions projects/core/src/shared-tests/db-tests.db-tests.ts
Expand Up @@ -28,6 +28,15 @@ testAll("what", async ({ remult, createEntity }) => {
await (await createEntity(stam)).create({ id: 1, title: 'noam' }).save();
expect(await remult.repo(stam).count()).toBe(1);
}, false);
testAll("data types", async ({ remult, createEntity }) => {
let r = await (await createEntity(stam)).create({
id: 1,
//@ts-ignore
title: 42
}).save();
expect(r.title).toEqual("42");

}, false);
testAll("filter works on all db",
async ({ createEntity }) => {
let s = await entityWithValidations.create4RowsInDp(createEntity);
Expand Down
17 changes: 17 additions & 0 deletions projects/core/src/valueConverters.ts
Expand Up @@ -164,6 +164,16 @@ export class ValueConverters {
},
inputType: InputTypes.number

}
static readonly String: ValueConverter<String> =
{
fromDb: enforceString,
toDb: enforceString,
fromJson: enforceString,
toJson: enforceString,
fromInput: enforceString,
toInput: enforceString,

}
static readonly Integer: ValueConverter<number> =
{
Expand Down Expand Up @@ -206,4 +216,11 @@ export class ValueConverters {
toInput: x => ValueConverters.JsonString.toJson(x),
fieldTypeInDb: 'json'
}
}
function enforceString(value: string) {
if (value === null || value === undefined)
return value;
if (typeof (value) !== "string")
return (value as any).toString()
return value;
}

0 comments on commit e3f0f78

Please sign in to comment.