easy-postgresql 0.0.1 → 0.0.2
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/README.md +287 -25
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,45 +1,307 @@
|
|
|
1
|
-
|
|
1
|
+
# easy-postgresql
|
|
2
|
+
|
|
3
|
+
[](https://choosealicense.com/licenses/mit/)
|
|
4
|
+
|
|
5
|
+
Simple and easy to use PostgreSQL library for Node.js.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
2
8
|
|
|
3
9
|
```bash
|
|
4
|
-
|
|
10
|
+
npm install easy-postgresql
|
|
5
11
|
```
|
|
6
12
|
|
|
7
|
-
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
8
15
|
```js
|
|
9
|
-
const
|
|
16
|
+
const { Connect, Table, Query, Procedure, Types } = require('easy-postgresql');
|
|
10
17
|
|
|
11
|
-
|
|
18
|
+
// PostgreSQL connection config
|
|
19
|
+
const sqlConfig = {
|
|
20
|
+
user: 'postgres',
|
|
21
|
+
password: 'postgres',
|
|
22
|
+
host: 'localhost',
|
|
23
|
+
port: 5432,
|
|
24
|
+
database: 'test'
|
|
25
|
+
};
|
|
12
26
|
|
|
13
|
-
|
|
14
|
-
|
|
27
|
+
// Connect to database
|
|
28
|
+
Connect(sqlConfig);
|
|
15
29
|
|
|
16
|
-
|
|
30
|
+
// Optional: enable logging
|
|
31
|
+
Config.logingMode(true);
|
|
17
32
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
33
|
+
// Check connection status
|
|
34
|
+
const status = await IsConnected();
|
|
35
|
+
// { status: 200, message: 'OK' }
|
|
36
|
+
```
|
|
21
37
|
|
|
22
|
-
|
|
23
|
-
// console.log(info);
|
|
38
|
+
## API Reference
|
|
24
39
|
|
|
25
|
-
|
|
26
|
-
// console.log(procedure);
|
|
40
|
+
### `Connect(config, callback?)`
|
|
27
41
|
|
|
28
|
-
|
|
29
|
-
// console.log(query);
|
|
42
|
+
Connects to a PostgreSQL database. Creates a connection pool internally.
|
|
30
43
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
44
|
+
```js
|
|
45
|
+
await Connect({
|
|
46
|
+
user: 'postgres',
|
|
47
|
+
password: 'password',
|
|
48
|
+
host: 'localhost',
|
|
49
|
+
port: 5432,
|
|
50
|
+
database: 'mydb'
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// With callback
|
|
54
|
+
Connect(sqlConfig, (config, err) => {
|
|
55
|
+
if (err) console.error('Connection failed:', err.message);
|
|
56
|
+
else console.log('Connected!');
|
|
57
|
+
});
|
|
58
|
+
```
|
|
34
59
|
|
|
35
|
-
|
|
36
|
-
// console.log(removedTable);
|
|
37
|
-
}, 1000);
|
|
60
|
+
### `IsConnected()`
|
|
38
61
|
|
|
62
|
+
Checks if the database connection is active.
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
const result = await IsConnected();
|
|
66
|
+
console.log(result.status); // 200 if connected
|
|
39
67
|
```
|
|
40
68
|
|
|
41
|
-
|
|
69
|
+
### `Table(name)`
|
|
70
|
+
|
|
71
|
+
Provides CRUD operations and table management for a specific table.
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
const users = Table('users');
|
|
75
|
+
|
|
76
|
+
// Find all users
|
|
77
|
+
const all = await users.find();
|
|
78
|
+
|
|
79
|
+
// Find with conditions
|
|
80
|
+
const admins = await users.find({ role: 'admin' });
|
|
81
|
+
|
|
82
|
+
// Find with options
|
|
83
|
+
const result = await users.find(
|
|
84
|
+
{ active: true },
|
|
85
|
+
{ limit: 10, selected_keys: ['id', 'name'], likes: { name: 'A%' } }
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
// Find one
|
|
89
|
+
const user = await users.findOne({ id: 1 });
|
|
90
|
+
|
|
91
|
+
// Create
|
|
92
|
+
await users.createOne({ name: 'John', email: 'john@test.com' });
|
|
93
|
+
|
|
94
|
+
// Update
|
|
95
|
+
await users.updateOne({ id: 1 }, { name: 'Jane' });
|
|
96
|
+
|
|
97
|
+
// Delete
|
|
98
|
+
await users.deleteOne({ id: 1 });
|
|
99
|
+
await users.deleteAll({ role: 'inactive' });
|
|
100
|
+
|
|
101
|
+
// Aggregation
|
|
102
|
+
const count = await users.count();
|
|
103
|
+
const minAge = await users.min('age');
|
|
104
|
+
const maxAge = await users.max('age');
|
|
105
|
+
const avgAge = await users.average('age');
|
|
106
|
+
|
|
107
|
+
// LINQ-style methods
|
|
108
|
+
const first = await users.first({ active: true });
|
|
109
|
+
const last = await users.last({ active: true });
|
|
110
|
+
const single = await users.single({ id: 1 }); // throws if 0 or >1
|
|
111
|
+
const result = await users.singleOrDefault({ id: 1 }); // null if 0, throws if >1
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### Table Management
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
const table = Table('products');
|
|
118
|
+
|
|
119
|
+
// Check if table exists
|
|
120
|
+
const exists = await table.functions.isThere();
|
|
121
|
+
|
|
122
|
+
// Get table info (name, type, schema)
|
|
123
|
+
const info = await table.functions.info();
|
|
124
|
+
// { name: 'products', type: 'Table', date: null, schema: { id: 'integer', ... } }
|
|
125
|
+
|
|
126
|
+
// Get all tables
|
|
127
|
+
const allTables = await table.functions.getAll();
|
|
128
|
+
|
|
129
|
+
// Create table with schema
|
|
130
|
+
const schema = {
|
|
131
|
+
id: Types.int() + Types.options.primaryKey() + Types.options.autoIncrement(),
|
|
132
|
+
name: Types.varchar(100) + Types.options.notNull(),
|
|
133
|
+
price: Types.decimal(10, 2),
|
|
134
|
+
created_at: Types.datetime() + ' DEFAULT NOW()'
|
|
135
|
+
};
|
|
136
|
+
await table.functions.create(schema);
|
|
137
|
+
|
|
138
|
+
// Drop table
|
|
139
|
+
await table.functions.remove();
|
|
140
|
+
|
|
141
|
+
// Column operations
|
|
142
|
+
await table.functions.updateColumn('email').add('VARCHAR(255)'); // Add
|
|
143
|
+
await table.functions.updateColumn('email').remove(); // Drop
|
|
144
|
+
await table.functions.updateColumn('name').rename('full_name'); // Rename
|
|
145
|
+
await table.functions.updateColumn('price').update('DECIMAL(12,2)'); // Alter type
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### `Query(sql, inputs?)`
|
|
149
|
+
|
|
150
|
+
Executes raw SQL queries. Supports both named parameters (`@paramName`) and positional arrays.
|
|
151
|
+
|
|
152
|
+
```js
|
|
153
|
+
// Simple query
|
|
154
|
+
const result = await Query('SELECT * FROM users WHERE active = true');
|
|
155
|
+
// { status: 200, message: 'Success', data: [...] }
|
|
156
|
+
|
|
157
|
+
// With named parameters
|
|
158
|
+
const user = await Query(
|
|
159
|
+
'SELECT * FROM users WHERE id = @userId AND role = @role',
|
|
160
|
+
{ userId: 1, role: 'admin' }
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
// With positional parameters ($1, $2...)
|
|
164
|
+
const result = await Query(
|
|
165
|
+
'SELECT * FROM users WHERE score > $1 AND age < $2',
|
|
166
|
+
[100, 30]
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
// INSERT/UPDATE/DELETE
|
|
170
|
+
await Query("INSERT INTO users (name) VALUES (@name)", { name: 'Alice' });
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### `Procedure(name?)`
|
|
174
|
+
|
|
175
|
+
Executes and manages PostgreSQL stored functions.
|
|
176
|
+
|
|
177
|
+
```js
|
|
178
|
+
// Execute a stored function
|
|
179
|
+
const result = await Procedure('calculate_total').Execute({
|
|
180
|
+
amount: 100,
|
|
181
|
+
tax_rate: 0.18
|
|
182
|
+
});
|
|
183
|
+
// { status: 200, message: 'Success', data: [...] }
|
|
184
|
+
|
|
185
|
+
// Get procedure info
|
|
186
|
+
const info = await Procedure('calculate_total').Info();
|
|
187
|
+
// { name: 'calculate_total', type: 'Function', date: null }
|
|
188
|
+
|
|
189
|
+
// Get all functions
|
|
190
|
+
const allFuncs = await Procedure().AllInfo();
|
|
191
|
+
|
|
192
|
+
// Get output schema (returns empty for now)
|
|
193
|
+
const schema = await Procedure('calculate_total').SimpleOutput();
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### `Types`
|
|
197
|
+
|
|
198
|
+
SQL type builders for creating table schemas.
|
|
199
|
+
|
|
200
|
+
```js
|
|
201
|
+
Types.int() // INT
|
|
202
|
+
Types.bigint() // BIGINT
|
|
203
|
+
Types.smallint() // SMALLINT
|
|
204
|
+
Types.varchar(255) // VARCHAR(255)
|
|
205
|
+
Types.text() // TEXT
|
|
206
|
+
Types.bit() // BOOLEAN
|
|
207
|
+
Types.float() // FLOAT
|
|
208
|
+
Types.decimal(10, 2) // DECIMAL(10, 2)
|
|
209
|
+
Types.date() // DATE
|
|
210
|
+
Types.time() // TIME
|
|
211
|
+
Types.datetime() // TIMESTAMP
|
|
212
|
+
Types.char() // CHAR
|
|
213
|
+
Types.uuid() // UUID (uniqueidentifier)
|
|
214
|
+
Types.money() // NUMERIC(19,4)
|
|
215
|
+
Types.image() // BYTEA
|
|
216
|
+
Types.xml() // XML
|
|
217
|
+
|
|
218
|
+
// Column options
|
|
219
|
+
Types.options.notNull() // NOT NULL
|
|
220
|
+
Types.options.primaryKey() // PRIMARY KEY
|
|
221
|
+
Types.options.unique() // UNIQUE
|
|
222
|
+
Types.options.autoIncrement() // GENERATED ALWAYS AS IDENTITY
|
|
223
|
+
Types.options.default(42) // DEFAULT 42
|
|
224
|
+
Types.options.check('age > 0') // CHECK(age > 0)
|
|
225
|
+
Types.options.foreignKey('users', 'id') // REFERENCES users(id)
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### `Config`
|
|
229
|
+
|
|
230
|
+
```js
|
|
231
|
+
Config.logingMode(true); // Enable query logging
|
|
232
|
+
Config.logingMode(false); // Disable query logging
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
## Error Handling
|
|
236
|
+
|
|
237
|
+
All operations return a consistent result format:
|
|
238
|
+
|
|
239
|
+
```js
|
|
240
|
+
// Success
|
|
241
|
+
{ status: 200, message: 'Success', data: [...] }
|
|
242
|
+
|
|
243
|
+
// Error
|
|
244
|
+
{ status: 500, message: 'Error message', data: null }
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
## Complete Example
|
|
248
|
+
|
|
249
|
+
```js
|
|
250
|
+
const { Connect, Table, Query, Types, Config } = require('easy-postgresql');
|
|
251
|
+
|
|
252
|
+
Connect({
|
|
253
|
+
user: 'postgres',
|
|
254
|
+
password: 'postgres',
|
|
255
|
+
host: 'localhost',
|
|
256
|
+
port: 5432,
|
|
257
|
+
database: 'test'
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
Config.logingMode(true);
|
|
261
|
+
|
|
262
|
+
const schema = {
|
|
263
|
+
id: Types.int() + Types.options.primaryKey() + Types.options.autoIncrement(),
|
|
264
|
+
name: Types.varchar(100) + Types.options.notNull(),
|
|
265
|
+
email: Types.varchar(100) + Types.options.unique(),
|
|
266
|
+
age: Types.int(),
|
|
267
|
+
created_at: Types.datetime() + ' DEFAULT NOW()'
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
const users = Table('users');
|
|
271
|
+
|
|
272
|
+
// Create table
|
|
273
|
+
if (!(await users.functions.isThere())) {
|
|
274
|
+
await users.functions.create(schema);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Insert
|
|
278
|
+
await users.createOne({ name: 'John Doe', email: 'john@test.com', age: 30 });
|
|
279
|
+
await users.createOne({ name: 'Jane Doe', email: 'jane@test.com', age: 25 });
|
|
280
|
+
|
|
281
|
+
// Query
|
|
282
|
+
const allUsers = await users.find();
|
|
283
|
+
const john = await users.findOne({ email: 'john@test.com' });
|
|
284
|
+
const adults = await users.find({}, { likes: { name: 'J%' } });
|
|
285
|
+
|
|
286
|
+
// Update
|
|
287
|
+
await users.updateOne({ id: john.id }, { age: 31 });
|
|
288
|
+
|
|
289
|
+
// Delete
|
|
290
|
+
await users.deleteOne({ id: john.id });
|
|
291
|
+
|
|
292
|
+
// Raw query
|
|
293
|
+
const stats = await Query(
|
|
294
|
+
'SELECT AVG(age)::numeric(10,1) AS avg_age, COUNT(*) AS total FROM users'
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
// Clean up
|
|
298
|
+
await users.functions.remove();
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
## License
|
|
302
|
+
|
|
303
|
+
MIT
|
|
42
304
|
|
|
43
|
-
|
|
305
|
+
## Feedback
|
|
44
306
|
|
|
45
307
|
**E-mail:** me@cihatksm.com
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "easy-postgresql",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "This is a simple and easy to use postgresql library for node.js",
|
|
5
5
|
"main": "dist/database.js",
|
|
6
6
|
"types": "dist/database.d.ts",
|
|
@@ -42,14 +42,14 @@
|
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"colors": "^1.4.0",
|
|
45
|
-
"pg": "^8.
|
|
46
|
-
"update-modules": "^0.3.
|
|
45
|
+
"pg": "^8.22.0",
|
|
46
|
+
"update-modules": "^0.3.4"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@types/pg": "^8.16.0",
|
|
50
49
|
"@types/node": "^26.1.1",
|
|
50
|
+
"@types/pg": "^8.20.0",
|
|
51
51
|
"nodemon": "^3.1.14",
|
|
52
52
|
"ts-node": "^10.9.2",
|
|
53
53
|
"typescript": "^7.0.2"
|
|
54
54
|
}
|
|
55
|
-
}
|
|
55
|
+
}
|