pogi 2.10.2 → 3.0.0-beta2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/launch.json +47 -15
- package/CHANGELOG.md +11 -0
- package/docs/API/PgDb.md +25 -0
- package/docs/notification.md +19 -0
- package/jest.config.js +23 -0
- package/lib/bin/generateInterface.js +3 -3
- package/lib/bin/generateInterface.js.map +1 -1
- package/lib/connectionOptions.d.ts +10 -0
- package/lib/index.d.ts +1 -1
- package/lib/pgConverters.d.ts +9 -8
- package/lib/pgConverters.js +46 -32
- package/lib/pgConverters.js.map +1 -1
- package/lib/pgConverters.test.d.ts +1 -0
- package/lib/pgConverters.test.js +13 -0
- package/lib/pgConverters.test.js.map +1 -0
- package/lib/pgDb.d.ts +27 -27
- package/lib/pgDb.js +293 -100
- package/lib/pgDb.js.map +1 -1
- package/lib/pgDb.test.d.ts +1 -0
- package/lib/pgDb.test.js +1126 -0
- package/lib/pgDb.test.js.map +1 -0
- package/lib/pgDbInterface.d.ts +53 -0
- package/lib/pgDbInterface.js +11 -0
- package/lib/pgDbInterface.js.map +1 -0
- package/lib/pgDbOperators.d.ts +3 -3
- package/lib/pgDbOperators.js +4 -7
- package/lib/pgDbOperators.js.map +1 -1
- package/lib/pgDbOperators.test.d.ts +1 -0
- package/lib/pgDbOperators.test.js +313 -0
- package/lib/pgDbOperators.test.js.map +1 -0
- package/lib/pgSchema.d.ts +10 -9
- package/lib/pgSchema.js.map +1 -1
- package/lib/pgSchemaInterface.d.ts +12 -0
- package/lib/pgSchemaInterface.js +3 -0
- package/lib/pgSchemaInterface.js.map +1 -0
- package/lib/pgTable.d.ts +15 -40
- package/lib/pgTable.js +54 -54
- package/lib/pgTable.js.map +1 -1
- package/lib/pgTableInterface.d.ts +102 -0
- package/lib/pgTableInterface.js +4 -0
- package/lib/pgTableInterface.js.map +1 -0
- package/lib/pgUtils.d.ts +16 -6
- package/lib/pgUtils.js +162 -31
- package/lib/pgUtils.js.map +1 -1
- package/lib/queryAble.d.ts +20 -53
- package/lib/queryAble.js +149 -80
- package/lib/queryAble.js.map +1 -1
- package/lib/queryAbleInterface.d.ts +55 -0
- package/lib/queryAbleInterface.js +7 -0
- package/lib/queryAbleInterface.js.map +1 -0
- package/lib/queryWhere.d.ts +2 -2
- package/lib/queryWhere.js +19 -23
- package/lib/queryWhere.js.map +1 -1
- package/mkdocs.yml +1 -0
- package/package.json +21 -11
- package/src/bin/generateInterface.ts +2 -2
- package/src/connectionOptions.ts +48 -13
- package/src/index.d.ts +7 -0
- package/src/index.ts +1 -1
- package/src/pgConverters.test.ts +10 -0
- package/src/pgConverters.ts +34 -22
- package/src/pgDb.test.ts +1324 -0
- package/src/pgDb.ts +318 -122
- package/src/pgDbInterface.ts +57 -0
- package/src/pgDbOperators.test.ts +478 -0
- package/src/pgDbOperators.ts +45 -22
- package/src/pgSchema.ts +10 -9
- package/src/pgSchemaInterface.ts +12 -0
- package/src/pgTable.ts +66 -98
- package/src/pgTableInterface.ts +131 -0
- package/src/pgUtils.ts +166 -42
- package/src/queryAble.ts +167 -125
- package/src/queryAbleInterface.ts +104 -0
- package/src/queryWhere.ts +42 -43
- package/{spec/resources → src/test}/init.sql +23 -0
- package/src/test/pgServiceRestartTest.ts +1500 -0
- package/{spec/resources → src/test}/throw_exception.sql +0 -0
- package/{spec/resources → src/test}/tricky.sql +0 -0
- package/{src/tsconfig.json → tsconfig.json} +12 -11
- package/spec/run.js +0 -5
- package/spec/support/jasmine.json +0 -9
- package/src/test/pgDbOperatorSpec.ts +0 -492
- package/src/test/pgDbSpec.ts +0 -994
package/lib/pgDb.test.js
ADDED
|
@@ -0,0 +1,1126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const pgDb_1 = require("./pgDb");
|
|
5
|
+
const _ = require("lodash");
|
|
6
|
+
function parseComplexTypeArray(str) {
|
|
7
|
+
let list = JSON.parse('[' + str.substring(1, str.length - 1) + ']');
|
|
8
|
+
let result = [];
|
|
9
|
+
for (let elementStr of list) {
|
|
10
|
+
result.push(parseComplexType(elementStr));
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
function parseComplexType(str) {
|
|
15
|
+
str = str.substring(1, str.length - 1);
|
|
16
|
+
let e = /"((?:[^"]|"")*)"(?:,|$)|([^,]*)(?:,|$)/g;
|
|
17
|
+
let valList = [];
|
|
18
|
+
let parsingResult;
|
|
19
|
+
let valStr;
|
|
20
|
+
let hasNextValue;
|
|
21
|
+
do {
|
|
22
|
+
parsingResult = e.exec(str);
|
|
23
|
+
if (!parsingResult)
|
|
24
|
+
break;
|
|
25
|
+
valStr = (parsingResult[0] == '' || parsingResult[0] == ',' || parsingResult[2] == 'null') ? null : parsingResult[1] || parsingResult[2];
|
|
26
|
+
if (parsingResult[0] == '"",' || parsingResult[0] == '""') {
|
|
27
|
+
valStr = '';
|
|
28
|
+
}
|
|
29
|
+
valList.push(valStr ? valStr.replace(/""/g, '"') : valStr);
|
|
30
|
+
hasNextValue = parsingResult[0].substring(parsingResult[0].length - 1, parsingResult[0].length) == ',';
|
|
31
|
+
} while (hasNextValue);
|
|
32
|
+
return valList;
|
|
33
|
+
}
|
|
34
|
+
describe("pgdb", () => {
|
|
35
|
+
let pgdb;
|
|
36
|
+
let schema = 'pgdb_test';
|
|
37
|
+
let hiddenSchema = 'pgdb_test_hidden';
|
|
38
|
+
let table;
|
|
39
|
+
let tableGroups;
|
|
40
|
+
let tableTypes;
|
|
41
|
+
beforeAll(() => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
42
|
+
try {
|
|
43
|
+
pgdb = yield pgDb_1.PgDb.connect({ connectionString: "postgres://" });
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
console.error("connection failed! Are you specified PGUSER/PGDATABASE/PGPASSWORD correctly?");
|
|
47
|
+
process.exit(1);
|
|
48
|
+
}
|
|
49
|
+
let current_role = yield pgdb.queryOneField('SELECT current_role');
|
|
50
|
+
yield pgdb.run('CREATE SCHEMA IF NOT EXISTS "' + hiddenSchema + '"');
|
|
51
|
+
yield pgdb.run('GRANT ALL ON SCHEMA "' + hiddenSchema + '" TO "' + current_role + '"');
|
|
52
|
+
yield pgdb.execute('src/test/init.sql', (cmd) => cmd.replace(/__SCHEMA__/g, '"' + hiddenSchema + '"'));
|
|
53
|
+
yield pgdb.run('REVOKE ALL ON SCHEMA "' + hiddenSchema + '" FROM "' + current_role + '" CASCADE');
|
|
54
|
+
yield pgdb.run('CREATE SCHEMA IF NOT EXISTS "' + schema + '"');
|
|
55
|
+
yield pgdb.execute('src/test/init.sql', (cmd) => cmd.replace(/__SCHEMA__/g, '"' + schema + '"'));
|
|
56
|
+
yield pgdb.reload();
|
|
57
|
+
pgdb.setLogger(console);
|
|
58
|
+
table = pgdb.schemas[schema]['users'];
|
|
59
|
+
tableGroups = pgdb.schemas[schema]['groups'];
|
|
60
|
+
tableTypes = pgdb.schemas[schema]['types'];
|
|
61
|
+
}));
|
|
62
|
+
beforeEach(() => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
63
|
+
yield table.delete({});
|
|
64
|
+
yield tableGroups.delete({});
|
|
65
|
+
yield tableTypes.delete({});
|
|
66
|
+
}));
|
|
67
|
+
afterEach(() => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
68
|
+
if (pgdb.pool.waitingCount != 0) {
|
|
69
|
+
expect('Not all connection is released').toBeFalsy();
|
|
70
|
+
yield pgdb.pool.end();
|
|
71
|
+
}
|
|
72
|
+
}));
|
|
73
|
+
afterAll(() => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
74
|
+
yield pgdb.close();
|
|
75
|
+
}));
|
|
76
|
+
it("Exception on name collision", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
77
|
+
yield table.insert({ name: 'A' });
|
|
78
|
+
try {
|
|
79
|
+
yield pgdb.query(`select u1.id, u2.id from ${table} u1 left join ${table} u2 ON true `);
|
|
80
|
+
expect(false).toBeTruthy();
|
|
81
|
+
}
|
|
82
|
+
catch (e) {
|
|
83
|
+
expect(/Name collision for the query, two or more fields have the same name./.test(e.message)).toBeTruthy();
|
|
84
|
+
}
|
|
85
|
+
}));
|
|
86
|
+
it("After adding parser should be able to parse complex type", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
87
|
+
yield pgdb.setTypeParser('permissionForResourceType', (val) => parseComplexType(val));
|
|
88
|
+
yield pgdb.setTypeParser('_permissionForResourceType', (val) => val == "{}" ? [] : parseComplexTypeArray(val));
|
|
89
|
+
yield table.insert({
|
|
90
|
+
name: 'Piszkos Fred',
|
|
91
|
+
permission: "(read,book)",
|
|
92
|
+
permissionList: [
|
|
93
|
+
'(admin,"chaos j ()"",""""j ,")',
|
|
94
|
+
'(write,book)',
|
|
95
|
+
'(write,)',
|
|
96
|
+
'(,"")',
|
|
97
|
+
'(,)',
|
|
98
|
+
'(write,null)'
|
|
99
|
+
],
|
|
100
|
+
});
|
|
101
|
+
let pf = yield table.findOne({ name: 'Piszkos Fred' });
|
|
102
|
+
expect(pf.permission).toEqual(['read', 'book']);
|
|
103
|
+
expect(pf.permissionList[0]).toEqual(['admin', 'chaos j ()",""j ,']);
|
|
104
|
+
expect(pf.permissionList[1]).toEqual(['write', 'book']);
|
|
105
|
+
expect(pf.permissionList[2]).toEqual(['write', null]);
|
|
106
|
+
expect(pf.permissionList[3]).toEqual([null, ""]);
|
|
107
|
+
expect(pf.permissionList[4]).toEqual([null, null]);
|
|
108
|
+
expect(pf.permissionList[5]).toEqual(['write', null]);
|
|
109
|
+
expect(pf.permissionList.length).toEqual(6);
|
|
110
|
+
}));
|
|
111
|
+
it("Complex type could be easily read and converted to json", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
112
|
+
yield table.insert({
|
|
113
|
+
name: 'Elveszett cirkalo',
|
|
114
|
+
permission: "(read,book)",
|
|
115
|
+
permissionList: [
|
|
116
|
+
'(admin,"chaos j ()"",""""j ,")',
|
|
117
|
+
'(write,book)',
|
|
118
|
+
'(write,)',
|
|
119
|
+
'(,"")',
|
|
120
|
+
'(,)',
|
|
121
|
+
'(write,null)'
|
|
122
|
+
],
|
|
123
|
+
});
|
|
124
|
+
let res = yield table.query(`SELECT to_json(permission) perjson, to_json("permissionList") perlistjson
|
|
125
|
+
FROM ${table}
|
|
126
|
+
WHERE name='Elveszett cirkalo' `);
|
|
127
|
+
expect(res[0].perjson).toEqual({ permission: 'read', resource: 'book' });
|
|
128
|
+
expect(res[0].perlistjson).toEqual([
|
|
129
|
+
{ permission: 'admin', resource: 'chaos j ()",""j ,' },
|
|
130
|
+
{ permission: 'write', resource: 'book' },
|
|
131
|
+
{ permission: 'write', resource: null },
|
|
132
|
+
{ permission: null, resource: '' },
|
|
133
|
+
{ permission: null, resource: null },
|
|
134
|
+
{ permission: 'write', resource: 'null' }
|
|
135
|
+
]);
|
|
136
|
+
}));
|
|
137
|
+
it("List for a non array column in the condition should be converted to 'IN Array'", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
138
|
+
yield table.insert({ name: 'Fulig Jimmy', favourites: ['sport'] });
|
|
139
|
+
yield table.insert({ name: 'Vanek ur', favourites: ['sport', 'food'] });
|
|
140
|
+
yield table.insert({ name: 'Gorcsev Ivan', favourites: ['tech', 'food'] });
|
|
141
|
+
let res1 = yield table.findAll();
|
|
142
|
+
let res2 = yield table.find({ id: res1.map(e => e.id) });
|
|
143
|
+
expect(res1.map(c => c.id)).toEqual(res2.map(c => c.id));
|
|
144
|
+
}));
|
|
145
|
+
it("Testing where function", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
146
|
+
yield table.insert({ name: 'Fulig Jimmy', favourites: ['sport'] });
|
|
147
|
+
yield table.insert({ name: 'Vanek ur', favourites: ['sport', 'food'] });
|
|
148
|
+
yield table.insert({ name: 'Gorcsev Ivan', favourites: ['tech', 'food', 'sport'] });
|
|
149
|
+
let res = yield table.findWhere(':fav = ANY("favourites")', { fav: 'sport' });
|
|
150
|
+
expect(res.length).toEqual(3);
|
|
151
|
+
res = yield table.findWhere(':fav = ANY("favourites")', { fav: 'food' });
|
|
152
|
+
expect(res.length).toEqual(2);
|
|
153
|
+
res = yield table.findWhere(':fav = ANY("favourites")', { fav: 'tech' });
|
|
154
|
+
expect(res.length).toEqual(1);
|
|
155
|
+
}));
|
|
156
|
+
it("Upsert - with column names", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
157
|
+
yield table.upsert({ name: 'Fulig Jimmy', textList: ['hiking'] }, { columns: ['name'] });
|
|
158
|
+
let res = yield table.findWhere(':fav = ANY("textList")', { fav: 'hiking' });
|
|
159
|
+
expect(res.length).toEqual(1);
|
|
160
|
+
yield table.upsert({ name: 'Fulig Jimmy', textList: ['biking'] }, { columns: ['name'] });
|
|
161
|
+
res = yield table.findWhere(':fav = ANY("textList")', { fav: 'hiking' });
|
|
162
|
+
expect(res.length).toEqual(0);
|
|
163
|
+
res = yield table.findWhere(':fav = ANY("textList")', { fav: 'biking' });
|
|
164
|
+
expect(res.length).toEqual(1);
|
|
165
|
+
}));
|
|
166
|
+
it("Upsert - with primary key", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
167
|
+
yield table.upsert({ id: 1, name: 'Fulig Jimmy', textList: ['hiking'] });
|
|
168
|
+
let res = yield table.findWhere(':fav = ANY("textList")', { fav: 'hiking' });
|
|
169
|
+
expect(res.length).toEqual(1);
|
|
170
|
+
yield table.upsert({ id: 1, name: 'Fulig Jimmy', textList: ['biking'] });
|
|
171
|
+
res = yield table.findWhere(':fav = ANY("textList")', { fav: 'hiking' });
|
|
172
|
+
expect(res.length).toEqual(0);
|
|
173
|
+
res = yield table.findWhere(':fav = ANY("textList")', { fav: 'biking' });
|
|
174
|
+
expect(res.length).toEqual(1);
|
|
175
|
+
}));
|
|
176
|
+
it("Upsert - with constraint name", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
177
|
+
yield table.upsert({ name: 'Fulig Jimmy', textList: ['hiking'] }, { constraint: "users_name_key" });
|
|
178
|
+
let res = yield table.findWhere(':fav = ANY("textList")', { fav: 'hiking' });
|
|
179
|
+
expect(res.length).toEqual(1);
|
|
180
|
+
yield table.upsert({ name: 'Fulig Jimmy', textList: ['biking'] }, { constraint: "users_name_key" });
|
|
181
|
+
res = yield table.findWhere(':fav = ANY("textList")', { fav: 'hiking' });
|
|
182
|
+
expect(res.length).toEqual(0);
|
|
183
|
+
res = yield table.findWhere(':fav = ANY("textList")', { fav: 'biking' });
|
|
184
|
+
expect(res.length).toEqual(1);
|
|
185
|
+
}));
|
|
186
|
+
it("UpsertAndGet - with constraint name", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
187
|
+
let res = yield table.upsertAndGet({ name: 'Fulig Jimmy', textList: ['hiking'] }, { constraint: "users_name_key" });
|
|
188
|
+
expect(res.textList).toEqual(['hiking']);
|
|
189
|
+
res = yield table.upsertAndGet({ name: 'Fulig Jimmy', textList: ['biking'] }, { constraint: "users_name_key" });
|
|
190
|
+
expect(res.textList).toEqual(['biking']);
|
|
191
|
+
}));
|
|
192
|
+
it("Ignore field with undefined value if requested, but keep with null value", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
193
|
+
yield table.insert({ name: 'A', numberList: [1, 2] });
|
|
194
|
+
let res = yield table.find({ name: undefined }, { skipUndefined: true });
|
|
195
|
+
expect(res.length).toEqual(1);
|
|
196
|
+
res = yield table.find({ name: null }, { skipUndefined: true });
|
|
197
|
+
expect(res.length).toEqual(0);
|
|
198
|
+
let res2 = yield table.updateAndGetOne({ name: 'A' }, {
|
|
199
|
+
numberList: undefined,
|
|
200
|
+
favourites: ['sport']
|
|
201
|
+
}, { skipUndefined: true });
|
|
202
|
+
expect(res2.numberList).toEqual([1, 2]);
|
|
203
|
+
res2 = yield table.updateAndGetOne({
|
|
204
|
+
name: 'A',
|
|
205
|
+
numberList: undefined
|
|
206
|
+
}, { numberList: null }, { skipUndefined: true });
|
|
207
|
+
expect(res2.numberList).toEqual(null);
|
|
208
|
+
}));
|
|
209
|
+
it("Test return only column values ", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
210
|
+
yield table.insert({ name: 'A', membership: 'gold' });
|
|
211
|
+
yield table.insert({ name: 'B', membership: 'gold' });
|
|
212
|
+
yield table.insert({ name: 'C', membership: 'bronze' });
|
|
213
|
+
let res1 = yield table.queryOneColumn("SELECT name || '_' || membership FROM " + table + " WHERE LENGTH(name)=1");
|
|
214
|
+
expect(res1).toEqual(['A_gold', 'B_gold', 'C_bronze']);
|
|
215
|
+
}));
|
|
216
|
+
it("Test count ", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
217
|
+
yield table.insert({ name: 'A', membership: 'gold' });
|
|
218
|
+
yield table.insert({ name: 'B', membership: 'gold' });
|
|
219
|
+
yield table.insert({ name: 'C', membership: 'bronze' });
|
|
220
|
+
let res1 = yield table.count({ membership: 'gold' });
|
|
221
|
+
expect(res1).toEqual(2);
|
|
222
|
+
}));
|
|
223
|
+
it("Test AND - OR ", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
224
|
+
yield table.insert({ name: 'A', membership: 'gold', favourites: ['sport'] });
|
|
225
|
+
yield table.insert([
|
|
226
|
+
{ name: 'BC', membership: 'gold', favourites: ['sport'] },
|
|
227
|
+
{ name: 'D', membership: 'bronze', favourites: ['tech'] },
|
|
228
|
+
{ name: 'E', membership: 'bronze', favourites: ['tech', 'food'] }
|
|
229
|
+
]);
|
|
230
|
+
let res;
|
|
231
|
+
res = yield table.find({ 'membership': "bronze", or: [{ 'name': 'BC' }, { favourites: 'food', name: 'E' }] });
|
|
232
|
+
expect(res.length).toEqual(1);
|
|
233
|
+
res = yield table.find({ name: 'A' });
|
|
234
|
+
res = yield table.count({
|
|
235
|
+
and: [
|
|
236
|
+
{ or: [{ name: ['A', 'BC'] }, { 'updated >': res[0].updated }] },
|
|
237
|
+
{ or: [{ membership: 'bronze' }, { 'favourites @>': ['food'] }] }
|
|
238
|
+
]
|
|
239
|
+
});
|
|
240
|
+
expect(res).toEqual(2);
|
|
241
|
+
}));
|
|
242
|
+
it("Test insert with switched fields", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
243
|
+
yield table.insert([{ name: 'A', membership: 'gold' }, { membership: 'gold', name: 'B' }]);
|
|
244
|
+
let res = yield pgdb.query("SELECT count(*) FROM :!schema.:!table WHERE membership = :membership", {
|
|
245
|
+
schema: schema,
|
|
246
|
+
table: 'users',
|
|
247
|
+
membership: 'gold'
|
|
248
|
+
});
|
|
249
|
+
expect(res[0].count).toEqual(2);
|
|
250
|
+
}));
|
|
251
|
+
it("Test named parameters ", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
252
|
+
yield table.insert({ name: 'A', membership: 'gold' });
|
|
253
|
+
yield table.insert({ name: 'B', membership: 'gold' });
|
|
254
|
+
yield table.insert({ name: 'C', membership: 'bronze' });
|
|
255
|
+
let res = yield pgdb.query("SELECT count(*) FROM :!schema.:!table WHERE membership = :membership", {
|
|
256
|
+
schema: schema,
|
|
257
|
+
table: 'users',
|
|
258
|
+
membership: 'gold'
|
|
259
|
+
});
|
|
260
|
+
expect(res[0].count).toEqual(2);
|
|
261
|
+
}));
|
|
262
|
+
it("text[]", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
263
|
+
yield table.insert({ name: 'A', textList: ['good', 'better', 'best'] });
|
|
264
|
+
let res = yield table.findOne({ name: 'A' });
|
|
265
|
+
expect(res.textList).toEqual(['good', 'better', 'best']);
|
|
266
|
+
}));
|
|
267
|
+
it("integer[]", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
268
|
+
yield table.insert({ name: 'A', numberList: [1, 2, 3] });
|
|
269
|
+
let res = yield table.findOne({ name: 'A' });
|
|
270
|
+
expect(res.numberList).toEqual([1, 2, 3]);
|
|
271
|
+
}));
|
|
272
|
+
it("integer[]", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
273
|
+
yield table.insert({ name: 'A', numberList: [null, 1, 2, 3] });
|
|
274
|
+
let res = yield table.findOne({ name: 'A' });
|
|
275
|
+
expect(res.numberList).toEqual([null, 1, 2, 3]);
|
|
276
|
+
}));
|
|
277
|
+
it("bigInt[]", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
278
|
+
yield table.insert({ name: 'A', bigNumberList: [1, 2, 3] });
|
|
279
|
+
let res = yield table.findOne({ name: 'A' });
|
|
280
|
+
expect(res.bigNumberList).toEqual([1, 2, 3]);
|
|
281
|
+
yield table.insert({ name: 'B', bigNumberList: [1, Number.MAX_SAFE_INTEGER + 10] });
|
|
282
|
+
try {
|
|
283
|
+
yield table.findOne({ name: 'B' });
|
|
284
|
+
expect(false).toBeTruthy();
|
|
285
|
+
}
|
|
286
|
+
catch (e) {
|
|
287
|
+
expect(/Number can't be represented in javascript/.test(e.message)).toBeTruthy();
|
|
288
|
+
}
|
|
289
|
+
}));
|
|
290
|
+
it("bigInt[] cursor callback", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
291
|
+
yield table.insert({ name: 'A', bigNumberList: [1, 2, 3] });
|
|
292
|
+
let res;
|
|
293
|
+
yield table.queryWithOnCursorCallback(`SELECT * FROM ${table}`, null, null, (rec) => {
|
|
294
|
+
res = rec.bigNumberList;
|
|
295
|
+
});
|
|
296
|
+
expect(res).toEqual([1, 2, 3]);
|
|
297
|
+
yield table.insert({ name: 'B', bigNumberList: [1, Number.MAX_SAFE_INTEGER + 10] });
|
|
298
|
+
try {
|
|
299
|
+
yield table.queryWithOnCursorCallback(`SELECT * FROM ${table}`, null, null, () => {
|
|
300
|
+
});
|
|
301
|
+
expect(false).toBeTruthy();
|
|
302
|
+
}
|
|
303
|
+
catch (e) {
|
|
304
|
+
expect(/Number can't be represented in javascript/.test(e.message)).toBeTruthy();
|
|
305
|
+
}
|
|
306
|
+
}));
|
|
307
|
+
it("timestamptz[]", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
308
|
+
yield table.insert({
|
|
309
|
+
name: 'A',
|
|
310
|
+
timestamptzList: [new Date('2000-01-01 00:00:00').toISOString(), new Date('2001-01-01 00:00:00').toISOString()]
|
|
311
|
+
});
|
|
312
|
+
let res = yield table.findOne({ name: 'A' });
|
|
313
|
+
expect(res.timestamptzList[0]).toEqual(new Date('2000-01-01 00:00:00'));
|
|
314
|
+
expect(res.timestamptzList[1]).toEqual(new Date('2001-01-01 00:00:00'));
|
|
315
|
+
expect(res.timestamptzList.length).toEqual(2);
|
|
316
|
+
}));
|
|
317
|
+
it("timestamp and timestamptz", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
318
|
+
yield table.insert({
|
|
319
|
+
name: 'A',
|
|
320
|
+
created: new Date('2000-01-01 00:00:00'),
|
|
321
|
+
createdtz: new Date('2000-01-01 00:00:00')
|
|
322
|
+
});
|
|
323
|
+
let res = yield table.findOne({ name: 'A' });
|
|
324
|
+
expect(res.created).toEqual(new Date('2000-01-01 00:00:00'));
|
|
325
|
+
expect(res.createdtz).toEqual(new Date('2000-01-01 00:00:00'));
|
|
326
|
+
res = yield table.count({ 'created': new Date('2000-01-01 00:00:00') });
|
|
327
|
+
expect(res).toEqual(1);
|
|
328
|
+
res = yield table.count({ 'createdtz': new Date('2000-01-01 00:00:00') });
|
|
329
|
+
expect(res).toEqual(1);
|
|
330
|
+
let d = new Date('2000-01-01 00:00:00').toISOString();
|
|
331
|
+
yield table.query(`INSERT INTO ${table} (name, created, createdtz) values ('A2', '${d}'::timestamptz, '${d}'::timestamptz)`);
|
|
332
|
+
res = yield table.findOne({ name: 'A2' });
|
|
333
|
+
expect(res.createdtz).toEqual(new Date('2000-01-01 00:00:00'));
|
|
334
|
+
res = yield table.query(`SELECT * FROM ${table} WHERE name='A2' AND created='${d}'::timestamptz`);
|
|
335
|
+
expect(res.length).toEqual(1);
|
|
336
|
+
res = yield table.query(`SELECT * FROM ${table} WHERE name='A2' AND createdtz='${d}'::timestamptz`);
|
|
337
|
+
expect(res.length).toEqual(1);
|
|
338
|
+
}));
|
|
339
|
+
it("transaction - rollback", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
340
|
+
yield table.insert({ name: 'A' });
|
|
341
|
+
let res;
|
|
342
|
+
let pgdbwt = yield pgdb.transactionBegin();
|
|
343
|
+
let tablewt = pgdbwt.schemas[schema]['users'];
|
|
344
|
+
yield tablewt.insert({ name: 'B' });
|
|
345
|
+
res = yield table.count();
|
|
346
|
+
expect(res).toEqual(1);
|
|
347
|
+
res = yield tablewt.count();
|
|
348
|
+
expect(res).toEqual(2);
|
|
349
|
+
yield pgdbwt.transactionRollback();
|
|
350
|
+
res = yield table.findAll();
|
|
351
|
+
expect(res.length).toEqual(1);
|
|
352
|
+
expect(res[0].name).toEqual('A');
|
|
353
|
+
}));
|
|
354
|
+
it("transaction - savepoint - rollback", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
355
|
+
let tr = yield pgdb.transactionBegin();
|
|
356
|
+
let table = tr.schemas[schema]['users'];
|
|
357
|
+
yield table.insert({ name: 'a' });
|
|
358
|
+
let res = yield table.count();
|
|
359
|
+
expect(res).toEqual(1);
|
|
360
|
+
tr.savePoint('name');
|
|
361
|
+
yield table.insert({ name: 'b' });
|
|
362
|
+
res = yield table.count();
|
|
363
|
+
expect(res).toEqual(2);
|
|
364
|
+
yield tr.transactionRollback({ savePoint: 'name' });
|
|
365
|
+
res = yield table.count();
|
|
366
|
+
expect(res).toEqual(1);
|
|
367
|
+
yield tr.transactionRollback();
|
|
368
|
+
res = yield table.count();
|
|
369
|
+
expect(res).toEqual(0);
|
|
370
|
+
}));
|
|
371
|
+
it("transaction should keep the table definitions", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
372
|
+
const pgDB = pgdb;
|
|
373
|
+
const pgDBTrans = yield pgDB.transactionBegin();
|
|
374
|
+
let list1 = Object.keys(pgDB.tables);
|
|
375
|
+
let list2 = Object.keys(pgDBTrans.tables);
|
|
376
|
+
yield pgDBTrans.transactionCommit();
|
|
377
|
+
expect(list1.length).toEqual(list2.length);
|
|
378
|
+
expect(list1.length > 0).toBeTruthy();
|
|
379
|
+
}));
|
|
380
|
+
it("transaction - commit", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
381
|
+
yield table.insert({ name: 'A' });
|
|
382
|
+
let pgdbwt = yield pgdb.transactionBegin();
|
|
383
|
+
let tablewt = pgdbwt.schemas[schema]['users'];
|
|
384
|
+
yield tablewt.insert({ name: 'B' });
|
|
385
|
+
let res;
|
|
386
|
+
res = yield table.findAll();
|
|
387
|
+
expect(res.length).toEqual(1);
|
|
388
|
+
expect(res[0].name).toEqual('A');
|
|
389
|
+
res = yield tablewt.count();
|
|
390
|
+
expect(res).toEqual(2);
|
|
391
|
+
yield pgdbwt.transactionCommit();
|
|
392
|
+
res = yield table.findAll();
|
|
393
|
+
expect(res.length).toEqual(2);
|
|
394
|
+
expect(res[0].name).toEqual('A');
|
|
395
|
+
expect(res[1].name).toEqual('B');
|
|
396
|
+
}));
|
|
397
|
+
it("transaction - error + rollback", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
398
|
+
let pgdbwt = yield pgdb.transactionBegin();
|
|
399
|
+
let tablewt = pgdbwt[schema]['users'];
|
|
400
|
+
yield tablewt.insert({ name: 'A' });
|
|
401
|
+
try {
|
|
402
|
+
yield tablewt.insertAndGet({ name: 'C', bigNumberList: [1, 2, Number.MAX_SAFE_INTEGER + 100] });
|
|
403
|
+
expect(false).toBeTruthy();
|
|
404
|
+
}
|
|
405
|
+
catch (e) {
|
|
406
|
+
expect(/Number can't be represented in javascript/.test(e.message)).toBeTruthy();
|
|
407
|
+
yield pgdbwt.transactionRollback();
|
|
408
|
+
}
|
|
409
|
+
let res = yield table.count();
|
|
410
|
+
expect(res).toEqual(0);
|
|
411
|
+
}));
|
|
412
|
+
it("cursor with callback", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
413
|
+
yield table.insert({ name: 'A', numberList: [1, 2, 3] });
|
|
414
|
+
yield table.insert({ name: 'B' });
|
|
415
|
+
let size = yield table.count();
|
|
416
|
+
let streamSize = 0;
|
|
417
|
+
yield table.queryWithOnCursorCallback(`SELECT * FROM ${table}`, null, null, (r) => {
|
|
418
|
+
streamSize++;
|
|
419
|
+
return true;
|
|
420
|
+
});
|
|
421
|
+
expect(streamSize).toBe(1);
|
|
422
|
+
}));
|
|
423
|
+
it("stream - auto connection handling - normal", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
424
|
+
let counter = 0;
|
|
425
|
+
let stream = yield table.queryAsStream(`SELECT * FROM generate_series(0, $1) num`, [1001]);
|
|
426
|
+
stream.on('data', (c) => {
|
|
427
|
+
if (c.num != counter) {
|
|
428
|
+
expect(false).toBeTruthy();
|
|
429
|
+
}
|
|
430
|
+
counter++;
|
|
431
|
+
});
|
|
432
|
+
yield new Promise(resolve => {
|
|
433
|
+
stream.on('end', resolve);
|
|
434
|
+
stream.on('error', resolve);
|
|
435
|
+
});
|
|
436
|
+
expect(counter).toEqual(1001 + 1);
|
|
437
|
+
}));
|
|
438
|
+
it("stream - auto connection handling - early close", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
439
|
+
let counter = 0;
|
|
440
|
+
let stream = yield table.queryAsStream(`SELECT * FROM generate_series(0,1002) num`);
|
|
441
|
+
yield new Promise((resolve, reject) => {
|
|
442
|
+
stream.on('end', resolve);
|
|
443
|
+
stream.on('error', reject);
|
|
444
|
+
stream.on('data', (c) => {
|
|
445
|
+
if (counter == 10) {
|
|
446
|
+
stream.emit('close', 'e');
|
|
447
|
+
return resolve(undefined);
|
|
448
|
+
}
|
|
449
|
+
counter++;
|
|
450
|
+
});
|
|
451
|
+
});
|
|
452
|
+
expect(counter).toEqual(10);
|
|
453
|
+
}));
|
|
454
|
+
it("stream - auto connection handling - error", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
455
|
+
let counter = 0;
|
|
456
|
+
let stillSafe = Number.MAX_SAFE_INTEGER - 5;
|
|
457
|
+
let wrongNum = Number.MAX_SAFE_INTEGER + 100;
|
|
458
|
+
let stream = yield table.queryAsStream(`SELECT * FROM generate_series(${stillSafe}, ${wrongNum}) num`);
|
|
459
|
+
stream.on('data', (c) => {
|
|
460
|
+
counter++;
|
|
461
|
+
});
|
|
462
|
+
yield new Promise(resolve => {
|
|
463
|
+
stream.on('end', resolve);
|
|
464
|
+
stream.on('error', resolve);
|
|
465
|
+
});
|
|
466
|
+
expect(counter).toEqual(6);
|
|
467
|
+
}));
|
|
468
|
+
it("stream - with transactions handling - normal", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
469
|
+
let pgdbwt = yield pgdb.transactionBegin();
|
|
470
|
+
let tablewt = pgdbwt[schema]['users'];
|
|
471
|
+
yield tablewt.insert({ name: 'A', numberList: [1, 2, 3] });
|
|
472
|
+
yield tablewt.insert({ name: 'B' });
|
|
473
|
+
let counter = 0;
|
|
474
|
+
let stream = yield tablewt.queryAsStream(`SELECT * FROM ${tablewt}`);
|
|
475
|
+
stream.on('data', (c) => counter++);
|
|
476
|
+
yield new Promise(resolve => {
|
|
477
|
+
stream.on('end', resolve);
|
|
478
|
+
stream.on('error', resolve);
|
|
479
|
+
});
|
|
480
|
+
expect(counter).toEqual(2);
|
|
481
|
+
counter = yield tablewt.count();
|
|
482
|
+
expect(counter).toEqual(2);
|
|
483
|
+
yield pgdbwt.transactionRollback();
|
|
484
|
+
counter = yield table.count();
|
|
485
|
+
expect(counter).toEqual(0);
|
|
486
|
+
}));
|
|
487
|
+
it("stream - with transactions handling - early close", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
488
|
+
let pgdbwt = yield pgdb.transactionBegin();
|
|
489
|
+
let tablewt = pgdbwt[schema]['users'];
|
|
490
|
+
yield tablewt.insert({ name: 'A', numberList: [1, 2, 3] });
|
|
491
|
+
yield tablewt.insert({ name: 'B' });
|
|
492
|
+
yield tablewt.insert({ name: 'C' });
|
|
493
|
+
yield tablewt.insert({ name: 'D' });
|
|
494
|
+
let counter = 0;
|
|
495
|
+
let stream = yield tablewt.queryAsStream(`SELECT * FROM ${tablewt}`);
|
|
496
|
+
yield new Promise((resolve, reject) => {
|
|
497
|
+
stream.on('end', resolve);
|
|
498
|
+
stream.on('error', reject);
|
|
499
|
+
stream.on('data', (c) => {
|
|
500
|
+
if (counter == 2) {
|
|
501
|
+
stream.emit('close', 'e');
|
|
502
|
+
return resolve(undefined);
|
|
503
|
+
}
|
|
504
|
+
counter++;
|
|
505
|
+
});
|
|
506
|
+
});
|
|
507
|
+
expect(counter).toEqual(2);
|
|
508
|
+
counter = yield tablewt.count();
|
|
509
|
+
expect(counter).toEqual(4);
|
|
510
|
+
yield pgdbwt.transactionRollback();
|
|
511
|
+
counter = yield table.count();
|
|
512
|
+
expect(counter).toEqual(0);
|
|
513
|
+
}));
|
|
514
|
+
it("stream - with transactions handling - error", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
515
|
+
let pgdbwt = yield pgdb.transactionBegin();
|
|
516
|
+
let tablewt = pgdbwt[schema]['users'];
|
|
517
|
+
yield tablewt.insert({ name: 'A', bigNumberList: [1, 2, 3] });
|
|
518
|
+
yield tablewt.insert({ name: 'B' });
|
|
519
|
+
yield tablewt.insert({ name: 'C', bigNumberList: [1, 2, Number.MAX_SAFE_INTEGER + 100] });
|
|
520
|
+
yield tablewt.insert({ name: 'D' });
|
|
521
|
+
let counter = 0;
|
|
522
|
+
let stream = yield tablewt.queryAsStream(`SELECT * FROM ${tablewt}`);
|
|
523
|
+
try {
|
|
524
|
+
stream.on('data', (c) => {
|
|
525
|
+
counter++;
|
|
526
|
+
});
|
|
527
|
+
yield new Promise((resolve, reject) => {
|
|
528
|
+
stream.on('end', resolve);
|
|
529
|
+
stream.on('error', reject);
|
|
530
|
+
});
|
|
531
|
+
expect(false).toBeTruthy();
|
|
532
|
+
}
|
|
533
|
+
catch (e) {
|
|
534
|
+
expect(/Number can't be represented in javascript/.test(e.message)).toBeTruthy();
|
|
535
|
+
}
|
|
536
|
+
expect(counter).toEqual(2);
|
|
537
|
+
counter = yield tablewt.count();
|
|
538
|
+
expect(counter).toEqual(4);
|
|
539
|
+
yield pgdbwt.transactionRollback();
|
|
540
|
+
counter = yield table.count();
|
|
541
|
+
expect(counter).toEqual(0);
|
|
542
|
+
}));
|
|
543
|
+
it("truncate", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
544
|
+
yield table.insert({ name: 'A' });
|
|
545
|
+
yield table.insert({ name: 'B' });
|
|
546
|
+
yield table.truncate();
|
|
547
|
+
let size = yield table.count();
|
|
548
|
+
expect(size).toEqual(0);
|
|
549
|
+
}));
|
|
550
|
+
it("truncate + special types", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
551
|
+
yield pgdb.setTypeParser('permissionForResourceType', (val) => parseComplexType(val));
|
|
552
|
+
yield pgdb.setTypeParser('_permissionForResourceType', (val) => val == "{}" ? [] : parseComplexTypeArray(val));
|
|
553
|
+
yield table.insert({
|
|
554
|
+
name: 'Piszkos Fred',
|
|
555
|
+
permission: "(read,book)",
|
|
556
|
+
permissionList: [
|
|
557
|
+
'(admin,"chaos j ()"",""""j ,")',
|
|
558
|
+
'(write,book)',
|
|
559
|
+
'(write,)',
|
|
560
|
+
'(,"")',
|
|
561
|
+
'(,)',
|
|
562
|
+
'(write,null)'
|
|
563
|
+
],
|
|
564
|
+
});
|
|
565
|
+
yield table.truncate();
|
|
566
|
+
yield table.insert({
|
|
567
|
+
name: 'Piszkos Fred',
|
|
568
|
+
permission: "(read,book)",
|
|
569
|
+
permissionList: [
|
|
570
|
+
'(admin,"chaos j ()"",""""j ,")',
|
|
571
|
+
'(write,book)',
|
|
572
|
+
'(write,)',
|
|
573
|
+
'(,"")',
|
|
574
|
+
'(,)',
|
|
575
|
+
'(write,null)'
|
|
576
|
+
],
|
|
577
|
+
});
|
|
578
|
+
let pf = yield table.findOne({ name: 'Piszkos Fred' });
|
|
579
|
+
expect(pf.permission).toEqual(['read', 'book']);
|
|
580
|
+
expect(pf.permissionList[0]).toEqual(['admin', 'chaos j ()",""j ,']);
|
|
581
|
+
}));
|
|
582
|
+
it("truncate - cascade", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
583
|
+
let g = yield tableGroups.insertAndGet({ name: 'G' });
|
|
584
|
+
yield table.insert({ name: 'A', mainGroup: g.id });
|
|
585
|
+
yield table.insert({ name: 'B', mainGroup: g.id });
|
|
586
|
+
yield tableGroups.truncate({ cascade: true, restartIdentity: true });
|
|
587
|
+
let size = yield table.count();
|
|
588
|
+
expect(size).toEqual(0);
|
|
589
|
+
let g2 = yield tableGroups.insertAndGet({ name: 'G' }, { return: ['id'] });
|
|
590
|
+
expect(g.id >= g2.id).toBeTruthy();
|
|
591
|
+
}));
|
|
592
|
+
it("orderBy", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
593
|
+
yield table.insert({ name: 'A', aCategory: 'A' });
|
|
594
|
+
yield table.insert({ name: 'B', aCategory: 'B' });
|
|
595
|
+
yield table.insert({ name: 'C', aCategory: 'C' });
|
|
596
|
+
yield table.insert({ name: 'A2', aCategory: 'A' });
|
|
597
|
+
yield table.insert({ name: 'B2', aCategory: 'B' });
|
|
598
|
+
yield table.insert({ name: 'C2', aCategory: 'C' });
|
|
599
|
+
let res;
|
|
600
|
+
res = yield table.find({}, { orderBy: ['aCategory', 'name'], fields: ['name'] });
|
|
601
|
+
expect(res.map(v => v.name)).toEqual(['A', 'A2', 'B', 'B2', 'C', 'C2']);
|
|
602
|
+
res = yield table.find({}, { orderBy: ['aCategory asc', 'name desc'], fields: ['name'] });
|
|
603
|
+
expect(res.map(v => v.name)).toEqual(['A2', 'A', 'B2', 'B', 'C2', 'C']);
|
|
604
|
+
res = yield table.find({}, { orderBy: ['+aCategory', '-name'], fields: ['name'] });
|
|
605
|
+
expect(res.map(v => v.name)).toEqual(['A2', 'A', 'B2', 'B', 'C2', 'C']);
|
|
606
|
+
res = yield table.find({}, { orderBy: '"aCategory" asc, name desc', fields: ['name'] });
|
|
607
|
+
expect(res.map(v => v.name)).toEqual(['A2', 'A', 'B2', 'B', 'C2', 'C']);
|
|
608
|
+
}));
|
|
609
|
+
it("stored proc", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
610
|
+
yield table.insert({ name: 'A', membership: 'gold' });
|
|
611
|
+
yield table.insert({ name: 'B', membership: 'gold' });
|
|
612
|
+
expect(pgdb[schema].fn['list_gold_users']).toBeDefined();
|
|
613
|
+
expect(pgdb.fn['increment']).toBeDefined();
|
|
614
|
+
let s = yield pgdb.run('select current_schema');
|
|
615
|
+
console.log(s);
|
|
616
|
+
let res = yield pgdb[schema].fn['list_gold_users']();
|
|
617
|
+
console.log(res);
|
|
618
|
+
expect(res).toEqual(['A', 'B']);
|
|
619
|
+
res = yield pgdb.fn['increment'](3);
|
|
620
|
+
console.log(res);
|
|
621
|
+
expect(res).toEqual(4);
|
|
622
|
+
}));
|
|
623
|
+
it("executing sql file - if there is an exception, should be thrown", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
624
|
+
try {
|
|
625
|
+
yield pgdb.execute('src/test/throw_exception.sql', (cmd) => cmd.replace(/__SCHEMA__/g, '"' + schema + '"'));
|
|
626
|
+
expect(false).toBeTruthy();
|
|
627
|
+
}
|
|
628
|
+
catch (e) {
|
|
629
|
+
expect('' + e).toEqual("error: division_by_zero");
|
|
630
|
+
}
|
|
631
|
+
}));
|
|
632
|
+
it("select/update/delete should throw exception if the condition contains undefined value", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
633
|
+
yield table.insert({ name: 'A', membership: 'gold' });
|
|
634
|
+
let conditions = { name: 'A', membership: undefined };
|
|
635
|
+
try {
|
|
636
|
+
yield table.find(conditions);
|
|
637
|
+
expect(false).toBeTruthy();
|
|
638
|
+
}
|
|
639
|
+
catch (e) {
|
|
640
|
+
expect('' + e).toEqual('Error: Invalid conditions! Field value undefined: "membership". Either delete the field, set it to null or use the options.skipUndefined parameter.');
|
|
641
|
+
}
|
|
642
|
+
let res = yield table.find(conditions, { skipUndefined: true });
|
|
643
|
+
expect(res.length == 1).toBeTruthy();
|
|
644
|
+
try {
|
|
645
|
+
yield table.update(conditions, { name: 'B' });
|
|
646
|
+
expect(false).toBeTruthy();
|
|
647
|
+
}
|
|
648
|
+
catch (e) {
|
|
649
|
+
expect('' + e).toEqual('Error: Invalid conditions! Field value undefined: "membership". Either delete the field, set it to null or use the options.skipUndefined parameter.');
|
|
650
|
+
}
|
|
651
|
+
yield table.update(conditions, { name: 'B' }, { skipUndefined: true });
|
|
652
|
+
res = yield table.find(conditions, { skipUndefined: true });
|
|
653
|
+
expect(res.length == 0).toBeTruthy();
|
|
654
|
+
try {
|
|
655
|
+
conditions.name = 'B';
|
|
656
|
+
yield table.delete(conditions);
|
|
657
|
+
expect(false).toBeTruthy();
|
|
658
|
+
}
|
|
659
|
+
catch (e) {
|
|
660
|
+
expect('' + e).toEqual('Error: Invalid conditions! Field value undefined: "membership". Either delete the field, set it to null or use the options.skipUndefined parameter.');
|
|
661
|
+
}
|
|
662
|
+
yield table.delete(conditions, { skipUndefined: true });
|
|
663
|
+
res = yield table.findAll();
|
|
664
|
+
expect(res.length == 0).toBeTruthy();
|
|
665
|
+
}));
|
|
666
|
+
it("Testing deleteAndGet ", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
667
|
+
yield table.insert({ name: 'A' });
|
|
668
|
+
let res = yield table.deleteAndGetOne({ name: 'A' });
|
|
669
|
+
expect(res != null).toBeTruthy();
|
|
670
|
+
expect(res.name == 'A').toBeTruthy();
|
|
671
|
+
let res2 = yield table.deleteAndGetOne({ name: 'A' });
|
|
672
|
+
expect(res2 == null).toBeTruthy();
|
|
673
|
+
}));
|
|
674
|
+
it("Testing postprocess function", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
675
|
+
yield table.insert({ name: 'A' });
|
|
676
|
+
pgdb.setPostProcessResult((res, fields, logger) => {
|
|
677
|
+
res[0].name = 'B';
|
|
678
|
+
});
|
|
679
|
+
let res = yield pgdb.query(`select * from ${table}`);
|
|
680
|
+
expect(res[0].name == 'B').toBeTruthy();
|
|
681
|
+
res = yield table.findAll();
|
|
682
|
+
expect(res[0].name == 'B').toBeTruthy();
|
|
683
|
+
pgdb.setPostProcessResult(null);
|
|
684
|
+
res = yield table.findAll();
|
|
685
|
+
expect(res[0].name == 'A').toBeTruthy();
|
|
686
|
+
}));
|
|
687
|
+
it("Testing deleteAndGet", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
688
|
+
yield table.insert([{ name: 'A' }, { name: 'B' }]);
|
|
689
|
+
let res = yield table.deleteAndGet({ name: ['A', 'B'] });
|
|
690
|
+
expect(res.length == 2).toBeTruthy();
|
|
691
|
+
}));
|
|
692
|
+
it("Testing sql execution", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
693
|
+
yield pgdb.execute('src/test/tricky.sql', (cmd) => cmd.replace(/__SCHEMA__/g, '"' + schema + '"'));
|
|
694
|
+
}));
|
|
695
|
+
it("Testing text array parsing", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
696
|
+
let list = ["'A'", '"A"', 'normal', '//', '\\', '\\\\\\"', '\\\\"', '\\"', '""', "''", '--', '/*', '', '<!--', JSON.stringify({
|
|
697
|
+
a: 1,
|
|
698
|
+
b: "aprocska\"kalapocska'bennecsacskamacskamocska"
|
|
699
|
+
})];
|
|
700
|
+
yield table.insert({ name: 'A', textList: list });
|
|
701
|
+
let rec = yield table.findOne({ name: 'A' });
|
|
702
|
+
console.log(list + '\n' + rec.textList);
|
|
703
|
+
let isDifferent = list.some((v, i) => rec.textList[i] !== v);
|
|
704
|
+
expect(isDifferent).toBeFalsy();
|
|
705
|
+
}));
|
|
706
|
+
it("Testing jsonb array parsing", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
707
|
+
let list = [{
|
|
708
|
+
"id": "fl1ace84f744",
|
|
709
|
+
"name": "Wire 2017-09-17 at 11.57.55.png",
|
|
710
|
+
"type": "image/png",
|
|
711
|
+
"path": "/data/files/fl1ace84f744/Wire 2017-09-17 at 11.57.55.png",
|
|
712
|
+
"title": "Wire 2017-09-17 at 11.57.55"
|
|
713
|
+
}];
|
|
714
|
+
yield table.insert({ name: 'A', jsonbList: list });
|
|
715
|
+
let rec = yield table.findOne({ name: 'A' });
|
|
716
|
+
console.log('xxx', rec.jsonbList, typeof rec.jsonbList[0]);
|
|
717
|
+
console.log(JSON.stringify(list) + '\n' + JSON.stringify(rec.jsonbList));
|
|
718
|
+
let isDifferent = list.some((v, i) => !_.isEqual(rec.jsonbList[i], v));
|
|
719
|
+
expect(isDifferent).toBeFalsy();
|
|
720
|
+
}));
|
|
721
|
+
it("Testing distinct", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
722
|
+
yield table.insert({ name: 'A', aCategory: 'A' });
|
|
723
|
+
yield table.insert({ name: 'B', aCategory: 'A' });
|
|
724
|
+
let recs = yield table.find({ aCategory: 'A' }, { fields: ['aCategory'], distinct: true });
|
|
725
|
+
expect(recs.length).toEqual(1);
|
|
726
|
+
}));
|
|
727
|
+
it("Testing queryOne", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
728
|
+
yield table.insert({ name: 'A', aCategory: 'A' });
|
|
729
|
+
yield table.insert({ name: 'B', aCategory: 'A' });
|
|
730
|
+
try {
|
|
731
|
+
let recs = yield table.queryOne(`SELECT * FROM ${table} WHERE "aCategory" = 'A'`);
|
|
732
|
+
expect(false).toBeTruthy();
|
|
733
|
+
}
|
|
734
|
+
catch (e) {
|
|
735
|
+
expect('' + e).toEqual("Error: More then one rows exists");
|
|
736
|
+
}
|
|
737
|
+
}));
|
|
738
|
+
it("Testing queryFirst", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
739
|
+
yield table.insert({ name: 'A', aCategory: 'A' });
|
|
740
|
+
yield table.insert({ name: 'B', aCategory: 'A' });
|
|
741
|
+
let rec = yield table.queryFirst(`SELECT * FROM ${table} WHERE "aCategory" = 'A'`);
|
|
742
|
+
expect(rec.aCategory).toEqual('A');
|
|
743
|
+
}));
|
|
744
|
+
it("Testing forUpdate", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
745
|
+
yield table.insert({ name: 'A', aCategory: 'A' });
|
|
746
|
+
let pgdb1 = yield table.db.transactionBegin();
|
|
747
|
+
yield pgdb1.tables['users'].find({ aCategory: 'A' }, { forUpdate: true });
|
|
748
|
+
let p = table.update({ aCategory: 'A' }, { name: 'C' });
|
|
749
|
+
yield new Promise(resolve => setTimeout(resolve, 500));
|
|
750
|
+
yield pgdb1.tables['users'].update({ aCategory: 'A' }, { name: 'B' });
|
|
751
|
+
yield pgdb1.transactionCommit();
|
|
752
|
+
yield p;
|
|
753
|
+
let rec = yield table.findFirst({ aCategory: 'A' });
|
|
754
|
+
expect(rec.name).toEqual('C');
|
|
755
|
+
}));
|
|
756
|
+
it("Testing update where something is null", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
757
|
+
yield table.insert({ name: 'A', aCategory: 'A' });
|
|
758
|
+
yield table.update({ textList: null }, { name: 'B' });
|
|
759
|
+
let rec = yield table.findFirst({ aCategory: 'A' });
|
|
760
|
+
expect(rec.name).toEqual('B');
|
|
761
|
+
}));
|
|
762
|
+
it("Testing query with array equals", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
763
|
+
yield table.insert({ name: 'A', aCategory: 'A' });
|
|
764
|
+
yield table.insert({ name: 'B', aCategory: 'A' });
|
|
765
|
+
yield table.insert({ name: 'C', aCategory: 'A' });
|
|
766
|
+
let recs = yield table.find({ name: ['A', 'B'] });
|
|
767
|
+
expect(recs.length).toEqual(2);
|
|
768
|
+
}));
|
|
769
|
+
it("Testing query with array not equals", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
770
|
+
yield table.insert({ name: 'A', aCategory: 'A' });
|
|
771
|
+
yield table.insert({ name: 'B', aCategory: 'A' });
|
|
772
|
+
yield table.insert({ name: 'C', aCategory: 'A' });
|
|
773
|
+
let recs = yield table.find({ '"name" !=': ['A', 'B'] });
|
|
774
|
+
expect(recs.length).toEqual(1);
|
|
775
|
+
}));
|
|
776
|
+
it("Testing empty result with one field", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
777
|
+
let recs = yield table.queryOneColumn(`select name from ${table}`);
|
|
778
|
+
expect(recs.length).toEqual(0);
|
|
779
|
+
}));
|
|
780
|
+
it("Testing empty result with one column", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
781
|
+
let rec = yield table.queryOneField(`select name from ${table}`);
|
|
782
|
+
expect(rec).toEqual(null);
|
|
783
|
+
}));
|
|
784
|
+
it("Testing NOTIFY and LISTEN (listen)", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
785
|
+
let called = null;
|
|
786
|
+
let payload = 'hello';
|
|
787
|
+
yield table.db.listen('test_channel', (notification) => called = notification.payload);
|
|
788
|
+
yield table.db.notify('test_channel', payload);
|
|
789
|
+
yield new Promise((resolve) => setTimeout(resolve, 1000));
|
|
790
|
+
expect(called).toEqual(payload);
|
|
791
|
+
}));
|
|
792
|
+
it("Testing NOTIFY and LISTEN (unlisten)", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
793
|
+
let called = null;
|
|
794
|
+
let payload = 'hello';
|
|
795
|
+
yield table.db.listen('test_channel', (notification) => called = notification.payload);
|
|
796
|
+
yield table.db.unlisten('test_channel');
|
|
797
|
+
yield table.db.notify('test_channel', payload);
|
|
798
|
+
yield new Promise((resolve) => setTimeout(resolve, 1000));
|
|
799
|
+
expect(called).toEqual(null);
|
|
800
|
+
}));
|
|
801
|
+
it("Testing Empty 'or' condition", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
802
|
+
yield table.insert({ name: 'A', aCategory: 'A' });
|
|
803
|
+
let recs = yield table.find({ or: [] });
|
|
804
|
+
expect(recs.length).toEqual(1);
|
|
805
|
+
}));
|
|
806
|
+
it("Testing Empty 'and' condition", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
807
|
+
yield table.insert({ name: 'A', aCategory: 'A' });
|
|
808
|
+
let recs = yield table.find({ and: [] });
|
|
809
|
+
expect(recs.length).toEqual(1);
|
|
810
|
+
}));
|
|
811
|
+
it("Testing row mode", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
812
|
+
yield table.insert({ name: 'A', aCategory: 'A' });
|
|
813
|
+
let result = yield table.queryAsRows(`select * from ${table}`);
|
|
814
|
+
expect(result.rows.length).toEqual(1);
|
|
815
|
+
expect(result.columns.includes('id'));
|
|
816
|
+
expect(result.columns.includes('name'));
|
|
817
|
+
expect(result.columns.includes('aCategory'));
|
|
818
|
+
}));
|
|
819
|
+
it("Testing output formats of default types", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
820
|
+
yield tableTypes.insert({
|
|
821
|
+
text: 'apple',
|
|
822
|
+
int: 23,
|
|
823
|
+
bigInt: 233,
|
|
824
|
+
real: 0.5,
|
|
825
|
+
double: 0.25,
|
|
826
|
+
bool: false,
|
|
827
|
+
json: { bool: true, number: 12, date: new Date(5), text: 'A' },
|
|
828
|
+
jsonB: { bool: true, number: 12, date: new Date(5), text: 'A' },
|
|
829
|
+
timestamptz: new Date(7),
|
|
830
|
+
arrayText: ['apple', 'pear'],
|
|
831
|
+
arrayInt: [1, 2],
|
|
832
|
+
arrayBigInt: [100, 101, 102],
|
|
833
|
+
arrayReal: [0.5, 1.5, 2.5],
|
|
834
|
+
arrayDouble: [0.25, 1.25, 2.25],
|
|
835
|
+
arrayBool: [true, false, true],
|
|
836
|
+
arrayJson: [
|
|
837
|
+
{ bool: true, number: 12, text: 'A' },
|
|
838
|
+
{ bool: true, number: 12, text: 'A' }
|
|
839
|
+
],
|
|
840
|
+
arrayJsonB: [
|
|
841
|
+
{ bool: true, number: 12, text: 'A' },
|
|
842
|
+
{ bool: true, number: 12, text: 'A' }
|
|
843
|
+
],
|
|
844
|
+
arrayTimestamptz: [new Date(1), new Date(2)]
|
|
845
|
+
});
|
|
846
|
+
let result = yield tableTypes.findFirst({});
|
|
847
|
+
expect(result.text).toEqual('apple');
|
|
848
|
+
expect(result.int).toEqual(23);
|
|
849
|
+
expect(result.bigInt).toEqual(233);
|
|
850
|
+
expect(result.real).toEqual(0.5);
|
|
851
|
+
expect(result.double).toEqual(0.25);
|
|
852
|
+
expect(result.bool).toEqual(false);
|
|
853
|
+
expect(result.json.bool).toEqual(true);
|
|
854
|
+
expect(result.json.number).toEqual(12);
|
|
855
|
+
expect(typeof result.json.date).toEqual('string');
|
|
856
|
+
expect(result.json.text).toEqual('A');
|
|
857
|
+
expect(result.jsonB.bool).toEqual(true);
|
|
858
|
+
expect(result.jsonB.number).toEqual(12);
|
|
859
|
+
expect(typeof result.jsonB.date).toEqual('string');
|
|
860
|
+
expect(result.jsonB.text).toEqual('A');
|
|
861
|
+
expect(result.timestamptz).toEqual(new Date(7));
|
|
862
|
+
expect(result.arrayText).toEqual(['apple', 'pear']);
|
|
863
|
+
expect(result.arrayInt).toEqual([1, 2]);
|
|
864
|
+
expect(result.arrayBigInt).toEqual([100, 101, 102]);
|
|
865
|
+
expect(result.arrayReal).toEqual([0.5, 1.5, 2.5]);
|
|
866
|
+
expect(result.arrayDouble).toEqual([0.25, 1.25, 2.25]);
|
|
867
|
+
expect(result.arrayBool).toEqual([true, false, true]);
|
|
868
|
+
expect(result.arrayJson).toEqual([
|
|
869
|
+
{ bool: true, number: 12, text: 'A' },
|
|
870
|
+
{ bool: true, number: 12, text: 'A' }
|
|
871
|
+
]);
|
|
872
|
+
expect(result.arrayJsonB).toEqual([
|
|
873
|
+
{ bool: true, number: 12, text: 'A' },
|
|
874
|
+
{ bool: true, number: 12, text: 'A' }
|
|
875
|
+
]);
|
|
876
|
+
expect(result.arrayTimestamptz).toEqual([new Date(1), new Date(2)]);
|
|
877
|
+
}));
|
|
878
|
+
it("Testing output formats of enum types - query", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
879
|
+
let pgdbwt = yield pgdb.transactionBegin();
|
|
880
|
+
yield pgdbwt.run(`DROP TYPE IF EXISTS ${schema}.mood CASCADE`);
|
|
881
|
+
yield pgdbwt.run(`DROP TABLE IF EXISTS ${schema}."enumTable"`);
|
|
882
|
+
yield pgdbwt.run(`CREATE TYPE ${schema}.mood AS ENUM ('sad', 'ok', 'happy')`);
|
|
883
|
+
yield pgdbwt.run(`CREATE TABLE ${schema}."enumTable" (m ${schema}.mood NULL)`);
|
|
884
|
+
yield pgdbwt.reload();
|
|
885
|
+
let table = pgdbwt.schemas[schema].tables["enumTable"];
|
|
886
|
+
yield table.insert({ m: 'sad' });
|
|
887
|
+
let result = yield table.findFirst({});
|
|
888
|
+
expect(result.m).toEqual('sad');
|
|
889
|
+
yield pgdbwt.run(`
|
|
890
|
+
CREATE TYPE ${schema}.mood_new AS ENUM ('sad', 'ok', 'happy', 'other');
|
|
891
|
+
ALTER TABLE ${schema}."enumTable" ALTER COLUMN m TYPE ${schema}.mood_new USING m::text::${schema}.mood_new;
|
|
892
|
+
`);
|
|
893
|
+
result = yield table.findFirst({});
|
|
894
|
+
expect(result.m).toEqual('sad');
|
|
895
|
+
yield pgdbwt.transactionRollback();
|
|
896
|
+
}));
|
|
897
|
+
it("Testing output formats of enum types - queryWithOnCursorCallback without stop", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
898
|
+
let pgdbwt = yield pgdb.transactionBegin();
|
|
899
|
+
yield pgdbwt.run(`DROP TYPE IF EXISTS ${schema}.mood CASCADE`);
|
|
900
|
+
yield pgdbwt.run(`DROP TABLE IF EXISTS ${schema}."enumTable"`);
|
|
901
|
+
yield pgdbwt.run(`CREATE TYPE ${schema}.mood AS ENUM ('sad', 'ok', 'happy')`);
|
|
902
|
+
yield pgdbwt.run(`CREATE TABLE ${schema}."enumTable" (m ${schema}.mood NULL)`);
|
|
903
|
+
yield pgdbwt.reload();
|
|
904
|
+
let table = pgdbwt.schemas[schema].tables["enumTable"];
|
|
905
|
+
yield table.insert([{ m: 'sad' }, { m: 'sad' }]);
|
|
906
|
+
let counter = 0;
|
|
907
|
+
yield table.queryWithOnCursorCallback(`select * from ${schema}."enumTable"`, [], {}, (data) => {
|
|
908
|
+
expect(data.m).toEqual('sad');
|
|
909
|
+
++counter;
|
|
910
|
+
});
|
|
911
|
+
expect(counter).toEqual(2);
|
|
912
|
+
yield pgdbwt.run(`
|
|
913
|
+
CREATE TYPE ${schema}.mood_new AS ENUM ('sad', 'ok', 'happy', 'other');
|
|
914
|
+
ALTER TABLE ${schema}."enumTable" ALTER COLUMN m TYPE ${schema}.mood_new USING m::text::${schema}.mood_new;
|
|
915
|
+
`);
|
|
916
|
+
counter = 0;
|
|
917
|
+
yield table.queryWithOnCursorCallback(`select * from ${schema}."enumTable"`, [], {}, (data) => {
|
|
918
|
+
expect(data.m).toEqual('sad');
|
|
919
|
+
++counter;
|
|
920
|
+
});
|
|
921
|
+
expect(counter).toEqual(2);
|
|
922
|
+
yield pgdbwt.transactionRollback();
|
|
923
|
+
}));
|
|
924
|
+
it("Testing output formats of enum types - queryWithOnCursorCallback with stop", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
925
|
+
let pgdbwt = yield pgdb.transactionBegin();
|
|
926
|
+
yield pgdbwt.run(`DROP TYPE IF EXISTS ${schema}.mood CASCADE`);
|
|
927
|
+
yield pgdbwt.run(`DROP TABLE IF EXISTS ${schema}."enumTable"`);
|
|
928
|
+
yield pgdbwt.run(`CREATE TYPE ${schema}.mood AS ENUM ('sad', 'ok', 'happy')`);
|
|
929
|
+
yield pgdbwt.run(`CREATE TABLE ${schema}."enumTable" (m ${schema}.mood NULL)`);
|
|
930
|
+
yield pgdbwt.reload();
|
|
931
|
+
let table = pgdbwt.schemas[schema].tables["enumTable"];
|
|
932
|
+
yield table.insert([{ m: 'sad' }, { m: 'sad' }]);
|
|
933
|
+
let counter = 0;
|
|
934
|
+
yield table.queryWithOnCursorCallback(`select * from ${schema}."enumTable"`, [], {}, (data) => {
|
|
935
|
+
expect(data.m).toEqual('sad');
|
|
936
|
+
++counter;
|
|
937
|
+
return true;
|
|
938
|
+
});
|
|
939
|
+
expect(counter).toEqual(1);
|
|
940
|
+
yield pgdbwt.run(`
|
|
941
|
+
CREATE TYPE ${schema}.mood_new AS ENUM ('sad', 'ok', 'happy', 'other');
|
|
942
|
+
ALTER TABLE ${schema}."enumTable" ALTER COLUMN m TYPE ${schema}.mood_new USING m::text::${schema}.mood_new;
|
|
943
|
+
`);
|
|
944
|
+
counter = 0;
|
|
945
|
+
yield table.queryWithOnCursorCallback(`select * from ${schema}."enumTable"`, [], {}, (data) => {
|
|
946
|
+
expect(data.m).toEqual('sad');
|
|
947
|
+
++counter;
|
|
948
|
+
return true;
|
|
949
|
+
});
|
|
950
|
+
expect(counter).toEqual(1);
|
|
951
|
+
yield pgdbwt.transactionRollback();
|
|
952
|
+
}));
|
|
953
|
+
it("Testing output formats of enum types - queryAsStream", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
954
|
+
let pgdbwt = yield pgdb.transactionBegin();
|
|
955
|
+
yield pgdbwt.run(`DROP TYPE IF EXISTS ${schema}.mood CASCADE`);
|
|
956
|
+
yield pgdbwt.run(`DROP TABLE IF EXISTS ${schema}."enumTable"`);
|
|
957
|
+
yield pgdbwt.run(`CREATE TYPE ${schema}.mood AS ENUM ('sad', 'ok', 'happy')`);
|
|
958
|
+
yield pgdbwt.run(`CREATE TABLE ${schema}."enumTable" (m ${schema}.mood NULL)`);
|
|
959
|
+
yield pgdbwt.reload();
|
|
960
|
+
let table = pgdbwt.schemas[schema].tables["enumTable"];
|
|
961
|
+
yield table.insert({ m: 'sad' });
|
|
962
|
+
let stream = yield table.find({}, { stream: true });
|
|
963
|
+
stream.on("data", (data) => {
|
|
964
|
+
expect(data.m).toEqual('sad');
|
|
965
|
+
});
|
|
966
|
+
yield new Promise((resolve, reject) => {
|
|
967
|
+
stream.on('error', reject);
|
|
968
|
+
stream.on('close', resolve);
|
|
969
|
+
});
|
|
970
|
+
yield pgdbwt.run(`
|
|
971
|
+
CREATE TYPE ${schema}.mood_new AS ENUM ('sad', 'ok', 'happy', 'other');
|
|
972
|
+
ALTER TABLE ${schema}."enumTable" ALTER COLUMN m TYPE ${schema}.mood_new USING m::text::${schema}.mood_new;
|
|
973
|
+
`);
|
|
974
|
+
stream = yield table.find({}, { stream: true });
|
|
975
|
+
stream.on("data", (data) => {
|
|
976
|
+
expect(data.m).toEqual('sad');
|
|
977
|
+
});
|
|
978
|
+
try {
|
|
979
|
+
yield new Promise((resolve, reject) => {
|
|
980
|
+
stream.on('error', reject);
|
|
981
|
+
stream.on('close', resolve);
|
|
982
|
+
});
|
|
983
|
+
expect(false).toBeTruthy();
|
|
984
|
+
}
|
|
985
|
+
catch (e) {
|
|
986
|
+
expect('' + e).toEqual('Error: [337] Query returns fields with unknown oid.');
|
|
987
|
+
}
|
|
988
|
+
yield pgdbwt.transactionRollback();
|
|
989
|
+
}));
|
|
990
|
+
it("Testing output formats of enum array types - query", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
991
|
+
let pgdbwt = yield pgdb.transactionBegin();
|
|
992
|
+
yield pgdbwt.run(`DROP TYPE IF EXISTS ${schema}.mood CASCADE`);
|
|
993
|
+
yield pgdbwt.run(`DROP TABLE IF EXISTS ${schema}."enumArrayTable"`);
|
|
994
|
+
yield pgdbwt.run(`CREATE TYPE ${schema}.mood AS ENUM ('sad', 'ok', 'happy')`);
|
|
995
|
+
yield pgdbwt.run(`CREATE TABLE ${schema}."enumArrayTable" (m ${schema}.mood[] NULL)`);
|
|
996
|
+
yield pgdbwt.reload();
|
|
997
|
+
let table = pgdbwt.schemas[schema].tables["enumArrayTable"];
|
|
998
|
+
yield table.insert({ m: ['sad', 'ok'] });
|
|
999
|
+
let result = yield table.findFirst({});
|
|
1000
|
+
expect(result.m).toEqual(['sad', 'ok']);
|
|
1001
|
+
yield pgdbwt.run(`
|
|
1002
|
+
CREATE TYPE ${schema}.mood_new AS ENUM ('sad', 'ok', 'happy', 'other');
|
|
1003
|
+
ALTER TABLE ${schema}."enumArrayTable" ALTER COLUMN m TYPE ${schema}.mood_new[] USING m::text[]::${schema}.mood_new[];
|
|
1004
|
+
`);
|
|
1005
|
+
result = yield table.findFirst({});
|
|
1006
|
+
expect(result.m).toEqual(['sad', 'ok']);
|
|
1007
|
+
yield pgdbwt.transactionRollback();
|
|
1008
|
+
}));
|
|
1009
|
+
it("Testing output formats of enum array types - queryWithOnCursorCallback without stop", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
1010
|
+
let pgdbwt = yield pgdb.transactionBegin();
|
|
1011
|
+
yield pgdbwt.run(`DROP TYPE IF EXISTS ${schema}.mood CASCADE`);
|
|
1012
|
+
yield pgdbwt.run(`DROP TABLE IF EXISTS ${schema}."enumArrayTable"`);
|
|
1013
|
+
yield pgdbwt.run(`CREATE TYPE ${schema}.mood AS ENUM ('sad', 'ok', 'happy')`);
|
|
1014
|
+
yield pgdbwt.run(`CREATE TABLE ${schema}."enumArrayTable" (m ${schema}.mood[] NULL)`);
|
|
1015
|
+
yield pgdbwt.reload();
|
|
1016
|
+
let table = pgdbwt.schemas[schema].tables["enumArrayTable"];
|
|
1017
|
+
yield table.insert([{ m: ['sad', 'ok'] }, { m: ['sad', 'ok'] }]);
|
|
1018
|
+
let counter = 0;
|
|
1019
|
+
yield table.queryWithOnCursorCallback(`select * from ${schema}."enumArrayTable"`, [], {}, (data) => {
|
|
1020
|
+
expect(data.m).toEqual(['sad', 'ok']);
|
|
1021
|
+
++counter;
|
|
1022
|
+
});
|
|
1023
|
+
expect(counter).toEqual(2);
|
|
1024
|
+
yield pgdbwt.run(`
|
|
1025
|
+
CREATE TYPE ${schema}.mood_new AS ENUM ('sad', 'ok', 'happy', 'other');
|
|
1026
|
+
ALTER TABLE ${schema}."enumArrayTable" ALTER COLUMN m TYPE ${schema}.mood_new[] USING m::text[]::${schema}.mood_new[];
|
|
1027
|
+
`);
|
|
1028
|
+
counter = 0;
|
|
1029
|
+
yield table.queryWithOnCursorCallback(`select * from ${schema}."enumArrayTable"`, [], {}, (data) => {
|
|
1030
|
+
expect(data.m).toEqual(['sad', 'ok']);
|
|
1031
|
+
++counter;
|
|
1032
|
+
});
|
|
1033
|
+
expect(counter).toEqual(2);
|
|
1034
|
+
yield pgdbwt.transactionRollback();
|
|
1035
|
+
}));
|
|
1036
|
+
it("Testing output formats of enum array types - queryWithOnCursorCallback with stop", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
1037
|
+
let pgdbwt = yield pgdb.transactionBegin();
|
|
1038
|
+
yield pgdbwt.run(`DROP TYPE IF EXISTS ${schema}.mood CASCADE`);
|
|
1039
|
+
yield pgdbwt.run(`DROP TABLE IF EXISTS ${schema}."enumArrayTable"`);
|
|
1040
|
+
yield pgdbwt.run(`CREATE TYPE ${schema}.mood AS ENUM ('sad', 'ok', 'happy')`);
|
|
1041
|
+
yield pgdbwt.run(`CREATE TABLE ${schema}."enumArrayTable" (m ${schema}.mood[] NULL)`);
|
|
1042
|
+
yield pgdbwt.reload();
|
|
1043
|
+
let table = pgdbwt.schemas[schema].tables["enumArrayTable"];
|
|
1044
|
+
yield table.insert([{ m: ['sad', 'ok'] }, { m: ['sad', 'ok'] }]);
|
|
1045
|
+
let counter = 0;
|
|
1046
|
+
yield table.queryWithOnCursorCallback(`select * from ${schema}."enumArrayTable"`, [], {}, (data) => {
|
|
1047
|
+
expect(data.m).toEqual(['sad', 'ok']);
|
|
1048
|
+
++counter;
|
|
1049
|
+
return true;
|
|
1050
|
+
});
|
|
1051
|
+
expect(counter).toEqual(1);
|
|
1052
|
+
yield pgdbwt.run(`
|
|
1053
|
+
CREATE TYPE ${schema}.mood_new AS ENUM ('sad', 'ok', 'happy', 'other');
|
|
1054
|
+
ALTER TABLE ${schema}."enumArrayTable" ALTER COLUMN m TYPE ${schema}.mood_new[] USING m::text[]::${schema}.mood_new[];
|
|
1055
|
+
`);
|
|
1056
|
+
counter = 0;
|
|
1057
|
+
yield table.queryWithOnCursorCallback(`select * from ${schema}."enumArrayTable"`, [], {}, (data) => {
|
|
1058
|
+
expect(data.m).toEqual(['sad', 'ok']);
|
|
1059
|
+
++counter;
|
|
1060
|
+
return true;
|
|
1061
|
+
});
|
|
1062
|
+
expect(counter).toEqual(1);
|
|
1063
|
+
yield pgdbwt.transactionRollback();
|
|
1064
|
+
}));
|
|
1065
|
+
it("Testing output formats of enum array types - queryAsStream", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
1066
|
+
let pgdbwt = yield pgdb.transactionBegin();
|
|
1067
|
+
yield pgdbwt.run(`DROP TYPE IF EXISTS ${schema}.mood CASCADE`);
|
|
1068
|
+
yield pgdbwt.run(`DROP TABLE IF EXISTS ${schema}."enumArrayTable"`);
|
|
1069
|
+
yield pgdbwt.run(`CREATE TYPE ${schema}.mood AS ENUM ('sad', 'ok', 'happy')`);
|
|
1070
|
+
yield pgdbwt.run(`CREATE TABLE ${schema}."enumArrayTable" (m ${schema}.mood[] NULL)`);
|
|
1071
|
+
yield pgdbwt.reload();
|
|
1072
|
+
let table = pgdbwt.schemas[schema].tables["enumArrayTable"];
|
|
1073
|
+
yield table.insert({ m: ['sad', 'ok'] });
|
|
1074
|
+
let stream = yield table.find({}, { stream: true });
|
|
1075
|
+
stream.on("data", (data) => {
|
|
1076
|
+
expect(data.m).toEqual(['sad', 'ok']);
|
|
1077
|
+
});
|
|
1078
|
+
yield new Promise((resolve, reject) => {
|
|
1079
|
+
stream.on('error', reject);
|
|
1080
|
+
stream.on('close', resolve);
|
|
1081
|
+
});
|
|
1082
|
+
yield pgdbwt.run(`
|
|
1083
|
+
CREATE TYPE ${schema}.mood_new AS ENUM ('sad', 'ok', 'happy', 'other');
|
|
1084
|
+
ALTER TABLE ${schema}."enumArrayTable" ALTER COLUMN m TYPE ${schema}.mood_new[] USING m::text[]::${schema}.mood_new[];
|
|
1085
|
+
`);
|
|
1086
|
+
stream = yield table.find({}, { stream: true });
|
|
1087
|
+
stream.on("data", (data) => {
|
|
1088
|
+
expect(data.m).toEqual(['sad', 'ok']);
|
|
1089
|
+
});
|
|
1090
|
+
try {
|
|
1091
|
+
yield new Promise((resolve, reject) => {
|
|
1092
|
+
stream.on('error', reject);
|
|
1093
|
+
stream.on('close', resolve);
|
|
1094
|
+
});
|
|
1095
|
+
expect(false).toBeTruthy();
|
|
1096
|
+
}
|
|
1097
|
+
catch (e) {
|
|
1098
|
+
expect('' + e).toEqual('Error: [337] Query returns fields with unknown oid.');
|
|
1099
|
+
}
|
|
1100
|
+
yield pgdbwt.transactionRollback();
|
|
1101
|
+
}));
|
|
1102
|
+
it("Testing output formats of enum array types - query on view", () => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
|
|
1103
|
+
let pgdbwt = yield pgdb.transactionBegin();
|
|
1104
|
+
yield pgdbwt.run(`DROP TYPE IF EXISTS ${schema}.mood CASCADE`);
|
|
1105
|
+
yield pgdbwt.run(`DROP TABLE IF EXISTS ${schema}."enumArrayTable"`);
|
|
1106
|
+
yield pgdbwt.run(`CREATE TYPE ${schema}.mood AS ENUM ('sad', 'ok', 'happy')`);
|
|
1107
|
+
yield pgdbwt.run(`CREATE TABLE ${schema}."enumArrayTable" (m ${schema}.mood[] NULL)`);
|
|
1108
|
+
yield pgdbwt.run(`CREATE VIEW ${schema}."enumArrayTableView" as select m from ${schema}."enumArrayTable"`);
|
|
1109
|
+
yield pgdbwt.reload();
|
|
1110
|
+
let table = pgdbwt.schemas[schema].tables["enumArrayTable"];
|
|
1111
|
+
yield table.insert({ m: ['sad', 'ok'] });
|
|
1112
|
+
let view = pgdbwt.schemas[schema].tables["enumArrayTableView"];
|
|
1113
|
+
let result = yield view.findFirst({});
|
|
1114
|
+
expect(result.m).toEqual(['sad', 'ok']);
|
|
1115
|
+
yield pgdbwt.run(`
|
|
1116
|
+
CREATE TYPE ${schema}.mood_new AS ENUM ('sad', 'ok', 'happy', 'other');
|
|
1117
|
+
DROP VIEW ${schema}."enumArrayTableView";
|
|
1118
|
+
ALTER TABLE ${schema}."enumArrayTable" ALTER COLUMN m TYPE ${schema}.mood_new[] USING m::text[]::${schema}.mood_new[];
|
|
1119
|
+
CREATE VIEW ${schema}."enumArrayTableView" as select m from ${schema}."enumArrayTable"
|
|
1120
|
+
`);
|
|
1121
|
+
result = yield view.findFirst({});
|
|
1122
|
+
expect(result.m).toEqual(['sad', 'ok']);
|
|
1123
|
+
yield pgdbwt.transactionRollback();
|
|
1124
|
+
}));
|
|
1125
|
+
});
|
|
1126
|
+
//# sourceMappingURL=pgDb.test.js.map
|