db-crud-api 0.2.1 → 0.3.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/CHANGELOG.md +43 -37
- package/README.md +97 -96
- package/index.js +10 -10
- package/lib/api-batch.js +2 -2
- package/lib/api-execute.js +2 -2
- package/lib/api-factory.js +26 -26
- package/lib/api-full.js +95 -95
- package/lib/api-ro.js +37 -37
- package/lib/db-operations.js +413 -404
- package/lib/mssql.js +273 -276
- package/lib/mysql.js +278 -0
- package/lib/schema.js +3 -3
- package/package.json +22 -17
package/CHANGELOG.md
CHANGED
|
@@ -1,37 +1,43 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
## v0.1.0 (2022-12-08)
|
|
4
|
-
|
|
5
|
-
Initial release
|
|
6
|
-
## v0.1.1 (2023-01-01)
|
|
7
|
-
|
|
8
|
-
Bug fix:
|
|
9
|
-
- default server/database
|
|
10
|
-
## v0.1.4 (2024-02-24)
|
|
11
|
-
|
|
12
|
-
Bug fix:
|
|
13
|
-
- update mssql to 9.3.2
|
|
14
|
-
- update uuid to 9.0.1
|
|
15
|
-
## v0.1.5 (2024-03-03)
|
|
16
|
-
|
|
17
|
-
Bug fix:
|
|
18
|
-
- deleteById function crash
|
|
19
|
-
- new function return undefined
|
|
20
|
-
## v0.1.5 (2024-03-03)
|
|
21
|
-
## v0.1.6 (2024-03-03)
|
|
22
|
-
|
|
23
|
-
Bug fix:
|
|
24
|
-
- update function crash with date value
|
|
25
|
-
## v0.1.9 (2024-03-12)
|
|
26
|
-
|
|
27
|
-
Added:
|
|
28
|
-
- execute store procedure
|
|
29
|
-
## v0.2.0 (2024-04-02)
|
|
30
|
-
|
|
31
|
-
Changes:
|
|
32
|
-
- parameters position have been change for some api
|
|
33
|
-
Added:
|
|
34
|
-
- batch job that run multiple instructions
|
|
35
|
-
## v0.2.1 (2024-12-05)
|
|
36
|
-
|
|
37
|
-
Refresh dependency
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## v0.1.0 (2022-12-08)
|
|
4
|
+
|
|
5
|
+
Initial release
|
|
6
|
+
## v0.1.1 (2023-01-01)
|
|
7
|
+
|
|
8
|
+
Bug fix:
|
|
9
|
+
- default server/database
|
|
10
|
+
## v0.1.4 (2024-02-24)
|
|
11
|
+
|
|
12
|
+
Bug fix:
|
|
13
|
+
- update mssql to 9.3.2
|
|
14
|
+
- update uuid to 9.0.1
|
|
15
|
+
## v0.1.5 (2024-03-03)
|
|
16
|
+
|
|
17
|
+
Bug fix:
|
|
18
|
+
- deleteById function crash
|
|
19
|
+
- new function return undefined
|
|
20
|
+
## v0.1.5 (2024-03-03)
|
|
21
|
+
## v0.1.6 (2024-03-03)
|
|
22
|
+
|
|
23
|
+
Bug fix:
|
|
24
|
+
- update function crash with date value
|
|
25
|
+
## v0.1.9 (2024-03-12)
|
|
26
|
+
|
|
27
|
+
Added:
|
|
28
|
+
- execute store procedure
|
|
29
|
+
## v0.2.0 (2024-04-02)
|
|
30
|
+
|
|
31
|
+
Changes:
|
|
32
|
+
- parameters position have been change for some api
|
|
33
|
+
Added:
|
|
34
|
+
- batch job that run multiple instructions
|
|
35
|
+
## v0.2.1 (2024-12-05)
|
|
36
|
+
|
|
37
|
+
Refresh dependency
|
|
38
|
+
## v0.3.0 (2024-12-28)
|
|
39
|
+
|
|
40
|
+
Changes:
|
|
41
|
+
- execute api definition
|
|
42
|
+
Added:
|
|
43
|
+
- support for mySQL
|
package/README.md
CHANGED
|
@@ -1,97 +1,98 @@
|
|
|
1
|
-
# db-crud-api 💡
|
|
2
|
-
CRUD api for database tables.
|
|
3
|
-
|
|
4
|
-
## Installation
|
|
5
|
-
npm install db-crud-api
|
|
6
|
-
|
|
7
|
-
# Usage
|
|
8
|
-
```javascript
|
|
9
|
-
import dbCrudApi from "db-crud-api";
|
|
10
|
-
const apiFactory = dbCrudApi(mySchema);
|
|
11
|
-
|
|
12
|
-
const mySchema = {
|
|
13
|
-
servers: {
|
|
14
|
-
server1: {
|
|
15
|
-
realName: "server1.mydomain.com",
|
|
16
|
-
type: "ms-sql",
|
|
17
|
-
instance: "DEFAULT",
|
|
18
|
-
user: "User1",
|
|
19
|
-
password: "mypassword",
|
|
20
|
-
options: {
|
|
21
|
-
appName: "myAppName",
|
|
22
|
-
...
|
|
23
|
-
},
|
|
24
|
-
databases: {
|
|
25
|
-
db1: {
|
|
26
|
-
realName: "db_orders",
|
|
27
|
-
tables: {
|
|
28
|
-
table1: {
|
|
29
|
-
realName: "tb_orders_head",
|
|
30
|
-
idField: "Id",
|
|
31
|
-
autoId: true,
|
|
32
|
-
fields: {
|
|
33
|
-
Id: { realName: "OrderId", type: "uuid", lengh: undefined, canBeNull: false, description: "Unique Id", defaultValue: undefined },
|
|
34
|
-
CustomerId: { type: "string", lengh: undefined, canBeNull: false, description: "Customer Id", defaultValue: undefined },
|
|
35
|
-
...
|
|
36
|
-
}
|
|
37
|
-
},
|
|
38
|
-
table2: {
|
|
39
|
-
realName: "tb_orders_detail",
|
|
40
|
-
idField: "Id",
|
|
41
|
-
autoId: true,
|
|
42
|
-
fields: {
|
|
43
|
-
Id: { realName: "OrderId", type: "uuid", lengh: undefined, canBeNull: false, description: "Unique Id", defaultValue: undefined },
|
|
44
|
-
OrderId: { type: "uuid", lengh: undefined, canBeNull: false, description: "Order Id", defaultValue: undefined },
|
|
45
|
-
RowSeq: { type: "numeric", lengh: undefined, canBeNull: false, description: "Row sequence", defaultValue: undefined },
|
|
46
|
-
...
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
...
|
|
50
|
-
}
|
|
51
|
-
},
|
|
52
|
-
...
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
...
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
const apiOrder = apiFactory.newROApi("server1.db1.table1"); // Read Only api
|
|
60
|
-
const apiOrderDetail = apiFactory.newFullApi("table2"); // full CRUD api
|
|
61
|
-
const apiExecuteSP = apiFactory.newExecuteApi("my_storeproc
|
|
62
|
-
|
|
63
|
-
console.log(await apiOrder.getById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx"));
|
|
64
|
-
console.log(await apiOrder.getById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx", {get: {fields: ["Id", "OrderNumber", "OrderTotal"]}}));
|
|
65
|
-
console.log(await apiOrder.getByFilter({get: {filters: ["OrderType in ('P', 'A')", "and", "OrderDate > '2022-12-01'"]}})); // get filterd rows
|
|
66
|
-
console.log(await apiOrder.getByFilter()); // this get all rows
|
|
67
|
-
console.log(await apiOrderDetail.patchById({patch: {sets: {ItemRef: "2024-TX-0001", ItemDate: "2024-01-31"}}}, "xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx")); // change some fields
|
|
68
|
-
console.log(await apiOrderDetail.patchByFilter({patch: {sets: {LastUpdate: "2023-04-30"}, filters: ["OrderId: 'xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx'"]}})); // change by filter
|
|
69
|
-
console.log(await apiOrderDetail.deleteById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx")); // delete row by Id
|
|
70
|
-
console.log(await apiOrderDetail.deleteByFilter({delete: {filters: ["ItemType in ('X', 'W')", "and", "ItemDate > '2022-12-01'"]}})); // delete filterd rows
|
|
71
|
-
console.log(await apiExecuteSP.execute(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
1
|
+
# db-crud-api 💡
|
|
2
|
+
CRUD api for database tables.
|
|
3
|
+
|
|
4
|
+
## Installation
|
|
5
|
+
npm install db-crud-api
|
|
6
|
+
|
|
7
|
+
# Usage
|
|
8
|
+
```javascript
|
|
9
|
+
import dbCrudApi from "db-crud-api";
|
|
10
|
+
const apiFactory = dbCrudApi(mySchema);
|
|
11
|
+
|
|
12
|
+
const mySchema = {
|
|
13
|
+
servers: {
|
|
14
|
+
server1: {
|
|
15
|
+
realName: "server1.mydomain.com",
|
|
16
|
+
type: "ms-sql",
|
|
17
|
+
instance: "DEFAULT",
|
|
18
|
+
user: "User1",
|
|
19
|
+
password: "mypassword",
|
|
20
|
+
options: {
|
|
21
|
+
appName: "myAppName",
|
|
22
|
+
...
|
|
23
|
+
},
|
|
24
|
+
databases: {
|
|
25
|
+
db1: {
|
|
26
|
+
realName: "db_orders",
|
|
27
|
+
tables: {
|
|
28
|
+
table1: {
|
|
29
|
+
realName: "tb_orders_head",
|
|
30
|
+
idField: "Id",
|
|
31
|
+
autoId: true,
|
|
32
|
+
fields: {
|
|
33
|
+
Id: { realName: "OrderId", type: "uuid", lengh: undefined, canBeNull: false, description: "Unique Id", defaultValue: undefined },
|
|
34
|
+
CustomerId: { type: "string", lengh: undefined, canBeNull: false, description: "Customer Id", defaultValue: undefined },
|
|
35
|
+
...
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
table2: {
|
|
39
|
+
realName: "tb_orders_detail",
|
|
40
|
+
idField: "Id",
|
|
41
|
+
autoId: true,
|
|
42
|
+
fields: {
|
|
43
|
+
Id: { realName: "OrderId", type: "uuid", lengh: undefined, canBeNull: false, description: "Unique Id", defaultValue: undefined },
|
|
44
|
+
OrderId: { type: "uuid", lengh: undefined, canBeNull: false, description: "Order Id", defaultValue: undefined },
|
|
45
|
+
RowSeq: { type: "numeric", lengh: undefined, canBeNull: false, description: "Row sequence", defaultValue: undefined },
|
|
46
|
+
...
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
...
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
...
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
...
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const apiOrder = apiFactory.newROApi("server1.db1.table1"); // Read Only api
|
|
60
|
+
const apiOrderDetail = apiFactory.newFullApi("table2"); // full CRUD api
|
|
61
|
+
const apiExecuteSP = apiFactory.newExecuteApi("db1.my_storeproc"); // execute my_storeproc
|
|
62
|
+
|
|
63
|
+
console.log(await apiOrder.getById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx"));
|
|
64
|
+
console.log(await apiOrder.getById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx", {get: {fields: ["Id", "OrderNumber", "OrderTotal"]}}));
|
|
65
|
+
console.log(await apiOrder.getByFilter({get: {filters: ["OrderType in ('P', 'A')", "and", "OrderDate > '2022-12-01'"]}})); // get filterd rows
|
|
66
|
+
console.log(await apiOrder.getByFilter()); // this get all rows
|
|
67
|
+
console.log(await apiOrderDetail.patchById({patch: {sets: {ItemRef: "2024-TX-0001", ItemDate: "2024-01-31"}}}, "xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx")); // change some fields
|
|
68
|
+
console.log(await apiOrderDetail.patchByFilter({patch: {sets: {LastUpdate: "2023-04-30"}, filters: ["OrderId: 'xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx'"]}})); // change by filter
|
|
69
|
+
console.log(await apiOrderDetail.deleteById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx")); // delete row by Id
|
|
70
|
+
console.log(await apiOrderDetail.deleteByFilter({delete: {filters: ["ItemType in ('X', 'W')", "and", "ItemDate > '2022-12-01'"]}})); // delete filterd rows
|
|
71
|
+
console.log(await apiExecuteSP.execute()); // execute procedure with no argument
|
|
72
|
+
console.log(await apiExecuteSP.execute({execute: {arguments: "@Company, @OrderNumber", params: {Company: "XXX", OrderNumber: "12345"}}})); // execute procedure with arguments and params
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
License
|
|
76
|
+
-------
|
|
77
|
+
|
|
78
|
+
MIT License
|
|
79
|
+
|
|
80
|
+
Copyright (c) 2022
|
|
81
|
+
|
|
82
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
83
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
84
|
+
in the Software without restriction, including without limitation the rights
|
|
85
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
86
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
87
|
+
furnished to do so, subject to the following conditions:
|
|
88
|
+
|
|
89
|
+
The above copyright notice and this permission notice shall be included in all
|
|
90
|
+
copies or substantial portions of the Software.
|
|
91
|
+
|
|
92
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
93
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
94
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
95
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
96
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
97
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
97
98
|
SOFTWARE.
|
package/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
import schema from "./lib/schema.js";
|
|
4
|
-
import * as apiFactory from "./lib/api-factory.js";
|
|
5
|
-
|
|
6
|
-
export default function(customSchema) {
|
|
7
|
-
if (customSchema && customSchema.servers) {
|
|
8
|
-
schema.servers = customSchema.servers;
|
|
9
|
-
}
|
|
10
|
-
return apiFactory;
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import schema from "./lib/schema.js";
|
|
4
|
+
import * as apiFactory from "./lib/api-factory.js";
|
|
5
|
+
|
|
6
|
+
export default function(customSchema) {
|
|
7
|
+
if (customSchema && customSchema.servers) {
|
|
8
|
+
schema.servers = customSchema.servers;
|
|
9
|
+
}
|
|
10
|
+
return apiFactory;
|
|
11
11
|
}
|
package/lib/api-batch.js
CHANGED
|
@@ -51,8 +51,8 @@ export default class apiBatch {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
// execute
|
|
54
|
-
execute(
|
|
55
|
-
const _schema = dbOpe.prepareSchema(
|
|
54
|
+
execute(procPath, reqOpe) {
|
|
55
|
+
const _schema = dbOpe.prepareSchema(procPath, dbOpe.objectType.procedure);
|
|
56
56
|
const _connection = dbOpe.prepareConnection(_schema);
|
|
57
57
|
this.#dbOperations.push(dbOpe.prepareExecute(_schema, _connection, reqOpe));
|
|
58
58
|
this.#operations.push(reqOpe);
|
package/lib/api-execute.js
CHANGED
|
@@ -8,8 +8,8 @@ export default class apiExecute {
|
|
|
8
8
|
#schema;
|
|
9
9
|
#connection;
|
|
10
10
|
|
|
11
|
-
constructor(
|
|
12
|
-
this.#schema = dbOpe.prepareSchema(
|
|
11
|
+
constructor(obj) {
|
|
12
|
+
this.#schema = dbOpe.prepareSchema(obj, dbOpe.objectType.procedure);
|
|
13
13
|
this.#connection = dbOpe.prepareConnection(this.#schema);
|
|
14
14
|
}
|
|
15
15
|
|
package/lib/api-factory.js
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
import * as dbOpe from "./db-operations.js"
|
|
4
|
-
import apiFull from "./api-full.js"
|
|
5
|
-
import apiRO from "./api-ro.js"
|
|
6
|
-
import apiExecute from "./api-execute.js"
|
|
7
|
-
import apiBatch from "./api-batch.js"
|
|
8
|
-
|
|
9
|
-
export function newFullApi(tablePath) {
|
|
10
|
-
return new apiFull(tablePath);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function newROApi(tablePath) {
|
|
14
|
-
return new apiRO(tablePath);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function newExecuteApi(
|
|
18
|
-
return new apiExecute(
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function newBatchApi(useTransaction) {
|
|
22
|
-
return new apiBatch(useTransaction);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function closeAllDbConnections() {
|
|
26
|
-
return dbOpe.closeAllConnections();
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import * as dbOpe from "./db-operations.js"
|
|
4
|
+
import apiFull from "./api-full.js"
|
|
5
|
+
import apiRO from "./api-ro.js"
|
|
6
|
+
import apiExecute from "./api-execute.js"
|
|
7
|
+
import apiBatch from "./api-batch.js"
|
|
8
|
+
|
|
9
|
+
export function newFullApi(tablePath) {
|
|
10
|
+
return new apiFull(tablePath);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function newROApi(tablePath) {
|
|
14
|
+
return new apiRO(tablePath);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function newExecuteApi(procPath) {
|
|
18
|
+
return new apiExecute(procPath);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function newBatchApi(useTransaction) {
|
|
22
|
+
return new apiBatch(useTransaction);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function closeAllDbConnections() {
|
|
26
|
+
return dbOpe.closeAllConnections();
|
|
27
27
|
}
|
package/lib/api-full.js
CHANGED
|
@@ -1,96 +1,96 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// Import modules
|
|
4
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
5
|
-
import * as dbOpe from './db-operations.js';
|
|
6
|
-
|
|
7
|
-
export default class apiFull {
|
|
8
|
-
|
|
9
|
-
#schema;
|
|
10
|
-
#connection;
|
|
11
|
-
|
|
12
|
-
constructor(tablePath) {
|
|
13
|
-
this.#schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
14
|
-
this.#connection = dbOpe.prepareConnection(this.#schema);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
get schema() { return this.#schema; }
|
|
18
|
-
get connection() { return this.#connection; }
|
|
19
|
-
|
|
20
|
-
// Get by id
|
|
21
|
-
async getById(id, reqOpe) {
|
|
22
|
-
const _dbOpe = dbOpe.prepareGetById(this.#schema, this.#connection, reqOpe, id);
|
|
23
|
-
const result = await dbOpe.runQuery(_dbOpe);
|
|
24
|
-
return Array.isArray(result) ? result[0] : result;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Get by filter
|
|
28
|
-
async getByFilter(reqOpe) {
|
|
29
|
-
const _dbOpe = dbOpe.prepareGet(this.#schema, this.#connection, reqOpe);
|
|
30
|
-
return await dbOpe.runQuery(_dbOpe);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Post by id (any operation like get/put/patch/delete...)
|
|
34
|
-
async postById(reqOpe, id) {
|
|
35
|
-
const _dbOpe = dbOpe.prepareRunById(this.#schema, this.#connection, reqOpe, id);
|
|
36
|
-
return await dbOpe.runQuery(_dbOpe);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// Post (any operation like get/put/patch/delete...)
|
|
40
|
-
async post(reqOpe) {
|
|
41
|
-
const _dbOpe = dbOpe.prepareRun(this.#schema, this.#connection, reqOpe);
|
|
42
|
-
return await dbOpe.runQuery(_dbOpe);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Add item
|
|
46
|
-
async put(reqOpe) {
|
|
47
|
-
const _dbOpe = dbOpe.preparePut(this.#schema, this.#connection, reqOpe);
|
|
48
|
-
if (_dbOpe.put.sets) {
|
|
49
|
-
if (this.#schema.autoId === true) // automatic Id
|
|
50
|
-
if (!Object.keys(_dbOpe.put.sets).find(key => key.toUpperCase() === (dbOpe.idField(this.#schema)).toUpperCase())) {
|
|
51
|
-
_dbOpe.put.sets[dbOpe.idField(this.#schema)] = uuidv4(); // automatic Id via uuidv4
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
await dbOpe.runQuery(_dbOpe);
|
|
55
|
-
return _dbOpe.put.sets;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Add by Id
|
|
59
|
-
async putById(reqOpe, id) {
|
|
60
|
-
const _dbOpe = dbOpe.preparePutById(this.#schema, this.#connection, reqOpe, id);
|
|
61
|
-
await dbOpe.runQuery(_dbOpe);
|
|
62
|
-
return _dbOpe.put.sets;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// New (put + getById)
|
|
66
|
-
async new(reqOpe) {
|
|
67
|
-
const obj = await this.put(reqOpe);
|
|
68
|
-
const _return = await this.getById(obj[dbOpe.idField(this.#schema)]);
|
|
69
|
-
return _return;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Delete by id
|
|
73
|
-
async delById(id) {
|
|
74
|
-
const _dbOpe = dbOpe.prepareDeleteById(this.#schema, this.#connection, id);
|
|
75
|
-
return await dbOpe.runQuery(_dbOpe);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// Delete by filters
|
|
79
|
-
async delByFilter(reqOpe) {
|
|
80
|
-
const _dbOpe = dbOpe.prepareDelete(this.#schema, this.#connection, reqOpe);
|
|
81
|
-
return await dbOpe.runQuery(_dbOpe);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// Patch (update) by id
|
|
85
|
-
async patchById(reqOpe, id) {
|
|
86
|
-
const _dbOpe = dbOpe.preparePatchById(this.#schema, this.#connection, reqOpe, id);
|
|
87
|
-
return await dbOpe.runQuery(_dbOpe);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Patch (update) by filters
|
|
91
|
-
async patchByFilter(reqOpe) {
|
|
92
|
-
const _dbOpe = dbOpe.preparePatch(this.#schema, this.#connection, reqOpe);
|
|
93
|
-
return await dbOpe.runQuery(_dbOpe);
|
|
94
|
-
}
|
|
95
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Import modules
|
|
4
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
5
|
+
import * as dbOpe from './db-operations.js';
|
|
6
|
+
|
|
7
|
+
export default class apiFull {
|
|
8
|
+
|
|
9
|
+
#schema;
|
|
10
|
+
#connection;
|
|
11
|
+
|
|
12
|
+
constructor(tablePath) {
|
|
13
|
+
this.#schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
14
|
+
this.#connection = dbOpe.prepareConnection(this.#schema);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get schema() { return this.#schema; }
|
|
18
|
+
get connection() { return this.#connection; }
|
|
19
|
+
|
|
20
|
+
// Get by id
|
|
21
|
+
async getById(id, reqOpe) {
|
|
22
|
+
const _dbOpe = dbOpe.prepareGetById(this.#schema, this.#connection, reqOpe, id);
|
|
23
|
+
const result = await dbOpe.runQuery(_dbOpe);
|
|
24
|
+
return Array.isArray(result) ? result[0] : result;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Get by filter
|
|
28
|
+
async getByFilter(reqOpe) {
|
|
29
|
+
const _dbOpe = dbOpe.prepareGet(this.#schema, this.#connection, reqOpe);
|
|
30
|
+
return await dbOpe.runQuery(_dbOpe);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Post by id (any operation like get/put/patch/delete...)
|
|
34
|
+
async postById(reqOpe, id) {
|
|
35
|
+
const _dbOpe = dbOpe.prepareRunById(this.#schema, this.#connection, reqOpe, id);
|
|
36
|
+
return await dbOpe.runQuery(_dbOpe);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Post (any operation like get/put/patch/delete...)
|
|
40
|
+
async post(reqOpe) {
|
|
41
|
+
const _dbOpe = dbOpe.prepareRun(this.#schema, this.#connection, reqOpe);
|
|
42
|
+
return await dbOpe.runQuery(_dbOpe);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Add item
|
|
46
|
+
async put(reqOpe) {
|
|
47
|
+
const _dbOpe = dbOpe.preparePut(this.#schema, this.#connection, reqOpe);
|
|
48
|
+
if (_dbOpe.put.sets) {
|
|
49
|
+
if (this.#schema.autoId === true) // automatic Id
|
|
50
|
+
if (!Object.keys(_dbOpe.put.sets).find(key => key.toUpperCase() === (dbOpe.idField(this.#schema)).toUpperCase())) {
|
|
51
|
+
_dbOpe.put.sets[dbOpe.idField(this.#schema)] = uuidv4(); // automatic Id via uuidv4
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
await dbOpe.runQuery(_dbOpe);
|
|
55
|
+
return _dbOpe.put.sets;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Add by Id
|
|
59
|
+
async putById(reqOpe, id) {
|
|
60
|
+
const _dbOpe = dbOpe.preparePutById(this.#schema, this.#connection, reqOpe, id);
|
|
61
|
+
await dbOpe.runQuery(_dbOpe);
|
|
62
|
+
return _dbOpe.put.sets;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// New (put + getById)
|
|
66
|
+
async new(reqOpe) {
|
|
67
|
+
const obj = await this.put(reqOpe);
|
|
68
|
+
const _return = await this.getById(obj[dbOpe.idField(this.#schema)]);
|
|
69
|
+
return _return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Delete by id
|
|
73
|
+
async delById(id) {
|
|
74
|
+
const _dbOpe = dbOpe.prepareDeleteById(this.#schema, this.#connection, id);
|
|
75
|
+
return await dbOpe.runQuery(_dbOpe);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Delete by filters
|
|
79
|
+
async delByFilter(reqOpe) {
|
|
80
|
+
const _dbOpe = dbOpe.prepareDelete(this.#schema, this.#connection, reqOpe);
|
|
81
|
+
return await dbOpe.runQuery(_dbOpe);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Patch (update) by id
|
|
85
|
+
async patchById(reqOpe, id) {
|
|
86
|
+
const _dbOpe = dbOpe.preparePatchById(this.#schema, this.#connection, reqOpe, id);
|
|
87
|
+
return await dbOpe.runQuery(_dbOpe);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Patch (update) by filters
|
|
91
|
+
async patchByFilter(reqOpe) {
|
|
92
|
+
const _dbOpe = dbOpe.preparePatch(this.#schema, this.#connection, reqOpe);
|
|
93
|
+
return await dbOpe.runQuery(_dbOpe);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
96
|
}
|
package/lib/api-ro.js
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// Import modules
|
|
4
|
-
import * as dbOpe from './db-operations.js';
|
|
5
|
-
|
|
6
|
-
export default class apiRO {
|
|
7
|
-
|
|
8
|
-
#schema;
|
|
9
|
-
#connection;
|
|
10
|
-
|
|
11
|
-
constructor(tablePath) {
|
|
12
|
-
this.#schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
13
|
-
this.#connection = dbOpe.prepareConnection(this.#schema);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
get schema() { return this.#schema; }
|
|
17
|
-
get connection() { return this.#connection; }
|
|
18
|
-
|
|
19
|
-
// Run query
|
|
20
|
-
// async runOpe(_dbOpe) {
|
|
21
|
-
// return await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
22
|
-
// }
|
|
23
|
-
|
|
24
|
-
// Get by id
|
|
25
|
-
async getById(id, reqOpe) {
|
|
26
|
-
const _dbOpe = dbOpe.prepareGetById(this.#schema, this.#connection, reqOpe, id);
|
|
27
|
-
const result = await dbOpe.runQuery(_dbOpe);
|
|
28
|
-
return Array.isArray(result) ? result[0] : result;
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// Get by filter
|
|
33
|
-
async getByFilter(reqOpe) {
|
|
34
|
-
const _dbOpe = dbOpe.prepareGet(this.#schema, this.#connection, reqOpe);
|
|
35
|
-
return await dbOpe.runQuery(_dbOpe);
|
|
36
|
-
}
|
|
37
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Import modules
|
|
4
|
+
import * as dbOpe from './db-operations.js';
|
|
5
|
+
|
|
6
|
+
export default class apiRO {
|
|
7
|
+
|
|
8
|
+
#schema;
|
|
9
|
+
#connection;
|
|
10
|
+
|
|
11
|
+
constructor(tablePath) {
|
|
12
|
+
this.#schema = dbOpe.prepareSchema(tablePath, dbOpe.objectType.table);
|
|
13
|
+
this.#connection = dbOpe.prepareConnection(this.#schema);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
get schema() { return this.#schema; }
|
|
17
|
+
get connection() { return this.#connection; }
|
|
18
|
+
|
|
19
|
+
// Run query
|
|
20
|
+
// async runOpe(_dbOpe) {
|
|
21
|
+
// return await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
22
|
+
// }
|
|
23
|
+
|
|
24
|
+
// Get by id
|
|
25
|
+
async getById(id, reqOpe) {
|
|
26
|
+
const _dbOpe = dbOpe.prepareGetById(this.#schema, this.#connection, reqOpe, id);
|
|
27
|
+
const result = await dbOpe.runQuery(_dbOpe);
|
|
28
|
+
return Array.isArray(result) ? result[0] : result;
|
|
29
|
+
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Get by filter
|
|
33
|
+
async getByFilter(reqOpe) {
|
|
34
|
+
const _dbOpe = dbOpe.prepareGet(this.#schema, this.#connection, reqOpe);
|
|
35
|
+
return await dbOpe.runQuery(_dbOpe);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
38
|
}
|