Files
Automata/packages/cli/src/databases/utils/transformers.ts
कारतोफ्फेलस्क्रिप्ट™ 698d96a617 refactor: Setup typescript project references across workflow, core, and cli (#4519)
* refactor: use consistent folder structure across workflow, core, and cli

* setup typescript project references across workflow, core, and cli
2022-11-09 15:25:00 +01:00

34 lines
1.1 KiB
TypeScript

import { jsonParse } from 'n8n-workflow';
import { ValueTransformer } from 'typeorm';
import config from '@/config';
export const idStringifier = {
from: (value: number): string | number => (typeof value === 'number' ? value.toString() : value),
to: (value: string): number | string => (typeof value === 'string' ? Number(value) : value),
};
export const lowerCaser = {
from: (value: string): string => value,
to: (value: string): string => (typeof value === 'string' ? value.toLowerCase() : value),
};
/**
* Unmarshal JSON as JS object.
*/
export const objectRetriever: ValueTransformer = {
to: (value: object): object => value,
from: (value: string | object): object => (typeof value === 'string' ? jsonParse(value) : value),
};
/**
* Transformer for sqlite JSON columns to mimic JSON-as-object behavior
* from Postgres and MySQL.
*/
const jsonColumn: ValueTransformer = {
to: (value: object): string | object =>
config.getEnv('database.type') === 'sqlite' ? JSON.stringify(value) : value,
from: (value: string | object): object => (typeof value === 'string' ? jsonParse(value) : value),
};
export const sqlite = { jsonColumn };