Skip to content

Commit

Permalink
Merge pull request #3 from shabalindn/type_json
Browse files Browse the repository at this point in the history
fix: type json
  • Loading branch information
shabalindn committed Apr 20, 2024
2 parents 5545c2d + 8e31a13 commit fd2bd15
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
16 changes: 11 additions & 5 deletions src/helpers/collector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { StorageType } from "../types";

export interface Collector {
/** Добавляет значение в параметры и возвращает порядковый номер в формате $1 */
param(value: any, wrapQuote?: boolean): string;
param(value: any, wrapQuote?: boolean, type?: StorageType): string;
/** Возвращает все зарегистрированные параметры */
getParams(): any[];
}
Expand All @@ -23,12 +25,16 @@ export class ParamsCollector implements Collector {

/** Используется как заглушка, когда не передан коллектор, чтобы не менять интерфейс */
export class ParamsEmptyCollector implements Collector {
param(value: any, wrapQuote: false): string {
if (value === null || value === undefined) return 'null';
return wrapQuote ? `'${value}'` : value;
param(value: any, wrapQuote: false, type: StorageType): string {
if (value === null || value === undefined) return "null";
let result = value;
if (type === "json") {
result = JSON.stringify(result);
}
return wrapQuote ? `'${result}'` : result;
}

getParams(): any[] {
return [];
}
}
}
4 changes: 2 additions & 2 deletions src/helpers/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const configs: TConverterConfig = {
quote: true,
},
json: {
process: (value) => `'${JSON.stringify(value)}'::jsonb`,
quote: false,
process: (value) => value,
quote: true,
},
date: {
process: (value) => value?.toISOString(),
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const prepare = ({ table, params, collector }: ToStorage, types: ('insert
const convert = converter(type);
value_ = convert.process(value_);
if ((willUpdate || willInsert) && type !== 'raw') {
value_ = collector.param(value_, convert.quote);
value_ = collector.param(value_, convert.quote, type);
}
if (willInsert) {
columns.push(`"${key}"`);
Expand Down
14 changes: 7 additions & 7 deletions tests/upsert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ describe('upsert', () => {
}).toThrow();
});

it('JSONB', () => {
it("JSONB", () => {
const sql = upsert({
table: { name: 'table', pk: 'table_id' },
table: { name: "table", pk: "table_id" },
params: {
table_id: ['id', 'string'],
list: [['asd', 'fgh'], 'json'],
object: [{ "a": 1, "b": 'string' }, 'json'],
table_id: ["id", "string"],
list: [["asd", "fgh"], "json"],
object: [{ a: 1, b: "string" }, "json"],
},
});

console.log(sql)
expect(sql).toContain(`'["asd","fgh"]'::jsonb, '{"a":1,"b":"string"}'::jsonb`);
expect(sql).toContain(`'["asd","fgh"]', '{"a":1,"b":"string"}'`);
});


it('Array', () => {
const sql = upsert({
table: { name: 'table', pk: 'table_id' },
Expand Down

0 comments on commit fd2bd15

Please sign in to comment.