Do you want to serve arbitrary files?

I think you can use stream server for that:

/// Return physical file contents
/// name - full path to file
ClassMethod serve(name) As %Status
{
    #dim sc As %Status = $$$OK
    #dim %response As %CSP.Response
    //kill %request.Data
    set %request.Data("STREAMOID",1)= ##class(%CSP.StreamServer).Encrypt(##class(%CSP.StreamServer).GetOidForFile(name))
    if ##class(%CSP.StreamServer).OnPreHTTP() {
        set %response.Headers("Content-Disposition")="attachment; filename*=UTF-8''" _ ##class(%CSP.Page).EscapeURL(##class(%File).GetFilename(name), "UTF8")
        set sc = ##class(%CSP.StreamServer).OnPage()
    }

    quit sc
}

Also parameter cannot be assigned.

Files in OS by themselves do not have Content-Type attribute (streams, in web context can have Content-Type attribute). However, knowing file extension Caché has FileClassify utility method that can determine content type. Here's the wrapper I usually use:

/// Determine file mime type
/// name - full path to file
ClassMethod getFileType(name) As %String
{
    set ext = $zcvt($p(name, ".", *), "U")
    do ##class(%CSP.StreamServer).FileClassify(ext, .type , .bin, .charset)
    set ext = "/" _ ext _ "/"
    if ext = "/RTF/" {
        set type = "application/rtf"
    }
    
    return type
}

Or you can additional types into:

 set ^%SYS("CSP","MimeFileClassify", ext) = $lb(type, bin, charset)

That's, I think is an unrelated issue. This SQL:

SELECT JSON_OBJECT('id': '{{}')

Also throws the same error:

