|
| 1 | +/* eslint-disable @typescript-eslint/no-redeclare */ |
| 2 | +// @ts-nocheck |
| 3 | +import { $, _ } from 'coa-helper' |
| 4 | +import { RedisBin, RedisCache } from 'coa-redis' |
| 5 | +import { MysqlBin, MysqlCache, MysqlSafeTransaction } from '..' |
| 6 | +// MySQL配置 |
| 7 | +const mysqlConfig = { |
| 8 | + host: '127.0.0.1', |
| 9 | + port: 3306, |
| 10 | + user: 'root', |
| 11 | + password: '', |
| 12 | + charset: 'utf8mb4', |
| 13 | + trace: true, |
| 14 | + debug: false, |
| 15 | + databases: { |
| 16 | + main: { database: 'coa-mysql', ms: 7 * 24 * 3600 * 1000 }, |
| 17 | + }, |
| 18 | +} |
| 19 | + |
| 20 | +const redisConfig = { |
| 21 | + host: '127.0.0.1', |
| 22 | + port: 6379, |
| 23 | + password: '', |
| 24 | + db: 1, |
| 25 | + prefix: 'coa-mysql', |
| 26 | + trace: false, |
| 27 | + |
| 28 | +} |
| 29 | +const redisBin = new RedisBin(redisConfig) |
| 30 | +const redisCache = new RedisCache(redisBin) |
| 31 | + |
| 32 | +// 初始化Mysql基本连接,后续所有模型均依赖此实例 |
| 33 | +const mysqlBin = new MysqlBin(mysqlConfig) |
| 34 | +const safeTransaction = new MysqlSafeTransaction(mysqlBin) |
| 35 | + |
| 36 | +const userScheme = { |
| 37 | + userId: '' as string, |
| 38 | + name: '' as string, |
| 39 | + mobile: '' as string, |
| 40 | + avatar: '' as string, |
| 41 | + gender: 1 as number, |
| 42 | + language: '' as string, |
| 43 | + status: 1 as number, |
| 44 | + created: 0 as number, |
| 45 | + updated: 0 as number, |
| 46 | +} |
| 47 | + |
| 48 | +// const userScheme1 = { |
| 49 | +// userId: '' as string, |
| 50 | +// name: '' as string, |
| 51 | +// mobile: '' as string, |
| 52 | +// avatar: '' as string, |
| 53 | +// gender: 1 as number, |
| 54 | +// language: '' as string, |
| 55 | +// status: 1 as number, |
| 56 | +// created: 0 as number, |
| 57 | +// updated: 0 as number, |
| 58 | +// } |
| 59 | + |
| 60 | +// 定义User类型(通过默认结构自动生成) |
| 61 | +type UserScheme = typeof userScheme |
| 62 | + |
| 63 | + |
| 64 | +// 通过基类初始化 |
| 65 | +const User = new (class extends MysqlCache<UserScheme> { |
| 66 | + constructor() { |
| 67 | + super( |
| 68 | + { |
| 69 | + name: 'User', // 表名,默认会转化为下划线(snackCase)形式,如 User->user UserPhoto->user_photo |
| 70 | + title: '用户表', // 表的备注名称 |
| 71 | + scheme: userScheme, // 表的默认结构 |
| 72 | + pick: ['userId', 'name'], // 查询列表时显示的字段信息 |
| 73 | + caches: { index: ['name'], count: ['userId', 'name'] }, |
| 74 | + |
| 75 | + }, |
| 76 | + mysqlBin, |
| 77 | + redisCache, |
| 78 | + ) |
| 79 | + } |
| 80 | +})() |
| 81 | + |
| 82 | +// const User1 = new (class extends MysqlCache<UserScheme> { |
| 83 | +// constructor() { |
| 84 | +// super( |
| 85 | +// { |
| 86 | +// name: 'User1', // 表名,默认会转化为下划线(snackCase)形式,如 User->user UserPhoto->user_photo |
| 87 | +// title: '用户表', // 表的备注名称 |
| 88 | +// scheme: userScheme1, // 表的默认结构 |
| 89 | +// pick: ['userId', 'name'], // 查询列表时显示的字段信息 |
| 90 | +// }, |
| 91 | +// mysqlBin, |
| 92 | +// redisCache, |
| 93 | +// ) |
| 94 | +// } |
| 95 | +// })() |
| 96 | + |
| 97 | +// 批量插入 |
| 98 | +// await User.mInsert([ |
| 99 | +// { name: '王小明', gender: 1 }, |
| 100 | +// { name: '宋小华', gender: 1 }, |
| 101 | +// ]) |
| 102 | + |
| 103 | +// await User.updateById('id002', { name: '李四' }) // 返回 1 |
| 104 | +const a = async () => { |
| 105 | + await User.checkById('41102319990728253X') |
| 106 | + await $.timeout(3000) |
| 107 | + await safeTransaction.safeTransaction(async (trx: CoaMysql.Transaction) => { |
| 108 | + await User.updateById('41102319990728253X', { name: 'mmm' }) |
| 109 | + for (let index = 0; index < 10; index++) { |
| 110 | + const userId = `${_.now()}-${index}-Y` |
| 111 | + await User.insert({ userId, name: 'heyifan2' }, trx) |
| 112 | + } |
| 113 | + }) |
| 114 | + await $.timeout(3000) |
| 115 | + const b = await User.checkById('41102319990728253X') |
| 116 | + console.log(b); |
| 117 | +} |
| 118 | +a() |
0 commit comments