@simtlix/simfinity-js 2.6.4 → 3.2.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.
- package/README.md +434 -72
- package/package.json +20 -5
- package/src/auth/errors.js +2 -44
- package/src/auth/expressions.js +2 -273
- package/src/auth/index.js +3 -390
- package/src/auth/register-mongo-object-id.js +7 -0
- package/src/auth/rules.js +3 -273
- package/src/const/QLOperator.js +1 -39
- package/src/const/QLSort.js +1 -28
- package/src/const/QLValue.js +1 -39
- package/src/errors/internal-server.error.js +1 -11
- package/src/errors/simfinity.error.js +1 -15
- package/src/index.js +43 -1990
- package/src/mcp.js +2 -0
- package/src/mongo/adapter.js +111 -0
- package/src/mongo/models.js +160 -0
- package/src/mongo/queries.js +570 -0
- package/src/mongo/transactions.js +81 -0
- package/src/plugins.js +2 -53
- package/src/scalars.js +2 -188
- package/src/validators.js +2 -250
- package/types/index.d.ts +198 -0
package/src/plugins.js
CHANGED
|
@@ -1,53 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export { createAuthPlugin } from './auth/index.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Apollo Server plugin to add count to GraphQL response extensions
|
|
7
|
-
* @returns {Object} Apollo Server plugin
|
|
8
|
-
*/
|
|
9
|
-
export const apolloCountPlugin = () => {
|
|
10
|
-
return {
|
|
11
|
-
async requestDidStart() {
|
|
12
|
-
return {
|
|
13
|
-
async willSendResponse({ contextValue, response }) {
|
|
14
|
-
if (response.body.kind === 'single' && contextValue?.count) {
|
|
15
|
-
response.body.singleResult.extensions = {
|
|
16
|
-
...(response.body.singleResult.extensions || {}),
|
|
17
|
-
count: contextValue.count,
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
},
|
|
21
|
-
};
|
|
22
|
-
},
|
|
23
|
-
};
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Envelop plugin to add count to GraphQL response extensions
|
|
28
|
-
* @returns {Object} Envelop plugin
|
|
29
|
-
*/
|
|
30
|
-
export const envelopCountPlugin = () => {
|
|
31
|
-
return {
|
|
32
|
-
onExecute() {
|
|
33
|
-
return {
|
|
34
|
-
onExecuteDone({ result, args }) {
|
|
35
|
-
if (args.contextValue?.count) {
|
|
36
|
-
result.extensions = {
|
|
37
|
-
...result.extensions,
|
|
38
|
-
count: args.contextValue.count,
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
};
|
|
43
|
-
},
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const plugins = {
|
|
48
|
-
createAuthPlugin,
|
|
49
|
-
apolloCountPlugin,
|
|
50
|
-
envelopCountPlugin,
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
export default plugins;
|
|
1
|
+
export * from '@simtlix/simfinity-core/plugins';
|
|
2
|
+
export { default } from '@simtlix/simfinity-core/plugins';
|
package/src/scalars.js
CHANGED
|
@@ -1,188 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
} from 'graphql';
|
|
4
|
-
import { createValidatedScalar } from './index.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Email scalar - validates email format
|
|
8
|
-
* Type name: Email_String
|
|
9
|
-
*/
|
|
10
|
-
export const EmailScalar = createValidatedScalar(
|
|
11
|
-
'Email',
|
|
12
|
-
'A valid email address',
|
|
13
|
-
GraphQLString,
|
|
14
|
-
(value) => {
|
|
15
|
-
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
16
|
-
if (!emailRegex.test(value)) {
|
|
17
|
-
throw new Error('Invalid email format');
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
|
-
);
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* URL scalar - validates URL format
|
|
24
|
-
* Type name: URL_String
|
|
25
|
-
*/
|
|
26
|
-
export const URLScalar = createValidatedScalar(
|
|
27
|
-
'URL',
|
|
28
|
-
'A valid URL',
|
|
29
|
-
GraphQLString,
|
|
30
|
-
(value) => {
|
|
31
|
-
try {
|
|
32
|
-
new URL(value);
|
|
33
|
-
} catch {
|
|
34
|
-
throw new Error('Invalid URL format');
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
);
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* PositiveInt scalar - validates positive integers
|
|
41
|
-
* Type name: PositiveInt_Int
|
|
42
|
-
*/
|
|
43
|
-
export const PositiveIntScalar = createValidatedScalar(
|
|
44
|
-
'PositiveInt',
|
|
45
|
-
'A positive integer',
|
|
46
|
-
GraphQLInt,
|
|
47
|
-
(value) => {
|
|
48
|
-
if (value <= 0) {
|
|
49
|
-
throw new Error('Value must be positive');
|
|
50
|
-
}
|
|
51
|
-
},
|
|
52
|
-
);
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* PositiveFloat scalar - validates positive floats
|
|
56
|
-
* Type name: PositiveFloat_Float
|
|
57
|
-
*/
|
|
58
|
-
export const PositiveFloatScalar = createValidatedScalar(
|
|
59
|
-
'PositiveFloat',
|
|
60
|
-
'A positive float',
|
|
61
|
-
GraphQLFloat,
|
|
62
|
-
(value) => {
|
|
63
|
-
if (value <= 0) {
|
|
64
|
-
throw new Error('Value must be positive');
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Factory function to create a bounded string scalar
|
|
71
|
-
* @param {string} name - Name for the scalar
|
|
72
|
-
* @param {number} min - Minimum length
|
|
73
|
-
* @param {number} max - Maximum length
|
|
74
|
-
* @returns {GraphQLScalarType} A scalar type with length validation
|
|
75
|
-
*/
|
|
76
|
-
export const createBoundedStringScalar = (name, min, max) => {
|
|
77
|
-
return createValidatedScalar(
|
|
78
|
-
name,
|
|
79
|
-
`A string with length between ${min} and ${max} characters`,
|
|
80
|
-
GraphQLString,
|
|
81
|
-
(value) => {
|
|
82
|
-
if (typeof value !== 'string') {
|
|
83
|
-
throw new Error('Value must be a string');
|
|
84
|
-
}
|
|
85
|
-
if (min !== undefined && value.length < min) {
|
|
86
|
-
throw new Error(`String must be at least ${min} characters`);
|
|
87
|
-
}
|
|
88
|
-
if (max !== undefined && value.length > max) {
|
|
89
|
-
throw new Error(`String must be at most ${max} characters`);
|
|
90
|
-
}
|
|
91
|
-
},
|
|
92
|
-
);
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Factory function to create a bounded integer scalar
|
|
97
|
-
* @param {string} name - Name for the scalar
|
|
98
|
-
* @param {number} min - Minimum value
|
|
99
|
-
* @param {number} max - Maximum value
|
|
100
|
-
* @returns {GraphQLScalarType} A scalar type with range validation
|
|
101
|
-
*/
|
|
102
|
-
export const createBoundedIntScalar = (name, min, max) => {
|
|
103
|
-
return createValidatedScalar(
|
|
104
|
-
name,
|
|
105
|
-
`An integer between ${min} and ${max}`,
|
|
106
|
-
GraphQLInt,
|
|
107
|
-
(value) => {
|
|
108
|
-
if (typeof value !== 'number' || isNaN(value)) {
|
|
109
|
-
throw new Error('Value must be a number');
|
|
110
|
-
}
|
|
111
|
-
if (min !== undefined && value < min) {
|
|
112
|
-
throw new Error(`Value must be at least ${min}`);
|
|
113
|
-
}
|
|
114
|
-
if (max !== undefined && value > max) {
|
|
115
|
-
throw new Error(`Value must be at most ${max}`);
|
|
116
|
-
}
|
|
117
|
-
},
|
|
118
|
-
);
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Factory function to create a bounded float scalar
|
|
123
|
-
* @param {string} name - Name for the scalar
|
|
124
|
-
* @param {number} min - Minimum value
|
|
125
|
-
* @param {number} max - Maximum value
|
|
126
|
-
* @returns {GraphQLScalarType} A scalar type with range validation
|
|
127
|
-
*/
|
|
128
|
-
export const createBoundedFloatScalar = (name, min, max) => {
|
|
129
|
-
return createValidatedScalar(
|
|
130
|
-
name,
|
|
131
|
-
`A float between ${min} and ${max}`,
|
|
132
|
-
GraphQLFloat,
|
|
133
|
-
(value) => {
|
|
134
|
-
if (typeof value !== 'number' || isNaN(value)) {
|
|
135
|
-
throw new Error('Value must be a number');
|
|
136
|
-
}
|
|
137
|
-
if (min !== undefined && value < min) {
|
|
138
|
-
throw new Error(`Value must be at least ${min}`);
|
|
139
|
-
}
|
|
140
|
-
if (max !== undefined && value > max) {
|
|
141
|
-
throw new Error(`Value must be at most ${max}`);
|
|
142
|
-
}
|
|
143
|
-
},
|
|
144
|
-
);
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Factory function to create a regex pattern string scalar
|
|
149
|
-
* @param {string} name - Name for the scalar
|
|
150
|
-
* @param {RegExp|string} pattern - Regex pattern to validate against
|
|
151
|
-
* @param {string} message - Error message if validation fails
|
|
152
|
-
* @returns {GraphQLScalarType} A scalar type with pattern validation
|
|
153
|
-
*/
|
|
154
|
-
export const createPatternStringScalar = (name, pattern, message) => {
|
|
155
|
-
const regex = typeof pattern === 'string' ? new RegExp(pattern) : pattern;
|
|
156
|
-
const errorMessage = message || 'Value does not match required pattern';
|
|
157
|
-
|
|
158
|
-
return createValidatedScalar(
|
|
159
|
-
name,
|
|
160
|
-
`A string matching the pattern: ${pattern}`,
|
|
161
|
-
GraphQLString,
|
|
162
|
-
(value) => {
|
|
163
|
-
if (typeof value !== 'string') {
|
|
164
|
-
throw new Error('Value must be a string');
|
|
165
|
-
}
|
|
166
|
-
if (!regex.test(value)) {
|
|
167
|
-
throw new Error(errorMessage);
|
|
168
|
-
}
|
|
169
|
-
},
|
|
170
|
-
);
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
// Export all scalars as an object for convenience
|
|
174
|
-
const scalars = {
|
|
175
|
-
// Pre-built scalars
|
|
176
|
-
EmailScalar,
|
|
177
|
-
URLScalar,
|
|
178
|
-
PositiveIntScalar,
|
|
179
|
-
PositiveFloatScalar,
|
|
180
|
-
// Factory functions
|
|
181
|
-
createBoundedStringScalar,
|
|
182
|
-
createBoundedIntScalar,
|
|
183
|
-
createBoundedFloatScalar,
|
|
184
|
-
createPatternStringScalar,
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
export default scalars;
|
|
188
|
-
|
|
1
|
+
export * from '@simtlix/simfinity-core/scalars';
|
|
2
|
+
export { default } from '@simtlix/simfinity-core/scalars';
|
package/src/validators.js
CHANGED
|
@@ -1,250 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Creates a validation object that works for both 'save' (CREATE) and 'update' (UPDATE) operations.
|
|
5
|
-
* The validators will be applied to both operations.
|
|
6
|
-
* For CREATE operations, the value must be provided and valid.
|
|
7
|
-
* For UPDATE operations, undefined/null values are allowed (field might not be updated),
|
|
8
|
-
* but if a value is provided, it must be valid.
|
|
9
|
-
*/
|
|
10
|
-
const createValidator = (validatorFn, required = false) => {
|
|
11
|
-
// Validator for CREATE operations - value is required if required=true
|
|
12
|
-
const validateCreate = async (typeName, fieldName, value, session) => {
|
|
13
|
-
if (required && (value === null || value === undefined)) {
|
|
14
|
-
throw new SimfinityError(`${fieldName} is required`, 'VALIDATION_ERROR', 400);
|
|
15
|
-
}
|
|
16
|
-
if (value !== null && value !== undefined) {
|
|
17
|
-
await validatorFn(typeName, fieldName, value, session);
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
// Validator for UPDATE operations - value is optional
|
|
22
|
-
const validateUpdate = async (typeName, fieldName, value, session) => {
|
|
23
|
-
// Skip validation if value is not provided (field is not being updated)
|
|
24
|
-
if (value === null || value === undefined) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
// If value is provided, validate it
|
|
28
|
-
await validatorFn(typeName, fieldName, value, session);
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
const validatorCreate = { validate: validateCreate };
|
|
32
|
-
const validatorUpdate = { validate: validateUpdate };
|
|
33
|
-
|
|
34
|
-
// Return validations for both CREATE and UPDATE operations
|
|
35
|
-
// Also support 'save'/'update' for backward compatibility (though code uses CREATE/UPDATE)
|
|
36
|
-
return {
|
|
37
|
-
CREATE: [validatorCreate],
|
|
38
|
-
UPDATE: [validatorUpdate],
|
|
39
|
-
save: [validatorCreate], // For backward compatibility
|
|
40
|
-
update: [validatorUpdate], // For backward compatibility
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* String validators
|
|
46
|
-
*/
|
|
47
|
-
export const stringLength = (name, min, max) => {
|
|
48
|
-
return createValidator(async (typeName, fieldName, value) => {
|
|
49
|
-
if (typeof value !== 'string') {
|
|
50
|
-
throw new SimfinityError(`${name} must be a string`, 'VALIDATION_ERROR', 400);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (min !== undefined && value.length < min) {
|
|
54
|
-
throw new SimfinityError(`${name} must be at least ${min} characters`, 'VALIDATION_ERROR', 400);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (max !== undefined && value.length > max) {
|
|
58
|
-
throw new SimfinityError(`${name} must be at most ${max} characters`, 'VALIDATION_ERROR', 400);
|
|
59
|
-
}
|
|
60
|
-
}, true); // Required for CREATE operations
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export const maxLength = (name, max) => {
|
|
64
|
-
return createValidator(async (typeName, fieldName, value) => {
|
|
65
|
-
if (typeof value !== 'string') {
|
|
66
|
-
throw new SimfinityError(`${name} must be a string`, 'VALIDATION_ERROR', 400);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (value.length > max) {
|
|
70
|
-
throw new SimfinityError(`${name} must be at most ${max} characters`, 'VALIDATION_ERROR', 400);
|
|
71
|
-
}
|
|
72
|
-
}, false); // Optional
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
export const pattern = (name, regex, message) => {
|
|
76
|
-
const regexObj = typeof regex === 'string' ? new RegExp(regex) : regex;
|
|
77
|
-
const errorMessage = message || `${name} format is invalid`;
|
|
78
|
-
|
|
79
|
-
return createValidator(async (typeName, fieldName, value) => {
|
|
80
|
-
if (typeof value !== 'string') {
|
|
81
|
-
throw new SimfinityError(`${name} must be a string`, 'VALIDATION_ERROR', 400);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (!regexObj.test(value)) {
|
|
85
|
-
throw new SimfinityError(errorMessage, 'VALIDATION_ERROR', 400);
|
|
86
|
-
}
|
|
87
|
-
}, false); // Optional
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
export const email = () => {
|
|
91
|
-
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
92
|
-
|
|
93
|
-
return createValidator(async (typeName, fieldName, value) => {
|
|
94
|
-
if (typeof value !== 'string') {
|
|
95
|
-
throw new SimfinityError('Email must be a string', 'VALIDATION_ERROR', 400);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (!emailRegex.test(value)) {
|
|
99
|
-
throw new SimfinityError('Invalid email format', 'VALIDATION_ERROR', 400);
|
|
100
|
-
}
|
|
101
|
-
}, false); // Optional
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
export const url = () => {
|
|
105
|
-
return createValidator(async (typeName, fieldName, value) => {
|
|
106
|
-
if (typeof value !== 'string') {
|
|
107
|
-
throw new SimfinityError('URL must be a string', 'VALIDATION_ERROR', 400);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
try {
|
|
111
|
-
// Use URL constructor for better validation
|
|
112
|
-
new URL(value);
|
|
113
|
-
} catch (e) {
|
|
114
|
-
console.log('Invalid URL format', e);
|
|
115
|
-
throw new SimfinityError('Invalid URL format', 'VALIDATION_ERROR', 400);
|
|
116
|
-
}
|
|
117
|
-
}, false); // Optional
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Number validators
|
|
122
|
-
*/
|
|
123
|
-
export const numberRange = (name, min, max) => {
|
|
124
|
-
return createValidator(async (typeName, fieldName, value) => {
|
|
125
|
-
if (typeof value !== 'number' || isNaN(value)) {
|
|
126
|
-
throw new SimfinityError(`${name} must be a number`, 'VALIDATION_ERROR', 400);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (min !== undefined && value < min) {
|
|
130
|
-
throw new SimfinityError(`${name} must be at least ${min}`, 'VALIDATION_ERROR', 400);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (max !== undefined && value > max) {
|
|
134
|
-
throw new SimfinityError(`${name} must be at most ${max}`, 'VALIDATION_ERROR', 400);
|
|
135
|
-
}
|
|
136
|
-
}, false); // Optional
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
export const positive = (name) => {
|
|
140
|
-
return createValidator(async (typeName, fieldName, value) => {
|
|
141
|
-
if (typeof value !== 'number' || isNaN(value)) {
|
|
142
|
-
throw new SimfinityError(`${name} must be a number`, 'VALIDATION_ERROR', 400);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (value <= 0) {
|
|
146
|
-
throw new SimfinityError(`${name} must be positive`, 'VALIDATION_ERROR', 400);
|
|
147
|
-
}
|
|
148
|
-
}, false); // Optional
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Array validators
|
|
153
|
-
*/
|
|
154
|
-
export const arrayLength = (name, maxItems, itemValidator) => {
|
|
155
|
-
return createValidator(async (typeName, fieldName, value, session) => {
|
|
156
|
-
if (!Array.isArray(value)) {
|
|
157
|
-
throw new SimfinityError(`${name} must be an array`, 'VALIDATION_ERROR', 400);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (maxItems !== undefined && value.length > maxItems) {
|
|
161
|
-
throw new SimfinityError(`${name} must have at most ${maxItems} items`, 'VALIDATION_ERROR', 400);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// If itemValidator is provided, validate each item
|
|
165
|
-
if (itemValidator && Array.isArray(itemValidator)) {
|
|
166
|
-
for (let i = 0; i < value.length; i++) {
|
|
167
|
-
for (const validator of itemValidator) {
|
|
168
|
-
await validator.validate(typeName, fieldName, value[i], session);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
}, false); // Optional
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Date validators
|
|
177
|
-
*/
|
|
178
|
-
export const dateFormat = (name, format) => {
|
|
179
|
-
return createValidator(async (typeName, fieldName, value) => {
|
|
180
|
-
// Handle Date objects, ISO strings, and timestamps
|
|
181
|
-
let date;
|
|
182
|
-
if (value instanceof Date) {
|
|
183
|
-
date = value;
|
|
184
|
-
} else if (typeof value === 'string') {
|
|
185
|
-
date = new Date(value);
|
|
186
|
-
} else if (typeof value === 'number') {
|
|
187
|
-
date = new Date(value);
|
|
188
|
-
} else {
|
|
189
|
-
throw new SimfinityError(`${name} must be a valid date`, 'VALIDATION_ERROR', 400);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
if (isNaN(date.getTime())) {
|
|
193
|
-
throw new SimfinityError(`${name} must be a valid date`, 'VALIDATION_ERROR', 400);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// If format is provided, validate format
|
|
197
|
-
if (format && typeof value === 'string') {
|
|
198
|
-
// Simple format validation - can be enhanced
|
|
199
|
-
const formatRegex = /^\d{4}-\d{2}-\d{2}$/; // YYYY-MM-DD
|
|
200
|
-
if (format === 'YYYY-MM-DD' && !formatRegex.test(value)) {
|
|
201
|
-
throw new SimfinityError(`${name} must be in format ${format}`, 'VALIDATION_ERROR', 400);
|
|
202
|
-
}
|
|
203
|
-
// Add more format patterns as needed
|
|
204
|
-
}
|
|
205
|
-
}, false); // Optional
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
export const futureDate = (name) => {
|
|
209
|
-
return createValidator(async (typeName, fieldName, value) => {
|
|
210
|
-
let date;
|
|
211
|
-
if (value instanceof Date) {
|
|
212
|
-
date = value;
|
|
213
|
-
} else if (typeof value === 'string') {
|
|
214
|
-
date = new Date(value);
|
|
215
|
-
} else if (typeof value === 'number') {
|
|
216
|
-
date = new Date(value);
|
|
217
|
-
} else {
|
|
218
|
-
throw new SimfinityError(`${name} must be a valid date`, 'VALIDATION_ERROR', 400);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
if (isNaN(date.getTime())) {
|
|
222
|
-
throw new SimfinityError(`${name} must be a valid date`, 'VALIDATION_ERROR', 400);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
if (date <= new Date()) {
|
|
226
|
-
throw new SimfinityError(`${name} must be a future date`, 'VALIDATION_ERROR', 400);
|
|
227
|
-
}
|
|
228
|
-
}, false); // Optional
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
// Export all validators as an object
|
|
232
|
-
const validators = {
|
|
233
|
-
// String validators
|
|
234
|
-
stringLength,
|
|
235
|
-
maxLength,
|
|
236
|
-
pattern,
|
|
237
|
-
email,
|
|
238
|
-
url,
|
|
239
|
-
// Number validators
|
|
240
|
-
numberRange,
|
|
241
|
-
positive,
|
|
242
|
-
// Array validators
|
|
243
|
-
arrayLength,
|
|
244
|
-
// Date validators
|
|
245
|
-
dateFormat,
|
|
246
|
-
futureDate,
|
|
247
|
-
};
|
|
248
|
-
|
|
249
|
-
export default validators;
|
|
250
|
-
|
|
1
|
+
export * from '@simtlix/simfinity-core/validators';
|
|
2
|
+
export { default } from '@simtlix/simfinity-core/validators';
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type declarations for @simtlix/simfinity-js.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors the MongoDB facade in src/index.js. Shared helper and MCP types are
|
|
5
|
+
* re-exported from their canonical packages; MongoDB model and session values
|
|
6
|
+
* remain deliberately typed loosely (`any`).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
GraphQLSchema,
|
|
11
|
+
GraphQLObjectType,
|
|
12
|
+
GraphQLInputObjectType,
|
|
13
|
+
GraphQLOutputType,
|
|
14
|
+
} from 'graphql';
|
|
15
|
+
import type {
|
|
16
|
+
DatabaseAdapter,
|
|
17
|
+
RuntimeRegistration,
|
|
18
|
+
SimfinityError,
|
|
19
|
+
} from '@simtlix/simfinity-core';
|
|
20
|
+
export {
|
|
21
|
+
auth,
|
|
22
|
+
buildErrorFormatter,
|
|
23
|
+
createRuntime,
|
|
24
|
+
createValidatedScalar,
|
|
25
|
+
InternalServerError,
|
|
26
|
+
plugins,
|
|
27
|
+
scalars,
|
|
28
|
+
SimfinityError,
|
|
29
|
+
validators,
|
|
30
|
+
} from '@simtlix/simfinity-core';
|
|
31
|
+
export type {
|
|
32
|
+
AuthPluginOptions,
|
|
33
|
+
AuthRule,
|
|
34
|
+
AuthRuleFunction,
|
|
35
|
+
EnvelopSchemaPlugin,
|
|
36
|
+
FieldValidations,
|
|
37
|
+
FieldValidator,
|
|
38
|
+
PermissionSchema,
|
|
39
|
+
PolicyExpression,
|
|
40
|
+
TypePermissions,
|
|
41
|
+
} from '@simtlix/simfinity-core';
|
|
42
|
+
export * from '@simtlix/simfinity-mcp';
|
|
43
|
+
export { default as mcp } from '@simtlix/simfinity-mcp';
|
|
44
|
+
export function createMongoAdapter(): DatabaseAdapter;
|
|
45
|
+
export function getRegistrations(): RuntimeRegistration[];
|
|
46
|
+
|
|
47
|
+
/* ========================================================================== *
|
|
48
|
+
* Core schema-building API (src/index.js)
|
|
49
|
+
* ========================================================================== */
|
|
50
|
+
|
|
51
|
+
/** Lifecycle hooks run before commit and may repeat on a transient transaction retry. */
|
|
52
|
+
export interface EntityController {
|
|
53
|
+
onSaving?(doc: any, args: any, session: any, context: any): void | Promise<void>;
|
|
54
|
+
onSaved?(result: any, args: any, session: any, context: any): void | Promise<void>;
|
|
55
|
+
onUpdating?(id: any, args: any, session: any, context: any): void | Promise<void>;
|
|
56
|
+
/** Receives the updated Mongoose document after parent and nested writes complete. */
|
|
57
|
+
onUpdated?(result: any, session: any, context: any): void | Promise<void>;
|
|
58
|
+
onDelete?(doc: any, session: any, context: any): void | Promise<void>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** State machine attached to an entity (initial state plus named action transitions). */
|
|
62
|
+
export interface StateMachine {
|
|
63
|
+
initialState: any;
|
|
64
|
+
actions: Record<string, any>;
|
|
65
|
+
[key: string]: any;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Register a Mongoose model + GraphQL type pair, exposing single and list endpoints. */
|
|
69
|
+
export function connect(
|
|
70
|
+
model: any,
|
|
71
|
+
gqltype: GraphQLObjectType,
|
|
72
|
+
simpleEntityEndpointName: string,
|
|
73
|
+
listEntitiesEndpointName: string,
|
|
74
|
+
controller?: EntityController | null,
|
|
75
|
+
onModelCreated?: ((model: any) => void) | null,
|
|
76
|
+
stateMachine?: StateMachine | null,
|
|
77
|
+
): void;
|
|
78
|
+
|
|
79
|
+
/** Register a GraphQL type without exposing root endpoints (e.g. embedded/related types). */
|
|
80
|
+
export function addNoEndpointType(gqltype: GraphQLObjectType): void;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Build the executable GraphQLSchema from every connected type. The first two
|
|
84
|
+
* allowlists are matched against the GraphQLObjectType INSTANCES passed to
|
|
85
|
+
* connect(); only custom mutations are matched by name.
|
|
86
|
+
*/
|
|
87
|
+
export function createSchema(
|
|
88
|
+
includedQueryTypes?: GraphQLObjectType[] | null,
|
|
89
|
+
includedMutationTypes?: GraphQLObjectType[] | null,
|
|
90
|
+
includedCustomMutations?: string[] | null,
|
|
91
|
+
): GraphQLSchema;
|
|
92
|
+
|
|
93
|
+
/** Register a custom mutation executed inside a transaction. */
|
|
94
|
+
export function registerMutation(
|
|
95
|
+
name: string,
|
|
96
|
+
description: string,
|
|
97
|
+
inputModel: GraphQLInputObjectType | null | undefined,
|
|
98
|
+
outputModel: GraphQLOutputType,
|
|
99
|
+
callback: (input: any, session: any, context: any) => any,
|
|
100
|
+
): void;
|
|
101
|
+
|
|
102
|
+
/** Operation context for root operations, generated non-embedded relation reads, and nested collection writes. */
|
|
103
|
+
export interface SimfinityMiddlewareContext {
|
|
104
|
+
args: any;
|
|
105
|
+
operation: string;
|
|
106
|
+
entry?: string;
|
|
107
|
+
type?: any;
|
|
108
|
+
context?: any;
|
|
109
|
+
[key: string]: any;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Register middleware before generated operations, including related-type reads and nested child writes.
|
|
114
|
+
* Nested operations keep root argument shapes and the GraphQL request context.
|
|
115
|
+
* Await next() to advance the middleware chain; throw to reject before the operation executes.
|
|
116
|
+
*/
|
|
117
|
+
export function use(
|
|
118
|
+
middleware: (
|
|
119
|
+
context: SimfinityMiddlewareContext,
|
|
120
|
+
next: () => Promise<void>,
|
|
121
|
+
) => void | Promise<void>,
|
|
122
|
+
): void;
|
|
123
|
+
|
|
124
|
+
/** Globally prevent Mongoose collection creation for generated models. */
|
|
125
|
+
export function preventCreatingCollection(prevent: boolean): void;
|
|
126
|
+
|
|
127
|
+
/** Process-wide limits for generated list and paginated aggregate queries. */
|
|
128
|
+
export interface QueryLimitsOptions {
|
|
129
|
+
/** Positive safe integer; defaults to 1000. Unpaged lists use min(100, maxPageSize). */
|
|
130
|
+
maxPageSize?: number;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Configure at startup. Invalid configuration throws INVALID_QUERY_LIMITS (400). */
|
|
134
|
+
export function configureQueryLimits(options?: QueryLimitsOptions): void;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Get the generated create input type, preserving field and list-item non-null
|
|
138
|
+
* wrappers. Update inputs remove only the outer wrapper (except entity id).
|
|
139
|
+
* References use IdInputType; embedded lists use nested inputs; referenced
|
|
140
|
+
* collections use added/updated/deleted operation inputs.
|
|
141
|
+
*/
|
|
142
|
+
export function getInputType(type: GraphQLObjectType | { name: string }): GraphQLInputObjectType;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Persist an object and its nested writes inside a transaction (runs controllers/validators).
|
|
146
|
+
* Without a session, owns a transaction on the model's connection and awaits cleanup.
|
|
147
|
+
* A supplied session must have an active transaction and belong to the model's MongoDB client.
|
|
148
|
+
* The caller then owns retries, commit, abort, and cleanup; inactive sessions are rejected.
|
|
149
|
+
* Owned transactions retry transient failures and uncertain commits separately, up to five times each.
|
|
150
|
+
*/
|
|
151
|
+
|
|
152
|
+
export function saveObject(
|
|
153
|
+
typeName: string,
|
|
154
|
+
args: Record<string, any>,
|
|
155
|
+
session?: any,
|
|
156
|
+
context?: any,
|
|
157
|
+
): Promise<any>;
|
|
158
|
+
|
|
159
|
+
/** Get the Mongoose model registered for a connected GraphQL type. */
|
|
160
|
+
export function getModel(gqltype: GraphQLObjectType | { name: string }): any;
|
|
161
|
+
|
|
162
|
+
/** Look up a connected GraphQL type by name (or by another type's name). */
|
|
163
|
+
export function getType(
|
|
164
|
+
typeName: string | { name: string },
|
|
165
|
+
): GraphQLObjectType | null | undefined;
|
|
166
|
+
|
|
167
|
+
/* ========================================================================== *
|
|
168
|
+
* Query building (src/index.js)
|
|
169
|
+
* ========================================================================== */
|
|
170
|
+
|
|
171
|
+
/** A QLFilterGroup input value (recursive AND/OR groups plus flat conditions). */
|
|
172
|
+
export interface FilterGroupInput {
|
|
173
|
+
AND?: FilterGroupInput[];
|
|
174
|
+
OR?: FilterGroupInput[];
|
|
175
|
+
conditions?: Array<{
|
|
176
|
+
field: string;
|
|
177
|
+
operator?: string;
|
|
178
|
+
value?: any;
|
|
179
|
+
path?: string;
|
|
180
|
+
}>;
|
|
181
|
+
[key: string]: any;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Translate list-query arguments (filters, AND/OR, sort, pagination) into a MongoDB aggregation pipeline. */
|
|
185
|
+
export function buildQuery(
|
|
186
|
+
input: Record<string, any>,
|
|
187
|
+
gqltype: GraphQLObjectType,
|
|
188
|
+
isCount?: boolean,
|
|
189
|
+
): Promise<Array<Record<string, any>>>;
|
|
190
|
+
|
|
191
|
+
/** Translate a QLFilterGroup into a MongoDB `$match` expression, collecting required lookups. */
|
|
192
|
+
export function buildFilterGroupMatch(
|
|
193
|
+
filterGroup: FilterGroupInput,
|
|
194
|
+
gqltype: GraphQLObjectType,
|
|
195
|
+
aggregateClauses: any[],
|
|
196
|
+
aggregationsIncluded: Record<string, any>,
|
|
197
|
+
depth?: number,
|
|
198
|
+
): Promise<any>;
|