@classytic/mongokit 2.0.0 → 2.1.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 (123) hide show
  1. package/README.md +221 -7
  2. package/dist/actions/index.cjs +479 -0
  3. package/dist/actions/index.cjs.map +1 -0
  4. package/dist/actions/index.d.cts +3 -0
  5. package/dist/actions/index.d.ts +3 -0
  6. package/dist/actions/index.js +473 -0
  7. package/dist/actions/index.js.map +1 -0
  8. package/dist/index-BfVJZF-3.d.cts +337 -0
  9. package/dist/index-CgOJ2pqz.d.ts +337 -0
  10. package/dist/index.cjs +2142 -0
  11. package/dist/index.cjs.map +1 -0
  12. package/dist/index.d.cts +239 -0
  13. package/dist/index.d.ts +239 -0
  14. package/dist/index.js +2108 -0
  15. package/dist/index.js.map +1 -0
  16. package/dist/memory-cache-DG2oSSbx.d.ts +142 -0
  17. package/dist/memory-cache-DqfFfKes.d.cts +142 -0
  18. package/dist/pagination/PaginationEngine.cjs +375 -0
  19. package/dist/pagination/PaginationEngine.cjs.map +1 -0
  20. package/dist/pagination/PaginationEngine.d.cts +117 -0
  21. package/dist/pagination/PaginationEngine.d.ts +117 -0
  22. package/dist/pagination/PaginationEngine.js +369 -0
  23. package/dist/pagination/PaginationEngine.js.map +1 -0
  24. package/dist/plugins/index.cjs +874 -0
  25. package/dist/plugins/index.cjs.map +1 -0
  26. package/dist/plugins/index.d.cts +275 -0
  27. package/dist/plugins/index.d.ts +275 -0
  28. package/dist/plugins/index.js +857 -0
  29. package/dist/plugins/index.js.map +1 -0
  30. package/dist/types-Nxhmi1aI.d.cts +510 -0
  31. package/dist/types-Nxhmi1aI.d.ts +510 -0
  32. package/dist/utils/index.cjs +667 -0
  33. package/dist/utils/index.cjs.map +1 -0
  34. package/dist/utils/index.d.cts +189 -0
  35. package/dist/utils/index.d.ts +189 -0
  36. package/dist/utils/index.js +643 -0
  37. package/dist/utils/index.js.map +1 -0
  38. package/package.json +44 -21
  39. package/src/Repository.js +0 -296
  40. package/src/actions/aggregate.js +0 -266
  41. package/src/actions/create.js +0 -59
  42. package/src/actions/delete.js +0 -88
  43. package/src/actions/index.js +0 -11
  44. package/src/actions/read.js +0 -188
  45. package/src/actions/update.js +0 -176
  46. package/src/hooks/lifecycle.js +0 -146
  47. package/src/index.js +0 -71
  48. package/src/pagination/PaginationEngine.js +0 -348
  49. package/src/pagination/utils/cursor.js +0 -119
  50. package/src/pagination/utils/filter.js +0 -42
  51. package/src/pagination/utils/limits.js +0 -82
  52. package/src/pagination/utils/sort.js +0 -101
  53. package/src/plugins/aggregate-helpers.plugin.js +0 -71
  54. package/src/plugins/audit-log.plugin.js +0 -60
  55. package/src/plugins/batch-operations.plugin.js +0 -66
  56. package/src/plugins/field-filter.plugin.js +0 -27
  57. package/src/plugins/index.js +0 -19
  58. package/src/plugins/method-registry.plugin.js +0 -140
  59. package/src/plugins/mongo-operations.plugin.js +0 -317
  60. package/src/plugins/soft-delete.plugin.js +0 -46
  61. package/src/plugins/subdocument.plugin.js +0 -66
  62. package/src/plugins/timestamp.plugin.js +0 -19
  63. package/src/plugins/validation-chain.plugin.js +0 -145
  64. package/src/types.d.ts +0 -87
  65. package/src/utils/error.js +0 -12
  66. package/src/utils/field-selection.js +0 -156
  67. package/src/utils/index.js +0 -12
  68. package/types/Repository.d.ts +0 -95
  69. package/types/Repository.d.ts.map +0 -1
  70. package/types/actions/aggregate.d.ts +0 -112
  71. package/types/actions/aggregate.d.ts.map +0 -1
  72. package/types/actions/create.d.ts +0 -21
  73. package/types/actions/create.d.ts.map +0 -1
  74. package/types/actions/delete.d.ts +0 -37
  75. package/types/actions/delete.d.ts.map +0 -1
  76. package/types/actions/index.d.ts +0 -6
  77. package/types/actions/index.d.ts.map +0 -1
  78. package/types/actions/read.d.ts +0 -135
  79. package/types/actions/read.d.ts.map +0 -1
  80. package/types/actions/update.d.ts +0 -58
  81. package/types/actions/update.d.ts.map +0 -1
  82. package/types/hooks/lifecycle.d.ts +0 -44
  83. package/types/hooks/lifecycle.d.ts.map +0 -1
  84. package/types/index.d.ts +0 -25
  85. package/types/index.d.ts.map +0 -1
  86. package/types/pagination/PaginationEngine.d.ts +0 -386
  87. package/types/pagination/PaginationEngine.d.ts.map +0 -1
  88. package/types/pagination/utils/cursor.d.ts +0 -40
  89. package/types/pagination/utils/cursor.d.ts.map +0 -1
  90. package/types/pagination/utils/filter.d.ts +0 -28
  91. package/types/pagination/utils/filter.d.ts.map +0 -1
  92. package/types/pagination/utils/limits.d.ts +0 -64
  93. package/types/pagination/utils/limits.d.ts.map +0 -1
  94. package/types/pagination/utils/sort.d.ts +0 -41
  95. package/types/pagination/utils/sort.d.ts.map +0 -1
  96. package/types/plugins/aggregate-helpers.plugin.d.ts +0 -6
  97. package/types/plugins/aggregate-helpers.plugin.d.ts.map +0 -1
  98. package/types/plugins/audit-log.plugin.d.ts +0 -6
  99. package/types/plugins/audit-log.plugin.d.ts.map +0 -1
  100. package/types/plugins/batch-operations.plugin.d.ts +0 -6
  101. package/types/plugins/batch-operations.plugin.d.ts.map +0 -1
  102. package/types/plugins/field-filter.plugin.d.ts +0 -6
  103. package/types/plugins/field-filter.plugin.d.ts.map +0 -1
  104. package/types/plugins/index.d.ts +0 -11
  105. package/types/plugins/index.d.ts.map +0 -1
  106. package/types/plugins/method-registry.plugin.d.ts +0 -3
  107. package/types/plugins/method-registry.plugin.d.ts.map +0 -1
  108. package/types/plugins/mongo-operations.plugin.d.ts +0 -4
  109. package/types/plugins/mongo-operations.plugin.d.ts.map +0 -1
  110. package/types/plugins/soft-delete.plugin.d.ts +0 -6
  111. package/types/plugins/soft-delete.plugin.d.ts.map +0 -1
  112. package/types/plugins/subdocument.plugin.d.ts +0 -6
  113. package/types/plugins/subdocument.plugin.d.ts.map +0 -1
  114. package/types/plugins/timestamp.plugin.d.ts +0 -6
  115. package/types/plugins/timestamp.plugin.d.ts.map +0 -1
  116. package/types/plugins/validation-chain.plugin.d.ts +0 -31
  117. package/types/plugins/validation-chain.plugin.d.ts.map +0 -1
  118. package/types/utils/error.d.ts +0 -11
  119. package/types/utils/error.d.ts.map +0 -1
  120. package/types/utils/field-selection.d.ts +0 -9
  121. package/types/utils/field-selection.d.ts.map +0 -1
  122. package/types/utils/index.d.ts +0 -2
  123. package/types/utils/index.d.ts.map +0 -1
