binhend 2.2.6 → 2.2.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/demo.js CHANGED
@@ -1,103 +1,11 @@
1
1
 
2
- const { HttpCodes, ConfigLoader, WebBuilder, HttpError, validation, createCSD } = require('./index2');
2
+ const { HttpCodes, ConfigLoader, HttpError, validation: must } = require('./index');
3
3
  const path = require('path');
4
4
 
5
- // var jsonPath = __dirname + '/jsconfig.json';
6
- // var cc = ConfigLoader.json(jsonPath);
7
- // console.log('cc', cc);
8
-
9
- // const config = {};
10
- // console.log('config', config);
11
- // const loader = new ConfigLoader(config);
12
-
13
- // loader.object({ foo: '1', koo: '2', hoo: '3' }, { filter: ['koo', 'hoo'] });
14
-
15
- // console.log('config', config);
16
-
17
- // console.log(path.resolve(__dirname, './app.secret'));
18
-
19
- function MongooseCSD(Model) {
20
- /** CREATE */
21
-
22
- this.create = async (data) => {
23
- console.log('create', arguments);
24
- // return await new Model(data).save();
25
- return await Model.create(data);
26
- };
27
-
28
- this.upsert = async (query, data) => {
29
- console.log('upsert', arguments);
30
- return await Model.findOneAndUpdate(query, data, { new: true, upsert: true });
31
- };
32
-
33
- /** READ */
34
-
35
- this.getAll = async () => {
36
- console.log('getAll', arguments);
37
- return await Model.find({});
38
- };
39
-
40
- this.getByID = async (id) => {
41
- console.log('getByID', arguments);
42
- return await Model.findById(id);
43
- };
44
-
45
- this.getByQuery = async (query) => {
46
- console.log('getByQuery', arguments);
47
- return await Model.find(query);
48
- };
49
-
50
- /** UPDATE */
51
-
52
- this.updateAll = async (data) => {
53
- console.log('updateAll', data);
54
- return await Model.updateMany({}, data);
55
- };
56
-
57
- this.updateByID = async (id, data) => {
58
- console.log('updateByID', arguments);
59
- return await Model.findByIdAndUpdate(id, data, { new: true });
60
- };
61
-
62
- this.updateByQuery = async (query, data) => {
63
- console.log('updateByQuery', arguments);
64
- return await Model.updateMany(query, data);
65
- };
66
-
67
- /** DELETE */
68
-
69
- this.deleteAll = async () => {
70
- console.log('deleteAll', arguments);
71
- return await Model.deleteMany({});
72
- };
73
-
74
- this.deleteByID = async (id) => {
75
- console.log('deleteByID', arguments);
76
- return await Model.findByIdAndDelete(id);
77
- };
78
-
79
- this.deleteByQuery = async (query) => {
80
- console.log('deleteByQuery', arguments);
81
- return await Model.deleteMany(query);
82
- };
5
+ function Testa(input) {
6
+ this.a = must.Function(input.a);
83
7
  }
84
8
 
85
- const createMongooseCSD = createCSD(MongooseCSD);
86
-
87
- const csd = createMongooseCSD({});
88
-
89
- csd.controller.create;
90
- csd.service.create;
91
-
92
- csd.dao;
93
-
94
- async function abc() {
95
- return csd.service.create({ a: 1 });
96
- }
97
-
98
-
99
-
100
- (async function () {
101
- var cc = await abc();
102
- })();
9
+ var p = new Testa();
103
10
 
11
+ p.a();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "2.2.6",
3
+ "version": "2.2.7",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -2,15 +2,15 @@
2
2
  const express = require('express');
3
3
 
4
4
  var server = express();
5
- var calledOnce = false;
5
+ var isCalledOnce = false;
6
6
 
7
7
  function getServer() {
8
- if (calledOnce) {
8
+ if (isCalledOnce) {
9
9
  server = server;
10
10
  }
11
11
  else {
12
12
  server = express();
13
- calledOnce = true;
13
+ isCalledOnce = true;
14
14
  }
15
15
 
16
16
  return server;
@@ -0,0 +1,35 @@
1
+
2
+ const { NotNullish, AsyncFunction } = require('@binhend/validation');
3
+
4
+ function DatabaseDriverAdapter(input) {
5
+ const required = { required: true };
6
+
7
+ NotNullish(input);
8
+
9
+ for (var property in input) {
10
+ this[property] = input[property];
11
+ }
12
+
13
+ //___ CREATE ___//
14
+ this.create = AsyncFunction(input.create, required);
15
+ this.upsert = AsyncFunction(input.upsert, required);
16
+
17
+ //___ READ ___//
18
+ this.getAll = AsyncFunction(input.getAll, required);
19
+ this.getByID = AsyncFunction(input.getByID, required);
20
+ this.getByQuery = AsyncFunction(input.getByQuery, required);
21
+
22
+ //___ UPDATE ___//
23
+ this.updateAll = AsyncFunction(input.updateAll, required);
24
+ this.updateByID = AsyncFunction(input.updateByID, required);
25
+ this.updateByQuery = AsyncFunction(input.updateByQuery, required);
26
+
27
+ //___ DELETE ___//
28
+ this.deleteAll = AsyncFunction(input.deleteAll, required);
29
+ this.deleteByID = AsyncFunction(input.deleteByID, required);
30
+ this.deleteByQuery = AsyncFunction(input.deleteByQuery, required);
31
+
32
+ return this;
33
+ }
34
+
35
+ module.exports = DatabaseDriverAdapter;
@@ -1,13 +1,11 @@
1
1
 
2
2
  const { isEmptyObject, isString } = require('@binhend/types');
3
- const { trycatch } = require('@binhend/middlewares');
4
3
  const { HttpError, HttpCodes } = require('@binhend/utils');
5
-
6
4
  const { OK, CREATED, BAD_REQUEST } = HttpCodes;
7
5
 
8
6
  const
9
- ERROR_MISSING_ID = 'Missing required parameter: id',
10
- ERROR_EMPTY_QUERY = 'Query should not be empty (null or no key-value)';
7
+ ERROR_MISSING_ID = 'Missing required parameter: id',
8
+ ERROR_EMPTY_QUERY = 'Query should not be empty (null or no key-value)';
11
9
 
12
10
  function Controller(service) {
13
11
  function parseID(request) {
@@ -30,65 +28,65 @@ function Controller(service) {
30
28
 
31
29
  //___ CREATE ___//
32
30
 
33
- this.create = trycatch(async (request, response) => {
31
+ this.create = async (request, response, next) => {
34
32
  let data = request.body;
35
33
  await process('create', [data], CREATED, response);
36
- });
34
+ };
37
35
 
38
- this.upsert = trycatch(async (request, response) => {
36
+ this.upsert = async (request, response, next) => {
39
37
  let query = parseQuery(request), data = request.body;
40
38
  await process('upsert', [query, data], OK, response);
41
- });
39
+ };
42
40
 
43
41
  //___ READ ___//
44
42
 
45
- this.getAll = trycatch(async (request, response) => {
43
+ this.getAll = async (request, response, next) => {
46
44
  await process('getAll', [], OK, response);
47
- });
45
+ };
48
46
 
49
- this.getByID = trycatch(async (request, response) => {
47
+ this.getByID = async (request, response, next) => {
50
48
  let id = parseID(request);
51
49
  await process('getByID', [id], OK, response);
52
- });
50
+ };
53
51
 
54
- this.getByQuery = trycatch(async (request, response) => {
52
+ this.getByQuery = async (request, response, next) => {
55
53
  let query = parseQuery(request);
56
54
  await process('getByQuery', [query], OK, response);
57
- });
55
+ };
58
56
 
59
57
  //___ UPDATE ___//
60
58
 
61
- this.updateAll = trycatch(async (request, response) => {
59
+ this.updateAll = async (request, response, next) => {
62
60
  let data = request.body;
63
61
  await process('updateAll', [data], OK, response);
64
- });
62
+ };
65
63
 
66
- this.updateByID = trycatch(async (request, response) => {
64
+ this.updateByID = async (request, response, next) => {
67
65
  let id = parseID(request);
68
66
  let data = request.body;
69
67
  await process('updateByID', [id, data], OK, response);
70
- });
68
+ };
71
69
 
72
- this.updateByQuery = trycatch(async (request, response) => {
70
+ this.updateByQuery = async (request, response, next) => {
73
71
  let query = parseQuery(request), data = request.body;
74
72
  await process('updateByQuery', [query, data], OK, response);
75
- });
73
+ };
76
74
 
77
75
  //___ DELETE ___//
78
76
 
79
- this.deleteAll = trycatch(async (request, response) => {
77
+ this.deleteAll = async (request, response, next) => {
80
78
  await process('deleteAll', [], OK, response);
81
- });
79
+ };
82
80
 
83
- this.deleteByID = trycatch(async (request, response) => {
81
+ this.deleteByID = async (request, response, next) => {
84
82
  let id = parseID(request);
85
83
  await process('deleteByID', [id], OK, response);
86
- });
84
+ };
87
85
 
88
- this.deleteByQuery = trycatch(async (request, response) => {
86
+ this.deleteByQuery = async (request, response, next) => {
89
87
  let query = parseQuery(request);
90
88
  await process('deleteByQuery', [query], OK, response);
91
- });
89
+ };
92
90
  }
93
91
 
94
92
  module.exports = (service) => {
@@ -1,12 +1,12 @@
1
1
 
2
- const initController = require('./controller');
3
- const initService = require('./service');
4
- const initDao = require('./dao');
2
+ const createController = require('./controller');
3
+ const createService = require('./service');
4
+ const createDao = require('./dao');
5
5
 
6
- function createCSD(instance) {
7
- var dao = initDao(instance);
8
- var service = initService(dao);
9
- var controller = initController(service);
6
+ function createCSD(databaseDriverAdapter) {
7
+ var dao = createDao(databaseDriverAdapter);
8
+ var service = createService(dao);
9
+ var controller = createController(service);
10
10
  return { controller, service, dao };
11
11
  };
12
12
 
@@ -1,31 +1,58 @@
1
1
 
2
- const must = require('@binhend/validation');
2
+ const DatabaseDriverAdapter = require('./adapter.val');
3
3
 
4
- function DAO(implementedInstance = {}) {
5
-
6
- const instance = implementedInstance,
7
- required = { required: true };
4
+ function DAO(databaseDriverAdapter) {
5
+ const database = new DatabaseDriverAdapter(databaseDriverAdapter);
8
6
 
9
7
  //___ CREATE ___//
10
- this.create = must.Function(instance.create, required);
11
- this.upsert = must.Function(instance.upsert, required);
8
+ this.create = async (data) => {
9
+ return await database.create(data);
10
+ };
11
+
12
+ this.upsert = async (query, data) => {
13
+ return await database.upsert(query, data);
14
+ };
12
15
 
13
16
  //___ READ ___//
14
- this.getAll = must.Function(instance.getAll, required);
15
- this.getByID = must.Function(instance.getByID, required);
16
- this.getByQuery = must.Function(instance.getByQuery, required);
17
+ this.getAll = async () => {
18
+ return await database.getAll();
19
+ };
20
+
21
+ this.getByID = async (id) => {
22
+ return await database.getByID(id);
23
+ };
24
+
25
+ this.getByQuery = async (query) => {
26
+ return await database.getByQuery(query);
27
+ };
17
28
 
18
29
  //___ UPDATE ___//
19
- this.updateAll = must.Function(instance.updateAll, required);
20
- this.updateByID = must.Function(instance.updateByID, required);
21
- this.updateByQuery = must.Function(instance.updateByQuery, required);
30
+ this.updateAll = async (data) => {
31
+ return await database.updateAll(data);
32
+ };
33
+
34
+ this.updateByID = async (id, data) => {
35
+ return await database.updateByID(id, data);
36
+ };
37
+
38
+ this.updateByQuery = async (query, data) => {
39
+ return await database.updateByQuery(query, data);
40
+ };
22
41
 
23
42
  //___ DELETE ___//
24
- this.deleteAll = must.Function(instance.deleteAll, required);
25
- this.deleteByID = must.Function(instance.deleteByID, required);
26
- this.deleteByQuery = must.Function(instance.deleteByQuery, required);
43
+ this.deleteAll = async () => {
44
+ return await database.deleteAll();
45
+ };
46
+
47
+ this.deleteByID = async (id) => {
48
+ return await database.deleteByID(id);
49
+ };
50
+
51
+ this.deleteByQuery = async (query) => {
52
+ return await database.deleteByQuery(query);
53
+ };
27
54
  }
28
55
 
29
- module.exports = (implementedInstance) => {
30
- return new DAO(implementedInstance);
56
+ module.exports = (databaseDriverAdapter) => {
57
+ return new DAO(databaseDriverAdapter);
31
58
  };
@@ -130,6 +130,20 @@ function Function(input, options = {}) {
130
130
  return Validator(input, types.isFunction, { ...options, type: 'function' });
131
131
  }
132
132
 
133
+ /**
134
+ * Validate an input being an async function
135
+ *
136
+ * @param {*} input - Input value needs to be validated.
137
+ * @param {Options} options - Additional options for validation.
138
+ * @returns {Function} - The validated input
139
+ * @throws {HttpError} - If validation fails
140
+ */
141
+ function AsyncFunction(input, options = {}) {
142
+ return Validator(input, (input) => input instanceof AsyncFunctionConstructor, { ...options, type: 'async function' });
143
+ }
144
+
145
+ const AsyncFunctionConstructor = global.Object.getPrototypeOf(async function() {}).constructor;
146
+
133
147
  /**
134
148
  * Validate an input being a date
135
149
  *
@@ -260,6 +274,7 @@ module.exports = {
260
274
  Array,
261
275
  Object,
262
276
  Function,
277
+ AsyncFunction,
263
278
  Date,
264
279
  DateLike,
265
280
  Enum,