npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dmg-file-storage

v1.0.6

Published

umijs mock help

Downloads

3

Readme

dmg-file-storage

主要解决在 umijs mock 中调用加载的自定义文件会有报错,但是加载包就不会,虽然不影响使用,但是还是做个包

安装及使用方法

安装

$ npm install https://github.com/zix2002/dmg-file-storage

打包

$ npm run build

使用

import fileStorage from 'dmg-file-storage';

方法

  • setItem 保存数据到文件
// 存入到 /cache/users.json
fileStorage.file('users.json').setItem([
  { id: 1, username: 'Donald', nickname: '乔丽' },
  { id: 2, username: 'Paul', nickname: '赵秀兰' },
  { id: 3, username: 'Mark', nickname: '曾涛' },
]);
  • getItem 从文件中取数据
// 取全部数据
const users = fileStorage.file('users.json').getItem();
  • where 设置过滤条件,需要searchpagination才返回对应数据
fileStorage
  .file('users.json')
  .where({ username: 'Donald' })
  .search();
  • orderBy 设置排序条件, 需要searchpagination才返回对应数据
fileStorage
  .file('users.json')
  .orderBy('id', 'asc') // 默认id , asc
  .search();
  • search 查询,配合whereorderBy使用,示例同上

  • paginate,分页查询,配合whereorderBy使用

const page = 1; // 1 是默认值
const pageSize = 10;
fileStorage
  .file('users.json')
  .orderBy('id', 'desc')
  .paginate(pageSize, page);
  • find 查询单条
fileStorage
  .file('users.json')
  .where({ id: 1 })
  .find();
  • create 添加一行数据
// 要添加的数据
const data = {
  username: 'Carol',
  nickname: '谢杰',
};
const pk = 'id'; // 主键 默认为id

fileStorage.file('users.json').create(data, pk);
  • update 更新一行数据
fileStorage
  .file('users.json')
  .where({ id: 1 })
  .update({
    username: 'Carol',
    nickname: '谢杰',
  });
  • destroy 删除一行数据
fileStorage
  .file('users.json')
  .where({ id: 1 })
  .destroy();
  • getTree 将 2 维数组转为树形结构
    • 默认检查parentId
    • 子数据为children
fileStorage
  .file('groups.json')
  .orderBy('sort', 'asc')
  .getTree();

如何配合umijs 的 mock

  • 先建立一个路由,例如:/api/seed/users
  • 通过mockjs, 生成模拟数据
  • 通过fileStorage.setItem, 存入指定文件, 如users.json
  • 再新建用于 mock 的路由,如/api/users, 通过 fileStorage.search方法进行查询,并返回