@modular-rest/server 1.11.12 → 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 -70
  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 -9
  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,99 @@
1
+ /**
2
+ * Type for database operations that can trigger a callback
3
+ */
4
+ export type DatabaseOperation =
5
+ | 'find'
6
+ | 'find-one'
7
+ | 'count'
8
+ | 'update-one'
9
+ | 'insert-one'
10
+ | 'remove-one'
11
+ | 'aggregate';
12
+
13
+ /**
14
+ * Context interface for database trigger callbacks
15
+ * @interface DatabaseTriggerContext
16
+ * @property {Record<string, any>} query - The query parameters used in the database operation
17
+ * @property {any | any[]} queryResult - The result of the database operation
18
+ */
19
+ export interface DatabaseTriggerContext {
20
+ query: Record<string, any>;
21
+ queryResult: any | any[];
22
+ }
23
+
24
+ /**
25
+ * The callback function to be executed on specific database operations
26
+ * @param {DatabaseTriggerContext} context - The context of the database operation
27
+ * @example
28
+ * ```typescript
29
+ * const trigger = new DatabaseTrigger('insert-one', (context) => {
30
+ * console.log('New document inserted:', context.queryResult);
31
+ * });
32
+ * ```
33
+ */
34
+ type DatabaseTriggerCallback = (context: DatabaseTriggerContext) => void;
35
+
36
+ /**
37
+ * in a complex application, you may need to perform additional actions after a database operation.
38
+ * this is where DatabaseTrigger comes in. so you can define a callback to be executed on specific database operations for a collection.
39
+ *
40
+ * Supported triggers are:
41
+ *
42
+ * | Trigger | Description |
43
+ * | ------------ | ------------------------------------------------------------ |
44
+ * | `find` | Triggered when a find query is executed on collection. |
45
+ * | `find-one` | Triggered when a find one query is executed on collection. |
46
+ * | `count` | Triggered when a count query is executed on collection. |
47
+ * | `update-one` | Triggered when a update one query is executed on collection. |
48
+ * | `insert-one` | Triggered when a insert one query is executed on collection. |
49
+ * | `remove-one` | Triggered when a remove one query is executed on collection. |
50
+ * | `aggregate` | Triggered when a aggregate query is executed on collection. |
51
+ *
52
+ * @property {DatabaseOperation} operation - The database operation that triggers the callback
53
+ * @property {DatabaseTriggerCallback} callback - The callback function to be executed
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * import { DatabaseTrigger } from '@server-ts/database';
58
+ *
59
+ * const trigger = new DatabaseTrigger('insert-one', ({ query, queryResult }) => {
60
+ * console.log('New document inserted:', queryResult);
61
+ *
62
+ * try {
63
+ * // Perform additional actions after document insertion
64
+ * } catch (error) {
65
+ * console.error('Error performing additional actions:', error);
66
+ * }
67
+ * });
68
+ *
69
+ * // Use the trigger in a collection definition
70
+ * const collection = new CollectionDefinition({
71
+ * triggers: [trigger]
72
+ * });
73
+ * ```
74
+ */
75
+ export class DatabaseTrigger {
76
+ operation: DatabaseOperation;
77
+ callback: (context: DatabaseTriggerContext) => void;
78
+
79
+ /**
80
+ * @hidden
81
+ *
82
+ * Creates a new DatabaseTrigger instance
83
+ * @param {DatabaseOperation} operation - The database operation to trigger on
84
+ * @param {DatabaseTriggerCallback} [callback=() => {}] - The callback function to execute
85
+ *
86
+ * @example
87
+ * ```typescript
88
+ * const trigger = new DatabaseTrigger('insert-one', (context) => {
89
+ * console.log('New document inserted:', context.queryResult);
90
+ * });
91
+ *
92
+ */
93
+ constructor(operation: DatabaseOperation, callback: DatabaseTriggerCallback = () => {}) {
94
+ this.operation = operation;
95
+ this.callback = callback;
96
+ }
97
+ }
98
+
99
+ export default DatabaseTrigger;
@@ -0,0 +1,44 @@
1
+ import mongoose from 'mongoose';
2
+ const { Schema } = mongoose;
3
+
4
+ /**
5
+ * File schema interface
6
+ */
7
+ export interface IFile {
8
+ originalName: string;
9
+ fileName: string;
10
+ owner: string;
11
+ format: string;
12
+ // Tag being used as the parent dir for files
13
+ // uploadDir/$format/$tag/timestamp.format
14
+ tag: string;
15
+ size: number;
16
+ createdAt?: Date;
17
+ updatedAt?: Date;
18
+ }
19
+
20
+ /**
21
+ * File schema
22
+ */
23
+ export const fileSchema = new Schema<IFile>(
24
+ {
25
+ originalName: String,
26
+ fileName: String,
27
+ owner: String,
28
+ format: String,
29
+ // Tag being used as the parent dir for files
30
+ // uploadDir/$format/$tag/timestamp.format
31
+ tag: String,
32
+ size: Number,
33
+ },
34
+ { timestamps: true }
35
+ );
36
+
37
+ /**
38
+ * Schema definitions
39
+ */
40
+ export const schemas = {
41
+ file: fileSchema,
42
+ };
43
+
44
+ export default schemas;
@@ -1,19 +1,39 @@
1
- const fs = require("fs");
2
- const path = require("path");
1
+ import fs from 'fs';
2
+ import path from 'path';
3
3
 
