# @easythings/easy-model

> 一组给model用的decorator

Latest version **0.1.2** (published 2022-08-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install @easythings/easy-model
pnpm add @easythings/easy-model
yarn add @easythings/easy-model
bun add @easythings/easy-model
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2022-08-09 |
| First published | 2022-08-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 40.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | 344275632@qq.com |
| Maintainers | q344275632 |
| Keywords | easy, model |

## Links

- npm: https://www.npmjs.com/package/@easythings/easy-model
- Repository: https://github.com/Dissme/easy-model
- Homepage: https://github.com/Dissme/easy-model#readme
- Issues: https://github.com/Dissme/easy-model/issues
- npm.io page: https://npm.io/package/@easythings/easy-model

## Recent versions

- 0.1.2 (latest) — 2022-08-09
- 0.1.1 — 2022-08-08
- 0.1.0 — 2022-08-08

## README

# 说明

## 使用方法
```bash
npm i @easythings/easy-model
```


```js
import { off, primaryKey, easy, inited } from "@easythings/easy-model";

@easy
class TestModel {
  @primaryKey id;
  @primaryKey tel;

  @off xx = 123;

  name = "123";

  lastName = "";

  constructor(initails) {
    Object.assign(this, initails);
  }

  get() {
    return; // 在这里请求，返回后段返回的数据
  }

  post() {
    return; // 在这里请求，返回后段返回的数据
  }

  put() {
    return; // 在这里请求，返回后段返回的数据
  }

  delete() {
    // 在这里请求
  }

  destroy() {
    // 销毁时候会触发
  }
}

const m = new TestModel({ id: 1, tel: 123 });
await m.sync(); // 会调用get
const m2 = new TestModel({ name: "xxx" });
await m2.sync(); // 会调用post
const m3 = new TestModel({ id: m2.id, tel: m2.tel });
m3.name = "asdf";
await m3.sync(); // 会调用put
console.log(m3.name, m3.name === m2.name, m3 === m2); // 相同的主键是同一个对象
m3.name = 'fasdf';
m3.lastName = 'galsadkfjlkj';
m3.reset();
console.log(m3.name, m2.name) // 还原成最近一次跟后端交互过的状态
m3.sync(); // 不会请求
```

## [changelog](./CHANGELOG.md)

---
_Source: https://npm.io/package/@easythings/easy-model · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
