【Node.js教程】01—启动项目-采用NodeJs构建一个金融科技银行应用程序
web前端开发
共 5788字,需浏览 12分钟
·
2020-10-19 22:58
1、你需要什么来启动Node.js项目?
2、安装Nest.js CLI并创建一个项目
npm i -g @ nestjs / cl
$ nest new lesson-one
3、添加Sequalize和Umzug
$ npm install --save sequelize sequelize-typescript
$ npm install pg
$ npm install @types/bluebird
$ npm install @types/validator
npm install umzug
4、创建一个数据库模块
import { Sequelize } from 'sequelize-typescript';
export const databaseProvider = [
{
provide: 'SEQUELIZE',
useFactory: async () => {
const sequelize = new Sequelize({
dialect: 'postgres',
host:
, port: 5432,
username:
, password:
, database:
});
sequelize.addModels([]);
return sequelize;
}
}
]
import { Module } from '@nestjs/common';
import { databaseProvider } from './database.provider';
@Module({
providers: [...databaseProvider],
exports: [...databaseProvider]
})
export class DatabaseModule { }
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { DatabaseModule } from './modules/database/database.module';
@Module({
imports: [
DatabaseModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
import { Sequelize } from 'sequelize-typescript';
import Umzug = require('umzug');
const sequelize = new Sequelize({
dialect: 'postgres',
host: 'YOUR_HOST',
port: 5432,
username: 'YOUR_USERNAME',
password: 'YOUR_PASSWORD',
database: 'YOUR_DB_NAME'
});
const umzug = new Umzug({
storage: 'sequelize',
storageOptions: { sequelize },
logging: false,
migrations: {
params: [
sequelize,
sequelize.constructor,
],
path: './src/migrations',
pattern: /\.ts$/,
},
});
const task = (process.argv[2] || '').trim();
switch (task) {
case 'up':
umzug.up()
.then((result) => {
console.log('Migrations up went successful!', result);
process.exit(0);
});
break;
case 'down':
umzug.down()
.then((result) => {
console.log('Migrations down went successful!', result);
process.exit(0);
});
break;
default:
break;
};
import * as Sequelize from 'sequelize';
const tableName = 'Users';
export async function up(i: any) {
const queryInterface = i.getQueryInterface() as Sequelize.QueryInterface;
queryInterface.createTable(tableName, {
id: {
type: Sequelize.INTEGER,
allowNull: false,
autoIncrement: true,
unique: true,
primaryKey: true,
},
Username: {
type: Sequelize.CHAR(200),
allowNull: false,
},
Email: {
type: Sequelize.CHAR(50),
allowNull: false,
},
Password: {
type: Sequelize.CHAR(250),
allowNull: false,
},
});
};
export async function down(i: any) {
const queryInterface = i.getQueryInterface() as Sequelize.QueryInterface;
queryInterface.dropTable(tableName);
}
import * as Sequelize from 'sequelize';
const tableName = 'Accounts';
export async function up(i: any) {
const queryInterface = i.getQueryInterface() as Sequelize.QueryInterface;
queryInterface.createTable(tableName, {
id: {
type: Sequelize.INTEGER,
allowNull: false,
autoIncrement: true,
unique: true,
primaryKey: true,
},
Type: {
type: Sequelize.CHAR(200),
allowNull: false,
},
Name: {
type: Sequelize.CHAR(200),
allowNull: false,
},
Balance: {
type: Sequelize.INTEGER,
allowNull: true,
},
UserId: {
type: Sequelize.INTEGER,
references: {
model: 'Users',
key: 'id',
},
},
});
};
export async function down(i: any) {
const queryInterface = i.getQueryInterface() as Sequelize.QueryInterface;
queryInterface.dropTable(tableName);
}
我们打开 package.json 文件,然后添加一个新命令如下:
{
“ migrate”:“ ts-node ./migrate.ts”,
...
}
npm run migrate up
npm run migrate down
评论