Skip to content

Commit

Permalink
Corrected type signature and parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
NavidMitchell committed Jan 2, 2025
1 parent 308d66c commit 124f23e
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .run/StructuresCLI.run.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="StructuresCLI" type="NodeJSConfigurationType" application-parameters="synchronize --dryRun" path-to-node="$USER_HOME$/.local/share/mise/installs/node/22/bin/node" node-parameters="--loader ts-node/esm" path-to-js-file="bin/dev.js" working-dir="$PROJECT_DIR$/structures-js/structures-cli">
<configuration default="false" name="StructuresCLI" type="NodeJSConfigurationType" application-parameters="synchronize --dryRun" path-to-node="$USER_HOME$/.local/share/mise/installs/node/22/bin/node" path-to-js-file="bin/dev.js" working-dir="$PROJECT_DIR$/structures-js/structures-cli">
<method v="2" />
</configuration>
</component>
3 changes: 1 addition & 2 deletions structures-js/structures-api/src/api/StructuresDecorators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'reflect-metadata'
import {EntityServiceDecoratorsConfig} from '@/api/idl/decorators/EntityServiceDecoratorsDecorator'
import {MultiTenancyType} from '@/api/idl/decorators/MultiTenancyType'
import {PrecisionType} from '@/api/idl/decorators/PrecisionType'

Expand Down Expand Up @@ -78,7 +77,7 @@ export function NotNull(target: any, propertyKey: string) {
}

