db-crud-api 0.3.15 → 0.3.19

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 CHANGED
@@ -61,4 +61,8 @@
61
61
 
62
62
  Added:
63
63
  - config
64
- - log via callback
64
+ - log via callback
65
+ ## v0.3.18 (2025-12-12)
66
+
67
+ Added:
68
+ - sessionStore
package/README.md CHANGED
@@ -1,100 +1,100 @@
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 apiFactory.testConnection()); // optional
64
-
65
- console.log(await apiOrder.getById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx"));
66
- console.log(await apiOrder.getById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx", {get: {fields: ["Id", "OrderNumber", "OrderTotal"]}}));
67
- console.log(await apiOrder.getByFilter({get: {filters: ["OrderType in ('P', 'A')", "and", "OrderDate > '2022-12-01'"]}})); // get filterd rows
68
- console.log(await apiOrder.getByFilter()); // this get all rows
69
- console.log(await apiOrderDetail.patchById({patch: {sets: {ItemRef: "2024-TX-0001", ItemDate: "2024-01-31"}}}, "xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx")); // change some fields
70
- console.log(await apiOrderDetail.patchByFilter({patch: {sets: {LastUpdate: "2023-04-30"}, filters: ["OrderId: 'xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx'"]}})); // change by filter
71
- console.log(await apiOrderDetail.deleteById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx")); // delete row by Id
72
- console.log(await apiOrderDetail.deleteByFilter({delete: {filters: ["ItemType in ('X', 'W')", "and", "ItemDate > '2022-12-01'"]}})); // delete filterd rows
73
- console.log(await apiExecuteSP.execute()); // execute procedure with no argument
74
- console.log(await apiExecuteSP.execute({execute: {arguments: "@Company, @OrderNumber", params: {Company: "XXX", OrderNumber: "12345"}}})); // execute procedure with arguments and params
75
- ```
76
-
77
- License
78
- -------
79
-
80
- MIT License
81
-
82
- Copyright (c) 2022
83
-
84
- Permission is hereby granted, free of charge, to any person obtaining a copy
85
- of this software and associated documentation files (the "Software"), to deal
86
- in the Software without restriction, including without limitation the rights
87
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
88
- copies of the Software, and to permit persons to whom the Software is
89
- furnished to do so, subject to the following conditions:
90
-
91
- The above copyright notice and this permission notice shall be included in all
92
- copies or substantial portions of the Software.
93
-
94
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
95
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
96
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
97
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
98
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
99
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
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 apiFactory.testConnection()); // optional
64
+
65
+ console.log(await apiOrder.getById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx"));
66
+ console.log(await apiOrder.getById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx", {get: {fields: ["Id", "OrderNumber", "OrderTotal"]}}));
67
+ console.log(await apiOrder.getByFilter({get: {filters: ["OrderType in ('P', 'A')", "and", "OrderDate > '2022-12-01'"]}})); // get filterd rows
68
+ console.log(await apiOrder.getByFilter()); // this get all rows
69
+ console.log(await apiOrderDetail.patchById({patch: {sets: {ItemRef: "2024-TX-0001", ItemDate: "2024-01-31"}}}, "xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx")); // change some fields
70
+ console.log(await apiOrderDetail.patchByFilter({patch: {sets: {LastUpdate: "2023-04-30"}, filters: ["OrderId: 'xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx'"]}})); // change by filter
71
+ console.log(await apiOrderDetail.deleteById("xxxxx-xxxxx-xxxxxxxxxxxx-xxxxxx")); // delete row by Id
72
+ console.log(await apiOrderDetail.deleteByFilter({delete: {filters: ["ItemType in ('X', 'W')", "and", "ItemDate > '2022-12-01'"]}})); // delete filterd rows
73
+ console.log(await apiExecuteSP.execute()); // execute procedure with no argument
74
+ console.log(await apiExecuteSP.execute({execute: {arguments: "@Company, @OrderNumber", params: {Company: "XXX", OrderNumber: "12345"}}})); // execute procedure with arguments and params
75
+ ```
76
+
77
+ License
78
+ -------
79
+
80
+ MIT License
81
+
82
+ Copyright (c) 2022
83
+
84
+ Permission is hereby granted, free of charge, to any person obtaining a copy
85
+ of this software and associated documentation files (the "Software"), to deal
86
+ in the Software without restriction, including without limitation the rights
87
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
88
+ copies of the Software, and to permit persons to whom the Software is
89
+ furnished to do so, subject to the following conditions:
90
+
91
+ The above copyright notice and this permission notice shall be included in all
92
+ copies or substantial portions of the Software.
93
+
94
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
95
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
96
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
97
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
98
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
99
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
100
100
  SOFTWARE.
package/index.js CHANGED
@@ -5,16 +5,17 @@ import * as apiFactory from "./lib/api-factory.js";
5
5
 
6
6
  export default function(customSchema, config) {
7
7
  if (customSchema && customSchema.servers) {
8
- schema.servers = customSchema.servers;
8
+ apiFactory.setSchemaServers(customSchema.servers);
9
9
  }
10
10
  if (config) {
11
11
  if (config.log) {
12
- if (config.log.level) {
13
- if (typeof(config.log.level) === 'number') { schema.config.log.level = config.log.level }
14
- else if (typeof(config.log.level) === 'string' && Number.parseInt(config.log.level) != NaN) { schema.config.log.level = Number.parseInt(config.log.level) }
15
- }
16
- if (config.log.callback && typeof(config.log.callback) === 'function') { schema.config.log.callback = config.log.callback }
12
+ if (config.log.level) { apiFactory.setLogLevel(config.log.level) }
13
+ if (config.log.callback) { apiFactory.setLogCallback(config.log.callback) }
14
+ if (config.log.maxAsyncInstances) { apiFactory.setLogMaxAsyncInstances(config.log.maxAsyncInstances) }
15
+ }
16
+ if (config.session) {
17
+ if (config.session.tablePath) { apiFactory.setSessionTablePath(config.session.tablePath) }
17
18
  }
18
19
  }
19
20
  return apiFactory;
20
- }
21
+ }
@@ -1,31 +1,60 @@
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
- }
28
-
29
- export function testDbConnection() {
30
- return dbOpe.testConnection();
1
+ "use strict";
2
+
3
+ import schema from "./schema.js";
4
+ import * as dbOpe from "./db-operations.js"
5
+ import apiFull from "./api-full.js"
6
+ import apiRO from "./api-ro.js"
7
+ import apiExecute from "./api-execute.js"
8
+ import apiBatch from "./api-batch.js"
9
+ import sessionStore from "./api-session-store.js"
10
+
11
+
12
+ export function newFullApi(tablePath) {
13
+ return new apiFull(tablePath);
14
+ }
15
+
16
+ export function newROApi(tablePath) {
17
+ return new apiRO(tablePath);
18
+ }
19
+
20
+ export function newExecuteApi(procPath) {
21
+ return new apiExecute(procPath);
22
+ }
23
+
24
+ export function newBatchApi(useTransaction) {
25
+ return new apiBatch(useTransaction);
26
+ }
27
+
28
+ export function closeAllDbConnections() {
29
+ return dbOpe.closeAllConnections();
30
+ }
31
+
32
+ export function newSessionStore(options) {
33
+ return new sessionStore(options);
34
+ }
35
+
36
+ export function testDbConnection() {
37
+ return dbOpe.testConnection();
38
+ }
39
+
40
+ export function setLogLevel(level) {
41
+ if (typeof level === 'number') { schema.config.log.level = level }
42
+ else if (typeof level === 'string' && Number.parseInt(level) != NaN) { schema.config.log.level = Number.parseInt(level) }
43
+ }
44
+
45
+ export function setLogCallback(callback) {
46
+ if (typeof callback === 'function') { schema.config.log.callback = callback }
47
+ }
48
+
49
+ export function setLogMaxAsyncInstances(maxAsyncInstances) {
50
+ if (typeof maxAsyncInstances === 'number') { schema.config.log.maxAsyncInstance = maxAsyncInstances }
51
+ else if (typeof maxAsyncInstances === 'string' && Number.parseInt(maxAsyncInstances) != NaN) { schema.config.log.maxAsyncInstance = Number.parseInt(maxAsyncInstances) }
52
+ }
53
+
54
+ export function setSessionTablePath(tablePath) {
55
+ if (typeof tablePath === 'string' && tablePath.length > 0) { schema.config.session.tablePath = tablePath }
56
+ }
57
+
58
+ export function setSchemaServers(servers) {
59
+ schema.servers = servers;
31
60
  }
package/lib/api-full.js CHANGED
@@ -1,96 +1,105 @@
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 (dbOpe.autoId(this.#schema)) // 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 (dbOpe.autoId(this.#schema)) // 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 or update item by Id
59
+ async putById(reqOpe, id) {
60
+ const _old = await this.getById(id, { get: { fields: [dbOpe.idField(this.#schema)] } });
61
+ if (!_old) {
62
+ const _dbOpePut = dbOpe.preparePutById(this.#schema, this.#connection, reqOpe, id);
63
+ await dbOpe.runQuery(_dbOpePut);
64
+ return _dbOpePut.put.sets;
65
+ }
66
+ else {
67
+ const _patchOpe = { patch: {sets: reqOpe.put.sets} };
68
+ await this.patchById(_patchOpe, id);
69
+ return _patchOpe.patch.sets;
70
+ }
71
+ }
72
+
73
+ // New (put + getById)
74
+ async new(reqOpe) {
75
+ const obj = await this.put(reqOpe);
76
+ //const _return = await this.getById(obj[dbOpe.idField(this.#schema)]);
77
+ const _return = await this.getById(dbOpe.getProperty(obj, dbOpe.idField(this.#schema)));
78
+ return _return;
79
+ }
80
+
81
+ // Delete by id
82
+ async delById(id) {
83
+ const _dbOpe = dbOpe.prepareDeleteById(this.#schema, this.#connection, id);
84
+ return await dbOpe.runQuery(_dbOpe);
85
+ }
86
+
87
+ // Delete by filters
88
+ async delByFilter(reqOpe) {
89
+ const _dbOpe = dbOpe.prepareDelete(this.#schema, this.#connection, reqOpe);
90
+ return await dbOpe.runQuery(_dbOpe);
91
+ }
92
+
93
+ // Patch (update) by id
94
+ async patchById(reqOpe, id) {
95
+ const _dbOpe = dbOpe.preparePatchById(this.#schema, this.#connection, reqOpe, id);
96
+ return await dbOpe.runQuery(_dbOpe);
97
+ }
98
+
99
+ // Patch (update) by filters
100
+ async patchByFilter(reqOpe) {
101
+ const _dbOpe = dbOpe.preparePatch(this.#schema, this.#connection, reqOpe);
102
+ return await dbOpe.runQuery(_dbOpe);
103
+ }
104
+
96
105
  }