0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Azure FunctionsのNodeでSQL Serverから値を取得したメモ

Last updated at Posted at 2020-11-03

概要

AzureのFunctionsでNodeを利用したSQL Serverへの接続をしたメモ。
async awaitを使ってConnectionをとれるようにした。
ORMapperを自前で作ってみた。
自動でやってくれるライブラリも探せばありそうだなー。

この時点のソース

ソース

models/db.ts
import {Connection,Request, TYPES } from 'tedious'

let connectionInstance = null;
export const getConnection = () => new Promise<Connection>(async (resolve, reject)=>{
  if(connectionInstance) {
    resolve(connectionInstance)
  }
  try {
    connectionInstance = await createConnection()
    resolve(connectionInstance)  
  } catch (err) {
    reject(err)
  }
})

const createConnection = ()=> new Promise<Connection>((resolve, reject)=>{
  const config = {
    server: process.env["SQLConnectionServer"],
    authentication: {
        type: 'default',
        options: {
            userName: process.env["SQLConnectionUserName"],
            password: process.env["SQLConnectionUserPassword"],
        }
    },
    options: {
        database: process.env["SQLConnectionDatabase"],
        rowCollectionOnDone: true, // Requestの第二引数のrowsに値を入れるために必要
        rowCollectionOnRequestCompletion: true,  // Requestの第二引数のrowsに値を入れるために必要
    }
  }
  const connection = new Connection(config);
  connection.on('connect', function(err) {  
    if (err) {
      reject(err)
      return
    }  
    resolve(connection)
  });
})

/**
 * レコードをオブジェクトにして返す
 * @param columns レコード1行
 */
const formatORMapping = <T>(columns: any[]) => {
  const ret = {} as T
  columns.forEach(col=>{
    console.log(col)
    ret[col.metadata.colName] = col.value
  })
  return ret
}
export const read = <T>(connection: Connection, query: string) => new Promise<T[]>((resolve, reject) =>{
  const request = new Request(query, (err, rowCount, rows) => {
    if (err) {
        reject(err);
        return;
    }
    console.log(rowCount + ' row(s) returned');
    resolve(rows.map<T>(formatORMapping))
  });

  connection.execSql(request);
})
models/users.ts
import * as db from './db';
interface User {
  UserID: number
  Name: string
}
export const select = async ()=>{
  const connection = await db.getConnection();
  const query = "select * from Users";
  return await db.read<User[]>(connection, query)
}
index.ts
import { AzureFunction, Context, HttpRequest } from "@azure/functions"
import * as users from './models/users';

const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
    const responseMessage = await users.select();
    context.res = {
        body: responseMessage
    };
};
export default httpTrigger;

mssqlで接続 2024.05.04 追記

mssql
ORMapperをやってくれるライブラリ。

参考

Azure Functions から SQLServerに接続する(node.js編)
クイック スタート:Node.js を使用して Azure SQL Database または Azure SQL Managed Instance 内のデータベースに対してクエリを実行する
Get started with SQL Server
Tedious API
Win10 + node.js から SQL Server 2019 Express に CSVファイルを書き込んでみる 修正版(tedious-connection-pool)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?