@modular-rest/server 1.11.13 → 1.12.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.
Files changed (181) hide show
  1. package/.nvmrc +1 -0
  2. package/.prettierrc.json +9 -0
  3. package/.releaserc.json +24 -0
  4. package/README.md +79 -94
  5. package/dist/application.d.ts +29 -0
  6. package/dist/application.js +217 -0
  7. package/dist/class/cms_trigger.d.ts +61 -0
  8. package/dist/class/cms_trigger.js +47 -0
  9. package/dist/class/collection_definition.d.ts +112 -0
  10. package/dist/class/collection_definition.js +87 -0
  11. package/dist/class/combinator.d.ts +43 -0
  12. package/dist/class/combinator.js +174 -0
  13. package/dist/class/database_trigger.d.ts +84 -0
  14. package/dist/class/database_trigger.js +64 -0
  15. package/dist/class/db_schemas.d.ts +25 -0
  16. package/dist/class/db_schemas.js +28 -0
  17. package/dist/class/directory.d.ts +20 -0
  18. package/dist/class/directory.js +87 -0
  19. package/dist/class/paginator.d.ts +31 -0
  20. package/dist/class/paginator.js +43 -0
  21. package/dist/class/reply.d.ts +29 -0
  22. package/dist/class/reply.js +44 -0
  23. package/dist/class/security.d.ts +186 -0
  24. package/dist/class/security.js +178 -0
  25. package/dist/class/trigger_operator.d.ts +92 -0
  26. package/dist/class/trigger_operator.js +99 -0
  27. package/dist/class/user.d.ts +81 -0
  28. package/dist/class/user.js +151 -0
  29. package/dist/class/validator.d.ts +19 -0
  30. package/dist/class/validator.js +101 -0
  31. package/dist/config.d.ts +112 -0
  32. package/dist/config.js +26 -0
  33. package/dist/defult-permissions.d.ts +2 -0
  34. package/dist/defult-permissions.js +31 -0
  35. package/dist/events.d.ts +23 -0
  36. package/dist/events.js +47 -0
  37. package/dist/helper/data_insertion.d.ts +38 -0
  38. package/dist/helper/data_insertion.js +110 -0
  39. package/dist/helper/presetup_services.d.ts +60 -0
  40. package/dist/helper/presetup_services.js +108 -0
  41. package/dist/index.d.ts +118 -0
  42. package/dist/index.js +79 -0
  43. package/dist/middlewares.d.ts +53 -0
  44. package/dist/middlewares.js +106 -0
  45. package/dist/play-test.d.ts +1 -0
  46. package/dist/play-test.js +9 -0
  47. package/dist/services/data_provider/router.d.ts +4 -0
  48. package/dist/services/data_provider/router.js +187 -0
  49. package/dist/services/data_provider/service.d.ts +131 -0
  50. package/dist/services/data_provider/service.js +252 -0
  51. package/dist/services/data_provider/typeCasters.d.ts +9 -0
  52. package/dist/services/data_provider/typeCasters.js +18 -0
  53. package/dist/services/file/db.d.ts +1 -0
  54. package/dist/services/file/db.js +31 -0
  55. package/dist/services/file/router.d.ts +4 -0
  56. package/dist/services/file/router.js +115 -0
  57. package/dist/services/file/service.d.ts +204 -0
  58. package/dist/services/file/service.js +341 -0
  59. package/dist/services/functions/router.d.ts +4 -0
  60. package/dist/services/functions/router.js +67 -0
  61. package/dist/services/functions/service.d.ts +132 -0
  62. package/dist/services/functions/service.js +159 -0
  63. package/dist/services/jwt/router.d.ts +4 -0
  64. package/dist/services/jwt/router.js +99 -0
  65. package/dist/services/jwt/service.d.ts +97 -0
  66. package/dist/services/jwt/service.js +135 -0
  67. package/dist/services/user_manager/db.d.ts +1 -0
  68. package/dist/services/user_manager/db.js +75 -0
  69. package/dist/services/user_manager/permissionManager.d.ts +19 -0
  70. package/dist/services/user_manager/permissionManager.js +42 -0
  71. package/dist/services/user_manager/router.d.ts +4 -0
  72. package/dist/services/user_manager/router.js +195 -0
  73. package/dist/services/user_manager/service.d.ts +317 -0
  74. package/dist/services/user_manager/service.js +628 -0
  75. package/docs/.keep +0 -0
  76. package/docs/system-access-type.md +26 -0
  77. package/package.json +58 -45
  78. package/src/application.ts +206 -0
  79. package/src/class/cms_trigger.ts +68 -0
  80. package/src/class/collection_definition.ts +134 -0
  81. package/src/class/combinator.ts +176 -0
  82. package/src/class/database_trigger.ts +99 -0
  83. package/src/class/db_schemas.ts +44 -0
  84. package/src/class/{directory.js → directory.ts} +40 -18
  85. package/src/class/paginator.ts +51 -0
  86. package/src/class/reply.ts +59 -0
  87. package/src/class/security.ts +250 -0
  88. package/src/class/trigger_operator.ts +142 -0
  89. package/src/class/user.ts +199 -0
  90. package/src/class/validator.ts +123 -0
  91. package/src/config.ts +121 -0
  92. package/src/defult-permissions.ts +31 -0
  93. package/src/events.ts +59 -0
  94. package/src/helper/data_insertion.ts +94 -0
  95. package/src/helper/presetup_services.ts +96 -0
  96. package/src/index.ts +146 -0
  97. package/src/middlewares.ts +75 -0
  98. package/src/play-test.ts +8 -0
  99. package/src/services/data_provider/router.ts +191 -0
  100. package/src/services/data_provider/service.ts +305 -0
  101. package/src/services/data_provider/typeCasters.ts +15 -0
  102. package/src/services/file/db.ts +29 -0
  103. package/src/services/file/router.ts +88 -0
  104. package/src/services/file/service.ts +387 -0
  105. package/src/services/functions/router.ts +34 -0
  106. package/src/services/functions/service.ts +203 -0
  107. package/src/services/jwt/router.ts +73 -0
  108. package/src/services/jwt/service.ts +139 -0
  109. package/src/services/user_manager/db.ts +87 -0
  110. package/src/services/user_manager/permissionManager.ts +49 -0
  111. package/src/services/user_manager/router.ts +193 -0
  112. package/src/services/user_manager/service.ts +698 -0
  113. package/tsconfig.json +16 -9
  114. package/typedoc.mjs +41 -0
  115. package/LICENSE +0 -21
  116. package/package-lock.json +0 -1373
  117. package/src/application.js +0 -239
  118. package/src/class/cms_trigger.js +0 -20
  119. package/src/class/collection_definition.js +0 -33
  120. package/src/class/combinator.js +0 -133
  121. package/src/class/database_trigger.js +0 -20
  122. package/src/class/db_schemas.js +0 -18
  123. package/src/class/paginator.js +0 -31
  124. package/src/class/reply.js +0 -37
  125. package/src/class/security.js +0 -141
  126. package/src/class/trigger_operator.js +0 -39
  127. package/src/class/user.js +0 -112
  128. package/src/class/validator.js +0 -91
  129. package/src/config.js +0 -67
  130. package/src/events.js +0 -15
  131. package/src/helper/data_insertion.js +0 -64
  132. package/src/helper/presetup_services.js +0 -31
  133. package/src/index.js +0 -66
  134. package/src/middlewares.js +0 -44
  135. package/src/services/data_provider/router.js +0 -552
  136. package/src/services/data_provider/service.js +0 -262
  137. package/src/services/data_provider/typeCasters.js +0 -10
  138. package/src/services/file/db.js +0 -29
  139. package/src/services/file/router.js +0 -92
  140. package/src/services/file/service.js +0 -231
  141. package/src/services/functions/router.js +0 -37
  142. package/src/services/functions/service.js +0 -74
  143. package/src/services/jwt/router.js +0 -82
  144. package/src/services/jwt/service.js +0 -37
  145. package/src/services/user_manager/db.js +0 -83
  146. package/src/services/user_manager/permissionManager.js +0 -43
  147. package/src/services/user_manager/router.js +0 -176
  148. package/src/services/user_manager/service.js +0 -377
  149. package/types/application.d.ts +0 -97
  150. package/types/class/cms_trigger.d.ts +0 -24
  151. package/types/class/collection_definition.d.ts +0 -36
  152. package/types/class/combinator.d.ts +0 -30
  153. package/types/class/database_trigger.d.ts +0 -28
  154. package/types/class/db_schemas.d.ts +0 -2
  155. package/types/class/directory.d.ts +0 -2
  156. package/types/class/paginator.d.ts +0 -8
  157. package/types/class/reply.d.ts +0 -8
  158. package/types/class/security.d.ts +0 -109
  159. package/types/class/trigger_operator.d.ts +0 -19
  160. package/types/class/user.d.ts +0 -24
  161. package/types/class/validator.d.ts +0 -9
  162. package/types/config.d.ts +0 -101
  163. package/types/events.d.ts +0 -7
  164. package/types/helper/data_insertion.d.ts +0 -4
  165. package/types/helper/presetup_services.d.ts +0 -5
  166. package/types/index.d.ts +0 -72
  167. package/types/middlewares.d.ts +0 -10
  168. package/types/services/data_provider/router.d.ts +0 -3
  169. package/types/services/data_provider/service.d.ts +0 -40
  170. package/types/services/data_provider/typeCasters.d.ts +0 -3
  171. package/types/services/file/db.d.ts +0 -3
  172. package/types/services/file/router.d.ts +0 -3
  173. package/types/services/file/service.d.ts +0 -81
  174. package/types/services/functions/router.d.ts +0 -3
  175. package/types/services/functions/service.d.ts +0 -23
  176. package/types/services/jwt/router.d.ts +0 -3
  177. package/types/services/jwt/service.d.ts +0 -10
  178. package/types/services/user_manager/db.d.ts +0 -3
  179. package/types/services/user_manager/permissionManager.d.ts +0 -3
  180. package/types/services/user_manager/router.d.ts +0 -3
  181. package/types/services/user_manager/service.d.ts +0 -131
