Skip to main content

Start Using

About 695 words

Start Using

Create Data Model

We are using a simple user model as an example to create a UserModel. A user has properties such as name, age, and gender.

import { BaseModel } from "tiny-crud";

export interface UserModel extends BaseModel {
    name: string;
    age: number;
    gender: string;
}

Tips

Data models that inherit from BaseModel will automatically have four fields: id, created_at, updated_at, and created_by. The content of these four fields will be automatically brought out from the corresponding data in the storage.

Create Data Repository

Create a UserRepository that inherits from GithubRepository and pass the generic parameter as the UserModel defined above.

github
import { GithubRepository } from "tiny-crud";
import { githubRequest } from "./github-request";

export class UserRepository extends GithubRepository<UserModel> {
    constructor() {
        super(githubRequest, "Your Issue Number");
    }
}

Tips

The second parameter of the constructor is the issue number of the repository, which is used to specify the issue for storing data.If you don't pass in this parameter, the issue number carried by the object of the first parameter you passed in will be used by default.

You can use the usual way to create a repository object, like this:

const userRepository = new UserRepository();

You can also use SingletonFactory to create a singleton object:

import { SingletonFactory } from "tiny-crud";

const userRepository = SingletonFactory.createInstance(UserRepository);

The data repository provides some basic CRUD methods, of course, you can also extend it further, for example:

import { GithubRepository } from "tiny-crud";

export class UserRepository extends GithubRepository<UserModel> {
    constructor() {
        super(GithubRequest);
    }

    async findByName(name: string): Promise<UserModel[]> {
        const users = await this.find();
        return users.filter((user) => user.name === name);
    }
}

CRUD Methods

detail

Get the details of the data entry.

userRepository.detail().then((res) => console.log(res));

The return value is an object, which contains the following fields:

field nametypedescription
idnumberIssue Id
issue_numberstringIssue Number
commentsnumberIssue Comments
created_atstringIssue Created Time
updated_atstringIssue Updated Time

Tips

For Github or Gitlab, the comments field displays the actual number, for Gitee, the comments field displays the cumulative number.

For example: Add 10 data and then delete them all. For Github or Gitlab, the comments field displays 0, for Gitee, the comments field displays 10.

create

Create a new data entry.

userRepository.create({
    name: "John",
    age: 30,
    gender: "male",
});

createAll

Create multiple data entries.

userRepository.createAll([
    {
        name: "John",
        age: 30,
        gender: "male",
    },
    {
        name: "Mary",
        age: 25,
        gender: "female",
    },
]);

find

Find all data entries.

userRepository.find().then((res) => console.log(res));

find method supports one or more parameters, you can pass in a parameter to filter the data, for example:

userRepository
    .find({
        since: "2021-01-01T00:00:00Z",
        per_page: 10,
        page: 1,
    })
    .then((res) => console.log(res));

The query parameters supported by the find method depending on the storage platform you are using:

github
field nametypedescription
sincestringOnly data updated after a given time is returned,format is ISO 8601 YYYY-MM-DDTHH:MM:SSZ
pagenumberReturns the page of data,default 1
per_pagenumberThe number of data items returned per page,default 30,max 100

findById

Find a data entry by id.

userRepository.findById(1).then((res) => console.log(res));

updateById

Update a data entry by id.

userRepository.updateById(1, {
    name: "Mary",
    age: 25,
    gender: "female",
});

deleteById

Delete a data entry by id.

userRepository.deleteById(1);

deleteAll

Caution

This method will delete all data entries in the issue and cannot be restored.please use with caution.

userRepository.deleteAll();
Last update: