The nodejs environment queries Oracle using a Typeorm connection

Posted by PHPHorizons on Sun, 08 Dec 2019 14:41:31 +0100

The first is typeorm's Official Address

Someone in the country has doubled Chinese version , timeliness is not guaranteed

/ Install the following packages via npm:

  • typeorm //typeorm connection database
  • @types/node//type system
  • typescript //ts base
  • oracledb //oracle base
  • ts-node //nodejs compiles tools that run ts;

/ Root Path Configuration:

  • package.json //project dependencies, scripts, descriptions, etc.
  • tsconfig.json //ts compilation settings
 1 {
 2     "compilerOptions": {
 3         "module": "commonjs",
 4         "noImplicitAny": true,
 5         "removeComments": true,
 6         "preserveConstEnums": true,
 7         "sourceMap": true,
 8         "outDir": "./dist",
 9         "emitDecoratorMetadata": true,  //typeorm Special Requirements
10         "experimentalDecorators": true  //typeorm Special Requirements
11     },
12     "include": [
13         "src/**/*"
14     ],
15     "exclude": [
16         "node_modules",
17         "**/*.spec.ts"
18     ]
19 }

 

  • ormconfig.json //database connection parameters
{
    "type": "oracle",
    "host": "10.16.2.41",
    "port": 1521,
    "username": "admin",
    "password": "admin",
    "sid": "ORCL",
    "synchronize": true,
    "logging": true,
    "entities": [
        "src/entity/**/*.ts"
    ],
    "migrations": [
        "src/migration/**/*.ts"
    ],
    "subscribers": [
        "src/subscriber/**/*.ts"
    ]
}

 

  • .vscode configuration: launch.json, mainly configures the js path that vscode compiles from ts when debug. This is not relevant to the project, just for debugging convenience
 1 {
 2     "name": "Current TS File",
 3     "type": "node",
 4     "request": "launch",
 5     "program": "${workspaceRoot}\\node_modules\\ts-node\\dist\\bin.js",
 6     "args": [
 7         "${relativeFile}"
 8     ],
 9     "cwd": "${workspaceRoot}",
10     "protocol": "inspector"
11 }

 

/ Writing body:

Create/edit index.ts under root path (name can be customized), configure the start script command in package as ts-node index.ts,
 1 import "reflect-metadata";
 2 import {createConnection} from "typeorm";
 3 import {xxx} from "./src/entity/xxx";  //Introducing a data table structure mapping file
 4 
 5 createConnection().then(async connection => {  //Automatically follow path when connection parameter is empty ormconfig.json Information Connection
 6     /*let a = await connection.query(
 7         `SELECT * FROM xxx`
 8     ); *///Use native directly sql Statement Query
 9  
10     let a = await connection.manager.find(xxx)  //Query using connectors connection.manager
11     console.log("result: ", a);
12 }).catch(error => console.log(error));

Build Datasheet Entity Structure xxx.js under src/entity/Format Reference Official Web

Run npm start on the cmd root path or use vscode debugging

At this point, we have successfully connected to the Oracle database using typeorm, adding middleware, etc. to form a complete backend

Differences from sequelize

Move from Sequelize to typeorm because sequelize officially does not support connecting to Oracle

typeorm, as described by its name, is a very complete database relationship map with a typescript type system, showing a screenshot of the data type:

 

Is this still js?Of course, such a complete type system benefits from typescript, and we can use type declarations as appropriate when building it because they are not required (they are still JS in nature)

Many types can be replaced by js native type + length, depending on actual requirements

 

Automatically generating/updating mapping file scripts from the database can be relatively complex

 

typescript is also your first contact. The article was only discovered after a few weeks of groping. It is unavoidable that there are errors in the content. If there are any, please call again. Thank you.

Topics: Javascript Database JSON TypeScript Oracle