@@ -0,0 +1,112 @@
1
+ import { Schema } from 'mongoose';
2
+ import { Permission } from './security';
3
+ import { DatabaseTrigger } from './database_trigger';
4
+ /**
5
+ * Configuration options for creating a collection definition.
6
+ * This interface defines the structure for configuring MongoDB collections with their associated
7
+ * schemas, permissions, and triggers.
8
+ *
9
+ * @inline
10
+ *
11
+ */
12
+ interface CollectionDefinitionOptions {
13
+ /** The name of the database where the collection resides */
14
+ database: string;
15
+ /** The name of the collection to be configured */
16
+ collection: string;
17
+ /** List of permissions controlling access to the collection */
18
+ permissions: Permission[];
19
+ /** Optional database triggers for custom operations */
20
+ triggers?: DatabaseTrigger[];
21
+ /**
22
+ * Mongoose schema definition for the collection
23
+ * @type {Schema}
24
+ * @see https://mongoosejs.com/docs/5.x/docs/guide.html
25
+ */
26
+ schema: Schema;
27
+ }
28
+ /**
29
+ * To have define any collection in your database you haveto use below method in your `db.[js|ts]` file and export an array of CollectionDefinition instances.
30
+ *
31
+ * @param {CollectionDefinitionOptions} options - The options for the collection
32
+ * @expandType CollectionDefinitionOptions
33
+ *
34
+ * @returns A new instance of CollectionDefinition
35
+ *
36
+ * @public
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * import { defineCollection } from '@modular-rest/server';
41
+ *
42
+ * export default [
43
+ * defineCollection({
44
+ * database: 'users',
45
+ * collection: 'info',
46
+ * // schema: Schema,
47
+ * // permissions: Permission[]
48
+ * // trigger: DatabaseTrigger[]
49
+ * })
50
+ * ]
51
+ * ```
52
+ */
53
+ export declare function defineCollection(options: CollectionDefinitionOptions): CollectionDefinition;
54
+ /**
55
+ * A class that represents a MongoDB collection configuration. Provides full support for schema validation, access control through permissions,
56
+ * and custom triggers for various database operations.
57
+ *
58
+ * @hideconstructor
59
+ *
60
+ * @deprecated Use `defineCollection` instead.
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * const userSchema = new Schema({
65
+ * name: String,
66
+ * email: String,
67
+ * age: Number
68
+ * });
69
+ *
70
+ * const collection = new CollectionDefinition({
71
+ * database: 'myapp',
72
+ * collection: 'users',
73
+ * schema: userSchema,
74
+ * permissions: [
75
+ * new Permission({
76
+ * type: 'user_access',
77
+ * read: true,
78
+ * write: true
79
+ * })
80
+ * ],
81
+ * triggers: [
82
+ * new DatabaseTrigger('insert-one', (data) => {
83
+ * console.log('New user created:', data);
84
+ * })
85
+ * ]
86
+ * });
87
+ * ```
88
+ *
89
+ * @private
90
+ */
91
+ export declare class CollectionDefinition {
92
+ /** @readonly The name of the database */
93
+ database: string;
94
+ /** @readonly The name of the collection */
95
+ collection: string;
96
+ /** @readonly Mongoose schema definition */
97
+ schema: Schema;
98
+ /** @readonly List of permissions for the collection */
99
+ permissions: Permission[];
100
+ /** @readonly Optional database triggers */
101
+ triggers?: DatabaseTrigger[];
102
+ /**
103
+ * Creates a new CollectionDefinition instance
104
+ *
105
+ * @param options - Configuration options for the collection
106
+ * @returns A new instance of CollectionDefinition
107
+ *
108
+ * @beta
109
+ */
110
+ constructor({ database, collection, schema, permissions, triggers, }: CollectionDefinitionOptions);
111
+ }
112
+ export {};
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CollectionDefinition = void 0;
4
+ exports.defineCollection = defineCollection;
5
+ /**
6
+ * To have define any collection in your database you haveto use below method in your `db.[js|ts]` file and export an array of CollectionDefinition instances.
7
+ *
8
+ * @param {CollectionDefinitionOptions} options - The options for the collection
9
+ * @expandType CollectionDefinitionOptions
10
+ *
11
+ * @returns A new instance of CollectionDefinition
12
+ *
13
+ * @public
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * import { defineCollection } from '@modular-rest/server';
18
+ *
19
+ * export default [
20
+ * defineCollection({
21
+ * database: 'users',
22
+ * collection: 'info',
23
+ * // schema: Schema,
24
+ * // permissions: Permission[]
25
+ * // trigger: DatabaseTrigger[]
26
+ * })
27
+ * ]
28
+ * ```
29
+ */
30
+ function defineCollection(options) {
31
+ return new CollectionDefinition(options);
32
+ }
33
+ /**
34
+ * A class that represents a MongoDB collection configuration. Provides full support for schema validation, access control through permissions,
35
+ * and custom triggers for various database operations.
36
+ *
37
+ * @hideconstructor
38
+ *
39
+ * @deprecated Use `defineCollection` instead.
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * const userSchema = new Schema({
44
+ * name: String,
45
+ * email: String,
46
+ * age: Number
47
+ * });
48
+ *
49
+ * const collection = new CollectionDefinition({
50
+ * database: 'myapp',
51
+ * collection: 'users',
52
+ * schema: userSchema,
53
+ * permissions: [
54
+ * new Permission({
55
+ * type: 'user_access',
56
+ * read: true,
57
+ * write: true
58
+ * })
59
+ * ],
60
+ * triggers: [
61
+ * new DatabaseTrigger('insert-one', (data) => {
62
+ * console.log('New user created:', data);
63
+ * })
64
+ * ]
65
+ * });
66
+ * ```
67
+ *
68
+ * @private
69
+ */
70
+ class CollectionDefinition {
71
+ /**
72
+ * Creates a new CollectionDefinition instance
73
+ *
74
+ * @param options - Configuration options for the collection
75
+ * @returns A new instance of CollectionDefinition
76
+ *
77
+ * @beta
78
+ */
79
+ constructor({ database, collection, schema, permissions, triggers, }) {
80
+ this.database = database;
81
+ this.collection = collection;
82
+ this.schema = schema;
83
+ this.permissions = permissions;
84
+ this.triggers = triggers;
85
+ }
86
+ }
87
+ exports.CollectionDefinition = CollectionDefinition;
@@ -0,0 +1,43 @@
1
+ import Koa from 'koa';
2
+ interface FilenameOption {
3
+ name: string;
4
+ extension: string;
5
+ }
6
+ interface ModuleOptions {
7
+ rootDirectory: string;
8
+ filename: FilenameOption;
9
+ combineWithRoot?: boolean;
10
+ convertToArray?: boolean;
11
+ }
12
+ interface FunctionOptions {
13
+ rootDirectory: string;
14
+ filename: FilenameOption;
15
+ }
16
+ declare class Combinator {
17
+ combineRoutesByFilePath(rootDirectory: string, app: Koa): Promise<void>;
18
+ /**
19
+ * Combine modules from files in a directory
20
+ * @param options - Configuration options
21
+ */
22
+ combineModulesByFilePath({ rootDirectory, filename, combineWithRoot, convertToArray, }: ModuleOptions): Promise<any>;
23
+ /**
24
+ * Combine functions from files in a directory
25
+ * @param options - Function options
26
+ */
27
+ combineFunctionsByFilePath({ rootDirectory, filename }: FunctionOptions): Promise<void>;
28
+ /**
29
+ * Add functions from an array
30
+ * @param functionList - List of functions to add
31
+ */
32
+ addFunctionsByArray(functionList: any[]): void;
33
+ /**
34
+ * Extend an object with properties from another
35
+ * @param obj - Target object
36
+ * @param src - Source object
37
+ * @returns Extended object
38
+ */
39
+ extendObj(obj: any, src: any): any;
40
+ static get instance(): Combinator;
41
+ }
42
+ declare const instance: Combinator;
43
+ export = instance;
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ const koa_router_1 = __importDefault(require("koa-router"));
39
+ const directory = __importStar(require("./directory"));
40
+ const service_1 = require("../services/functions/service");
41
+ class Combinator {
42
+ async combineRoutesByFilePath(rootDirectory, app) {
43
+ // find route paths
44
+ const option = {
45
+ name: 'router',
46
+ filter: ['.js'],
47
+ };
48
+ let routerPaths = [];
49
+ try {
50
+ routerPaths = await directory.find(rootDirectory, option);
51
+ }
52
+ catch (e) {
53
+ console.log(e);
54
+ }
55
+ // create and combine routes into the app
56
+ for (let i = 0; i < routerPaths.length; i++) {
57
+ const service = require(routerPaths[i]);
58
+ const name = service.name;
59
+ const serviceRouter = new koa_router_1.default();
60
+ serviceRouter.use(`/${name}`, service.main.routes());
61
+ app.use(serviceRouter.routes());
62
+ }
63
+ }
64
+ /**
65
+ * Combine modules from files in a directory
66
+ * @param options - Configuration options
67
+ */
68
+ async combineModulesByFilePath({ rootDirectory, filename, combineWithRoot, convertToArray, }) {
69
+ // find route paths
70
+ let rootObject_temp;
71
+ const option = {
72
+ name: filename.name,
73
+ filter: [filename.extension],
74
+ };
75
+ let modulesPath = [];
76
+ try {
77
+ modulesPath = await directory.find(rootDirectory, option);
78
+ }
79
+ catch (e) {
80
+ console.log(e);
81
+ }
82
+ // create and combine routes into the app
83
+ for (let i = 0; i < modulesPath.length; i++) {
84
+ const moduleObject = require(modulesPath[i]);
85
+ // act by otherOption
86
+ if (combineWithRoot) {
87
+ if (moduleObject.name)
88
+ delete moduleObject.name;
89
+ if (Array.isArray(moduleObject)) {
90
+ if (!rootObject_temp)
91
+ rootObject_temp = [];
92
+ rootObject_temp = [...rootObject_temp, ...moduleObject];
93
+ }
94
+ else {
95
+ rootObject_temp = this.extendObj(rootObject_temp, moduleObject);
96
+ }
97
+ }
98
+ // default act
99
+ else {
100
+ const name = moduleObject.name;
101
+ rootObject_temp[name] = moduleObject;
102
+ }
103
+ }
104
+ // options
105
+ // convertToArray
106
+ if (convertToArray) {
107
+ rootObject_temp = Object.values(rootObject_temp);
108
+ }
109
+ // set result to main rootObject
110
+ return rootObject_temp;
111
+ }
112
+ /**
113
+ * Combine functions from files in a directory
114
+ * @param options - Function options
115
+ */
116
+ async combineFunctionsByFilePath({ rootDirectory, filename }) {
117
+ // find route paths
118
+ const option = {
119
+ name: filename.name,
120
+ filter: [filename.extension],
121
+ };
122
+ let functionsPaths = [];
123
+ try {
124
+ functionsPaths = await directory.find(rootDirectory, option);
125
+ }
126
+ catch (e) {
127
+ console.log(e);
128
+ }
129
+ // create and combine routes into the app
130
+ for (let i = 0; i < functionsPaths.length; i++) {
131
+ const modularFunctions = require(functionsPaths[i]);
132
+ if (!modularFunctions.functions) {
133
+ throw new Error(`Module file ${functionsPaths[i]} does not have functions property.`);
134
+ }
135
+ // if array
136
+ if (Array.isArray(modularFunctions.functions)) {
137
+ for (const moduleFunction of modularFunctions.functions) {
138
+ (0, service_1.addFunction)(moduleFunction);
139
+ }
140
+ }
141
+ else {
142
+ (0, service_1.addFunction)(modularFunctions.functions);
143
+ }
144
+ }
145
+ }
146
+ /**
147
+ * Add functions from an array
148
+ * @param functionList - List of functions to add
149
+ */
150
+ addFunctionsByArray(functionList) {
151
+ for (const functionItem of functionList) {
152
+ (0, service_1.addFunction)(functionItem);
153
+ }
154
+ }
155
+ /**
156
+ * Extend an object with properties from another
157
+ * @param obj - Target object
158
+ * @param src - Source object
159
+ * @returns Extended object
160
+ */
161
+ extendObj(obj, src) {
162
+ obj = obj || {};
163
+ for (const key in src) {
164
+ if (Object.prototype.hasOwnProperty.call(src, key))
165
+ obj[key] = src[key];
166
+ }
167
+ return obj;
168
+ }
169
+ static get instance() {
170
+ return instance;
171
+ }
172
+ }
173
+ const instance = new Combinator();
174
+ module.exports = instance;
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Type for database operations that can trigger a callback
3
+ */
4
+ export type DatabaseOperation = 'find' | 'find-one' | 'count' | 'update-one' | 'insert-one' | 'remove-one' | 'aggregate';
5
+ /**
6
+ * Context interface for database trigger callbacks
7
+ * @interface DatabaseTriggerContext
8
+ * @property {Record<string, any>} query - The query parameters used in the database operation
9
+ * @property {any | any[]} queryResult - The result of the database operation
10
+ */
11
+ export interface DatabaseTriggerContext {
12
+ query: Record<string, any>;
13
+ queryResult: any | any[];
14
+ }
15
+ /**
16
+ * The callback function to be executed on specific database operations
17
+ * @param {DatabaseTriggerContext} context - The context of the database operation
18
+ * @example
19
+ * ```typescript
20
+ * const trigger = new DatabaseTrigger('insert-one', (context) => {
21
+ * console.log('New document inserted:', context.queryResult);
22
+ * });
23
+ * ```
24
+ */
25
+ type DatabaseTriggerCallback = (context: DatabaseTriggerContext) => void;
26
+ /**
27
+ * in a complex application, you may need to perform additional actions after a database operation.
28
+ * this is where DatabaseTrigger comes in. so you can define a callback to be executed on specific database operations for a collection.
29
+ *
30
+ * Supported triggers are:
31
+ *
32
+ * | Trigger | Description |
33
+ * | ------------ | ------------------------------------------------------------ |
34
+ * | `find` | Triggered when a find query is executed on collection. |
35
+ * | `find-one` | Triggered when a find one query is executed on collection. |
36
+ * | `count` | Triggered when a count query is executed on collection. |
37
+ * | `update-one` | Triggered when a update one query is executed on collection. |
38
+ * | `insert-one` | Triggered when a insert one query is executed on collection. |
39
+ * | `remove-one` | Triggered when a remove one query is executed on collection. |
40
+ * | `aggregate` | Triggered when a aggregate query is executed on collection. |
41
+ *
42
+ * @property {DatabaseOperation} operation - The database operation that triggers the callback
43
+ * @property {DatabaseTriggerCallback} callback - The callback function to be executed
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * import { DatabaseTrigger } from '@server-ts/database';
48
+ *
49
+ * const trigger = new DatabaseTrigger('insert-one', ({ query, queryResult }) => {
50
+ * console.log('New document inserted:', queryResult);
51
+ *
52
+ * try {
53
+ * // Perform additional actions after document insertion
54
+ * } catch (error) {
55
+ * console.error('Error performing additional actions:', error);
56
+ * }
57
+ * });
58
+ *
59
+ * // Use the trigger in a collection definition
60
+ * const collection = new CollectionDefinition({
61
+ * triggers: [trigger]
62
+ * });
63
+ * ```
64
+ */
65
+ export declare class DatabaseTrigger {
66
+ operation: DatabaseOperation;
67
+ callback: (context: DatabaseTriggerContext) => void;
68
+ /**
69
+ * @hidden
70
+ *
71
+ * Creates a new DatabaseTrigger instance
72
+ * @param {DatabaseOperation} operation - The database operation to trigger on
73
+ * @param {DatabaseTriggerCallback} [callback=() => {}] - The callback function to execute
74
+ *
75
+ * @example
76
+ * ```typescript
77
+ * const trigger = new DatabaseTrigger('insert-one', (context) => {
78
+ * console.log('New document inserted:', context.queryResult);
79
+ * });
80
+ *
81
+ */
82
+ constructor(operation: DatabaseOperation, callback?: DatabaseTriggerCallback);
83
+ }
84
+ export default DatabaseTrigger;
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DatabaseTrigger = void 0;
4
+ /**
5
+ * in a complex application, you may need to perform additional actions after a database operation.
6
+ * this is where DatabaseTrigger comes in. so you can define a callback to be executed on specific database operations for a collection.
7
+ *
8
+ * Supported triggers are:
9
+ *
10
+ * | Trigger | Description |
11
+ * | ------------ | ------------------------------------------------------------ |
12
+ * | `find` | Triggered when a find query is executed on collection. |
13
+ * | `find-one` | Triggered when a find one query is executed on collection. |
14
+ * | `count` | Triggered when a count query is executed on collection. |
15
+ * | `update-one` | Triggered when a update one query is executed on collection. |
16
+ * | `insert-one` | Triggered when a insert one query is executed on collection. |
17
+ * | `remove-one` | Triggered when a remove one query is executed on collection. |
18
+ * | `aggregate` | Triggered when a aggregate query is executed on collection. |
19
+ *
20
+ * @property {DatabaseOperation} operation - The database operation that triggers the callback
21
+ * @property {DatabaseTriggerCallback} callback - The callback function to be executed
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * import { DatabaseTrigger } from '@server-ts/database';
26
+ *
27
+ * const trigger = new DatabaseTrigger('insert-one', ({ query, queryResult }) => {
28
+ * console.log('New document inserted:', queryResult);
29
+ *
30
+ * try {
31
+ * // Perform additional actions after document insertion
32
+ * } catch (error) {
33
+ * console.error('Error performing additional actions:', error);
34
+ * }
35
+ * });
36
+ *
37
+ * // Use the trigger in a collection definition
38
+ * const collection = new CollectionDefinition({
39
+ * triggers: [trigger]
40
+ * });
41
+ * ```
42
+ */
43
+ class DatabaseTrigger {
44
+ /**
45
+ * @hidden
46
+ *
47
+ * Creates a new DatabaseTrigger instance
48
+ * @param {DatabaseOperation} operation - The database operation to trigger on
49
+ * @param {DatabaseTriggerCallback} [callback=() => {}] - The callback function to execute
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * const trigger = new DatabaseTrigger('insert-one', (context) => {
54
+ * console.log('New document inserted:', context.queryResult);
55
+ * });
56
+ *
57
+ */
58
+ constructor(operation, callback = () => { }) {
59
+ this.operation = operation;
60
+ this.callback = callback;
61
+ }
62
+ }
63
+ exports.DatabaseTrigger = DatabaseTrigger;
64
+ exports.default = DatabaseTrigger;
@@ -0,0 +1,25 @@
1
+ import mongoose from 'mongoose';
2
+ /**
3
+ * File schema interface
4
+ */
5
+ export interface IFile {
6
+ originalName: string;
7
+ fileName: string;
8
+ owner: string;
9
+ format: string;
10
+ tag: string;
11
+ size: number;
12
+ createdAt?: Date;
13
+ updatedAt?: Date;
14
+ }
15
+ /**
16
+ * File schema
17
+ */
18
+ export declare const fileSchema: mongoose.Schema<IFile, mongoose.Model<IFile, any, any>, undefined, {}>;
19
+ /**
20
+ * Schema definitions
21
+ */
22
+ export declare const schemas: {
23
+ file: mongoose.Schema<IFile, mongoose.Model<IFile, any, any>, undefined, {}>;
24
+ };
25
+ export default schemas;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.schemas = exports.fileSchema = void 0;
7
+ const mongoose_1 = __importDefault(require("mongoose"));
8
+ const { Schema } = mongoose_1.default;
9
+ /**
10
+ * File schema
11
+ */
12
+ exports.fileSchema = new Schema({
13
+ originalName: String,
14
+ fileName: String,
15
+ owner: String,
16
+ format: String,
17
+ // Tag being used as the parent dir for files
18
+ // uploadDir/$format/$tag/timestamp.format
19
+ tag: String,
20
+ size: Number,
21
+ }, { timestamps: true });
22
+ /**
23
+ * Schema definitions
24
+ */
25
+ exports.schemas = {
26
+ file: exports.fileSchema,
27
+ };
28
+ exports.default = exports.schemas;
@@ -0,0 +1,20 @@
1
+ interface DirectorySettings {
2
+ name?: string;
3
+ filter?: string[];
4
+ }
5
+ type WalkCallback = (err: Error | null, results: string[]) => void;
6
+ /**
7
+ * Walk through a directory and its subdirectories
8
+ * @param dir - Directory to walk
9
+ * @param settings - Settings for filtering files
10
+ * @param done - Callback function
11
+ */
12
+ declare function walk(dir: string, settings: DirectorySettings, done: WalkCallback): void;
13
+ /**
14
+ * Find files in a directory with Promise API
15
+ * @param dir - Directory to search
16
+ * @param settings - Settings for filtering files
17
+ * @returns Promise resolving to an array of file paths
18
+ */
19
+ declare function find(dir: string, settings: DirectorySettings): Promise<string[]>;
20
+ export { walk, find };