Skip to content

Commit

Permalink
wip: Work on supporting sub-items for CRUD records
Browse files Browse the repository at this point in the history
- This pattern should make it easier to support storing sub-items inside record items.
- Key aspects of a record item is that it has an address and one or more markers.
- Key aspects of sub-items are that they can have an arbitrary key (underneath an address), and inherit markers.
  • Loading branch information
KallynGowdy committed Nov 12, 2024
1 parent 50bee8f commit 59d9aef
Show file tree
Hide file tree
Showing 6 changed files with 2,178 additions and 0 deletions.
124 changes: 124 additions & 0 deletions src/aux-records/crud/sub/SubCrudRecordsController.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { MemoryStore } from '../../MemoryStore';
import {
AuthorizationContext,
AuthorizeUserAndInstancesForResourcesSuccess,
PolicyController,
} from '../../PolicyController';
import { RecordsController } from '../../RecordsController';
import {
createTestControllers,
createTestRecordKey,
createTestUser,
} from '../../TestUtils';
import { MemoryCrudRecordsStore } from './../MemoryCrudRecordsStore';
import {
CrudRecord,
CrudRecordsStore,
CrudSubscriptionMetrics,
} from './../CrudRecordsStore';
import {
CheckSubscriptionMetricsResult,
CheckSubscriptionMetricsSuccess,
CrudRecordItemSuccess,
CrudRecordsConfiguration,
CrudRecordsController,
} from '../CrudRecordsController';
import { MemorySubCrudRecordsStore } from './SubMemoryCrudRecordsStore';
import {
ActionKinds,
PRIVATE_MARKER,
PUBLIC_READ_MARKER,
} from '@casual-simulation/aux-common';
import { testCrudRecordsController } from './SubCrudRecordsControllerTests';

console.log = jest.fn();

describe('SubCrudRecordsController', () => {
describe('allows record key access', () => {
testCrudRecordsController<
TestItem,
CrudRecordsStore<TestItem>,
TestController
>(
true,
'data',
(services) => new MemorySubCrudRecordsStore(services.store),
(config, services) =>
new TestController({
...config,
resourceKind: 'data',
name: 'testItem',
}),
(item) => item
);
});

describe('denies record key access', () => {
testCrudRecordsController<
TestItem,
CrudRecordsStore<TestItem>,
TestController
>(
false,
'marker',
(services) => new MemoryCrudRecordsStore(services.store),
(config, services) =>
new TestController({
...config,
resourceKind: 'marker',
name: 'testItem',
}),
(item) => item
);
});
});

export interface TestItem extends CrudRecord {}

export class TestController extends CrudRecordsController<TestItem> {
private __checkSubscriptionMetrics: (
action: ActionKinds,
authorization: AuthorizeUserAndInstancesForResourcesSuccess,
item?: TestItem
) => Promise<CheckSubscriptionMetricsResult>;

set checkSubscriptionMetrics(
value: (
action: ActionKinds,
authorization: AuthorizeUserAndInstancesForResourcesSuccess,
item?: TestItem
) => Promise<CheckSubscriptionMetricsResult>
) {
this.__checkSubscriptionMetrics = value;
}

constructor(
config: CrudRecordsConfiguration<TestItem>,
checkSubscriptionMetrics?: (
action: ActionKinds,
authorization: AuthorizeUserAndInstancesForResourcesSuccess,
item?: TestItem
) => Promise<CheckSubscriptionMetricsResult>
) {
super(config);
this.__checkSubscriptionMetrics = checkSubscriptionMetrics as any;
}

protected async _checkSubscriptionMetrics(
action: ActionKinds,
context: AuthorizationContext,
authorization: AuthorizeUserAndInstancesForResourcesSuccess,
item?: TestItem
): Promise<CheckSubscriptionMetricsResult> {
if (this.__checkSubscriptionMetrics) {
return await this.__checkSubscriptionMetrics(
action,
authorization,
item
);
}
return {
success: true,
};
}
}
Loading

0 comments on commit 59d9aef

Please sign in to comment.