|
@@ -4,7 +4,7 @@ import { compact, get, isArray, isObject } from 'lodash';
|
|
|
import path = require('path');
|
|
|
import { Repository } from 'typeorm';
|
|
|
// import { Achievement } from '../entity/platform/achievement.entity';
|
|
|
-import { InjectEntityModel } from '@midwayjs/typeorm';
|
|
|
+import { InjectEntityModel, TypeORMDataSourceManager } from '@midwayjs/typeorm';
|
|
|
import { News } from '../entity/platform/news.entity';
|
|
|
import { Demand } from '../entity/platform/demand.entity';
|
|
|
import { Expert } from '../entity/users/expert.entity';
|
|
@@ -12,13 +12,13 @@ import { Company } from '../entity/users/company.entity';
|
|
|
import { Context } from '@midwayjs/koa';
|
|
|
import * as Excel from 'exceljs';
|
|
|
import { Achievement } from '../entity/platform/achievement.entity';
|
|
|
+import { Test } from '../entity/test.entity';
|
|
|
+import { ServiceError } from '../error/service.error';
|
|
|
// import { upperFirst } from 'lodash';
|
|
|
|
|
|
// import { User } from '../entity/system/user.entity';
|
|
|
@Provide()
|
|
|
export class UtilService {
|
|
|
- // @InjectEntityModel(User)
|
|
|
- // uModel: ReturnModelType<typeof User>;
|
|
|
@Inject()
|
|
|
ctx: Context;
|
|
|
randomStr(len = 6) {
|
|
@@ -34,6 +34,37 @@ export class UtilService {
|
|
|
// await this.uModel.updateOne({ _id: data.user }, { status: '0' });
|
|
|
// }
|
|
|
}
|
|
|
+ @Inject()
|
|
|
+ dataSourceManager: TypeORMDataSourceManager;
|
|
|
+ async DBTransaction() {
|
|
|
+ const dataSource = this.dataSourceManager.getDataSource('default');
|
|
|
+ const data: Test = {
|
|
|
+ nick_name: '测试',
|
|
|
+ account: `test-${new Date().getTime()}`, //
|
|
|
+ hobby: ['睡觉'],
|
|
|
+ props: { hobby: '睡觉' },
|
|
|
+ array_props: [{ hobby: '睡觉', lik: 'nothing' }],
|
|
|
+ };
|
|
|
+ await dataSource.transaction(async em => {
|
|
|
+ // 事务中的操作会依次做好,并且下一步会在上一步的基础上进行.只有整个函数执行完,才会落实到数据库中
|
|
|
+ // const res = await em.update(Test, { id: 1 }, { account: 'test' });
|
|
|
+ // const ri = await em.insert(Test, data);
|
|
|
+ // const r2 = await em.count(Test, { where: { account: 'test-2' } });
|
|
|
+ // if (r2 > 0) throw new ServiceError('data exists');
|
|
|
+ // const r = await em.getRepository(Test).query('select json_query(\'{"a": [1, 2, 3, 4, 5], "b": ["2", "a b"], "c": {"d": 4, "e": "ab c"}}\'::jsonb,\'$.b\');');
|
|
|
+ const tb = em
|
|
|
+ .getRepository(Test)
|
|
|
+ .createQueryBuilder()
|
|
|
+ // .where('props @> :hobby', { hobby: { hobby: '睡觉' } });
|
|
|
+ .where('hobby @> :hobby', { hobby: '睡觉' });
|
|
|
+ const tres = await tb.getOne();
|
|
|
+ console.log(tres);
|
|
|
+ // const builder = em.getRepository(Test).createQueryBuilder('test').select(['test.id', 'test.array_props']).where({ id: 1 });
|
|
|
+ // console.log(builder.getSql());
|
|
|
+ // const res = await builder.getOne();
|
|
|
+ // console.log(res);
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
@InjectEntityModel(Demand)
|
|
|
model: Repository<Demand>;
|