node-oracledb typeorm 集成试用

主要是测试下typeorm与node-oracledb thin 模式的集成

环境准备

  • docker-compose 文件
version: '3'
services:
   db:
      image: gvenzl/oracle-xe:21.3.0-slim
      ports:
         - "1521:1521"
      environment:
       - ORACLE_PASSWORD=Ccda5662E
       - APP_USER=dalong
       - APP_USER_PASSWORD=Ccda5662E

typeorm 集成

  • entity.js
const EntitySchema = require("typeorm").EntitySchema
module.exports = new EntitySchema({
    name: "sensor", // Will use table name `category` as default behaviour.
    tableName: "sensor", // Optional: Provide `tableName` property to override the default behaviour for table name.
    columns: {
        id: {
            primary: true,
            type: "varchar",
        },
        name: {
            type: "varchar",
        },
        address: {
            type: "varchar"
        }
    }
})
  • datasource.js
const typeorm = require("typeorm")
 
/**
 * data store operation for sensor data
 */
const dataSource = new typeorm.DataSource({
    type: "oracle",
    username: "dalong",
    password: "Ccda5662E",
    connectString:"127.0.0.1/XEPDB1",
    synchronize: true,
    entities: [require("./entity")],
})
 
module.exports = dataSource
  • index.js
const dataSource = require('./datasource')
async function run() {
    const dataSourceInstance = await dataSource.initialize()
    if(dataSourceInstance.isInitialized) {
        const sensorRepository = dataSourceInstance.getRepository('sensor')
        let body = {
            id: 11,
            name: 'test',
            address: 'test',
            status: true
        }
        let saveResult = await sensorRepository.save(body)
        console.log(saveResult)
        dataSourceInstance.destroy()
    }
}
run();

说明

node-oracledb typeorm 的集成还是比较方便的,没有什么问题

参考资料

https://github.com/oracle/node-oracledb
http://typeorm.io/#/

posted on 2023-10-14 21:55  荣锋亮  阅读(17)  评论(0编辑  收藏  举报

导航