I am in the process of converting a Parse Server codebase from Javascript to Typescript – learning Typescript as I go. I would welcome any insight on best practices.
I’m wondering what the best way to approach strongly typing Parse Classes in my codebase? (are there any good reference Parse Server projects written in Typescript?)
Currently, I have a classes.ts file in my root src directory that holds all of my Class and Attribute definitions like so:
import { Attributes } from 'parse'
export interface ItemAttributes extends Attributes {
color: string
size: number
enabled: boolean
}
export class Item extends Object<ItemAttributes> {
constructor (data?: Partial<ItemAttributes>) {
super('Item', data as ItemAttributes)
}
}
Parse.Object.registerSubclass('Item', Item)
Has anyone found success with different folder structures than the above? It is getting a bit unwieldy with all of my classes in the one root file.
Also, I am looking to type the getter and setter arguments that currently exist as strings (for example: myItem.get('color')).
I am looking for a solution that doesn’t require an additional enum definition. I find it tedious to have to maintain multiple definitions when updating class attributes.
We went though this in the past years and we wrote a utility library that allows us to wrap the non-ts ParseJS code into OOP TS code that supports getters and setters and many more useful utilities.
Library is stable and we use it actively in about 10 distinct projects., but there is no explicit docs written (readme,…)
Most of the package also have good code coverage and tests.
It allow us to do 99% of operations using Typescript and IDE are intelligent enough that they will also provide type hints and autocomplete which is so awesome!
You can place it at the entry point to your project, just after you run Parse.Initialize on the client, or, on the server, before all your other cloud code