1

如何使用 loopback 4 typescript 在 Mssql 中调用存储过程

如何在数据源中进行查询,例如调用存储过程

import { DataSourceConstructor} from '@loopback/repository';

export const db = new DataSourceConstructor({
  connector: 'mssql',
  host: '192.168.1.110',
  port: 1433,
  database: 'FastDryNew',
  password: 'sa$123',
  user: 'sa',
});

目前我正在使用具有简单 where 条件的 Default Crud Repository

  @get('/Job/{CustomerID}')
  @param.path.number('CustomerID')
  async findJobByCustomerID(CustomerID: number): Promise<Jobs[]> {
    return await this.JobsRepo.find({ where: { CustomerID: CustomerID } });
  }

提前致谢。

4

1 回答 1

1

更改数据源配置

var DataSource = require('loopback-datasource-juggler').DataSource;

export var db = new DataSource({
    connector: require('loopback-connector-mssql'),
    host: '192.168.1.110',
    port: 1433,
    database: 'FastDryNew',
    password: 'sa$123',
    user: 'sa',
});

并在Controller上导入数据源

import { db } from '../datasources/db.datasource';

@get('/user')
async findUser() {
    let sql = 'select * from [User]';
    db.connector.query(sql, function (err: any, results: User[]) {
        if (err) {
            throw err;
        } else {
            return results;
        }
    });
}
于 2017-11-21T15:31:35.087 回答