easy-postgresql 0.0.1

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.
Files changed (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +45 -0
  3. package/SECURITY.md +15 -0
  4. package/database/procedure.ts +129 -0
  5. package/database/query.ts +78 -0
  6. package/database/sql_types.d.ts +52 -0
  7. package/database/sql_types.ts +90 -0
  8. package/database/table.ts +378 -0
  9. package/database.ts +26 -0
  10. package/dist/database/procedure.d.ts +26 -0
  11. package/dist/database/procedure.js +107 -0
  12. package/dist/database/procedure.js.map +1 -0
  13. package/dist/database/query.d.ts +13 -0
  14. package/dist/database/query.js +79 -0
  15. package/dist/database/query.js.map +1 -0
  16. package/dist/database/sql_types.d.ts +48 -0
  17. package/dist/database/sql_types.js +50 -0
  18. package/dist/database/sql_types.js.map +1 -0
  19. package/dist/database/table.d.ts +54 -0
  20. package/dist/database/table.js +308 -0
  21. package/dist/database/table.js.map +1 -0
  22. package/dist/database.d.ts +24 -0
  23. package/dist/database.js +28 -0
  24. package/dist/database.js.map +1 -0
  25. package/dist/operations/config.d.ts +22 -0
  26. package/dist/operations/config.js +30 -0
  27. package/dist/operations/config.js.map +1 -0
  28. package/dist/operations/connect_to_server.d.ts +28 -0
  29. package/dist/operations/connect_to_server.js +108 -0
  30. package/dist/operations/connect_to_server.js.map +1 -0
  31. package/dist/operations/data_types.d.ts +6 -0
  32. package/dist/operations/data_types.js +120 -0
  33. package/dist/operations/data_types.js.map +1 -0
  34. package/dist/operations/get_pieces.d.ts +29 -0
  35. package/dist/operations/get_pieces.js +56 -0
  36. package/dist/operations/get_pieces.js.map +1 -0
  37. package/dist/operations/log.d.ts +12 -0
  38. package/dist/operations/log.js +27 -0
  39. package/dist/operations/log.js.map +1 -0
  40. package/dist/operations/procedure_execute.d.ts +18 -0
  41. package/dist/operations/procedure_execute.js +53 -0
  42. package/dist/operations/procedure_execute.js.map +1 -0
  43. package/operations/config.ts +28 -0
  44. package/operations/connect_to_server.ts +111 -0
  45. package/operations/data_types.ts +129 -0
  46. package/operations/get_pieces.ts +63 -0
  47. package/operations/log.ts +27 -0
  48. package/operations/procedure_execute.ts +58 -0
  49. package/package.json +55 -0
  50. package/simple/advanced-test.js +631 -0
  51. package/simple/config.simple.js +9 -0
  52. package/simple/config.simple.ts +9 -0
  53. package/simple/test.js +398 -0
  54. package/simple/test.ts +392 -0
  55. package/tsconfig.json +21 -0
package/simple/test.ts ADDED
@@ -0,0 +1,392 @@
1
+ import { sqlConfig } from './config';
2
+ import EasyPostgresql from '../dist/database';
3
+ import { SqlConfig } from '../operations/connect_to_server';
4
+ import 'colors';
5
+
6
+ console.log('\neasy-postgresql Test Suite');
7
+ console.log('========================');
8
+ console.log('Starting tests...\n');
9
+
10
+ EasyPostgresql.Config.logingMode(true);
11
+
12
+ console.log('Test 1: Testing database connection...');
13
+ console.log('Waiting for PostgreSQL to be ready...');
14
+
15
+ EasyPostgresql.Connect(sqlConfig, async (config: SqlConfig, err?: Error) => {
16
+ if (err) {
17
+ console.error('Connection failed:', err.message);
18
+ process.exit(1);
19
+ }
20
+ console.log('Connection successful\n');
21
+
22
+ console.log('Test 2: Testing table operations...');
23
+ try {
24
+ const schema = {
25
+ id: EasyPostgresql.Types.int() + EasyPostgresql.Types.options.primaryKey(),
26
+ name: EasyPostgresql.Types.varchar(255),
27
+ created_at: EasyPostgresql.Types.datetime()
28
+ };
29
+
30
+ const table = EasyPostgresql.Table('test_table');
31
+ await table.functions.remove();
32
+
33
+ const createResult = await table.functions.create(schema);
34
+ if (createResult) {
35
+ console.log('Table creation successful');
36
+ } else {
37
+ console.log('Table creation failed');
38
+ throw new Error('Table creation failed');
39
+ }
40
+
41
+ const now = new Date();
42
+
43
+ const insertResult = await table.createOne({
44
+ id: 1,
45
+ name: 'Test Record',
46
+ created_at: now
47
+ });
48
+ if (insertResult) {
49
+ console.log('Data insertion successful');
50
+ } else {
51
+ console.log('Data insertion failed');
52
+ throw new Error('Data insertion failed');
53
+ }
54
+
55
+ const queryResult = await table.find();
56
+ if (queryResult && queryResult.length > 0) {
57
+ console.log('Data query successful');
58
+ } else {
59
+ console.log('Data query failed');
60
+ throw new Error('Data query failed');
61
+ }
62
+
63
+ const removeResult = await table.functions.remove();
64
+ if (removeResult) {
65
+ console.log('Table cleanup successful\n');
66
+ } else {
67
+ console.log('Table cleanup failed\n');
68
+ throw new Error('Table cleanup failed');
69
+ }
70
+
71
+ console.log('Test 3: Testing all table functions...');
72
+ try {
73
+ await table.functions.create(schema);
74
+ console.log('Table recreated for comprehensive tests');
75
+
76
+ const testRecords = [
77
+ { id: 1, name: 'Test Record 1', created_at: now },
78
+ { id: 2, name: 'Test Record 2', created_at: now },
79
+ { id: 3, name: 'Test Record 3', created_at: now }
80
+ ];
81
+
82
+ const createOneResult = await table.createOne(testRecords[0]);
83
+ if (createOneResult) {
84
+ console.log('createOne test successful');
85
+ } else {
86
+ throw new Error('createOne test failed');
87
+ }
88
+
89
+ const findOneResult = await table.findOne({ id: 1 });
90
+ if (findOneResult && findOneResult.id === 1) {
91
+ console.log('findOne test successful');
92
+ } else {
93
+ throw new Error('findOne test failed');
94
+ }
95
+
96
+ const updateOneResult = await table.updateOne({ id: 1 }, { name: 'Updated Record' });
97
+ if (updateOneResult) {
98
+ console.log('updateOne test successful');
99
+ } else {
100
+ throw new Error('updateOne test failed');
101
+ }
102
+
103
+ const deleteOneResult = await table.deleteOne({ id: 1 });
104
+ if (deleteOneResult) {
105
+ console.log('deleteOne test successful');
106
+ } else {
107
+ throw new Error('deleteOne test failed');
108
+ }
109
+
110
+ for (const record of testRecords) {
111
+ await table.createOne(record);
112
+ }
113
+
114
+ const firstResult = await table.first({ id: 1 });
115
+ if (firstResult && firstResult.id === 1) {
116
+ console.log('first test successful');
117
+ } else {
118
+ throw new Error('first test failed');
119
+ }
120
+
121
+ const firstOrDefaultResult = await table.firstOrDefault({ id: 999 });
122
+ if (firstOrDefaultResult === null) {
123
+ console.log('firstOrDefault test successful');
124
+ } else {
125
+ throw new Error('firstOrDefault test failed');
126
+ }
127
+
128
+ const singleResult = await table.single({ id: 2 });
129
+ if (singleResult && singleResult.id === 2) {
130
+ console.log('single test successful');
131
+ } else {
132
+ throw new Error('single test failed');
133
+ }
134
+
135
+ const singleOrDefaultResult = await table.singleOrDefault({ id: 999 });
136
+ if (singleOrDefaultResult === null) {
137
+ console.log('singleOrDefault test successful');
138
+ } else {
139
+ throw new Error('singleOrDefault test failed');
140
+ }
141
+
142
+ const lastResult = await table.last();
143
+ if (lastResult && lastResult.id === 3) {
144
+ console.log('last test successful');
145
+ } else {
146
+ throw new Error('last test failed');
147
+ }
148
+
149
+ const lastOrDefaultResult = await table.lastOrDefault({ id: 999 });
150
+ if (lastOrDefaultResult === null) {
151
+ console.log('lastOrDefault test successful');
152
+ } else {
153
+ throw new Error('lastOrDefault test failed');
154
+ }
155
+
156
+ const countResult = await table.count();
157
+ if (countResult === 3) {
158
+ console.log('count test successful');
159
+ } else {
160
+ throw new Error('count test failed');
161
+ }
162
+
163
+ const minResult = await table.min('id');
164
+ if (minResult === 1) {
165
+ console.log('min test successful');
166
+ } else {
167
+ throw new Error('min test failed');
168
+ }
169
+
170
+ const maxResult = await table.max('id');
171
+ if (maxResult === 3) {
172
+ console.log('max test successful');
173
+ } else {
174
+ throw new Error('max test failed');
175
+ }
176
+
177
+ const averageResult = await table.average('id');
178
+ if (averageResult === 2) {
179
+ console.log('average test successful');
180
+ } else {
181
+ throw new Error('average test failed');
182
+ }
183
+
184
+ const deleteAllWithParamResult = await table.deleteAll({ name: 'Test Record 2' });
185
+ if (deleteAllWithParamResult) {
186
+ console.log('deleteAll with parameter test successful');
187
+ } else {
188
+ throw new Error('deleteAll with parameter test failed');
189
+ }
190
+
191
+ const deleteAllResult = await table.deleteAll();
192
+ if (deleteAllResult) {
193
+ console.log('deleteAll without parameter test successful');
194
+ } else {
195
+ throw new Error('deleteAll without parameter test failed');
196
+ }
197
+
198
+ await table.functions.remove();
199
+ console.log('All table function tests completed successfully\n');
200
+ } catch (error) {
201
+ console.error('Comprehensive table function tests failed:', error);
202
+ process.exit(1);
203
+ }
204
+ } catch (error) {
205
+ console.error('Table operations failed:', error);
206
+ process.exit(1);
207
+ }
208
+
209
+ console.log('Test 4: Testing raw query...');
210
+ try {
211
+ const createProcSQL = `
212
+ CREATE OR REPLACE FUNCTION TestProcedure(param1 INT, param2 VARCHAR)
213
+ RETURNS TABLE(id INT, name VARCHAR) AS $$
214
+ BEGIN
215
+ RETURN QUERY SELECT param1 AS id, param2::VARCHAR AS name;
216
+ END;
217
+ $$ LANGUAGE plpgsql;
218
+ `;
219
+
220
+ await EasyPostgresql.Query(createProcSQL);
221
+
222
+ const procResult = await EasyPostgresql.Procedure('TestProcedure').Execute({
223
+ param1: 1,
224
+ param2: 'Test'
225
+ });
226
+
227
+ if (procResult && procResult.status === 200) {
228
+ console.log('Function execution test successful\n');
229
+ } else {
230
+ console.log('Function execution test failed\n');
231
+ throw new Error('Function execution test failed');
232
+ }
233
+
234
+ const dropFunction = await EasyPostgresql.Query('DROP FUNCTION IF EXISTS TestProcedure');
235
+ if (dropFunction) {
236
+ console.log('Function cleanup successful\n');
237
+ } else {
238
+ console.log('Function cleanup failed\n');
239
+ throw new Error('Function cleanup failed');
240
+ }
241
+ } catch (error) {
242
+ console.error('Raw query test failed:', error);
243
+ process.exit(1);
244
+ }
245
+
246
+ console.log('Test 5: Testing table functions (info, isThere, getAll, updateColumn)...');
247
+ try {
248
+ const table2 = EasyPostgresql.Table('test_table_functions');
249
+ const schema2 = {
250
+ id: EasyPostgresql.Types.int() + EasyPostgresql.Types.options.primaryKey(),
251
+ name: EasyPostgresql.Types.varchar(100),
252
+ email: EasyPostgresql.Types.varchar(100)
253
+ };
254
+ await table2.functions.remove();
255
+ await table2.functions.create(schema2);
256
+
257
+ const isThere = await table2.functions.isThere();
258
+ if (isThere === true) {
259
+ console.log('isThere test successful');
260
+ } else {
261
+ throw new Error('isThere test failed');
262
+ }
263
+
264
+ const info = await table2.functions.info();
265
+ if (info && info.name === 'test_table_functions' && info.type === 'Table') {
266
+ console.log('info test successful');
267
+ } else {
268
+ throw new Error('info test failed');
269
+ }
270
+
271
+ const allTables = await table2.functions.getAll();
272
+ if (allTables && Array.isArray(allTables) && allTables.some((t: any) => t.name === 'test_table_functions')) {
273
+ console.log('getAll test successful');
274
+ } else {
275
+ throw new Error('getAll test failed');
276
+ }
277
+
278
+ const colRename = await table2.functions.updateColumn('name').rename('full_name');
279
+ if (colRename === true) {
280
+ console.log('updateColumn.rename test successful');
281
+ } else {
282
+ throw new Error('updateColumn.rename test failed');
283
+ }
284
+
285
+ const colAdd = await table2.functions.updateColumn('age').add('INT');
286
+ if (colAdd === true) {
287
+ console.log('updateColumn.add test successful');
288
+ } else {
289
+ throw new Error('updateColumn.add test failed');
290
+ }
291
+
292
+ await table2.functions.remove();
293
+ console.log('Table functions tests completed successfully\n');
294
+ } catch (error) {
295
+ console.error('Table functions tests failed:', error);
296
+ process.exit(1);
297
+ }
298
+
299
+ console.log('Test 6: Testing find with selected_keys and likes...');
300
+ try {
301
+ const table3 = EasyPostgresql.Table('test_table_options');
302
+ const schema3 = {
303
+ id: EasyPostgresql.Types.int() + EasyPostgresql.Types.options.primaryKey(),
304
+ name: EasyPostgresql.Types.varchar(100),
305
+ email: EasyPostgresql.Types.varchar(100)
306
+ };
307
+ await table3.functions.remove();
308
+ await table3.functions.create(schema3);
309
+
310
+ await table3.createOne({ id: 1, name: 'Alice', email: 'alice@test.com' });
311
+ await table3.createOne({ id: 2, name: 'Bob', email: 'bob@test.com' });
312
+ await table3.createOne({ id: 3, name: 'Charlie', email: 'charlie@other.com' });
313
+
314
+ const selectedResult = await table3.find({}, { selected_keys: ['id', 'name'] });
315
+ const firstRow = selectedResult[0];
316
+ if (firstRow && firstRow.id !== undefined && firstRow.name !== undefined && firstRow.email === undefined) {
317
+ console.log('find with selected_keys test successful');
318
+ } else {
319
+ throw new Error('find with selected_keys test failed');
320
+ }
321
+
322
+ const likeResult = await table3.find({}, { likes: { name: 'A?' } });
323
+ if (likeResult && likeResult.length === 1 && likeResult[0].name === 'Alice') {
324
+ console.log('find with likes test successful');
325
+ } else {
326
+ throw new Error('find with likes test failed');
327
+ }
328
+
329
+ await table3.functions.remove();
330
+ console.log('find options tests completed successfully\n');
331
+ } catch (error) {
332
+ console.error('find options tests failed:', error);
333
+ process.exit(1);
334
+ }
335
+
336
+ console.log('Test 7: Testing raw query with named parameters...');
337
+ try {
338
+ const rawResult = await EasyPostgresql.Query('SELECT @val1 AS col1, @val2 AS col2', { val1: 100, val2: 'hello' });
339
+ if (rawResult && rawResult.status === 200 && rawResult.data[0].col1 === 100 && rawResult.data[0].col2 === 'hello') {
340
+ console.log('Raw query with named params test successful');
341
+ } else {
342
+ throw new Error('Raw query with named params test failed');
343
+ }
344
+ console.log('Raw query named params test completed successfully\n');
345
+ } catch (error) {
346
+ console.error('Raw query named params test failed:', error);
347
+ process.exit(1);
348
+ }
349
+
350
+ console.log('Test 8: Testing Procedure.Info and IsConnected...');
351
+ try {
352
+ const createProcSQL2 = `
353
+ CREATE OR REPLACE FUNCTION TestProc2(x INT)
354
+ RETURNS INT AS $$
355
+ BEGIN
356
+ RETURN x * 2;
357
+ END;
358
+ $$ LANGUAGE plpgsql;
359
+ `;
360
+ await EasyPostgresql.Query(createProcSQL2);
361
+
362
+ const procInfo = await EasyPostgresql.Procedure('TestProc2').Info();
363
+ if (procInfo && procInfo.name === 'testproc2' && procInfo.type === 'Function') {
364
+ console.log('Procedure.Info test successful');
365
+ } else {
366
+ throw new Error('Procedure.Info test failed');
367
+ }
368
+
369
+ const allProcInfo = await EasyPostgresql.Procedure().AllInfo();
370
+ if (allProcInfo && Array.isArray(allProcInfo) && allProcInfo.some((p: any) => p.name === 'testproc2')) {
371
+ console.log('Procedure.AllInfo test successful');
372
+ } else {
373
+ throw new Error('Procedure.AllInfo test failed');
374
+ }
375
+
376
+ const connected = await EasyPostgresql.IsConnected();
377
+ if (connected && connected.status === 200) {
378
+ console.log('IsConnected test successful');
379
+ } else {
380
+ throw new Error('IsConnected test failed');
381
+ }
382
+
383
+ await EasyPostgresql.Query('DROP FUNCTION IF EXISTS TestProc2');
384
+ console.log('Procedure info and IsConnected tests completed successfully\n');
385
+ } catch (error) {
386
+ console.error('Procedure info tests failed:', error);
387
+ process.exit(1);
388
+ }
389
+
390
+ console.log('All tests completed!');
391
+ process.exit(0);
392
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2021",
4
+ "module": "node16",
5
+ "lib": ["ES2021"],
6
+ "declaration": true,
7
+ "outDir": "./dist",
8
+ "rootDir": "./",
9
+ "strict": true,
10
+ "esModuleInterop": true,
11
+ "skipLibCheck": true,
12
+ "forceConsistentCasingInFileNames": true,
13
+ "moduleResolution": "node16",
14
+ "resolveJsonModule": true,
15
+ "sourceMap": true,
16
+ "noImplicitAny": false,
17
+ "typeRoots": ["./types", "./node_modules/@types"]
18
+ },
19
+ "include": ["operations/**/*", "database/**/*", "simple/**/*", "*.ts"],
20
+ "exclude": ["node_modules", "dist", "dist/**/*", "simple"]
21
+ }