@@ -1,176 +0,0 @@
1
- /**
2
- * Update Actions
3
- * Pure functions for document updates with optimizations
4
- */
5
-
6
- import { createError } from '../utils/error.js';
7
-
8
- /**
9
- * Update by ID
10
- */
11
- export async function update(Model, id, data, options = {}) {
12
- const document = await Model.findByIdAndUpdate(id, data, {
13
- new: true,
14
- runValidators: true,
15
- session: options.session,
16
- ...(options.updatePipeline !== undefined ? { updatePipeline: options.updatePipeline } : {}),
17
- })
18
- .select(options.select)
19
- .populate(parsePopulate(options.populate))
20
- .lean(options.lean);
21
-
22
- if (!document) {
23
- throw createError(404, 'Document not found');
24
- }
25
-
26
- return document;
27
- }
28
-
29
- /**
30
- * Update with query constraints (optimized)
31
- * Returns null if constraints not met (not an error)
32
- */
33
- export async function updateWithConstraints(Model, id, data, constraints = {}, options = {}) {
34
- const query = { _id: id, ...constraints };
35
-
36
- const document = await Model.findOneAndUpdate(query, data, {
37
- new: true,
38
- runValidators: true,
39
- session: options.session,
40
- ...(options.updatePipeline !== undefined ? { updatePipeline: options.updatePipeline } : {}),
41
- })
42
- .select(options.select)
43
- .populate(parsePopulate(options.populate))
44
- .lean(options.lean);
45
-
46
- return document;
47
- }
48
-
49
- /**
50
- * Update with validation (smart optimization)
51
- * 1-query on success, 2-queries for detailed errors
52
- */
53
- export async function updateWithValidation(
54
- Model,
55
- id,
56
- data,
57
- validationOptions = {},
58
- options = {}
59
- ) {
60
- const { buildConstraints, validateUpdate } = validationOptions;
61
-
62
- // Try optimized update with constraints
63
- if (buildConstraints) {
64
- const constraints = buildConstraints(data);
65
- const document = await updateWithConstraints(Model, id, data, constraints, options);
66
-
67
- if (document) {
68
- return { success: true, data: document };
69
- }
70
- }
71
-
72
- // Fetch for validation
73
- const existing = await Model.findById(id)
74
- .select(options.select)
75
- .lean();
76
-
77
- if (!existing) {
78
- return {
79
- success: false,
80
- error: {
81
- code: 404,
82
- message: 'Document not found',
83
- },
84
- };
85
- }
86
-
87
- // Run custom validation
88
- if (validateUpdate) {
89
- const validation = validateUpdate(existing, data);
90
- if (!validation.valid) {
91
- return {
92
- success: false,
93
- error: {
94
- code: 403,
95
- message: validation.message || 'Update not allowed',
96
- violations: validation.violations,
97
- },
98
- };
99
- }
100
- }
101
-
102
- // Validation passed - perform update
103
- const updated = await update(Model, id, data, options);
104
- return { success: true, data: updated };
105
- }
106
-
107
- /**
108
- * Update many documents
109
- */
110
- export async function updateMany(Model, query, data, options = {}) {
111
- const result = await Model.updateMany(query, data, {
112
- runValidators: true,
113
- session: options.session,
114
- ...(options.updatePipeline !== undefined ? { updatePipeline: options.updatePipeline } : {}),
115
- });
116
-
117
- return {
118
- matchedCount: result.matchedCount,
119
- modifiedCount: result.modifiedCount,
120
- };
121
- }
122
-
123
- /**
124
- * Update by query
125
- */
126
- export async function updateByQuery(Model, query, data, options = {}) {
127
- const document = await Model.findOneAndUpdate(query, data, {
128
- new: true,
129
- runValidators: true,
130
- session: options.session,
131
- ...(options.updatePipeline !== undefined ? { updatePipeline: options.updatePipeline } : {}),
132
- })
133
- .select(options.select)
134
- .populate(parsePopulate(options.populate))
135
- .lean(options.lean);
136
-
137
- if (!document && options.throwOnNotFound !== false) {
138
- throw createError(404, 'Document not found');
139
- }
140
-
141
- return document;
142
- }
143
-
144
- /**
145
- * Increment field
146
- */
147
- export async function increment(Model, id, field, value = 1, options = {}) {
148
- return update(Model, id, { $inc: { [field]: value } }, options);
149
- }
150
-
151
- /**
152
- * Push to array
153
- */
154
- export async function pushToArray(Model, id, field, value, options = {}) {
155
- return update(Model, id, { $push: { [field]: value } }, options);
156
- }
157
-
158
- /**
159
- * Pull from array
160
- */
161
- export async function pullFromArray(Model, id, field, value, options = {}) {
162
- return update(Model, id, { $pull: { [field]: value } }, options);
163
- }
164
-
165
- // Utilities
166
- function parsePopulate(populate) {
167
- if (!populate) return [];
168
- if (typeof populate === 'string') {
169
- return populate.split(',').map(p => p.trim());
170
- }
171
- if (Array.isArray(populate)) {
172
- return populate.map(p => typeof p === 'string' ? p.trim() : p);
173
- }
174
- return [populate];
175
- }
176
-
@@ -1,146 +0,0 @@
1
- /**
2
- * Lifecycle Hooks
3
- * Event system for repository actions
4
- */
5
-
6
- import { EventEmitter } from 'events';
7
-
8
- export class RepositoryLifecycle extends EventEmitter {
9
- constructor() {
10
- super();
11
- this.hooks = new Map();
12
- }
13
-
14
- /**
15
- * Register hook
16
- */
17
- on(event, handler) {
18
- if (!this.hooks.has(event)) {
19
- this.hooks.set(event, []);
20
- }
21
- this.hooks.get(event).push(handler);
22
- return super.on(event, handler);
23
- }
24
-
25
- /**
26
- * Execute hooks before action
27
- */
28
- async runBeforeHooks(action, context) {
29
- const event = `before:${action}`;
30
- await this.emit(event, context);
31
-
32
- const hooks = this.hooks.get(event) || [];
33
- for (const hook of hooks) {
34
- await hook(context);
35
- }
36
- }
37
-
38
- /**
39
- * Execute hooks after action
40
- */
41
- async runAfterHooks(action, context, result) {
42
- const event = `after:${action}`;
43
- await this.emit(event, context, result);
44
-
45
- const hooks = this.hooks.get(event) || [];
46
- for (const hook of hooks) {
47
- await hook(context, result);
48
- }
49
- }
50
-
51
- /**
52
- * Execute hooks on error
53
- */
54
- async runErrorHooks(action, context, error) {
55
- const event = `error:${action}`;
56
- await this.emit(event, context, error);
57
-
58
- const hooks = this.hooks.get(event) || [];
59
- for (const hook of hooks) {
60
- await hook(context, error);
61
- }
62
- }
63
- }
64
-
65
- /**
66
- * Hook decorators for common patterns
67
- */
68
- export const hooks = {
69
- /**
70
- * Auto-timestamp before create/update
71
- */
72
- autoTimestamp: () => ({
73
- 'before:create': (context) => {
74
- context.data.createdAt = new Date();
75
- context.data.updatedAt = new Date();
76
- },
77
- 'before:update': (context) => {
78
- context.data.updatedAt = new Date();
79
- },
80
- }),
81
-
82
- /**
83
- * Auto-inject user context
84
- */
85
- autoUser: (userField = 'userId') => ({
86
- 'before:create': (context) => {
87
- if (context.user && !context.data[userField]) {
88
- context.data[userField] = context.user._id || context.user.id;
89
- }
90
- },
91
- }),
92
-
93
- /**
94
- * Auto-inject organization scope
95
- */
96
- autoOrganization: (orgField = 'organizationId') => ({
97
- 'before:create': (context) => {
98
- if (context.organizationId && !context.data[orgField]) {
99
- context.data[orgField] = context.organizationId;
100
- }
101
- },
102
- }),
103
-
104
- /**
105
- * Audit log
106
- */
107
- auditLog: (logger) => ({
108
- 'after:create': (context, result) => {
109
- logger.info('Document created', {
110
- model: context.model,
111
- id: result._id,
112
- user: context.user?.id,
113
- });
114
- },
115
- 'after:update': (context, result) => {
116
- logger.info('Document updated', {
117
- model: context.model,
118
- id: result._id,
119
- user: context.user?.id,
120
- });
121
- },
122
- 'after:delete': (context, result) => {
123
- logger.info('Document deleted', {
124
- model: context.model,
125
- user: context.user?.id,
126
- });
127
- },
128
- }),
129
-
130
- /**
131
- * Cache invalidation
132
- */
133
- cacheInvalidation: (cache) => ({
134
- 'after:create': async (context, result) => {
135
- await cache.invalidate(`${context.model}:*`);
136
- },
137
- 'after:update': async (context, result) => {
138
- await cache.invalidate(`${context.model}:${result._id}`);
139
- await cache.invalidate(`${context.model}:*`);
140
- },
141
- 'after:delete': async (context) => {
142
- await cache.invalidate(`${context.model}:*`);
143
- },
144
- }),
145
- };
146
-
package/src/index.js DELETED
@@ -1,71 +0,0 @@
1
- /**
2
- * Repository Pattern - Data Access Layer
3
- *
4
- * Event-driven, plugin-based abstraction for MongoDB operations
5
- * Inspired by Meta & Stripe's repository patterns
6
- *
7
- * @module common/repositories
8
- *
9
- * Documentation:
10
- * - README.md - Main documentation (concise overview)
11
- * - QUICK_REFERENCE.md - One-page cheat sheet
12
- * - EXAMPLES.md - Detailed examples and patterns
13
- */
14
-
15
- /**
16
- * MongoKit - Event-driven repository pattern for MongoDB
17
- *
18
- * @module @classytic/mongokit
19
- * @author Sadman Chowdhury (Github: @siam923)
20
- * @license MIT
21
- */
22
-
23
- /**
24
- * @typedef {import('./pagination/PaginationEngine.js').PaginationConfig} PaginationConfig
25
- * @typedef {import('./pagination/PaginationEngine.js').OffsetPaginationOptions} OffsetPaginationOptions
26
- * @typedef {import('./pagination/PaginationEngine.js').KeysetPaginationOptions} KeysetPaginationOptions
27
- * @typedef {import('./pagination/PaginationEngine.js').AggregatePaginationOptions} AggregatePaginationOptions
28
- * @typedef {import('./pagination/PaginationEngine.js').OffsetPaginationResult} OffsetPaginationResult
29
- * @typedef {import('./pagination/PaginationEngine.js').KeysetPaginationResult} KeysetPaginationResult
30
- * @typedef {import('./pagination/PaginationEngine.js').AggregatePaginationResult} AggregatePaginationResult
31
- */
32
-
33
- export { Repository } from './Repository.js';
34
- export { PaginationEngine } from './pagination/PaginationEngine.js';
35
-
36
- // Plugins
37
- export { fieldFilterPlugin } from './plugins/field-filter.plugin.js';
38
- export { timestampPlugin } from './plugins/timestamp.plugin.js';
39
- export { auditLogPlugin } from './plugins/audit-log.plugin.js';
40
- export { softDeletePlugin } from './plugins/soft-delete.plugin.js';
41
- export { methodRegistryPlugin } from './plugins/method-registry.plugin.js';
42
- export {
43
- validationChainPlugin,
44
- blockIf,
45
- requireField,
46
- autoInject,
47
- immutableField,
48
- uniqueField,
49
- } from './plugins/validation-chain.plugin.js';
50
- export { mongoOperationsPlugin } from './plugins/mongo-operations.plugin.js';
51
- export { batchOperationsPlugin } from './plugins/batch-operations.plugin.js';
52
- export { aggregateHelpersPlugin } from './plugins/aggregate-helpers.plugin.js';
53
- export { subdocumentPlugin } from './plugins/subdocument.plugin.js';
54
-
55
- // Utilities
56
- export {
57
- getFieldsForUser,
58
- getMongooseProjection,
59
- filterResponseData,
60
- createFieldPreset,
61
- } from './utils/field-selection.js';
62
-
63
- export * as actions from './actions/index.js';
64
-
65
- import { Repository } from './Repository.js';
66
-
67
- export const createRepository = (Model, plugins = []) => {
68
- return new Repository(Model, plugins);
69
- };
70
-
71
- export default Repository;