Skip to content
0

Storage

包括所有的储存相关库

localForage ---> 浏览器端存储库

优先使用 IndexDB,如果 IndexDB 不可用,则使用 WebSQL,如果 WebSQL 也不可用,则使用 localStorage。开发者无需关心具体实现,直接使用即可。

import localforage from 'localforage'

localforage.setItem('key', 'value', function (err) {
  // if err is non-null, we got an error
  localforage.getItem('key', function (err, value) {
    // if err is non-null, we got an error. otherwise, value is the value
  });
});

jsonnet ---> 数据模板语言

如下的 jsonnet

local application = 'my-app';
local module = 'uwsgi_module';
local dir = '/var/www';
local permission = 644;

{
  'uwsgi.ini': std.manifestIni({
    sections: {
      uwsgi: {
        module: module,
        pythonpath: dir,
        socket: dir + '/uwsgi.sock',
        'chmod-socket': permission,
        callable: application,
        logto: '/var/log/uwsgi/uwsgi.log',
      },
    },
  }),

  'init.sh': |||
    #!/usr/bin/env bash
    mkdir -p %(dir)s
    touch %(dir)s/initialized
    chmod %(perm)d %(dir)s/initialized
  ||| % {dir: dir, perm: permission},

  'cassandra.conf': std.manifestYamlDoc({
    cluster_name: application,
    seed_provider: [
      {
        class_name: 'SimpleSeedProvider',
        parameters: [{ seeds: '127.0.0.1' }],
      },
    ],
  }),
}

可以生成如下的文件:

"cluster_name": "my-app"
"seed_provider":
- "class_name": "SimpleSeedProvider"
  "parameters":
  - "seeds": "127.0.0.1"
#!/usr/bin/env bash
mkdir -p /var/www
touch /var/www/initialized
chmod 644 /var/www/initialized
[uwsgi]
callable = my-app
chmod-socket = 644
logto = /var/log/uwsgi/uwsgi.log
module = uwsgi_module
pythonpath = /var/www
socket = /var/www/uwsgi.sock

fake-indexeddb ---> 用JS轻松操作 IndexedDB

import "fake-indexeddb/auto"

var request = indexedDB.open("test", 3);
request.onupgradeneeded = function () {
  var db = request.result;
  var store = db.createObjectStore("books", {keyPath: "isbn"});
  store.createIndex("by_title", "title", {unique: true});

  store.put({title: "Quarry Memories", author: "Fred", isbn: 123456});
  store.put({title: "Water Buffaloes", author: "Fred", isbn: 234567});
  store.put({title: "Bedrock Nights", author: "Barney", isbn: 345678});
}

相似项目:jakearchibald/idb-keyval

Dexie.js ---> IndexedDB lib

<!doctype html>
<html>
 <head>
  <script src="https://unpkg.com/dexie@latest/dist/dexie.js"></script>
  <script>
   //
   // Declare Database
   //
   var db = new Dexie("FriendDatabase");
   db.version(1).stores({
     friends: "++id,name,age"
   });

   //
   // Manipulate and Query Database
   //
   db.friends.add({name: "Josephine", age: 21}).then(function() {
       return db.friends.where("age").below(25).toArray();
   }).then(function (youngFriends) {
       alert ("My young friends: " + JSON.stringify(youngFriends));
   }).catch(function (e) {
       alert ("Error: " + (e.stack || e));
   });
  </script>
 </head>
</html>

drizzle-orm ---> ORM 框架

支持 MySql 等多种数据库

// schema.ts
import { mysqlTable, serial, text, varchar } from "drizzle-orm/mysql-core";

export const users = mysqlTable('users', {
  id: serial('id').primaryKey(),
  fullName: text('full_name'),
  phone: varchar('phone', { length: 256 }),
});

lru-cache ---> LRU 缓存

LRU 缓存是非常常用的缓存策略

import { LRUCache } from 'lru-cache'

const config = {}

const cache = new LRUCache(config)

cache.set('key', 'value')
cache.get('key') // "value"

ioredis ---> redis 客户端

功能非常丰富的 Redis 客户端

import Redis from "ioredis"

const redis = new Redis();

redis.set("mykey", "value"); // Returns a promise which resolves to "OK" when the command succeeds.

// ioredis supports the node.js callback style
redis.get("mykey", (err, result) => {
  if (err) {
    console.error(err);
  } else {
    console.log(result); // Prints "value"
  }
})

Released under the MIT License.