FoalTS / foal

Full-featured Node.js framework 🚀
https://foalts.org/
MIT License
1.9k stars 141 forks source link

[Mongodb-Other] Fast config API #985

Closed c0ncentus closed 3 years ago

c0ncentus commented 3 years ago

Hi,

I want make config file for every endpoints because i don't want waste time to do x times the same file with unless code [...]

"one config file to rule them all" in config.ts


// config for suffixe
const suffixe ={
"GET_ONE":"some1",
"GET_ALL":"some",
...
 }

export const Api: Project_ = {
    project: "Lindale",
    // Lindale/
    collection: [
        {
            name: "Composer",
           entity: Composer,
            permission: ["role2", "role1"],
            endpoint: [
                // /Lindale/Composer(/some1)/:id
                 { http: "GET_ONE", permission: ["role5"] },
                // /Lindale/Composer(/some)                
                { http: "GET_ALL", permission: ["role5"], middleware: (context) => {...}}
                "SEARCH", "POST_ONE", "POST_MANY", "UPDATE_ONE", "UPDATE_MANY", "DELETE_ONE",
                "DELETE_ALL", "DELETE_MANY"
            ]
        }
    ]
}

each endpoint are very classic ... nothing change.

or

export const Api :Project_ =    {
    project: "Lindale",
    // Lindale/
collection: [
        {
            name: "Composer", 
            entity: Composer,
           permission: ["role2", "role1"]
            endpoint:[
      // override specific endpoint
      { http:"GET_ONE", permission:["role5"] },
      { http: "GET_ALL", permission :["role5"] , middleware:(context)=>{...}},
"SEARCH", "POST_ONE", "POST_MANY", "UPDATE_ONE", "UPDATE_MANY", "DELETE_ONE", "DELETE_ALL", "DELETE_MANY"]
        }
]

ps: 8 files for one object ... if I have 20 ententies then it generates 8* 20 routes = 20 annoying files or 160 on one config. "annoying" because sometimes it is very very basic need. More you write, more you duplicate more errors occured and more tests is required so ... with this config everything is "less required".

here you can generate swagger and log in the console all routes of the app.

Tests is not longer verify the endpoint but if it generate well.

I already do this with older version express but with this new version some dummy error occur with Express and so it's why I using FoalTs now. I wish have a backend without do lot of things that users can't see.

PS: Yes Search can be dynamic with request body !

LoicPoullain commented 3 years ago

Foal is not designed to be used that way (i.e. using a global object to define the routes and their permissions). You have to use controllers with hooks.

There are two things however that could probably reduce the code size: