Using enums in Angular with NgModel

Enums are really nice I think because you can have it to keep static data structured and easily work with it. Let us create a new enum type called LanguageTypes, it will hold some languages, we will use it in another class to set the language for that object. We will have a select element to try the enum out with the help of the ngModel property which binds the value from the enum to our object.

First of all, create a new file called language.types.ts or something similar in your Angular project under your src folder. Fill it with values you want for each enum member. Let’s go with numbers.

export enum LanguageTypes {
    English = 0,
    Swedish = 1,
  }

Okay good, we got some languages! Now, let’s create a Select form element to try it out, inside your component, have a myObject property or similar.

<div class="form-group">
  <label for="language">Language</label>
  <select [(ngModel)]="myObject.language" class="form-control" name="language" id="language">
    <option value="0">English</option>
    <option value="1">Swedish</option>
  </select>
</div>

Pretend your myObject is of a class that contains the property language like this:

import { LanguageTypes } from "./languageTypes.model";

export class MyCoolObject {
    name: string;
    language: LanguageTypes = LanguageTypes.Custom;
    constructor(name: string, lang: LanguageTypes = LanguageTypes.Swedish) {
        this.name = name;
        this.language = lang;
    }
}

Then now we are taking use of the LanguageType enum! Inside our class we have a constructor that take the default language Swedish (value 1), and we can easily change these up because enum is great in that way. We can take it a step further and replace the hardcoded values in our Select list to use the actual enum as well.

Add in your component.ts file a getter:

  public get language(): typeof LanguageTypes {
    return LanguageTypes; 
  }

Now, whenever we call the language getter property we will retrieve the enum type and can use it as normal, so replace the hard coded values 0 1 to make use of the getter instead.

<div class="form-group">
  <label for="language">Language</label>
  <select [(ngModel)]="myObject.language" class="form-control" name="language" id="language">
    <option [value]="language.English">English</option>
    <option [value]="language.Swedish">Swedish</option>
  </select>
</div>

And now our Angular app is using our own enum type.

Run SQL queries on multiple databases

Sometimes you have multiple databases that you want to run SQL queries on. It can be time consuming to go through each one after one. The method that works the easiest for me to accomplish this is to do it through Powershell. You can also do it by creating an SQL function where you pass the database name and run the script through the script itself, but I found that Powershell works just as good if not even better, specially if you have long queries and multiple sql script files. I should say that, I am running these commands against a SQL Azure but there this should work in other situations as well.

First of all, open up Notepad or something to write the Powershell code, and name your powershell file to whatever and wherever you want, I’ll go with the name run.ps1. Keep in mind that it must have the file extension ps1 since that is what makes it a powershell file. Let us write this:

Get-ChildItem "C:\Users\Administrator\Desktop\script\testing" -Filter RUN*.sql | `
Foreach-Object{
    Write-Host 'Executing' $_.FullName
    sqlcmd -U myservices@azuresqlserver.database.windows.net -P mypassword -S azuresqlserver.database.windows.net -d MyFirstDatabase -i $_.FullName
    sqlcmd -U myservices@azuresqlserver.database.windows.net -P mypassword  -S azuresqlserver.database.windows.net -d MySecondDatabase -i $_.FullName
    sqlcmd -U myservices@azuresqlserver.database.windows.net -P mypassword  -S azuresqlserver.database.windows.net -d MyThirdDatabase -i $_.FullName
}

So what the script does is that it goes through the files inside the folder C:\Users\Administrator\Desktop\script\testing and it will open up each file that ends with .sql and has the file name RUN in it. The script execute the .sql from these files (we will create these soon) and then execute that sql code into our databases MyFirstDatabase, MySecondDatabase and MyThirdDatabase.

So this means we should create a SQL file inside the testing folder and in it, call it something like RUN-1.sql with your SQL code. Put anything it such as your INSERT or UPDATE or whatever. I am going to have a simple hello script:

print 'hello'

Assuming you have the MyFirstDatabase, MySecondDatabase and MyThirdDatabase databases and the file RUN-1.sql file inside the C:\Users\Administrator\Desktop\script\testing you can now run your powershell file, note that it can be saved anywhere, as long as the sql files are at the correct destination. Open up Powershell with administrator rights (right click and run as administrator) or Windows Powershell IDE and browse to the location of your powershell file, and enter the run.ps1 (which I named my powershell file) and click enter. Powershell will now run the script and print our hello three times which means we’ve just run some SQL code through powershell successfully!

You’d obviously have some actual SQL that does something, but for now, we can see it is working as we expected. And you can create multiple sql scripts with various queries if you want to structure things and they will be included if you name them RUN in the file name.

Now the script is still little bit clutterly, so why not put the database names into an array so we can just update that instead of the whole sqlcmd command each time.

$databases = @('MyFirstDatabase','MySecondDatabase','MyThirdDatabase')
For ($i=0; $i -lt $databases.Length; $i++) {
    $databases[$i]
   
Get-ChildItem "C:\Users\Administrator\Desktop\script\testing" -Filter RUN*.sql | `

Foreach-Object{
   Write-Host 'Executing' $_.FullName
   sqlcmd -U myservices@azuresqlserver.database.windows.net -P mypassword  -S azuresqlserver.database.windows.net -d$databases[$i] -i $_.FullName
}  
}

We pass the database name into the sqlcmd -d (database) command and thus we can loop through the list easily and run SQL queries on multiple databases.