pogi 2.11.0
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 +35 -0
- package/CHANGELOG.md +277 -0
- package/LICENSE +21 -0
- package/README.md +85 -0
- package/docs/API/PgDb.md +218 -0
- package/docs/API/PgSchema.md +91 -0
- package/docs/API/PgTable.md +365 -0
- package/docs/API/QueryOptions.md +77 -0
- package/docs/API/condition.md +133 -0
- package/docs/connection.md +91 -0
- package/docs/css/docs.css +164 -0
- package/docs/executingSqlFile.md +44 -0
- package/docs/faq.md +15 -0
- package/docs/functions.md +19 -0
- package/docs/generatingInterfaceForTables.md +35 -0
- package/docs/index.md +48 -0
- package/docs/logger.md +40 -0
- package/docs/mappingDatabaseTypes.md +89 -0
- package/docs/notification.md +19 -0
- package/docs/pitfalls.md +73 -0
- package/docs/streams.md +68 -0
- package/docs/transaction.md +65 -0
- package/lib/bin/generateInterface.d.ts +1 -0
- package/lib/bin/generateInterface.js +53 -0
- package/lib/bin/generateInterface.js.map +1 -0
- package/lib/connectionOptions.d.ts +25 -0
- package/lib/connectionOptions.js +3 -0
- package/lib/connectionOptions.js.map +1 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +10 -0
- package/lib/index.js.map +1 -0
- package/lib/pgConverters.d.ts +10 -0
- package/lib/pgConverters.js +66 -0
- package/lib/pgConverters.js.map +1 -0
- package/lib/pgDb.d.ts +86 -0
- package/lib/pgDb.js +745 -0
- package/lib/pgDb.js.map +1 -0
- package/lib/pgDbLogger.d.ts +5 -0
- package/lib/pgDbLogger.js +3 -0
- package/lib/pgDbLogger.js.map +1 -0
- package/lib/pgDbOperators.d.ts +113 -0
- package/lib/pgDbOperators.js +44 -0
- package/lib/pgDbOperators.js.map +1 -0
- package/lib/pgSchema.d.ts +16 -0
- package/lib/pgSchema.js +16 -0
- package/lib/pgSchema.js.map +1 -0
- package/lib/pgTable.d.ts +131 -0
- package/lib/pgTable.js +322 -0
- package/lib/pgTable.js.map +1 -0
- package/lib/pgUtils.d.ts +31 -0
- package/lib/pgUtils.js +157 -0
- package/lib/pgUtils.js.map +1 -0
- package/lib/queryAble.d.ts +76 -0
- package/lib/queryAble.js +330 -0
- package/lib/queryAble.js.map +1 -0
- package/lib/queryWhere.d.ts +8 -0
- package/lib/queryWhere.js +249 -0
- package/lib/queryWhere.js.map +1 -0
- package/mkdocs.yml +25 -0
- package/package.json +65 -0
- package/spec/resources/init.sql +122 -0
- package/spec/resources/throw_exception.sql +5 -0
- package/spec/resources/tricky.sql +13 -0
- package/spec/run.js +5 -0
- package/spec/support/jasmine.json +9 -0
- package/src/bin/generateInterface.ts +54 -0
- package/src/connectionOptions.ts +42 -0
- package/src/index.ts +6 -0
- package/src/pgConverters.ts +55 -0
- package/src/pgDb.ts +820 -0
- package/src/pgDbLogger.ts +13 -0
- package/src/pgDbOperators.ts +62 -0
- package/src/pgSchema.ts +15 -0
- package/src/pgTable.ts +401 -0
- package/src/pgUtils.ts +176 -0
- package/src/queryAble.ts +393 -0
- package/src/queryWhere.ts +326 -0
- package/src/test/pgDbOperatorSpec.ts +492 -0
- package/src/test/pgDbSpec.ts +1339 -0
- package/src/test/pgServiceRestartTest.ts +1500 -0
- package/src/tsconfig.json +33 -0
- package/utils_sql/lower.sql +4 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
For all the examples below
|
|
2
|
+
```js
|
|
3
|
+
import {PgDb, PgSchema} from "pogi";
|
|
4
|
+
|
|
5
|
+
let pgdb:PgDb = PgDb.connect(..);
|
|
6
|
+
let schema:PgSchema = pgdb.schemas['test1'];
|
|
7
|
+
```
|
|
8
|
+
#Properties
|
|
9
|
+
## db
|
|
10
|
+
<span class="def">db:</span><span class="type">PgDb</span>
|
|
11
|
+
Back reference to the db instance
|
|
12
|
+
## tables
|
|
13
|
+
<span class="def">tables:</span><span class="type">{[name:string]:PgTable}</span>
|
|
14
|
+
Tables, also merged to schema object.
|
|
15
|
+
## fn
|
|
16
|
+
<span class="def">fn:</span><span class="type">{[name:string]:Function}</span>
|
|
17
|
+
Stored procedures and functions
|
|
18
|
+
|
|
19
|
+
#Functions
|
|
20
|
+
|
|
21
|
+
##toString
|
|
22
|
+
<span class="def"><span class="func">toString</span>()</span>
|
|
23
|
+
|
|
24
|
+
returns the name of the schema
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
##setLogger
|
|
28
|
+
<span class="def"><span class="func">setLogger</span>(logger:<span class="type">PgDbLogger</span>) </span>
|
|
29
|
+
|
|
30
|
+
Note: inherited.
|
|
31
|
+
|
|
32
|
+
Sets the fallback logger for all queries (if no schema, table or query level logger is set, this will be used).
|
|
33
|
+
```js
|
|
34
|
+
pgdb.setLogger(console);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
#Functions - async
|
|
38
|
+
##query
|
|
39
|
+
<span class="def"><span class="func">query</span>(sql:<span class="type">string</span>, params?:<span class="type">any[]|{}</span>, options?:<a href="../QueryOptions"><span class="type">SqlQueryOptions</span></a>):Promise<<span class="type">any[]</span>></span>
|
|
40
|
+
|
|
41
|
+
<a name="query"></a>
|
|
42
|
+
Note: inherited, uses schema level log if present (if not then the db level log).
|
|
43
|
+
|
|
44
|
+
Executes an arbitrary sql string with parameters / named parameters;
|
|
45
|
+
```js
|
|
46
|
+
let res1 = await schema.query('SELECT MAX(point) from game1.scores WHERE name=$1 ', ['player1']);
|
|
47
|
+
let res2 = await schema.query('SELECT MAX(point) from !:schema.scores WHERE name=:name ', {schema:'game1', name:'player1'});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
## queryOneField
|
|
52
|
+
<span class="def"><span class="func">queryOneField</span>(sql:<span class="type">string</span>, params?:<span class="type">any[]|{}</span>, options?:<a href="../QueryOptions"><span class="type">SqlQueryOptions</span></a>):Promise<<span class="type">any</span>></span>
|
|
53
|
+
|
|
54
|
+
Note: inherited, uses schema level log if present (if not then the db level log).
|
|
55
|
+
|
|
56
|
+
If there is only one record and one field that we are interested in. For the params usage see [query](#query).
|
|
57
|
+
```js
|
|
58
|
+
let winner = await schema.getOneField(`SELECT 'The winner is ' || name FROM test1.users LIMIT 1`);
|
|
59
|
+
console.log(winner); //The winner is Admin
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
## queryOneColumn
|
|
64
|
+
<span class="def"><span class="func">queryOneColumn</span>(sql:<span class="type">string</span>, params?:<span class="type">any[]|{}</span>, options?:<a href="../QueryOptions"><span class="type">SqlQueryOptions</span></a>):Promise<<span class="type">any[]</span>></span>
|
|
65
|
+
|
|
66
|
+
Note: inherited, uses schema level log if present (if not then the db level log).
|
|
67
|
+
|
|
68
|
+
If there is only one column that we are interested in. For the params usage see [query](#query).
|
|
69
|
+
```js
|
|
70
|
+
let userList = await schema.getOneColumn('SELECT name FROM test1.users');
|
|
71
|
+
console.dir(userList); //['Admin', 'User1', 'User2']
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
## queryAsStream
|
|
76
|
+
<span class="def"><span class="func">queryAsStream</span>(sql:<span class="type">string</span>, params?:<span class="type">any[]|{}</span>, options?:<a href="../QueryOptions"><span class="type">SqlQueryOptions</span></a>):Promise<<span class="type">any[]</span>></span>
|
|
77
|
+
|
|
78
|
+
see [streams](/streams)
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
##run
|
|
82
|
+
<span class="def"><span class="func">run</span>(sql:<span class="type">string</span>):Promise<<span class="type">any[]</span>></span>
|
|
83
|
+
|
|
84
|
+
Executes an arbitrary sql string.
|
|
85
|
+
Note: inherited, uses schema level log if present (if not then the db level log).
|
|
86
|
+
|
|
87
|
+
Executes an arbitrary sql string;
|
|
88
|
+
```js
|
|
89
|
+
await schema.run('CREATE schema myschema');
|
|
90
|
+
```
|
|
91
|
+
|
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
For all the examples below
|
|
2
|
+
|
|
3
|
+
```js
|
|
4
|
+
|
|
5
|
+
import {PgDb, PgSchema, PgTable} from "pogi";
|
|
6
|
+
|
|
7
|
+
let pgdb:PgDb = PgDb.connect(..);
|
|
8
|
+
let table:PgTable<User> = pgdb.schemas.test1.users;
|
|
9
|
+
|
|
10
|
+
export interface InsertOption {
|
|
11
|
+
logger?: PgDbLogger;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface UpdateDeleteOption {
|
|
15
|
+
logger?: PgDbLogger;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface TruncateOptions{
|
|
19
|
+
restartIdentity?: boolean,
|
|
20
|
+
cascade?: boolean,
|
|
21
|
+
logger?: PgDbLogger;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface Return {
|
|
25
|
+
return?:string[]|'*';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface Stream {
|
|
29
|
+
stream: true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
#Properties
|
|
35
|
+
## db
|
|
36
|
+
<span class="def">db:</span><span class="type">PgDb</span>
|
|
37
|
+
Back reference to the db instance
|
|
38
|
+
|
|
39
|
+
#Functions
|
|
40
|
+
##toString
|
|
41
|
+
<span class="def"><span class="func">toString</span>()</span>
|
|
42
|
+
|
|
43
|
+
Returns the fully qualified name of the table
|
|
44
|
+
|
|
45
|
+
##setLogger
|
|
46
|
+
<span class="def"><span class="func">setLogger</span>(logger:<span class="type">PgDbLogger</span>) </span>
|
|
47
|
+
|
|
48
|
+
Note: inherited.
|
|
49
|
+
|
|
50
|
+
Sets the logger per table (not used if the query has logger specified).
|
|
51
|
+
|
|
52
|
+
#Functions - async
|
|
53
|
+
|
|
54
|
+
## count
|
|
55
|
+
<span class="def"><span class="func">count</span>(conditions?:<span class="type">{}</span>):Promise<<span class="type">number</span>><span class="type">
|
|
56
|
+
|
|
57
|
+
Run a count query
|
|
58
|
+
```js
|
|
59
|
+
let count = await table.count({id:2});
|
|
60
|
+
console.log(count); //most probably 1
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
## delete
|
|
65
|
+
<span class="def"><span class="func">delete</span>(conditions:<span class="type">{}</span>, options?:<a href="../QueryOptions"><span class="type">UpdateDeleteOption</span></a>):Promise<<span class="type">number</span>></span>
|
|
66
|
+
|
|
67
|
+
Executes a delete-where query.
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
let numberOfRowsDeleted = await table.delete({id:[1,2,3]});
|
|
71
|
+
if (numberOfRowsDeleted!=3) {
|
|
72
|
+
//alarm!!
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
## deleteOne
|
|
78
|
+
<span class="def"><span class="func">deleteOne</span>(conditions:<span class="type">{}</span>, options?:<a href="../QueryOptions"><span class="type">UpdateDeleteOption</span></a>):Promise<<span class="type">number</span>></span>
|
|
79
|
+
|
|
80
|
+
Executes a delete-where query, but throws exception if more then one record is deleted;
|
|
81
|
+
```js
|
|
82
|
+
let numberOfDeleted = await table.deleteOne({id:[1,2,3]}); //throws exception if more then one record is deleted
|
|
83
|
+
console.log(numberOfDeleted); //0 or 1
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
## deleteAndGet
|
|
88
|
+
<span class="def"><span class="func">deleteAndGet</span>(conditions:<span class="type">{}</span>, options?:<a href="../QueryOptions"><span class="type">UpdateDeleteOption & Return</span></a>):Promise<<span class="type">T[]</span>></span>
|
|
89
|
+
|
|
90
|
+
Executes a delete-where query and returns with the deleted records;
|
|
91
|
+
```js
|
|
92
|
+
let playersDeleted = await table.deleteAndGet({id:[1,2,3]});
|
|
93
|
+
for (let player of playersDeleted) {
|
|
94
|
+
console.log(player.id); //1 then 2 then 3
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
## deleteAndGetOne
|
|
100
|
+
<span class="def"><span class="func">deleteAndGetOne</span>(conditions:<span class="type">{}</span>, options?:<a href="../QueryOptions"><span class="type">UpdateDeleteOption & Return</span></a>):Promise<<span class="type">T</span>></span>
|
|
101
|
+
|
|
102
|
+
Executes a delete-where query, but throws exception if more then one record is deleted;
|
|
103
|
+
Returns with the deleted record if any.
|
|
104
|
+
```js
|
|
105
|
+
let playerDeleted = await table.deleteAndGet({id:[1,2,3]}); //throws exception if more then one record is deleted
|
|
106
|
+
console.log(player.id); //Either 1, 2, 3 or null if no record is deleted
|
|
107
|
+
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
## find
|
|
112
|
+
<span class="def"><span class="func">find</span>(conditions:<span class="type">{}</span>, options?:<a href="../QueryOptions"><span class="type">QueryOptions</span></a>):Promise<<span class="type">T[]</span>></span>
|
|
113
|
+
|
|
114
|
+
<span class="def"><span class="func">find</span>(conditions:<span class="type">{}</span>, options?:<a href="../QueryOptions"><span class="type">QueryOptions & Stream</span></a>):Promise<<span class="type">ReadableStream</span>></span>
|
|
115
|
+
|
|
116
|
+
Executes a select-where query.
|
|
117
|
+
```js
|
|
118
|
+
|
|
119
|
+
let playerList = await table.find({id:[1,2,3]});
|
|
120
|
+
for (let player of playerList) {
|
|
121
|
+
console.log(player.id); //1..2..3
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
playerList = await table.find({id:[1,2,3]}, {fields:['id', 'name'], limit:3});
|
|
125
|
+
|
|
126
|
+
```
|
|
127
|
+
for more options for [conditions](condition) and [queryOptions](QueryOptions) see those sections.
|
|
128
|
+
|
|
129
|
+
If the option has `{stream:true}` parameter it returns a stream instead of an array.
|
|
130
|
+
See [streams](/streams) for example.
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
## findWhere
|
|
135
|
+
<span class="def"><span class="func">findWhere</span>(where:<span class="type">string</span>,params:<span class="type">any[]|{}</span>,options?:<a href="../QueryOptions"><span class="type">QueryOptions</span></a>):Promise<<span class="type">ReadableStream</span>></span>
|
|
136
|
+
|
|
137
|
+
<span class="def"><span class="func">findWhere</span>(where:<span class="type">string</span>,params:<span class="type">any[]|{}</span>,options?:<a href="../QueryOptions"><span class="type">QueryOptions & Stream</span></a>):Promise<<span class="type">ReadableStream</span>></span>
|
|
138
|
+
|
|
139
|
+
Executes a select-where query with free text where etc.
|
|
140
|
+
```js
|
|
141
|
+
|
|
142
|
+
let res;
|
|
143
|
+
|
|
144
|
+
res = await table.where("permissions @> {'admin'} AND name!=username AND id=$1 LIMIT 2", [1]);
|
|
145
|
+
|
|
146
|
+
res = await table.where("permissions @> {'admin'} AND name!=username AND id=:id LIMIT 2", {id:1});
|
|
147
|
+
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
If the option has `{stream:true}` parameter it returns a stream instead of an array.
|
|
151
|
+
See [streams](/streams) for example.
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
## findAll
|
|
156
|
+
<span class="def"><span class="func">findAll</span>(options?:<a href="../QueryOptions"><span class="type">QueryOptions</span></a>):Promise<<span class="type">T[]</span>></span>
|
|
157
|
+
|
|
158
|
+
<span class="def"><span class="func">findAll</span>(options?:<a href="../QueryOptions"><span class="type">QueryOptions & Stream</span></a>):Promise<<span class="type">ReadableStream</span>></span>
|
|
159
|
+
|
|
160
|
+
Returns everything from the table. Same as table.find({})
|
|
161
|
+
```js
|
|
162
|
+
let res = await table.findAll();
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
If the option has `{stream:true}` parameter it returns a stream instead of an array.
|
|
166
|
+
See [streams](/streams) for example.
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
## findOne
|
|
172
|
+
<span class="def"><span class="func">findOne</span>(conditions, options?:<a href="../QueryOptions"><span class="type">QueryOptions</span></a>):Promise<<span class="type">T</span>></span>
|
|
173
|
+
|
|
174
|
+
Most system get this wrong, as they use it as "_findFirst_" instead of using as "_findOnly_".
|
|
175
|
+
While 99% of the time the programmer means the latter, by default they use the formal.
|
|
176
|
+
That is mostly just hiding bugs instead of revealing issues as soon as possible.
|
|
177
|
+
It's hard to count how much time it saved me to find an issue, not to mention that it found earlier
|
|
178
|
+
then otherwise would find out. Very good investment for a small bit of _Defensive programming_.
|
|
179
|
+
|
|
180
|
+
Therefore it **throws exception** if more then one record match the select query.
|
|
181
|
+
```js
|
|
182
|
+
let res1 = await table.findOne({id:1});
|
|
183
|
+
let res2 = await table.findOne({'name like': 'A%'}); //most probably throws an exception
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
---
|
|
187
|
+
## findFirst
|
|
188
|
+
<span class="def"><span class="func">findFirst</span>(conditions, options?:<a href="../QueryOptions"><span class="type">QueryOptions</span></a>):Promise<<span class="type">T</span>></span>
|
|
189
|
+
|
|
190
|
+
Same as await table.find(condition, {limit:1})
|
|
191
|
+
```js
|
|
192
|
+
let somebody = await table.findFirst({'score >':9000});
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
## findOneFieldOnly
|
|
197
|
+
<span class="def"><span class="func">findOneFieldOnly</span>(conditions:<span class="type">{}</span>, field:<span class="type">string</span>, options?:<a href="../QueryOptions"><span class="type">QueryOptions</span></a>):Promise<<span class="type">any</span>></span>
|
|
198
|
+
|
|
199
|
+
Returns directly the value of a column/field directly.
|
|
200
|
+
|
|
201
|
+
```js
|
|
202
|
+
let nameOfUser = await table.findOneFieldOnly({id:1}, 'name');
|
|
203
|
+
console.log(nameOfUser); //most probably 'Admin'
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
---
|
|
207
|
+
## insert
|
|
208
|
+
<span class="def"><span class="func">insert</span>(records:<span class="type">T</span>, options:<a href="../QueryOptions"><span class="type">InsertOption</span></a>): Promise<<span class="type">T</span>></span>
|
|
209
|
+
|
|
210
|
+
<span class="def"><span class="func">insert</span>(records:<span class="type">T[]</span>, options:<a href="../QueryOptions"><span class="type">InsertOption</span></a>): Promise<<span class="type">T[]</span>></span>
|
|
211
|
+
|
|
212
|
+
You can insert one or multiple records, by default the new record(s) will be returned. This can be prevented if not needed;
|
|
213
|
+
```js
|
|
214
|
+
let user = await table.insert({username:'anonymous'}); //returns the whole record
|
|
215
|
+
console.log(user.id); // generated by postgresql
|
|
216
|
+
//or
|
|
217
|
+
let userList = await table.insert([{username:'anonymous'},{username:'anonymous2'}], {return:['id']});
|
|
218
|
+
console.log(userList[0].id); // generated by postgresql
|
|
219
|
+
|
|
220
|
+
await table.insert({username:'anonymous2'}, {return:[]}); //returns [{}]
|
|
221
|
+
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
---
|
|
225
|
+
## insertAndGet
|
|
226
|
+
<span class="def"><span class="func">insert</span>(records:<span class="type">T</span>, options:<span class="type">InsertOption & Return</span>): Promise<<span class="type">T</span>></span>
|
|
227
|
+
|
|
228
|
+
<span class="def"><span class="func">insert</span>(records:<span class="type">T[]</span>, options:<span class="type">InsertOption & Return</span>): Promise<<span class="type">T[]</span>></span>
|
|
229
|
+
|
|
230
|
+
You can insert one or multiple records, by default the new record(s) will be returned. This can be prevented if not needed;
|
|
231
|
+
```js
|
|
232
|
+
let user = await table.insert({username:'anonymous'}); //returns the whole record
|
|
233
|
+
console.log(user.id); // generated by postgresql
|
|
234
|
+
//or
|
|
235
|
+
let userList = await table.insert([{username:'anonymous'},{username:'anonymous2'}], {return:['id']});
|
|
236
|
+
console.log(userList[0].id); // generated by postgresql
|
|
237
|
+
|
|
238
|
+
await table.insert({username:'anonymous2'}, {return:[]}); //returns [{}]
|
|
239
|
+
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
---
|
|
243
|
+
## update
|
|
244
|
+
<span class="def"><span class="func">update</span>(conditions:<span class="type">{}</span>, fields:<span class="type">{}</span>, options?:<a href="../QueryOptions"><span class="type">UpdateDeleteOption</span></a>):Promise<<span class="type">number</span>></span>
|
|
245
|
+
|
|
246
|
+
Run an update query on the table, returns the number of records changed.
|
|
247
|
+
```js
|
|
248
|
+
await table.update({},{score:null}); //all record is updated
|
|
249
|
+
await table.update({'name ~': '^G'}, {numOfLifes:4}); //all record where name starts with G has the numOfLifes set to 4. It's a G'day!
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
---
|
|
253
|
+
## updateOne
|
|
254
|
+
<span class="def"><span class="func">updateOne</span>(conditions:<span class="type">{}</span>, fields:<span class="type">{}</span>, options?:<a href="../QueryOptions"><span class="type">UpdateDeleteOption</span></a>): Promise<<span class="type">number</span>></span>
|
|
255
|
+
|
|
256
|
+
Run an update query, throws exception if more then one record has been updated. (Handy if you roll back on exception)
|
|
257
|
+
```js
|
|
258
|
+
await table.updateOne({id:1},{password:null});
|
|
259
|
+
await table.updateOne({notUniqId:1},{password:null}); //throws exception if more then 1 rec has been updated;
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
---
|
|
263
|
+
## updateAndGet
|
|
264
|
+
<span class="def"><span class="func">updateAndGet</span>(conditions:<span class="type">{}</span>, fields:<span class="type">{}</span>, options?:<a href="../QueryOptions"><span class="type">UpdateDeleteOption & Return</span></a>):Promise<<span class="type">T[]</span>></span>
|
|
265
|
+
|
|
266
|
+
Run an update query on the table
|
|
267
|
+
```js
|
|
268
|
+
let playerList = await table.updateAndGet({'score >': '9000'}, {achivement:"It's Over 9000!"});
|
|
269
|
+
//update the achievement fields for all players where the score is over 9000 then returns the updated list
|
|
270
|
+
|
|
271
|
+
let playerIdList = await table.updateAndGet({'score >': '9000'}, {achivement:"It's Over 9000!"}, {return:['id']});
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
## updateAndGetOne
|
|
276
|
+
<span class="def"><span class="func">updateAndGetOne</span>(conditions:<span class="type">{}</span>, fields:<span class="type">{}</span>, options?:<a href="../QueryOptions"><span class="type">UpdateDeleteOption & Return</span></a>): Promise<<span class="type">T</span>></span>
|
|
277
|
+
|
|
278
|
+
Run an update query and returns with the updated record,
|
|
279
|
+
throws exception if more then one record has been updated. (Handy if you roll back on exception)
|
|
280
|
+
```js
|
|
281
|
+
let user = await table.updateOne({id:1},{password:null});
|
|
282
|
+
console.log(user.name); //the whole record is returned
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
---
|
|
287
|
+
##query
|
|
288
|
+
<span class="def"><span class="func">query</span>(sql:<span class="type">string</span>, params?:<span class="type">any[]|{}</span>, options?:<a href="../QueryOptions"><span class="type">SqlQueryOptions</span></a>):Promise<<span class="type">any[]</span>></span>
|
|
289
|
+
|
|
290
|
+
<a name="query"></a>
|
|
291
|
+
Note: inherited, uses table level log if present (if not then schema, then db).
|
|
292
|
+
|
|
293
|
+
Executes an arbitrary sql string with parameters / named parameters;
|
|
294
|
+
```js
|
|
295
|
+
|
|
296
|
+
let res1 = await table.query('SELECT MAX(point) from game1.scores WHERE name=$1 ', ['player1']);
|
|
297
|
+
let res2 = await table.query('SELECT MAX(point) from !:schema.scores WHERE name=:name ', {schema:'game1', name:'player1'});
|
|
298
|
+
|
|
299
|
+
```
|
|
300
|
+
---
|
|
301
|
+
## queryOneField
|
|
302
|
+
<span class="def"><span class="func">queryOneField</span>(sql:<span class="type">string</span>, params?:<span class="type">any[]|{}</span>, options?:<a href="../QueryOptions"><span class="type">SqlQueryOptions</span></a>):Promise<<span class="type">any</span>></span>
|
|
303
|
+
|
|
304
|
+
Note: inherited, uses table level log if present (if not then schema, then db).
|
|
305
|
+
|
|
306
|
+
If there is only one record and one field that we are interested in. For the params usage see [query](#query).
|
|
307
|
+
```js
|
|
308
|
+
|
|
309
|
+
let winner = await table.getOneField(`SELECT 'The winner is ' || name FROM ${table} LIMIT 1`);
|
|
310
|
+
console.log(winner); //The winner is Admin
|
|
311
|
+
|
|
312
|
+
```
|
|
313
|
+
---
|
|
314
|
+
## queryOneColumn
|
|
315
|
+
<span class="def"><span class="func">queryOneColumn</span>(sql:<span class="type">string</span>, params?:<span class="type">any[]|{}</span>, options?:<a href="../QueryOptions"><span class="type">SqlQueryOptions</span></a>):Promise<<span class="type">any[]</span>></span>
|
|
316
|
+
|
|
317
|
+
Note: inherited, uses table level log if present (if not then schema, then db).
|
|
318
|
+
|
|
319
|
+
If there is only one column that we are interested in. For the params usage see [query](#query).
|
|
320
|
+
```js
|
|
321
|
+
|
|
322
|
+
let userList = await table.getOneColumn(`SELECT name FROM ${table}`);
|
|
323
|
+
console.dir(userList); //['Admin', 'User1', 'User2']
|
|
324
|
+
|
|
325
|
+
```
|
|
326
|
+
---
|
|
327
|
+
## queryAsStream
|
|
328
|
+
<span class="def"><span class="func">queryAsStream</span>(sql:<span class="type">string</span>, params?:<span class="type">any[]|{}</span>, options?:<a href="../QueryOptions"><span class="type">SqlQueryOptions</span></a>):Promise<<span class="type">any[]</span>></span>
|
|
329
|
+
|
|
330
|
+
see [streams](/streams)
|
|
331
|
+
|
|
332
|
+
---
|
|
333
|
+
##run
|
|
334
|
+
<span class="def"><span class="func">run</span>(sql:<span class="type">string</span>):Promise<<span class="type">any[]</span>></span>
|
|
335
|
+
|
|
336
|
+
Note: inherited, uses table level log if present (if not then schema, then db).
|
|
337
|
+
|
|
338
|
+
Executes an arbitrary sql string;
|
|
339
|
+
```js
|
|
340
|
+
|
|
341
|
+
await table.run('CREATE schema myschema');
|
|
342
|
+
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
---
|
|
346
|
+
## truncate
|
|
347
|
+
<span class="def"><span class="func">truncate</span>(options?:<span class="type">TruncateOptions</span>):Promise<void></span>
|
|
348
|
+
|
|
349
|
+
Will truncate the table, see [PostgreSQL doc](https://www.postgresql.org/docs/current/static/sql-truncate.html).
|
|
350
|
+
|
|
351
|
+
```js
|
|
352
|
+
export interface TruncateOptions{
|
|
353
|
+
restartIdentity?: boolean,
|
|
354
|
+
cascade?: boolean,
|
|
355
|
+
logger?: PgDbLogger;
|
|
356
|
+
}
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|