Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make RxJsonSchema Optionally Generic? #1093

Closed
phil-lgr opened this issue Apr 30, 2019 · 3 comments
Closed

Make RxJsonSchema Optionally Generic? #1093

phil-lgr opened this issue Apr 30, 2019 · 3 comments

Comments

@phil-lgr
Copy link
Contributor

Case

Improvement to class RxJsonSchema

Issue

Improve the DX when writing RxJsonSchema files in TypeScript.

Info

  • Environment: NodeJS v12
  • Stack: Typescript

Code

Before:

export declare class RxJsonSchema {
    title?: string;
    description?: string;
    version: number;
    /**
     * TODO this looks like a typescript-bug
     * we have to allows all string because the 'object'-literal is not recognized
     * retry this in later typescript-versions
     */
    type: 'object' | string;
    properties: { [key: string]: RxJsonSchemaTopLevel };
    required?: string[];
    compoundIndexes?: string[] | string[][];
    keyCompression?: boolean;
    /**
     * if not set, rxdb will set 'false' as default
     */
    additionalProperties?: boolean;
    attachments?: {
            encrypted?: boolean
    };
}

after:

export declare class RxJsonSchema<T> {
    title?: string;
    description?: string;
    version: number;
    /**
     * TODO this looks like a typescript-bug
     * we have to allows all string because the 'object'-literal is not recognized
     * retry this in later typescript-versions
     */
    type: 'object' | string;
    properties: { [key in keyof T]: RxJsonSchemaTopLevel };
    required?: [keyof T];
    compoundIndexes?: string[] | string[][];
    keyCompression?: boolean;
    /**
     * if not set, rxdb will set 'false' as default
     */
    additionalProperties?: boolean;
    attachments?: {
        encrypted?: boolean;
    };
}

Usage example:

export type EmployeeProfile = {
    hockeyUserId: string;
    soccerUserId: string;
    footballUserId: string;
};
const EmployeeProfileSchema: RxJsonSchema<EmployeeProfile> = {
    title: 'human schema',
    description: 'describes a human being',
    version: 0,
    keyCompression: true,
    type: 'object',
    properties: {
        hockeyUserId: {
            type: 'string',
            primary: true
        },
        soccerUserId: {
            type: 'string',
            primary: true
        },
        footballUserId: {
            type: 'string',
            primary: true
        }
    },
    required: ['hockeyUserId']
};

so you get type checking on the properties
image

Any thoughts?

@pubkey
Copy link
Owner

pubkey commented May 2, 2019

Hi @phil-lgr
I understand your usecase and I think this is a good proposal.
But I think it should be RxJsonSchema<T = any> so that it does not crash when T is not provided.

Can you create a PR for this?

Hint: There are some tests for the typings here which can be run via npm run test:typings

@phil-lgr
Copy link
Contributor Author

phil-lgr commented May 2, 2019

jfhbrook/node-ecstatic#255 npm i is broken :(

@phil-lgr
Copy link
Contributor Author

phil-lgr commented May 2, 2019

do we use http-server? I see no usage in the repo!

@phil-lgr phil-lgr mentioned this issue May 2, 2019
4 tasks
@phil-lgr phil-lgr closed this as completed May 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants