typeorm#ValueTransformer TypeScript Examples

The following examples show how to use typeorm#ValueTransformer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.ts    From context-mod with MIT License 7 votes vote down vote up
/**
 * @see https://github.com/typeorm/typeorm/issues/873#issuecomment-424643086
 *
 * */
export class ColumnDurationTransformer implements ValueTransformer {
    to(data?: Duration): number | undefined {
        if (!isNullOrUndefined(data)) {
            return data.asSeconds();
        }
       return undefined;
    }

    from(data?: number | null): Duration | undefined {
        if (!isNullOrUndefined(data)) {
            return dayjs.duration(data, 'seconds')
        }
        return undefined
    }
}
Example #2
Source File: AmountTransformer.ts    From cashcash-desktop with MIT License 6 votes vote down vote up
export default class AmountTransformer implements ValueTransformer {
    to(stringAmount: string): number {
        return +stringAmount * 100;
    }

    from(amountCent: number): string {
        return (amountCent / 100).toFixed(2);
    }
}
Example #3
Source File: JsonTransformer.ts    From cashcash-desktop with MIT License 6 votes vote down vote up
export default class JsonTransformer<T> implements ValueTransformer {
    to(obj: T): string {
        return JSON.stringify(obj);
    }

    from(raw: string): T {
        return JSON.parse(raw);
    }
}
Example #4
Source File: TagTransformer.ts    From cashcash-desktop with MIT License 6 votes vote down vote up
export default class TagTransformer implements ValueTransformer {
    to(tagIdList: number[]): string {
        const list = tagIdList || [];
        return (
            list
                // We sort it to be able to search for several tags in one query
                .sort()
                // We wrap the ids with "-" to have a simple regex when searching
                .map((item) => `${TAG_SEPARATOR}${item}${TAG_SEPARATOR}`)
                .join('')
        );
    }

    from(stringTagIdList: string): number[] {
        if (!stringTagIdList) {
            return [];
        }
        return stringTagIdList
            .slice(1, stringTagIdList.length - 1)
            .split(`${TAG_SEPARATOR}${TAG_SEPARATOR}`)
            .map((item) => +item);
    }
}
Example #5
Source File: transformer.ts    From typeorm-query-builder-wrapper with MIT License 6 votes vote down vote up
// Ref: https://github.com/typeorm/typeorm/issues/873#issuecomment-502294597
export class ColumnNumericTransformer implements ValueTransformer {
  /**
   * Convert string of number to real number.
   *
   * @param {(number | null)} [data]
   * @return {*}  {(number | null)}
   * @memberof ColumnNumericTransformer
   */
  to(data?: number | null): number | null {
    if (!isNullOrUndefined(data)) {
      return data;
    }
    return null;
  }

  from(data?: string | null): number | null {
    if (!isNullOrUndefined(data)) {
      const res = parseFloat(data);
      if (isNaN(res)) {
        return null;
      } else {
        return res;
      }
    }
    return null;
  }
}
Example #6
Source File: Base64Transformer.ts    From bluebubbles-server with Apache License 2.0 5 votes vote down vote up
Base64Transformer: ValueTransformer = {
    from: dbValue => dbValue ? Buffer.from(dbValue, 'base64') : null,
    to: entityValue => entityValue ? entityValue.toString('base64') : null
}
Example #7
Source File: BooleanTransformer.ts    From bluebubbles-server with Apache License 2.0 5 votes vote down vote up
BooleanTransformer: ValueTransformer = {
    from: dbValue => Boolean(dbValue),
    to: entityValue => Number(entityValue)
}
Example #8
Source File: DateTransformer.ts    From bluebubbles-server with Apache License 2.0 5 votes vote down vote up
DateTransformer: ValueTransformer = {
    from: dbValue => getDateUsing2001(dbValue),
    to: entityValue => convertDateTo2001Time(entityValue)
}
Example #9
Source File: MessageTypeTransformer.ts    From bluebubbles-server with Apache License 2.0 5 votes vote down vote up
MessageTypeTransformer: ValueTransformer = {
    from: (dbValue: number) =>
        Object.keys(ReactionIdToString).includes(dbValue.toString())
            ? ReactionIdToString[dbValue.toString()]
            : dbValue.toString(),
    to: entityValue => ReactionStringToId[entityValue] ?? 0
}
Example #10
Source File: index.ts    From context-mod with MIT License 5 votes vote down vote up
export class ColumnDecimalTransformer implements ValueTransformer {
    to(data: number): number {
        return data;
    }
    from(data: string): number {
        return parseFloat(data);
    }
}