db-crud-api 0.1.5 → 0.1.7
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 +6 -1
- package/README.md +96 -93
- package/index.js +10 -10
- package/lib/api-execute.js +25 -0
- package/lib/api-factory.js +21 -20
- package/lib/api-full.js +13 -13
- package/lib/api-ro.js +5 -5
- package/lib/db-operations.js +439 -354
- package/lib/mssql.js +267 -267
- package/lib/schema.js +3 -3
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1,94 +1,97 @@
|
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
console.log(await
|
|
64
|
-
console.log(await
|
|
65
|
-
console.log(await
|
|
66
|
-
console.log(await apiOrderDetail.
|
|
67
|
-
console.log(await apiOrderDetail.
|
|
68
|
-
console.log(await apiOrderDetail.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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 @Company, @OrderNumber"); // execute my_storeproc
|
|
62
|
+
|
|
63
|
+
console.log(await apiOrder.getById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx"));
|
|
64
|
+
console.log(await apiOrder.getByFilter({get: {filters: ["OrderType in ('P', 'A')", "and", "OrderDate > '2022-12-01'"]}})); // get filterd rows
|
|
65
|
+
console.log(await apiOrder.getByFilter()); // this get all rows
|
|
66
|
+
console.log(await apiOrderDetail.patchById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx", {patch: {sets: {ItemRef: "2024-TX-0001", ItemDate: "2024-01-31"}}})); // change some fields
|
|
67
|
+
console.log(await apiOrderDetail.patchByFilter({patch: {sets: {LastUpdate: "2023-04-30"}, filters: ["OrderId: 'xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx'"]}})); // change by filter
|
|
68
|
+
console.log(await apiOrderDetail.deleteById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx")); // delete row by Id
|
|
69
|
+
console.log(await apiOrderDetail.deleteByFilter({delete: {filters: ["ItemType in ('X', 'W')", "and", "ItemDate > '2022-12-01'"]}})); // delete filterd rows
|
|
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({params: {Company: "XXX", OrderNumber: "12345"}}})); // execute
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
License
|
|
75
|
+
-------
|
|
76
|
+
|
|
77
|
+
MIT License
|
|
78
|
+
|
|
79
|
+
Copyright (c) 2022
|
|
80
|
+
|
|
81
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
82
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
83
|
+
in the Software without restriction, including without limitation the rights
|
|
84
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
85
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
86
|
+
furnished to do so, subject to the following conditions:
|
|
87
|
+
|
|
88
|
+
The above copyright notice and this permission notice shall be included in all
|
|
89
|
+
copies or substantial portions of the Software.
|
|
90
|
+
|
|
91
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
92
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
93
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
94
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
95
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
96
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
94
97
|
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
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Import modules
|
|
4
|
+
import * as dbOpe from './db-operations.js';
|
|
5
|
+
|
|
6
|
+
export default class apiExecute {
|
|
7
|
+
|
|
8
|
+
#schema;
|
|
9
|
+
#connection;
|
|
10
|
+
|
|
11
|
+
constructor(command) {
|
|
12
|
+
this.#schema = dbOpe.prepareSchema(command, "SP");
|
|
13
|
+
this.#connection = dbOpe.prepareConnection(this.#schema);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
get schema() { return this.#schema; }
|
|
17
|
+
get connection() { return this.#connection; }
|
|
18
|
+
|
|
19
|
+
// Post (any operation like get/put/patch/delete...)
|
|
20
|
+
async execute(reqOpe) {
|
|
21
|
+
const _dbOpe = dbOpe.prepareRun(this.#schema, reqOpe);
|
|
22
|
+
return await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
}
|
package/lib/api-factory.js
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
+
|
|
8
|
+
export function newFullApi(tablePath) {
|
|
9
|
+
return new apiFull(tablePath);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function newROApi(tablePath) {
|
|
13
|
+
return new apiRO(tablePath);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function newExecuteXApi(command) {
|
|
17
|
+
return new apiExecute(command);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function closeAllDbConnections() {
|
|
21
|
+
return dbOpe.closeAllConnections();
|
|
21
22
|
}
|
package/lib/api-full.js
CHANGED
|
@@ -18,14 +18,14 @@ export default class apiFull {
|
|
|
18
18
|
get connection() { return this.#connection; }
|
|
19
19
|
|
|
20
20
|
// Run query
|
|
21
|
-
async runOpe(_dbOpe) {
|
|
22
|
-
return await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
23
|
-
}
|
|
21
|
+
// async runOpe(_dbOpe) {
|
|
22
|
+
// return await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
23
|
+
// }
|
|
24
24
|
|
|
25
25
|
// Get by id
|
|
26
26
|
async getById(id, reqOpe) {
|
|
27
27
|
const _dbOpe = dbOpe.prepareGetById(this.#schema, id, reqOpe);
|
|
28
|
-
const result = await
|
|
28
|
+
const result = await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
29
29
|
return Array.isArray(result) ? result[0] : result;
|
|
30
30
|
|
|
31
31
|
}
|
|
@@ -33,19 +33,19 @@ export default class apiFull {
|
|
|
33
33
|
// Get by filter
|
|
34
34
|
async getByFilter(reqOpe) {
|
|
35
35
|
const _dbOpe = dbOpe.prepareGet(this.#schema, reqOpe);
|
|
36
|
-
return await
|
|
36
|
+
return await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
// Post by id (any operation like get/put/patch/delete...)
|
|
40
40
|
async postById(id, reqOpe) {
|
|
41
41
|
const _dbOpe = dbOpe.prepareRunById(this.#schema, id, reqOpe);
|
|
42
|
-
return await
|
|
42
|
+
return await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
// Post (any operation like get/put/patch/delete...)
|
|
46
46
|
async post(reqOpe) {
|
|
47
47
|
const _dbOpe = dbOpe.prepareRun(this.#schema, reqOpe);
|
|
48
|
-
return await
|
|
48
|
+
return await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
// Add item
|
|
@@ -57,14 +57,14 @@ export default class apiFull {
|
|
|
57
57
|
_dbOpe.put.sets[dbOpe.idField(this.#schema)] = uuidv4(); // automatic Id via uuidv4
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
|
-
await
|
|
60
|
+
await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
61
61
|
return _dbOpe.put.sets;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
// Add by Id
|
|
65
65
|
async putById(id, reqOpe) {
|
|
66
66
|
const _dbOpe = dbOpe.preparePutById(this.#schema, id, reqOpe);
|
|
67
|
-
await
|
|
67
|
+
await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
68
68
|
return _dbOpe.put.sets;
|
|
69
69
|
}
|
|
70
70
|
|
|
@@ -78,25 +78,25 @@ export default class apiFull {
|
|
|
78
78
|
// Delete by id
|
|
79
79
|
async delById(id) {
|
|
80
80
|
const _dbOpe = dbOpe.prepareDeleteById(this.#schema, id);
|
|
81
|
-
return await
|
|
81
|
+
return await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
// Delete by filters
|
|
85
85
|
async delByFilter(reqOpe) {
|
|
86
86
|
const _dbOpe = dbOpe.prepareDelete(this.#schema, reqOpe);
|
|
87
|
-
return await
|
|
87
|
+
return await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
// Patch (update) by id
|
|
91
91
|
async patchById(id, reqOpe) {
|
|
92
92
|
const _dbOpe = dbOpe.preparePatchById(this.#schema, id, reqOpe);
|
|
93
|
-
return await
|
|
93
|
+
return await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
// Patch (update) by filters
|
|
97
97
|
async patchByFilter(reqOpe) {
|
|
98
98
|
const _dbOpe = dbOpe.preparePatch(this.#schema, reqOpe);
|
|
99
|
-
return await
|
|
99
|
+
return await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
}
|
package/lib/api-ro.js
CHANGED
|
@@ -17,14 +17,14 @@ export default class apiRO {
|
|
|
17
17
|
get connection() { return this.#connection; }
|
|
18
18
|
|
|
19
19
|
// Run query
|
|
20
|
-
async runOpe(_dbOpe) {
|
|
21
|
-
return await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
22
|
-
}
|
|
20
|
+
// async runOpe(_dbOpe) {
|
|
21
|
+
// return await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
22
|
+
// }
|
|
23
23
|
|
|
24
24
|
// Get by id
|
|
25
25
|
async getById(id, reqOpe) {
|
|
26
26
|
const _dbOpe = dbOpe.prepareGetById(this.#schema, id, reqOpe);
|
|
27
|
-
const result = await
|
|
27
|
+
const result = await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
28
28
|
return Array.isArray(result) ? result[0] : result;
|
|
29
29
|
|
|
30
30
|
}
|
|
@@ -32,7 +32,7 @@ export default class apiRO {
|
|
|
32
32
|
// Get by filter
|
|
33
33
|
async getByFilter(reqOpe) {
|
|
34
34
|
const _dbOpe = dbOpe.prepareGet(this.#schema, reqOpe);
|
|
35
|
-
return await
|
|
35
|
+
return await dbOpe.runQuery(this.#connection, _dbOpe);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
}
|