Recommend file structure for Fluent app

DJ Wolfson
Kilo Sage

I'm currently building my first application and I'm somewhat of an organization freak so I'm trying to not overthink this. It's been a long time since I've developed outside of ServiceNow IU so I'm a bit rusty on the best practices. I'd like to structure my project in a way where files are grouped by the context that they relate to but are separated by Type. So Table related components would be on file, Business Rules in another, Client Script in another, and so on. Does this make sense? Here's an example of the schema I was thinking of:

 

src/

  • generated/
    • ...
  • exampleTableName/
    • table.now.ts
    • businessrules.now.ts
    • clientscripts.now.ts
    • ...
  • server/
    • ...

 

Let me know if this is a realistic path for building out applications for Fluent.

1 ACCEPTED SOLUTION

Matthew Clase
ServiceNow Employee
ServiceNow Employee

Hey DJ, 

You're directory structure is pretty close to the recommended one. Only comment would be that the SDK has separate now.config.json properties for server modules (serverModulesDir) and for .now.ts files (fluentDir). So generally we would recommend something like:

src/

  • fluent
    • generated/
    • ...
    • exampleTableName/
      • table.now.ts
      • businessrules.now.ts
      • clientscripts.now.ts
      • ...
  • server/
    • ...

These properties are configurable, but generally we would recommend a separation between `.now.ts` files and you're modular JS server code that is imported into any metadata in `.now.ts`. So in this case fluentDir would be the default  value `src/fluent` and serverModulesDir would be the default `src/server`

View solution in original post

3 REPLIES 3

Matthew Clase
ServiceNow Employee
ServiceNow Employee

Hey DJ, 

You're directory structure is pretty close to the recommended one. Only comment would be that the SDK has separate now.config.json properties for server modules (serverModulesDir) and for .now.ts files (fluentDir). So generally we would recommend something like:

src/

  • fluent
    • generated/
    • ...
    • exampleTableName/
      • table.now.ts
      • businessrules.now.ts
      • clientscripts.now.ts
      • ...
  • server/
    • ...

These properties are configurable, but generally we would recommend a separation between `.now.ts` files and you're modular JS server code that is imported into any metadata in `.now.ts`. So in this case fluentDir would be the default  value `src/fluent` and serverModulesDir would be the default `src/server`

Nice catch, thanks. Aside from the missing /fluent/, do you see any other rooms for improvement?

Ramon Cordova
Tera Contributor

Hey @DJ Wolfson ,

How are you handling the organization of files under the "server" folder?
Just like you, I've been trying to figure out the best file structure and came across this thread.

I have the following simple app created so far:

RamonCordova_1-1760235742059.png

So, trying to follow the structure proposed in this post and if my understanding is correct, now.ts files should define the metadata and in these files is where we would import some JS file containing our business logic, but how would you organize this under your server folder?
In a real project you can have multiple tables, each with multiple configurations such as BRs, UI Policies, Client Scripts, etc.

I've been struggling to find information about the best practices on organizing the files of a fluent project, and it is understandable as it is new feature.
I have many questions and ideas, but here are two important ones I'd like to clarify.
Having some feedback about this question could really help me to figure out some other stuff I'm thinking about.

  1. If I want to add some business rules to my table "Menu Item", should all the BRs be defined on the menu_item > business_rules.now.ts file?
    Or is it better to create a folder called "business rules" and in there put individual files where each define one business rule? Example: menu_item > business_rules > first_business_rule.now.ts
  2. So, regarding the logic of the business rule, how should it be organized under the "server" file?
    Is the structure from my image close to the recommended practice?
    Or should we use a similar approach to fluent folder? i.e: create a table name folder and then put all the related files inside? Example: server > menu_item > business_rules > first_business_rule.js
    I guess one of my concerns with putting the business logic into individual files is the naming. Generally, one names business rules describing what is the action being performed and that can get a little verbose. In my experience I haven't seen a file named like: server > business_rules > menu_item > validate_menu_item_on_insert.js
    That's more like a function name, not a file.
    Another way, to mitigate that could be to put the business logic into entities/classes, then refer to those classes from the now.ts files.
    Or I can be in the wrong direction, and in reality, the recommended best practice can be that we put the business logic directly into the BR definition file (now.ts).

I know this can be worked out in different ways and I don't expect you to answer my questions/concerns as you will probably be in the same is spot as I am or you may have figured this out already.

 

I'd love to see someone else's fluent projects to kind of get an idea of the file structure/organization.

Thanks!