Posts

GUID nonclustered index

Need a globally unique key for a table, but don’t want to risk performance issues arising from a random clustered index ? And you don’t want to deal with similar keys by using newsequentialid ? The solution is to make a clustered index out of a regular identity integer, and use a GUID as a nonclustered primary key: /* Identity is the clustered index, but not the primary key.   Primary key has a non-clustered index.   The records are physically inserted in the database in order by the clustered key, but the primary key as a random GUID is still indexed. */ create table dbo . SomeTable ( CX int not null identity , SomeTableKey uniqueidentifier not null , . . . ) ; go alter table dbo . SomeTable add constraint PK_SomeTable primary key nonclustered ( SomeTableKey ) ; go create unique clustered index CIX_SomeTable on dbo . SomeTable ( CX ) ; go alter table dbo . SomeTable add constraint DF_SomeTable_Key default ( newid ( ) ) for Some

Send AWS SES email with javascript

 Using the node-ses package: " use strict " ; var ses = require ( ' node-ses ' ) , client = ses . createClient ( { key : ' mykey ' , secret : ' mysecret ' , amazon : " https://email.us-west-2.amazonaws.com " } ) ; // Give SES the details and let it construct the message for you. client . sendEmail ( { to : ' someone@example.com ' , , from : ' someone@example.com ' , , cc : ' someone@example.com ' , , bcc : [ ' canbearray@example.com, ' someone@example . com ' ]     , subject: ' Test Message ' + new Date()     , message: ' your < b > message < / b > goes here '     , altText: ' plain text '     }, function (err, data, res) {           if (err) {             console.log("ERROR");             console.log(err);         } else {             console.log("Done

Create PDF from website using MS Edge

"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"    --headless     --print-to-pdf=c:\temp\google.pdf     https://google.com/

Do not await an async method

From this answer on StackOverflow – to avoid the compiler warning you about not awaiting an async method. Not sure if this is the best answer, but it works for me: public static class TaskExtensions { public static void DoNotAwait ( this Task task ) { } } GetNameAsync ( ) . DoNotAwait ( ) ;

Javascript default parameters

" use strict " ; // no IE support // no Safari support function doSomething ( a = 1 , b = 2 ) { console . log ( ` a= ${ a } ` ) ; console . log ( ` b= ${ b } ` ) ; } doSomething ( ) ; // 1, 2 doSomething ( 10 ) ; // 10, 2 doSomething ( undefined , 20 ) ; // 1, 20 doSomething ( 10 , 20 ) ; // 10, 20 doSomething ( null , null ) ; // null, null // default value can be a function call, like an argument exception: function go ( val = missingValue ( " val " ) ) { } function missingValue ( paramName ) { throw new Error ( ` Parameter ${ paramName } is missing ` ) ; } try { go ( 34 ) ; console . log ( " ok " ) ; } catch ( ex ) { console . log ( ex ) ; } // ok try { go ( ) ; console . log ( " ok " ) ; } catch ( ex ) { console . log ( ex ) ; }

Base-64 conversion in SQL Server

create function dbo . ConvertFromBase64String ( @Base64Value varchar ( max ) ) returns varbinary ( max ) as begin return cast ( '' as xml ) . value ( 'xs:base64Binary(sql:variable("@Base64Value"))' , 'varbinary(max)' ) ; end ; go create function dbo . ConvertToBase64String ( @BinaryValue varbinary ( max ) ) returns varchar ( max ) as begin return cast ( '' as xml ) . value ( 'xs:base64Binary(sql:variable("@BinaryValue"))' , 'varchar(max)' ) ; end ; go declare @b varbinary ( max ) = cast ( 'hello' as varbinary ( max ) ) ; select @b ; -- 0x68656C6C6F declare @b64 varchar ( max ) ; select @b64 = dbo . ConvertToBase64String ( @b ) ; select @b64 ; -- aGVsbG8= declare @result varbinary ( max ) ; select @result = dbo . ConvertFromBase64String ( @b64 ) ; select @result ; -- 0x68656C6C6F declare @t varchar ( max ) ; select @t = cast ( @result as va

Javascript random characters

// Asynchronous const crypto = require ( ' crypto ' ) ; function getRandomChars ( len , callback ) { const CHARS = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 " ; const NUM_CHARS = CHARS . length ; crypto . randomBytes ( len , ( err , buf ) = > { let result = " " ; buf . forEach ( b = > { let idx = Math . floor ( b * NUM_CHARS / 256 ) ; result += CHARS [ idx ] ; } ) ; callback ( null , result ) ; } ) ; }