Skip to content

Commit fd2bd15

Browse files
authored
Merge pull request #3 from shabalindn/type_json
fix: type json
2 parents 5545c2d + 8e31a13 commit fd2bd15

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

src/helpers/collector.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { StorageType } from "../types";
2+
13
export interface Collector {
24
/** Добавляет значение в параметры и возвращает порядковый номер в формате $1 */
3-
param(value: any, wrapQuote?: boolean): string;
5+
param(value: any, wrapQuote?: boolean, type?: StorageType): string;
46
/** Возвращает все зарегистрированные параметры */
57
getParams(): any[];
68
}
@@ -23,12 +25,16 @@ export class ParamsCollector implements Collector {
2325

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

3137
getParams(): any[] {
3238
return [];
3339
}
34-
}
40+
}

src/helpers/converter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const configs: TConverterConfig = {
1515
quote: true,
1616
},
1717
json: {
18-
process: (value) => `'${JSON.stringify(value)}'::jsonb`,
19-
quote: false,
18+
process: (value) => value,
19+
quote: true,
2020
},
2121
date: {
2222
process: (value) => value?.toISOString(),

src/helpers/prepare.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const prepare = ({ table, params, collector }: ToStorage, types: ('insert
2121
const convert = converter(type);
2222
value_ = convert.process(value_);
2323
if ((willUpdate || willInsert) && type !== 'raw') {
24-
value_ = collector.param(value_, convert.quote);
24+
value_ = collector.param(value_, convert.quote, type);
2525
}
2626
if (willInsert) {
2727
columns.push(`"${key}"`);

tests/upsert.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,20 @@ describe('upsert', () => {
4242
}).toThrow();
4343
});
4444

45-
it('JSONB', () => {
45+
it("JSONB", () => {
4646
const sql = upsert({
47-
table: { name: 'table', pk: 'table_id' },
47+
table: { name: "table", pk: "table_id" },
4848
params: {
49-
table_id: ['id', 'string'],
50-
list: [['asd', 'fgh'], 'json'],
51-
object: [{ "a": 1, "b": 'string' }, 'json'],
49+
table_id: ["id", "string"],
50+
list: [["asd", "fgh"], "json"],
51+
object: [{ a: 1, b: "string" }, "json"],
5252
},
5353
});
5454

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

58+
5959
it('Array', () => {
6060
const sql = upsert({
6161
table: { name: 'table', pk: 'table_id' },

0 commit comments

Comments
 (0)