4
- function walk(dir, settings, done) {
5
- let results = [];
4
+ interface DirectorySettings {
5
+ name?: string;
6
+ filter?: string[];
7
+ }
8
+
9
+ type WalkCallback = (err: Error | null, results: string[]) => void;
10
+
11
+ /**
12
+ * Walk through a directory and its subdirectories
13
+ * @param dir - Directory to walk
14
+ * @param settings - Settings for filtering files
15
+ * @param done - Callback function
16
+ */
17
+ function walk(dir: string, settings: DirectorySettings, done: WalkCallback): void {
18
+ let results: string[] = [];
6
19
 
7
20
  // Read director file and folders
8
21
  fs.readdir(dir, function (err, list) {
9
22
  if (err) return done(err, results);
10
23
 
11
- var pending = list.length;
24
+ let pending = list.length;
12
25
  if (!pending) return done(null, results);
13
26
 
14
27
  list.forEach(function (file) {
15
28
  file = path.join(dir, file);
16
29
  fs.stat(file, function (err, stat) {
30
+ if (err) {
31
+ // Handle file stat error but continue with other files
32
+ console.error(`Error reading file stats for ${file}:`, err);
33
+ if (!--pending) done(null, results);
34
+ return;
35
+ }
36
+
17
37
  // If directory, execute a recursive call
18
38
  if (stat && stat.isDirectory()) {
19
39
  // Add directory to array [comment if you need to remove the directories from the array]
@@ -24,9 +44,9 @@ function walk(dir, settings, done) {
24
44
  });
25
45
  } else {
26
46
  // file filter
27
- var extension = path.extname(file);
28
- var fileName = path.basename(file).split(".")[0];
29
- var fileNameKey = true;
47
+ const extension = path.extname(file);
48
+ const fileName = path.basename(file).split('.')[0];
49
+ let fileNameKey = true;
30
50
 
31
51
  // name filter
32
52
  if (settings.name && settings.name === fileName) fileNameKey = true;
@@ -35,9 +55,8 @@ function walk(dir, settings, done) {
35
55
  // extension filter
36
56
  if (settings.filter && fileNameKey) {
37
57
  settings.filter.forEach(function (element) {
38
- if (element.toLowerCase() === extension.toLowerCase())
39
- results.push(file);
40
- }, this);
58
+ if (element.toLowerCase() === extension.toLowerCase()) results.push(file);
59
+ });
41
60
  }
42
61
 
43
62
  // push any file if no option
@@ -50,16 +69,19 @@ function walk(dir, settings, done) {
50
69
  });
51
70
  }
52
71
 
53
- function find(dir, settings) {
54
- return new Promise((don, reject) => {
72
+ /**
73
+ * Find files in a directory with Promise API
74
+ * @param dir - Directory to search
75
+ * @param settings - Settings for filtering files
76
+ * @returns Promise resolving to an array of file paths
77
+ */
78
+ function find(dir: string, settings: DirectorySettings): Promise<string[]> {
79
+ return new Promise((resolve, reject) => {
55
80
  walk(dir, settings, (err, result) => {
56
81
  if (err) reject(err);
57
- else don(result);
82
+ else resolve(result);
58
83
  });
59
84
  });
60
85
  }
61
86
 
62
- module.exports = {
63
- walk,
64
- find,
65
- };
87
+ export { walk, find };
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Pagination result interface
3
+ */
4
+ export interface PaginationResult {
5
+ pages: number;
6
+ page: number;
7
+ from: number;
8
+ to: number;
9
+ }
10
+
11
+ /**
12
+ * Creates a pagination object based on the given parameters.
13
+ * @param count - The total number of items to paginate.
14
+ * @param perPage - The number of items to display per page.
15
+ * @param page - The current page number.
16
+ * @returns An object containing pagination information.
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * import { paginator } from '@modular-rest/server';
21
+ *
22
+ * const pagination = paginator.create(100, 10, 1);
23
+ * // json response will be like this
24
+ * // {
25
+ * // pages: 10,
26
+ * // page: 1,
27
+ * // from: 0,
28
+ * // to: 10,
29
+ * // }
30
+ * ```
31
+ */
32
+ export function create(count: number, perPage: number, page: number): PaginationResult {
33
+ const totalPages = Math.ceil(count / perPage);
34
+
35
+ if (page > totalPages) page = 1;
36
+
37
+ let from = 0;
38
+ if (perPage === 1) from = page - 1;
39
+ else from = perPage * page - perPage;
40
+
41
+ if (page <= 1) from = 0;
42
+
43
+ const result: PaginationResult = {
44
+ pages: totalPages,
45
+ page: page,
46
+ from: from,
47
+ to: perPage,
48
+ };
49
+
50
+ return result;
51
+ }
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Response status type
3
+ */
4
+ export type ResponseStatus = 's' | 'f' | 'e';
5
+
6
+ /**
7
+ * Response object interface
8
+ */
9
+ export interface ResponseObject {
10
+ status: 'success' | 'fail' | 'error';
11
+ [key: string]: any;
12
+ }
13
+
14
+ /**
15
+ * Creates a response object with the given status and detail.
16
+ *
17
+ * @param status - The status of the response. Can be "s" for success, "f" for fail, or "e" for error.
18
+ * @param detail - The detail of the response. Can contain any additional information about the response.
19
+ * @returns The response object with the given status and detail.
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * import { reply } from '@modular-rest/server';
24
+ *
25
+ * // inside the router
26
+ * const response = reply.create("s", { message: "Hello, world!" });
27
+ * ctx.body = response;
28
+ * ctx.status = 200;
29
+ * ```
30
+ */
31
+ export function create(status: ResponseStatus, detail: Record<string, any> = {}): ResponseObject {
32
+ // Initialize with a default status that will be overwritten
33
+ const result: ResponseObject = {
34
+ status: 'success',
35
+ ...detail,
36
+ };
37
+
38
+ // define status
39
+ switch (status) {
40
+ case 's':
41
+ result.status = 'success';
42
+ break;
43
+
44
+ case 'f':
45
+ result.status = 'fail';
46
+ break;
47
+
48
+ case 'e':
49
+ result.status = 'error';
50
+ break;
51
+
52
+ default:
53
+ result.status = 'success';
54
+ break;
55
+ }
56
+
57
+ // return
58
+ return result;
59
+ }
@@ -0,0 +1,250 @@
1
+ /**
2
+ * Permission type string literal type that defines various access levels and capabilities
3
+
4
+ * @inline
5
+ */
6
+ export type AccessType =
7
+ | 'god_access'
8
+ | 'user_access'
9
+ | 'upload_file_access'
10
+ | 'remove_file_access'
11
+ | 'anonymous_access'
12
+ | 'advanced_settings'
13
+ | string;
14
+
15
+ /**
16
+ * Defines access control for a specific database collection
17
+ *
18
+ * @internal
19
+ */
20
+ export class AccessDefinition {
21
+ /** @hidden */
22
+ database: string;
23
+ /** @hidden */
24
+ collection: string;
25
+ /** @hidden */
26
+ permissionList: Permission[];
27
+
28
+ /**
29
+ * Creates a new AccessDefinition instance
30
+ * @param {Object} options - Configuration options
31
+ * @param {string} options.database - The name of the database
32
+ * @param {string} options.collection - The name of the collection
33
+ * @param {Permission[]} options.permissionList - List of permissions
34
+ */
35
+ constructor({
36
+ database,
37
+ collection,
38
+ permissionList,
39
+ }: {
40
+ database: string;
41
+ collection: string;
42
+ permissionList: Permission[];
43
+ }) {
44
+ this.database = database;
45
+ this.collection = collection;
46
+ this.permissionList = permissionList;
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Defines a permission for accessing data within the system. This class is a fundamental component used in both the {@link defineCollection} method and {@link CollectionDefinition} class
52
+ * by specifying which permission types can interact with them. The permission system matches a user's assigned permission types against
53
+ * the collection's permissions to determine access levels. For example, a collection can allow read access for 'user_access' while
54
+ * restricting writes to 'advanced_settings' permissions.
55
+ *
56
+ * @remark
57
+ * {@include ../../docs/system-access-type.md}
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * import { Permission } from '@modular-rest/server';
62
+ *
63
+ * const permission = new Permission({
64
+ * type: 'user_access',
65
+ * read: true,
66
+ * write: true,
67
+ * onlyOwnData: true,
68
+ * ownerIdField: 'userId'
69
+ * });
70
+ * ```
71
+ */
72
+ export class Permission {
73
+ /** @hidden */
74
+ accessType: AccessType;
75
+ /** @hidden */
76
+ read: boolean;
77
+ /** @hidden */
78
+ write: boolean;
79
+ /** @hidden */
80
+ onlyOwnData: boolean;
81
+ /** @hidden */
82
+ ownerIdField: string;
83
+
84
+ /**
85
+ * Creates a new Permission instance
86
+ * @param {Object} options - Configuration options
87
+ *
88
+ * @param {AccessType} options.type - The type of permission,system defined or custom. check the **Remarks section** for more information.
89
+ *
90
+ * @param {boolean} [options.read=false] - Whether read access is granted
91
+ * @param {boolean} [options.write=false] - Whether write access is granted
92
+ * @param {boolean} [options.onlyOwnData=false] - Whether access is limited to own data
93
+ * @param {string} [options.ownerIdField='refId'] - Field name for owner identification
94
+ */
95
+ constructor({
96
+ accessType: type,
97
+ read = false,
98
+ write = false,
99
+ onlyOwnData = false,
100
+ ownerIdField = 'refId',
101
+ }: {
102
+ accessType: AccessType;
103
+ read?: boolean;
104
+ write?: boolean;
105
+ onlyOwnData?: boolean;
106
+ ownerIdField?: string;
107
+ }) {
108
+ this.accessType = type;
109
+ this.read = read;
110
+ this.write = write;
111
+ this.onlyOwnData = onlyOwnData;
112
+ this.ownerIdField = ownerIdField;
113
+ }
114
+ }
115
+
116
+ /**
117
+ * A comprehensive access control mechanism that manages user permissions through grouped access types.
118
+ *
119
+ * Permission groups are a fundamental security concept that define and enforce what actions users
120
+ * can perform within the system. They provide a flexible and maintainable way to handle authorization
121
+ * by grouping related access types together.
122
+ *
123
+ * These groups enable users to:
124
+ * 1. Read and write data from collections that match their access types, allowing granular control
125
+ * over database operations
126
+ * 2. Execute specific functions that require matching access types, ensuring that sensitive
127
+ * operations are only performed by authorized users
128
+ * 3. Perform custom developer-defined actions by validating against the user's access types,
129
+ * enabling extensible permission-based features
130
+ *
131
+ * Permission groups can be configured as default groups for new users or anonymous groups for
132
+ * unauthenticated access, providing a complete authorization framework.
133
+ *
134
+ * @class PermissionGroup
135
+ * @property {string} title - The title of the permission group
136
+ * @property {boolean} isDefault - This is a default group, on `true` this permission group will be given to any new user automatically.
137
+ * @property {boolean} isAnonymous - This is a anonymous group, on `true` will be used for anonymous users.
138
+ * @property {AccessType[]} allowedAccessTypes - List of valid access types.
139
+ * @example
140
+ * ```typescript
141
+ * const group = new PermissionGroup({
142
+ * title: 'Admin',
143
+ * isDefault: true,
144
+ * allowedAccessTypes: ['god_access', 'advanced_settings']
145
+ * });
146
+ * ```
147
+ */
148
+ export class PermissionGroup {
149
+ /** @hidden */
150
+ title: string;
151
+ /** @hidden */
152
+ isDefault: boolean;
153
+ /** @hidden */
154
+ isAnonymous: boolean;
155
+ /** @hidden */
156
+ allowedAccessTypes: AccessType[];
157
+
158
+ /**
159
+ * Creates a new PermissionGroup instance
160
+ * @param {Object} options - Configuration options
161
+ * @param {string} options.title - The title of the group
162
+ * @param {boolean} [options.isDefault=false] - Whether this is a default group
163
+ * @param {boolean} [options.isAnonymous=false] - Whether this group is for anonymous users
164
+ * @param {AccessType[]} [options.allowedAccessTypes=[]] - List of valid permission types
165
+ */
166
+ constructor({
167
+ title,
168
+ isDefault = false,
169
+ isAnonymous = false,
170
+ allowedAccessTypes = [],
171
+ }: {
172
+ title: string;
173
+ isDefault?: boolean;
174
+ isAnonymous?: boolean;
175
+ allowedAccessTypes?: AccessType[];
176
+ }) {
177
+ this.title = title;
178
+ this.isDefault = isDefault;
179
+ this.isAnonymous = isAnonymous;
180
+ this.allowedAccessTypes = allowedAccessTypes;
181
+ }
182
+ }
183
+
184
+ /**
185
+ * Provides static access to access type constants
186
+ * @class AccessTypes
187
+ */
188
+ export class AccessTypes {
189
+ /**
190
+ * Get the string representing read access type
191
+ * @returns {string} The read access type
192
+ */
193
+ static get read(): string {
194
+ return 'read';
195
+ }
196
+
197
+ /**
198
+ * Get the string representing write access type
199
+ * @returns {string} The write access type
200
+ */
201
+ static get write(): string {
202
+ return 'write';
203
+ }
204
+ }
205
+
206
+ /**
207
+ * Provides static access to permission type constants
208
+ * @class PermissionTypes
209
+ */
210
+ export class PermissionTypes {
211
+ /**
212
+ * Get the string representing god access permission type
213
+ * @returns {string} The god access permission type
214
+ */
215
+ static get god_access(): AccessType {
216
+ return 'god_access';
217
+ }
218
+
219
+ /**
220
+ * Get the string representing advanced settings permission type
221
+ * @returns {string} The advanced settings permission type
222
+ */
223
+ static get advanced_settings(): AccessType {
224
+ return 'advanced_settings';
225
+ }
226
+
227
+ /**
228
+ * Get the string representing user access permission type
229
+ * @returns {string} The user access permission type
230
+ */
231
+ static get user_access(): AccessType {
232
+ return 'user_access';
233
+ }
234
+
235
+ /**
236
+ * Get the string representing upload file access permission type
237
+ * @returns {string} The upload file access permission type
238
+ */
239
+ static get upload_file_access(): AccessType {
240
+ return 'upload_file_access';
241
+ }
242
+
243
+ /**
244
+ * Get the string representing remove file access permission type
245
+ * @returns {string} The remove file access permission type
246
+ */
247
+ static get remove_file_access(): AccessType {
248
+ return 'remove_file_access';
249
+ }
250
+ }