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

ORM for Notion Database? #391

Open
bitabs opened this issue Mar 8, 2023 · 3 comments
Open

ORM for Notion Database? #391

bitabs opened this issue Mar 8, 2023 · 3 comments

Comments

@bitabs
Copy link

bitabs commented Mar 8, 2023

Will we ever have an ORM like Prisma for Notion Database?

@janwirth
Copy link

I am having the same problem right now. The types are often not exported, the labels change etc. Some type of code generation would be ideal.

@janwirth
Copy link

I wrote this helper function here, where sample is the JSON dump of a page from the SDK imported into typescript. This will infer the field labels and the types. It is also resilient to changing labels as it uses the IDs from the sample page to retrieve props from the actual page.

export const getPropertyUsingLabelToInferId = <T extends {[k: string]: {id: string}}, Label extends keyof T>(sample: T, value: unknown, label: Label): T[Label] => {
    
    
    const name_to_id_mappings: Record<keyof T, string> =
        Object.fromEntries(Object.entries(sample as {[k: string]: {id: string}}).map(([k,v]) => ([k, (v).id]))) as Record<keyof T, string>
        
    const key = name_to_id_mappings[label]
    const fields = Object.values(value as {[k: string]: {id: string}})
    const field = fields.find(field => field.id === key) 
    if (!field) {
      throw Error(`Field ${label.toString()}`)
    }
    return field as unknown as T[Label]
}

@schickling
Copy link

👀

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

3 participants