Using Database Scripts

Coming from this example:

Declarative Validation | Documentation (servicestack.net)

It mentions we can do something similar to this:

[ValidateRequest(Condition = "!dbExistsSync('SELECT * FROM RockstarAlbum WHERE RockstarId = @Id', { it.Id })", 
    ErrorCode = "HasForeignKeyReferences")]

Yet anytime I try this, it errors out with

Filter in ‘{temp file}’ named ‘dbExistsSync’ was not found. No similar filters named ‘dbExists’ were found in registered filter(s): ‘DefaultScripts’, ‘HtmlScripts’, ‘ProtectedScripts’, ‘InfoScripts’, ‘ServiceStackScripts’, ‘ValidateScripts’, ‘BootstrapScripts’, ‘MarkdownScriptMethods’.

I have SharpPagesFeature enabled:

        Plugins.Add(new SharpPagesFeature()
        {
            ScriptMethods =
            {
                new DbScripts()
            },
        });

Unsure if I need to enable scripts for Database usage somewhere else?

My Actual Code:

[ValidateRequest(Condition = "dbExists('SELECT * FROM Projects WHERE Id = @Id AND CreatedBy = @CreatedBy', { it.Id, userAuthId })")]

Just doing some lightweight security checks for an MVP

DbScripts is deprecated use DbScriptsAsync:

Plugins.Add(new SharpPagesFeature { 
    ScriptMethods = { new DbScriptsAsync() }, 
});

If I change it to DbScriptsAsync, then update the script to

[ValidateRequest(Condition = "dbExists('SELECT * FROM Project WHERE Id = @Id AND CreatedBy = @CreatedBy', { it.Id, userAuthId })")]

It still gives the same error

Filter in ‘{temp file}’ named ‘dbExists’ was not found. No similar filters named ‘dbExists’ were found in registered filter(s): ‘DefaultScripts’, ‘HtmlScripts’, ‘ProtectedScripts’, ‘InfoScripts’, ‘ServiceStackScripts’, ‘ValidateScripts’, ‘BootstrapScripts’, ‘MarkdownScriptMethods’.

You should continue to use sync APIs in Condition attributes as per the docs, i.e. dbExistsSync.

But the error message says DbScriptsAsync does not exist. So something is wrong with your registration. Are you registering the SharpPagesFeature plugin in your AppHost?

Otherwise use ScriptContext which will use the default ScriptContext if SharpPagesFeature plugin does not exist, e.g:

ScriptContext.ScriptMethods.AddRange(new ScriptMethods[] {
    new DbScriptsAsync(),
});
1 Like

Ah yes, there was a previous registration of SharpScripts that I missed.

Thank you for the help!

Been a couple of years since I got to use ServiceStack, awesome job on the updates!

1 Like