#1 2025-12-15 10:39:54

Mo0211
Member
Registered: 2018-12-30
Posts: 41

Read SQL Table Properties from a config file

Hello ab,

is it somehow possible to read names for columns and tables out of a config file?
I want to move code to mormot, but current code reads sql table and column names out of a config file and prepare the query based on this input.

  DbBirthdayRecord = class(TOrmNoCase)
  private
    DbCountry: RawUTF8;
    DbBirthdayDate: TDateTime;
    DBirthdayDateExtraInfo: RawUTF8;
    DbData:  DbDataRecord;
  published
    property DbCountry: RawUTF8 read FDbCountry write FDbCountry;
    property IMDbReleaseDate: TDateTime read FDbBirthdayDate write FDbBirthdayDate;
    property DBirthdayDateExtraInfo: RawUTF8 read FDBirthdayDateExtraInfo write FDBirthdayDateExtraInfo;
    property DbData: TDbDataRecordRecord read FDbDataRecord write FDbDataRecord;
  end;

So, DbBirthday will be read from somewhere and maybe changed to DbBirthdayCelebration.

Thank you for giving me a hint.

Best regards

Moe

Offline

#2 2025-12-15 14:26:50

zen010101
Member
Registered: 2024-06-15
Posts: 154

Re: Read SQL Table Properties from a config file

Yes, mORMot2 supports reading table/column names from config files using OrmMapExternal + MapField.

  Key Mechanism

  1. Define ORM class with internal property names (compile-time):
  TDbBirthdayRecord = class(TOrmNoCase)
  published
    property DbCountry: RawUtf8 ...;
    property DbBirthdayDate: TDateTime ...;
  end;

  2. Load mapping from config (runtime):
  {
    "tableName": "BirthdayCelebration",
    "columns": {
      "DbCountry": "country_code",
      "DbBirthdayDate": "celebration_date"
    }
  }

  3. Apply mapping:
  // Map table name
  Mapping := OrmMapExternal(Model, TDbBirthdayRecord, ExternalDB, TableName);

  // Map column names
  Mapping^.MapField('DbCountry', 'country_code');
  Mapping^.MapField('DbBirthdayDate', 'celebration_date');

Complete demo: https://gist.github.com/zen010101/713cf … 44f277c1ee

Offline

#3 2025-12-15 15:47:22

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,346
Website

Re: Read SQL Table Properties from a config file

zen010101 and his AI tools knows mORMot better than I do.

I am so happy I did write so many AI-friendly comments, even before AI was a real thing, just because I am lazy and want to keep my memory clean.
cool

Offline

#4 2025-12-15 16:45:49

zen010101
Member
Registered: 2024-06-15
Posts: 154

Re: Read SQL Table Properties from a config file

Hah, that's only because your AI-friendly comments are so well-written that even the AI can "get" mORMot quickly -- it's like you left a super clear treasure map, and my AI tools just followed the lines faster than I could read them myself. wink

Offline

#5 2025-12-16 07:53:36

Mo0211
Member
Registered: 2018-12-30
Posts: 41

Re: Read SQL Table Properties from a config file

great stuff!
I didn't expect this smile

Thank you for this hint!

Offline

#6 2025-12-17 14:40:45

zen010101
Member
Registered: 2024-06-15
Posts: 154

Re: Read SQL Table Properties from a config file

I created a working demo for this: https://github.com/zen010101/Claude_Mor … pping-demo

It loads table/column mappings from mapping_config.json and applies them via OrmMapExternal + MapField at runtime - no recompilation needed.

Offline

#7 2025-12-17 20:36:41

Mo0211
Member
Registered: 2018-12-30
Posts: 41

Re: Read SQL Table Properties from a config file

great! thanks alot!
From my perspective i use claude, too and need to use it more for this stuff.

Mormot2 is really good documented, but you really need to be an expert to understand all those stuff.
i'm not a professional coder, but understand some things about coding.

It would be really great to have some more examples like this!

What is best practice to connect with mysql/sqlite?
How to use the orm with this?

Example for sql queries written in orm.

This would be really great!

I also look for what i can deliver smile

Offline

Board footer

Powered by FluxBB