npm install xiaoming-mssql --save
exports.mssql = {
enable: true,
package: 'xiaoming-mssql',
};
config.mssql = {
clients: {
db1: {
user: '你的数据库用户名',
password: '数据库连接密码',
server: '数据库服务器ip',
database: '数据库名',
port: 端口号,
},
},
};
const pool = await this.app.mssql.get('db1');
const result = await pool.request().asyncExecute(`select * from [user] where Id = @Id`, { Id: 2 });
{ recordsets: [ [ [Object] ] ],
recordset: [ { Id: 2, name: '小李', age: 14 } ],
output: {},
rowsAffected: [ 1 ],
sql: 'select * from [user] where Id = 2' }
recordsets 和 recordset都是返回结果,但是recordsets返回的是对象,recordset返回的是json的字符串 rowsAffected代表查询的行数 sql代表的是真正执行的sql语句
查询有两种方式
- asyncExecute
- asyncQuery
插入数据也有三种方式
- asyncQuery
- asyncInsert
- parameterInsert 注意:当调用asyncQuery方法插入数据时,传入的参数一定要用数组包裹着 三种使用方法,都在下面可以看到演示的方法
更新数据有两种三种方式
- asyncQuery
- asyncUpdate
- parameterUpdate 下面也都有具体的传参的方式
- asyncQuery
- asyncDelete
const pool = this.app.mssql.get('db1');
const transaction = pool.transaction();
return await transaction.asyncBegin(async function(err) {
const result_6 = await transaction.request().asyncQuery(`select * from [User] where UserName=@userName and Status=1`, { userName: body.UserName });
await transaction.commit();
await transaction.rollback(function() { });
}
- linkedQuery
function query() {
const sql = `select * from [user] where Id=@id and customerCompanyId=@customerCompanyId`;
const b = translator.execute(sql, {
id: 1,
customerCompanyId: '123',
});
console.log(b)
}
select * from [user] where Id=1 and customerCompanyId='123'
function query2() {
const sql = `
declare @CustomerTypeId int
select stringValue,nullValue,numberValue,CustomerType from log
where stringValue = @stringValue and nullValue = @nullValue and numberValue = @numberValue and CustomerType=@CustomerType and number=@number`
const b = translator.execute(sql, {
stringValue: "string",
nullValue: null,
numberValue: 0,
CustomerType: 'Type',
number: undefined,
});
console.log(b)
}
declare @CUSTOMERTYPEID int
select stringValue,nullValue,numberValue,CustomerType from log
where stringValue = 'string' and nullValue = '' and numberValue = 0 and CustomerType='Type' and number=''
query2();
// 测试排序
function query3() {
const sql = `select
* from table
where id=@Id and name=@name
`;
const a = translator.execute(sql, {
id: 1,
name: "你好"
}, {
order: [{
'OrderBy': "id",
"Asc": 0
}, {
'OrderBy': 'name'
}]
});
console.log(a)
}
select
* from table
where id=1 and name='你好'
order by id desc,name asc
function query36() {
const sql = `select
* from table
where id=@Id and array in (@array) and name=@name order by @OrderBy @Asc
`;
const sql_c = translator.execute(sql, {
id: 1,
array: [1, 2, 3, 4],
name: "你好"
}, {
OrderBy: "id",
"Asc": 0
});
console.log(sql_c)
}
select
* from table
where id=1 and array in ('1','2','3','4') and name='你好' order by id DESC
function query4() {
const sql = `select
* from table
where id=@Id and array in (@array) and name=@name;
`;
const a = translator.execute(sql, {
id: 1,
array: [1, 2, 3, 4],
name: "你好"
}, {
order: {
'OrderBy': "id",
"Asc": 0
}
});
console.log(a)
}
select
* from table
where id=1 and array in ('1','2','3','4') and name='你好'
order by id desc
function query6() {
const sql = `insert into table (text,CreatorId,CreatorName)
Values(@text,@CreatorId,@CreatorName)`;
const a = translator.insert(
sql, [{
text1: "1",
}, {
text1: "2"
}], {
alisa: {
text: "text1"
},
extraData: {
CreatorId: 2,
CreatorName: "clg"
}
}
);
console.log(a)
}
insert into table (text,CreatorId,CreatorName)
values('1',2,'clg'),('2',2,'clg')
function query7() {
const sql = `INSERT INTO MaterialSheet (TaskId,Type,CreatorId,CreatorName,Status,CompanyId,Warehouse)
VALUES (@TaskId,@Type,@CreatorId,@CreatorName,@Status,@CompanyId,@MaterialWarehouseEngineer)
SELECT SCOPE_IDENTITY() AS id `;
const a = translator.insert(
sql, [{
TaskId: 1,
Type: 2,
CreatorId: 3,
CreatorName: 4,
Status: 5,
CompanyId: 6,
MaterialWarehouseEngineer: 7,
}]
);
console.log(a)
}
INSERT INTO MaterialSheet (TaskId,Type,CreatorId,CreatorName,Status,CompanyId,Warehouse)
values ('1','2','3','4','5','6','7')
SELECT SCOPE_IDENTITY() AS id
function query8() {
const sql = `INSERT [OrderLngLat] (Lng,Lat,Index,OrderId,CreatorId)
OUTPUT INSERTED.Iden INTO @batchInsertedIds(ID)
Values(@Lng,@Lat,@Index,@OrderId,@CreatorId);
`;
const a = translator.insert(sql, [{
"Index": 1,
"Lat": "113.122816",
"Lng": "23.028713"
},
{
"Index": 2,
"Lat": "113.123471",
"Lng": "23.029058"
},
{
"Index": 3,
"Lat": "113.12339",
"Lng": "23.028357"
},
{
"Index": 4,
"Lat": "113.1228",
"Lng": "23.028293"
}
], {
extraData: {
OrderId: 4,
CreatorId: 1
}
});
console.log(a)
}
INSERT [OrderLngLat] (Lng,Lat,Index,OrderId,CreatorId)
OUTPUT INSERTED.Iden INTO @BATCHINSERTEDIDS(ID)
values('23.028713','113.122816','1',4,1),('23.029058','113.123471','2',4,1),('23.028357','113.12339','3',4,1),('23.028293','113.1228','4',4,1)
function query13() {
const sql = `select Id,Name,age from [user] where Id=@Id and Name=@Name and age=@age`
const b = translator.execute(sql, {
age: 18,
Id: 2,
}, {
alisa: {
name: 'name1',
},
extraData: {
name1: 18,
}
});
console.log(b)
}
select Id,Name,age from [user] where Id=2 and Name=18 and age=18
function query18() {
const sql = 'update user set name=@name where Id=@id'
const sql_c = translator.update(sql, {
Name: '小李',
id: 2,
})
console.log(sql_c);
}
update user set name='小李' where Id=2
function query22() {
const sql = `IF EXISTS(SELECT * FROM [user] WHERE Id=@id)
INSERT INTO user2(name,age,gender) values(@name,@age,@gender)
ELSE
INSERT INTO [user](name,age) values(@name,@age);`
const sql_c = translator.insert(sql, [{
name: 'xiaohong',
age: 13,
gender: '1355'
},
{
name: 'xiaozhang',
age: 14,
gender: '1223'
},
{
name: 'xiaoli',
age: 17,
gender: '15'
},
{
name: 'xiaoming',
age: 20,
gender: '23'
},
],{extraData:{
Id:2,
}})
console.log(sql_c);
}
IF EXISTS(SELECT * FROM [user] WHERE Id=2)
INSERT INTO user2(name,age,gender) values('xiaohong','13','1355'),('xiaozhang','14','1223'),('xiaoli','17','15'),('xiaoming','20','23')
ELSE
INSERT INTO [user](name,age) values('xiaohong','13'),('xiaozhang','14'),('xiaoli','17'),('xiaoming','20');
function query14() {
const sql_data = [{
name: 'xiaoming',
age: 18
}, {
name: 'xiaohong'
}]
const sql_c = translator.parameterInsert('db.dbo.user', sql_data)
console.log(sql_c)
}
INSERT INTO db.dbo.user(name,age) VALUES('xiaoming','18'),('xiaohong',null)
function query15() {
const sql_data = [{
name: 'xiaoming',
age: 18
}, {
name: 'xiaohong'
}]
const sql_c = translator.parameterInsert('db.dbo.user', sql_data, {
alisa: {
name1: 'name',
},
extraData: {
gender: '女',
}
})
console.log(sql_c)
}
INSERT INTO db.dbo.user(age,gender,name1) VALUES('18','女','xiaoming'),(null,'女','xiaohong')
function query16() {
const sql_data = {
name: 'xiaoming',
age: 18
}
const sql_c = translator.parameterInsert('db.dbo.user', sql_data)
console.log(sql_c)
}
INSERT INTO db.dbo.user(name,age) VALUES('xiaoming','18')
function query31() {
const tableName = 'Employee'
const sql_c = translator.parameterUpdate(tableName, {
set: {
Name: 'aa',
Tel: 3333
},
where: {
and: {
id: 1
},
orNot: {
name: '123',
Tel: 333
}
}
})
console.log(sql_c)
}
update [Employee] set Name = 'aa',Tel = '3333' where id = '1'
function query44(){
console.time('计时器1')
const sql = translator.linkedQuery({
leftJoin:'abc',
rightJoin:'ddd',
joinKey:{'abca': 'ddda','test': 'test2'},
joinMode:'left'
},['abc.d','ddd.b'],{where: {
and: {'abc.Id': 123,'ddd.name':'xiaoming'}
},alias:{'abc':'a'}})
const sql_text = `select abc.d,ddd.b from abc left join ddd on abc.abca = ddd.ddda and abc.test = ddd.test2 where abc.Id = 123 and ddd.name = 'xiaoming'`
console.timeEnd('计时器1')
}
SELECT abc.d,ddd.b FROM abc AS a left join ddd ON abc.abca=ddd.ddda AND abc.test=ddd.test2 WHERE abc.Id=123 AND ddd.name='xiaoming'
function query45(){
console.time('计时器1')
const sql = translator.linkedQuery([{
leftJoin: 'ABC',
rightJoin: 'ddd',
joinKey:{'aaa':'ccc'},
joinMode:'inner',
},{
leftJoin: 'ddd',
rightJoin:'eee',
joinKey:{'accc':'dddd'},
joinMode:'left',
}],['ABC.qwer','eee.oiuy'],{where:{
and:{'ABC.qwer': 'test'},
},alias:{'ddd':'d'}})
console.log(sql)
console.timeEnd('计时器1')
}
SELECT ABC.qwer,eee.oiuy FROM ABC inner join ddd ON ABC.aaa=ddd.ccc left join eee ON ddd.accc=eee.dddd WHERE ABC.qwer='test'