@objectql/types 1.6.1 → 1.7.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/src/permission.ts DELETED
@@ -1,477 +0,0 @@
1
- /**
2
- * Permission and Security Metadata Types
3
- *
4
- * This module defines the TypeScript interfaces for ObjectQL's permission system,
5
- * implementing role-based access control (RBAC), field-level security, and
6
- * record-level rules.
7
- *
8
- * Based on specification: docs/spec/permission.md
9
- */
10
-
11
- /**
12
- * Basic CRUD operations for object-level permissions
13
- */
14
- export type ObjectOperation =
15
- | 'create' // Create new records
16
- | 'read' // View existing records
17
- | 'update' // Modify existing records
18
- | 'delete' // Delete records
19
- | 'view_all' // See all records (bypass ownership rules)
20
- | 'modify_all'; // Edit all records (bypass ownership rules)
21
-
22
- /**
23
- * Field-level operations
24
- */
25
- export type FieldOperation = 'read' | 'update';
26
-
27
- /**
28
- * Comparison operators for permission conditions
29
- */
30
- export type PermissionOperator =
31
- | '='
32
- | '!='
33
- | '>'
34
- | '>='
35
- | '<'
36
- | '<='
37
- | 'in'
38
- | 'not_in'
39
- | 'contains'
40
- | 'not_contains'
41
- | 'starts_with'
42
- | 'ends_with';
43
-
44
- /**
45
- * Object-level permissions controlling CRUD operations
46
- */
47
- export interface ObjectPermissions {
48
- /** Roles that can create new records */
49
- create?: string[];
50
- /** Roles that can view records */
51
- read?: string[];
52
- /** Roles that can update records */
53
- update?: string[];
54
- /** Roles that can delete records */
55
- delete?: string[];
56
- /** Roles that can view all records (bypass ownership rules) */
57
- view_all?: string[];
58
- /** Roles that can modify all records (bypass ownership rules) */
59
- modify_all?: string[];
60
- }
61
-
62
- /**
63
- * Field-level permissions for a specific field
64
- */
65
- export interface FieldPermission {
66
- /** Roles that can read this field */
67
- read?: string[];
68
- /** Roles that can update this field */
69
- update?: string[];
70
- }
71
-
72
- /**
73
- * Map of field names to their permissions
74
- */
75
- export type FieldPermissions = Record<string, FieldPermission>;
76
-
77
- /**
78
- * Condition type for record-level rules
79
- */
80
- export type ConditionType =
81
- | 'simple' // Single field comparison
82
- | 'complex' // Multiple conditions with AND/OR
83
- | 'formula' // Custom JavaScript/formula expression
84
- | 'lookup'; // Check related record data
85
-
86
- /**
87
- * Simple condition for a single field comparison
88
- */
89
- export interface SimpleCondition {
90
- type?: 'simple';
91
- /** Field to check */
92
- field: string;
93
- /** Comparison operator */
94
- operator: PermissionOperator;
95
- /** Value to compare against (can include variables like $current_user.id) */
96
- value: any;
97
- }
98
-
99
- /**
100
- * Simple condition element (inline form for complex expressions)
101
- */
102
- export interface ConditionElement {
103
- field: string;
104
- operator: PermissionOperator;
105
- value: any;
106
- }
107
-
108
- /**
109
- * Expression element in complex conditions
110
- */
111
- export type ConditionExpression =
112
- | SimpleCondition
113
- | ConditionElement
114
- | 'and'
115
- | 'or';
116
-
117
- /**
118
- * Complex condition with multiple clauses
119
- */
120
- export interface ComplexCondition {
121
- type: 'complex';
122
- /** Array of conditions and logical operators */
123
- expression: ConditionExpression[];
124
- }
125
-
126
- /**
127
- * Formula-based condition using custom logic
128
- */
129
- export interface FormulaCondition {
130
- type: 'formula';
131
- /** JavaScript expression or formula */
132
- formula: string;
133
- }
134
-
135
- /**
136
- * Lookup condition checking related record data
137
- */
138
- export interface LookupCondition {
139
- type: 'lookup';
140
- /** Related object to check */
141
- object: string;
142
- /** Field linking to related object */
143
- via: string;
144
- /** Condition to check on related object */
145
- condition: RecordRuleCondition;
146
- }
147
-
148
- /**
149
- * Union type for all condition types
150
- */
151
- export type RecordRuleCondition =
152
- | SimpleCondition
153
- | ComplexCondition
154
- | FormulaCondition
155
- | LookupCondition;
156
-
157
- /**
158
- * Permissions granted by a record rule
159
- */
160
- export interface RecordRulePermissions {
161
- /** Grant read permission */
162
- read?: boolean;
163
- /** Grant update permission */
164
- update?: boolean;
165
- /** Grant delete permission */
166
- delete?: boolean;
167
- }
168
-
169
- /**
170
- * Record-level rule for dynamic access control
171
- */
172
- export interface RecordRule {
173
- /** Unique name of the rule */
174
- name: string;
175
- /** Priority (higher priority rules take precedence, default: 0) */
176
- priority?: number;
177
- /** Human-readable description */
178
- description?: string;
179
- /** Condition for applying this rule */
180
- condition: RecordRuleCondition;
181
- /** Permissions granted when condition matches */
182
- permissions: RecordRulePermissions;
183
- }
184
-
185
- /**
186
- * Sharing rule type
187
- */
188
- export type SharingRuleType =
189
- | 'manual' // Users can manually share records
190
- | 'criteria' // Automatic sharing based on criteria
191
- | 'team'; // Team-based sharing
192
-
193
- /**
194
- * Who to share with
195
- */
196
- export interface SharedWith {
197
- type: 'role' | 'team' | 'user' | 'group';
198
- roles?: string[];
199
- teams?: string[];
200
- users?: string[];
201
- groups?: string[];
202
- }
203
-
204
- /**
205
- * Base sharing rule
206
- */
207
- export interface BaseSharingRule {
208
- /** Unique name of the rule */
209
- name: string;
210
- /** Type of sharing rule */
211
- type: SharingRuleType;
212
- /** Description */
213
- description?: string;
214
- /** Whether this rule is enabled */
215
- enabled?: boolean;
216
- /** Permissions granted through sharing */
217
- permissions: RecordRulePermissions;
218
- }
219
-
220
- /**
221
- * Manual sharing rule - users can manually share records
222
- */
223
- export interface ManualSharingRule extends BaseSharingRule {
224
- type: 'manual';
225
- }
226
-
227
- /**
228
- * Criteria-based sharing rule - automatic sharing
229
- */
230
- export interface CriteriaSharingRule extends BaseSharingRule {
231
- type: 'criteria';
232
- /** Condition for automatic sharing */
233
- condition: RecordRuleCondition;
234
- /** Who to share with */
235
- shared_with: SharedWith;
236
- }
237
-
238
- /**
239
- * Team-based sharing rule
240
- */
241
- export interface TeamSharingRule extends BaseSharingRule {
242
- type: 'team';
243
- /** Field containing team ID */
244
- team_field: string;
245
- }
246
-
247
- /**
248
- * Union type for all sharing rules
249
- */
250
- export type SharingRule = ManualSharingRule | CriteriaSharingRule | TeamSharingRule;
251
-
252
- /**
253
- * Condition for action execution
254
- */
255
- export interface ActionCondition {
256
- /** Field to check */
257
- field: string;
258
- /** Comparison operator */
259
- operator: PermissionOperator;
260
- /** Value to compare */
261
- value: any;
262
- }
263
-
264
- /**
265
- * Rate limiting configuration for actions
266
- */
267
- export interface ActionRateLimit {
268
- /** Maximum requests per hour */
269
- requests_per_hour?: number;
270
- /** Maximum requests per day */
271
- requests_per_day?: number;
272
- }
273
-
274
- /**
275
- * Permission configuration for a specific action
276
- */
277
- export interface ActionPermission {
278
- /** Roles that can execute this action */
279
- execute: string[];
280
- /** Conditions that must be met for execution */
281
- conditions?: ActionCondition[];
282
- /** Rate limiting */
283
- rate_limit?: ActionRateLimit;
284
- }
285
-
286
- /**
287
- * Map of action names to their permissions
288
- */
289
- export type ActionPermissions = Record<string, ActionPermission>;
290
-
291
- /**
292
- * Field restrictions for a view
293
- */
294
- export interface ViewFieldRestriction {
295
- /** Field name */
296
- [fieldName: string]: {
297
- /** Roles that can see this field in the view */
298
- visible_to: string[];
299
- };
300
- }
301
-
302
- /**
303
- * Permission configuration for a specific view
304
- */
305
- export interface ViewPermission {
306
- /** Roles that can access this view */
307
- access: string[];
308
- /** Field-level restrictions within the view */
309
- field_restrictions?: ViewFieldRestriction;
310
- }
311
-
312
- /**
313
- * Map of view names to their permissions
314
- */
315
- export type ViewPermissions = Record<string, ViewPermission>;
316
-
317
- /**
318
- * Row-level security configuration
319
- */
320
- export interface RowLevelSecurity {
321
- /** Whether RLS is enabled */
322
- enabled: boolean;
323
- /** Default rule applied to all users */
324
- default_rule?: SimpleCondition;
325
- /** Exceptions to the default rule */
326
- exceptions?: Array<{
327
- /** Role to apply exception to */
328
- role: string;
329
- /** Whether to bypass RLS entirely */
330
- bypass?: boolean;
331
- /** Custom condition for this role */
332
- condition?: RecordRuleCondition;
333
- }>;
334
- }
335
-
336
- /**
337
- * Field masking configuration for a specific field
338
- */
339
- export interface FieldMaskConfig {
340
- /** Mask format (e.g., "****-****-****-{last4}") */
341
- mask_format: string;
342
- /** Roles that can see the unmasked value */
343
- visible_to: string[];
344
- }
345
-
346
- /**
347
- * Map of field names to their masking configuration
348
- */
349
- export type FieldMasking = Record<string, FieldMaskConfig>;
350
-
351
- /**
352
- * Audit event type
353
- */
354
- export type AuditEventType =
355
- | 'permission_grant'
356
- | 'permission_revoke'
357
- | 'access_denied'
358
- | 'sensitive_field_access';
359
-
360
- /**
361
- * Alert configuration for suspicious activity
362
- */
363
- export interface AuditAlert {
364
- /** Event type to alert on */
365
- event: AuditEventType;
366
- /** Number of events to trigger alert */
367
- threshold: number;
368
- /** Time window in minutes */
369
- window_minutes: number;
370
- /** Who to notify (roles or user IDs) */
371
- notify: string[];
372
- }
373
-
374
- /**
375
- * Audit trail configuration
376
- */
377
- export interface AuditConfig {
378
- /** Whether auditing is enabled */
379
- enabled: boolean;
380
- /** Events to log */
381
- events?: AuditEventType[];
382
- /** Retention period in days */
383
- retention_days?: number;
384
- /** Alert configurations */
385
- alerts?: AuditAlert[];
386
- }
387
-
388
- /**
389
- * Complete permission configuration for an object
390
- */
391
- export interface PermissionConfig {
392
- /** Unique name of the permission configuration */
393
- name: string;
394
- /** Object this permission applies to */
395
- object: string;
396
- /** Description */
397
- description?: string;
398
-
399
- /**
400
- * Roles referenced in this permission configuration.
401
- *
402
- * Best Practice: Define roles centrally in ApplicationConfig and reference them here.
403
- * This field serves to:
404
- * 1. Document which roles apply to this object
405
- * 2. Validate that only defined roles are used
406
- * 3. Support standalone usage without application context
407
- *
408
- * Example:
409
- * - Define in app: permissions.roles: [admin, manager, user]
410
- * - Reference here: roles: [admin, manager, user]
411
- */
412
- roles?: string[];
413
-
414
- /** Object-level permissions */
415
- object_permissions?: ObjectPermissions;
416
-
417
- /** Field-level security */
418
- field_permissions?: FieldPermissions;
419
-
420
- /** Record-level access rules */
421
- record_rules?: RecordRule[];
422
-
423
- /** Sharing rules */
424
- sharing_rules?: SharingRule[];
425
-
426
- /** Action permissions */
427
- action_permissions?: ActionPermissions;
428
-
429
- /** View permissions */
430
- view_permissions?: ViewPermissions;
431
-
432
- /** Row-level security */
433
- row_level_security?: RowLevelSecurity;
434
-
435
- /** Field masking */
436
- field_masking?: FieldMasking;
437
-
438
- /** Audit configuration */
439
- audit?: AuditConfig;
440
- }
441
-
442
- /**
443
- * Context for permission checks
444
- */
445
- export interface PermissionCheckContext {
446
- /** Current user */
447
- user: {
448
- id: string;
449
- /** User roles (array for consistency, single role represented as one-element array) */
450
- roles?: string[];
451
- department_id?: string;
452
- team_id?: string;
453
- [key: string]: any;
454
- };
455
- /** Object name */
456
- object: string;
457
- /** Operation to check */
458
- operation: ObjectOperation | FieldOperation;
459
- /** Record ID (for record-level checks) */
460
- recordId?: string;
461
- /** Field name (for field-level checks) */
462
- field?: string;
463
- /** Record data (for condition evaluation) */
464
- record?: any;
465
- }
466
-
467
- /**
468
- * Result of a permission check
469
- */
470
- export interface PermissionCheckResult {
471
- /** Whether permission is granted */
472
- granted: boolean;
473
- /** Reason if denied */
474
- reason?: string;
475
- /** Rule that granted/denied access */
476
- rule?: string;
477
- }
package/src/plugin.ts DELETED
@@ -1,6 +0,0 @@
1
- import { IObjectQL } from './app';
2
-
3
- export interface ObjectQLPlugin {
4
- name: string;
5
- setup(app: IObjectQL): void | Promise<void>;
6
- }
package/src/query.ts DELETED
@@ -1,23 +0,0 @@
1
- export type FilterCriterion = [string, string, any];
2
- export type FilterExpression = FilterCriterion | 'and' | 'or' | FilterExpression[];
3
-
4
- export type AggregateFunction = 'count' | 'sum' | 'avg' | 'min' | 'max';
5
-
6
- export interface AggregateOption {
7
- func: AggregateFunction;
8
- field: string;
9
- alias?: string; // Optional: rename the result field
10
- }
11
-
12
- export interface UnifiedQuery {
13
- fields?: string[];
14
- filters?: FilterExpression[];
15
- sort?: [string, 'asc' | 'desc'][];
16
- skip?: number;
17
- limit?: number;
18
- expand?: Record<string, UnifiedQuery>;
19
-
20
- // === Aggregation Support ===
21
- groupBy?: string[];
22
- aggregate?: AggregateOption[];
23
- }
package/src/registry.ts DELETED
@@ -1,61 +0,0 @@
1
- export interface Metadata {
2
- type: string;
3
- id: string;
4
- path?: string;
5
- package?: string;
6
- content: any;
7
- }
8
-
9
- export class MetadataRegistry {
10
- // Map<type, Map<id, Metadata>>
11
- private store: Map<string, Map<string, Metadata>> = new Map();
12
-
13
- register(type: string, metadata: Metadata) {
14
- if (!this.store.has(type)) {
15
- this.store.set(type, new Map());
16
- }
17
- this.store.get(type)!.set(metadata.id, metadata);
18
- }
19
-
20
- unregister(type: string, id: string) {
21
- const map = this.store.get(type);
22
- if (map) {
23
- map.delete(id);
24
- }
25
- }
26
-
27
- unregisterPackage(packageName: string) {
28
- for (const [type, map] of this.store.entries()) {
29
- const entriesToDelete: string[] = [];
30
-
31
- for (const [id, meta] of map.entries()) {
32
- if (meta.package === packageName) {
33
- entriesToDelete.push(id);
34
- }
35
- }
36
-
37
- // Delete all collected entries
38
- for (const id of entriesToDelete) {
39
- map.delete(id);
40
- }
41
- }
42
- }
43
-
44
- get<T = any>(type: string, id: string): T | undefined {
45
- const map = this.store.get(type);
46
- if (!map) return undefined;
47
- const entry = map.get(id);
48
- return entry ? entry.content as T : undefined;
49
- }
50
-
51
- list<T = any>(type: string): T[] {
52
- const map = this.store.get(type);
53
- if (!map) return [];
54
- return Array.from(map.values()).map(m => m.content as T);
55
- }
56
-
57
- getEntry(type: string, id: string): Metadata | undefined {
58
- const map = this.store.get(type);
59
- return map ? map.get(id) : undefined;
60
- }
61
- }
package/src/repository.ts DELETED
@@ -1,17 +0,0 @@
1
- import { UnifiedQuery } from "./query";
2
-
3
- export interface IObjectRepository {
4
- find(query?: UnifiedQuery): Promise<any[]>;
5
- findOne(idOrQuery: string | number | UnifiedQuery): Promise<any>;
6
- count(filters: any): Promise<number>;
7
- create(doc: any): Promise<any>;
8
- update(id: string | number, doc: any, options?: any): Promise<any>;
9
- delete(id: string | number): Promise<any>;
10
- aggregate(query: any): Promise<any>;
11
- distinct(field: string, filters?: any): Promise<any[]>;
12
- findOneAndUpdate?(filters: any, update: any, options?: any): Promise<any>;
13
- createMany(data: any[]): Promise<any>;
14
- updateMany(filters: any, data: any): Promise<any>;
15
- deleteMany(filters: any): Promise<any>;
16
- execute(actionName: string, id: string | number | undefined, params: any): Promise<any>;
17
- }