[SQLCODE: <-400>:<Fatal error occurred>]
[%msg: <Unexpected error occurred in JSON_OBJECT() function execution of <JSON_OBJECT>.%FromJSON({{}).Parsing error :: Line 1 Offset 2>]

Seems like some data escaping is required.

Very simple escaping (this query executes successfully):

SELECT JSON_OBJECT('id': ' '||'{{}')

Great article, Fabio!

The problem can be that when data changes, then the whole record is copied, i.e. also data which does not change.

That can be solved by checking m%<property> value. It has false positives sometimes (when you change value back, i.e.: a-b-a) and calling log code only if property value has changed.

Forgot that it's only about object access. Still, you have old and new value, so:

Do %code.WriteLine($Char(9,9,9)_"Set tOldValue = {"_tProperty.SqlFieldName_"*O}")
Do %code.WriteLine($Char(9,9,9)_"Set tNewValue = {"_tProperty.SqlFieldName_"*N}")
Do %code.WriteLine($Char(9,9,9)_"if {" _ tProperty.SqlFieldName _ "*C},tOldValue'=tNewValue {")

 

All data changes are logged in a common table.

Why not generate a separate log table for each class, inheriting from logged class, so 1 change == 1 record in log table. It also gives you the possibility of writing "easy" diff tool, so audit results can be displayed conveniently.

If table person has a column "photo" with binary data (stream) containing the photography then each and every time yhe user changes the picture  the role stream is recorded (consuming disk space). 

That problem can be solved by creating immutable stream-containing class.

/// Immutable wrapper for stream
Class User.Document Extends %Persistent
{

/// Global identifier to give to user (prevent id traversal)
Property guid As %String [ InitialExpression = {$system.Util.CreateGUID()}, Required ];

/// Creation date time
Property createDateTime As %TimeStamp [ InitialExpression = {$ZDATETIME($ZTIMESTAMP, 3, 1, 3)};

/// File name as supplied by user (only for display purposes)
Property fileName As %String(MAXLEN =1000) [ Required ];

/// File stream
Property content As %FileBinaryStream;

/// User who uploaded the file
Property user As %String [ InitialExpression = {$username} ];

/// Add new stream
/// realName - real name of a stored file
/// suppliedName - name supplied by user, used only for display purposes
/// stream - stream with data
Method %OnNew(realName As %String = "", suppliedName As %String = "", stream As %Stream.Object = {##class(%FileBinaryStream).%New()}) As %Status [ Private, ServerOnly = 1 ]
{
    #dim sc As %Status = $$$OK
    set ..fileName = ##class(%File).GetFilename(suppliedName)
    set ..content.Filename = realName
    set sc = ..content.CopyFromAndSave(stream)
    quit sc
}

/// Serve file on web
Method serve() As %Status
{
    #dim sc As %Status = $$$OK
    #dim %response As %CSP.Response
    kill %request.Data
    set %request.Data("STREAMOID",1)= ##class(%CSP.StreamServer).Encrypt(..content.%Oid())
    if ##class(%CSP.StreamServer).OnPreHTTP() {
        set %response.Headers("Content-Disposition")="attachment; filename*=UTF-8''"_##class(%CSP.Page).EscapeURL(..fileName,"UTF8")
        set st = ##class(%CSP.StreamServer).OnPage()
    }

    quit sc
}

That way your audit log table would contain only id references.

You can combine triggers and method generators into trigger generators like this:

Class User.Class1 Extends %Persistent
{

Property prop1 As %String;

Property prop2 As %String;

Trigger NewTrigger [ CodeMode = objectgenerator, Event = INSERT, Time = AFTER ]
{
    #dim class As %Dictionary.CompiledClass = %compiledclass
    set proplist = ""
    for i=1:1:class.Properties.Count() {
        #dim prop As %Dictionary.CompiledProperty = class.Properties.GetAt(i)
        if prop.Internal || prop.Calculated || prop.ReadOnly || prop.Private || prop.Identity || prop.MultiDimensional continue
        set proplist = proplist _ $lb(prop.Name)
    }
    
    do %code.WriteLine($$$TAB _ "set ^dbg($i(^dbg)) = $lb({" _$lts(proplist, "},{") _ "})")
    quit $$$OK
}

/// do ##class(User.Class1).Test()
ClassMethod Test()
{
    do ..%KillExtent()
    kill ^dbg
    
    &sql(INSERT INTO Class1 (prop1, prop2) Values ('Alice', 1))
    &sql(INSERT INTO Class1 (prop1, prop2) Values ('Bob'  , 2))
    zw ^dbg
}

Test:

do ##class(User.Class1).Test()
^dbg=2
^dbg(1)=$lb("Alice",1)
^dbg(2)=$lb("Bob",2)

Here's whats going on during compilation.

  1. Build list of all relevant properties and write them into proplist variable. 
  2. Generate trigger code:  set ^dbg($i(^dbg)) = $lb({prop1}, {prop2})

And at runtime only set ^dbg gets hit

That said I'd have automatically generated some log class and called method log there, passing all properties.

Some docs:

Just redefine the trigger with the same name in a child class:

Class FormsDev.NewClass1 Extends %Persistent
{

Property Name As %String;

Trigger Insert [ Event = INSERT ]
{
    Set ^dbg = {Name}
}

/// do ##class(FormsDev.NewClass1).Test()
ClassMethod Test()
{
    Kill ^FormsDev.NewClass1D, ^dbg, ^dbg2
    
    &sql(INSERT INTO FormsDev.NewClass1 (Name) Values ('Alice'))
    &sql(INSERT INTO FormsDev.NewClass2 (Name) Values ('Bob'))
    zw ^dbg, ^dbg2
}

}

and child class:

Class FormsDev.NewClass2 Extends FormsDev.NewClass1
{

Trigger Insert [ Event = INSERT ]
{
    Set ^dbg2 = {Name}
}

}

Test:

>do ##class(FormsDev.NewClass1).Test()
^dbg="Alice"
^dbg2="Bob"

 

Also, if you change trigger name, both would be executed. For example Insert in NewClass1:

Trigger Insert [ Event = INSERT ]
{
    Set ^dbg($i(^dbg)) = {Name}
}

And Insert2 in NewClass2:

Trigger Insert2 [ Event = INSERT ]
{
    Set ^dbg2($i(^dbg2)) = {Name}
}

Would result in:

>do ##class(FormsDev.NewClass1).Test()
^dbg=2
^dbg(1)="Alice"
^dbg(2)="Bob"
^dbg2=1
^dbg2(1)="Bob"