// @ts-ignore
export function Policy(policies: [string[]]) {
export function Policy(policies: string[][]) {
// Definition below allows decorator to be used for class, method, or properties
// @ts-ignore
return function (target: any, propertyKey?: string, descriptor?: PropertyDescriptor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {EntityServiceDecorator} from '@/api/idl/decorators/EntityServiceDecorato

export class PolicyDecorator extends EntityServiceDecorator {

public policies: [string[]]
public policies: string[][]

constructor(policies: [string[]]) {
constructor(policies: string[][]) {
super()
this.type = 'PolicyDecorator'
this.policies = policies
Expand All @@ -16,6 +16,6 @@ export class PolicyDecorator extends EntityServiceDecorator {
* @param policies to be supplied
*/
// @ts-ignore
export function $Policy(policies: [string[]]): PolicyDecorator{
export function $Policy(policies: string[][]): PolicyDecorator{
return new PolicyDecorator(policies)
}
1 change: 1 addition & 0 deletions structures-js/structures-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './api/domain/Structure'
export * from './api/idl/decorators/AutoGeneratedIdDecorator'
export * from './api/idl/decorators/DiscriminatorDecorator'
export * from './api/idl/decorators/EntityDecorator'
export * from './api/idl/decorators/EntityServiceDecoratorsConfig'
export * from './api/idl/decorators/EntityServiceDecoratorsDecorator'
export * from './api/idl/decorators/FlattenedDecorator'
export * from './api/idl/decorators/IdDecorator'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function tsDecoratorToC3Decorator(decorator: Decorator): C3Decorator | nu
const obj = parseExpressionToJs(argument);
ret = {
type: 'PolicyDecorator',
policies: obj as [string[]],
policies: obj as string[][],
} as PolicyDecorator;
} else {
throw new Error('Policy must have an array literal argument');
Expand Down Expand Up @@ -167,12 +167,16 @@ function parseExpressionToJs(expression: any): any {
function convertCallExpressionToJs(expression: CallExpression): any {
const text = expression.getText()
if(text.startsWith('$Policy')){
if(expression.getArguments().length == 1){
const argument = expression.getArguments()[0];
if(expression.getArguments().length === 1){
const argument = expression.getArguments()[0]
const obj = parseExpressionToJs(argument)
// Validate the parsed object structure
if (!Array.isArray(obj) || !obj.every(arr => Array.isArray(arr) && arr.every(item => typeof item === 'string'))) {
throw new Error('Policy must contain an array of string arrays (string[][])')
}
return {
type: 'PolicyDecorator',
policies: obj as [string[]],
policies: obj as string[][], // Fixed type assertion
} as PolicyDecorator
}else {
throw new Error('Policy must have an array literal argument')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,83 @@
# Structures Data
This is the Structures DAO client javascript library
# Structures API Overview

The Structures API provides a robust framework for managing data structures within the C3 Interface Definition Language (IDL) environment. Designed to facilitate efficient data operations, the API offers a comprehensive suite of services that cater to structure and entity management across diverse application namespaces.

## Key Features

- **Structure Management**: The API allows for seamless handling of data structures. Through dedicated services, users can publish, unpublish, find, and count structures within specific namespaces, enabling precise control over data configurations.

- **Entity Operations**: Offering full CRUD capabilities, the API enables the management of entities within a structure. Users can save, update, delete, and query entities, with support for batch operations that enhance performance in high-volume scenarios.

- **Advanced Querying**: With built-in support for named queries and pagination, the API facilitates efficient data retrieval. Users can execute complex queries and manage datasets with ease, leveraging the API's pagination mechanisms for optimized data flow.

- **Namespace Flexibility**: The API includes services for namespace management, ensuring that data operations are contextualized within appropriate domains. Users can create and maintain namespaces dynamically, promoting organized data handling.

- **Sophisticated Data Modelling**: Utilizing classes designed for complex object typing, the API supports intricate data models with features like inheritance. This allows users to define sophisticated data types that align with specific application requirements.

- **Service Integration**: The API integrates several services, such as Namespace, Structure, and Entities services, to provide a seamless experience for managing and interacting with data structures, streamlining workflow efficiency across the application.

- **Customizable Utilities**: A variety of utilities and decorators are available for defining and enforcing data structure properties. These tools support configurations like auto-generated IDs and property constraints, ensuring precise data operation controls.


## Details
In the Structures API for TypeScript, decorators like `@Entity` are used to annotate classes and properties to define entities and configure their behavior within the C3 IDL framework. Here's a breakdown of how some of these decorators are used to define a structure:

### `@Entity` Decorator
- **Purpose**: Signifies that a class is an entity, and optionally specifies multi-tenancy behavior.
- **Usage**: Place this decorator above a class definition to mark it as an entity. It can take a parameter to define multi-tenancy type (`MultiTenancyType`, such as `NONE` or `SHARED`).
- **Example**:
```typescript
@Entity(MultiTenancyType.SHARED)
export class Person {
// properties and methods
}
```

### `@Id` Decorator
- **Purpose**: Identifies the primary key of an entity.
- **Usage**: Place above a property that serves as the unique identifier.
- **Example**:
```typescript
export class Person {
@Id
public id!: string;
}
```

### `@Precision` Decorator
- **Purpose**: Configures precision for numerical properties.
- **Usage**: Place above a numeric property, specifying its precision type.
- **Example**:
```typescript
export class Person {
@Precision(PrecisionType.SHORT)
public age?: number;
}
```

### `@Policy` Decorator
- **Purpose**: Applies access policies to entities or specific properties.
- **Usage**: Can be applied at the class level to control access to methods or properties based on specified policies.
- **Example**:
```typescript
@Policy([['data:read']])
export class Person {
// properties and methods
}
```

### `@Flattened` and `@Nested` Decorators
- **Purpose**: Define how complex properties are treated, either integrating them directly inline (`Flattened`) or as separate entities (`Nested`).
- **Usage**: Apply these decorators to properties that are objects, depending on how you want their properties to be included in the main entity.
- **Example**:
```typescript
export class Person{
@Nested
public address!: Address;
}
```

Using these decorators, you can effectively map and configure structures in TypeScript, facilitating integration with the C3 IDL framework for entity management and operations. These annotations guide the system on how to handle each property and its various operational contexts, such as storage, retrieval, and permissions.

## Local Development
- Run Tests with ```pnpm run test```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
"@kinotic/continuum-client": "^2.10.5"
},
"packageManager": "[email protected]+sha512.c8180b3fbe4e4bca02c94234717896b5529740a6cbadf19fa78254270403ea2f27d4e1d46a08a0f56c89b63dc8ebfd3ee53326da720273794e6200fcf0d184ab",
"yalcSig": "36543027f4674bcd54b1130ad8a9d59c"
"yalcSig": "04dd8f8916e0e686a7e5eadb2e69e092"
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
36543027f4674bcd54b1130ad8a9d59c
04dd8f8916e0e686a7e5eadb2e69e092
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {IterablePage, Pageable} from '@kinotic/continuum-client'
import {IEntitiesService, Query} from '@kinotic/structures-api'
import {IEntitiesService, Query, Policy} from '@kinotic/structures-api'
import { BasePersonEntityService } from './generated/BasePersonEntityService.js'

export class CityCount {
Expand Down Expand Up @@ -42,6 +42,7 @@ export class PersonEntityService extends BasePersonEntityService {
* @param lastName The last name to get the count for
* @param pageable The pageable to use for the query
*/
@Policy([['person:read'], ['person:write', 'person:update']])
@Query('select count(firstName) as count, lastName from "struct_city.person" where lastName = ? group by lastName')
public async countByLastName(lastName: string, pageable: Pageable): Promise<IterablePage<LastNameCount>> {
/** Autogenerated code, changes will be overwritten. **/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
"multiTenancyType": 1
},
{
"type": "RoleDecorator",
"roles": [
"admin"
"type": "PolicyDecorator",
"policies": [
[
"data:read"
]
]
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@
{
"type": "Query",
"statements": "select count(firstName) as count, lastName from \"struct_city.person\" where lastName = ? group by lastName"
},
{
"type": "PolicyDecorator",
"policies": [
[
"person:read"
],
[
"person:write",
"person:update"
]
]
}
],
"returnType": {
Expand Down
2 changes: 1 addition & 1 deletion structures-js/structures-ui-example/yalc.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"replaced": "^0.1.0"
},
"@kinotic/structures-api": {
"signature": "36543027f4674bcd54b1130ad8a9d59c",
"signature": "04dd8f8916e0e686a7e5eadb2e69e092",
"file": true,
"replaced": "^2.0.0"
}
Expand Down

0 comments on commit 124f23e

Please sign in to comment.