0.0.3-beta.5 • Published 1 month ago

@xwink/dao v0.0.3-beta.5

Weekly downloads
-
License
-
Repository
github
Last release
1 month ago

😉 欢迎使用 @xwink/dao

版本 文档

💎 安装依赖

npm install --save @xwink/dao
pnpm add --save @xwink/dao

📖 快速入门

创建实例

import { useDao, QueryBuilder } from '@xwink/dao';

const dao = useDao({
    config: {
        host: '',
        port: 0,
        user: '',
        password: '',
        datebase: '',
    },
    debug: true,
    removeOptions: {
        controlField: 'del_flag',
        normalValue: 0,
        removedValue: 1,
    },
});

创建数据表

interface TestEntity {
    id: number;
    name: string;
    phone: string;
    age: number;
    sex: number;
    delFlag: boolean;
    createTime: Date;
    updateTime: Date;
    removeTime: Date;
}
const table = 't_user';
await dao.exec(`
    create table if not exists ${table} (
        id int primary key auto_increment,
        name varchar(255) not null,
        phone varchar(20) not null,
        age int not null default 0,
        sex int not null default 0,
        del_flag tinyint not null default 0,
        create_time datetime not null default now(),
        update_time datetime,
        remove_time datetime,
        unique (phone)
    )
`);

删除数据表

await dao.exec(`drop table if exists ${table}`);

插入数据

const data: Partial<TestEntity> = {
    name: 'test',
    phone: '10086',
};
// 单条数据
const id = await dao.insert({ table, data: [data] });
// 批量插入
await dao.insert({
    table,
    data: Array.from({ length: total - 1 }, (_, index) => ({
        name: Math.random().toString(36).substring(2, 8),
        phone: 1.32e10 + index,
        age: index,
        sex: index % 2,
    })),
});

统计数量

const count = await dao.count({ table, where: { sex: 0 } });

主键查询

const entity = await dao.detail<TestEntity>(table, id);

查询单条

const entity = await dao.get<TestEntity>({ table, where: { id } });

条件查询

const entities = await dao.select<TestEntity>({ table, where: { sex: 0 } });

分页查询

const page = await dao.page<TestEntity>({ table, where: { sex: 0 }, page: [1, 10] });
console.info(page.list);
console.info(page.total);

高级查询

const condition = { name: 'test' };
const builder = new QueryBuilder()
    .from(table)
    .equal('sex', 0, () => typeof condition.sex !== 'undefined')
    .like('name', condition.name, () => typeof condition.name !== 'undefined')
    .orderBy('age', 'desc')
    .page(1, 10);
const entities = await dao.query<TestEntity>(builder);

更新数据

const count = await dao.update({ table, data: { name: 'new test' }, where: { id } });

逻辑删除

const count = await dao.remove({ table, where: { id } });

逻辑恢复

const count = await dao.revoke({ table, where: { id } });

物理删除

const count = await dao.deletion({ table, where: { id } });

📦 进阶使用

import { useOrm, AutoTablePolicies, ColumnType } from '@xwink/dao';
const orm = useOrm(dao, { autoTablePolicy: AutoTablePolicies.UPDATE, normalrizeName: true });

创建仓库

const repository = registRepository<TestEntity>({
    name: table,
    columnDefines: [
        {
            name: 'id',
            type: ColumnType.INT,
            autoIncrement: true,
            primary: true,
            required: true,
            comment: '自增主键',
        },
        {
            name: 'delFlag',
            type: ColumnType.BOOLEAN,
            required: true,
            defaultValue: '0',
            comment: '逻辑删除标识',
        },
        {
            name: 'createTime',
            type: ColumnType.DATETIME,
            required: true,
            defaultValue: 'CURRENT_TIMESTAMP',
            comment: '创建时间',
        },
        {
            name: 'updateTime',
            type: ColumnType.DATETIME,
            comment: '修改时间',
        },
        {
            name: 'removeTime',
            type: ColumnType.DATETIME,
            comment: '移除时间',
        },
        {
            name: 'name',
            type: ColumnType.STRING,
            length: 64,
            required: true,
        },
        {
            name: 'phone',
            type: ColumnType.STRING,
            length: 20,
            required: true,
            unique: true,
        },
        {
            name: 'sex',
            type: ColumnType.INT,
            defaultValue: '0',
            required: true,
        },
        {
            name: 'age',
            type: ColumnType.INT,
            defaultValue: '0',
            required: true,
        },
    ],
});

主键查询

const entity = await repository.detail(id);

单条查询

const entity = await repository.get({ where: { id } });

条件查询

const entities = await repository.select({ where: { sex: 0 } });

数量查询

const count = await repository.count({ where: { sex: 0 } });

分页查询

const page = await repository.page({ where: { sex: 0 }, page: [1, 10] });
console.info(page.list);
console.info(page.total);

高级查询

const condition = { name: 'test' };
const builder = new QueryBuilder()
    .from(table)
    .equal('sex', 0, () => typeof condition.sex !== 'undefined')
    .like('name', condition.name, () => typeof condition.name !== 'undefined')
    .orderBy('age', 'desc')
    .page(1, 10);
const entities = await repository.query<TestEntity>(builder);

插入数据

const id = await repository.create([data]);

更新数据

const successful = await repository.update(data, { where: { id } });

逻辑删除

const successful = await repository.remove([id]);

逻辑恢复

const successful = await repository.revoke([id]);

物理删除

const successful = await repository.deletion([id]);

执行自定义语句

const entities = await repository.exec<TestEntity[]>(`select * from ${table} where sex = ? sort by age desc`, [0]);

📄 待办列表

  • 【feat-relaction】处理关联关系
  • 【refactor-adapter】支持适配多种数据库
  • 【feat-docs】新增vitepress文档项目并完善文档
  • 【chore】寻找伙(da)伴(lao)一起合作

🆘问题求助

🎯 框架依赖

👤 作者

向文可

  • Email: 1041367524@qq.com
  • Github: @x-wink

🤝 贡献

欢迎大家随时点击这里为我提供贡献、问题和功能建议

😘 感谢支持

如果觉得项目对你有帮助,就帮我点个小星星吧~ ⭐️

0.0.3-beta.5

1 month ago

0.0.3-beta.4

2 months ago

0.0.3-beta.3

3 months ago

0.0.3-beta.2

3 months ago

0.0.3-beta.0

4 months ago

0.0.3-beta.1

4 months ago

0.0.2

4 months ago

0.0.2-beta.4

5 months ago

0.0.2-beta.3

5 months ago

0.0.2-beta.2

5 months ago

0.0.2-beta.0

5 months ago

0.0.1

6 months ago