• Stars
    star
    3
  • Rank 3,963,521 (Top 79 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 2 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A minimalistic, secure, type-safe, zero-dependencies, persistent data store

Stand With Ukraine


Nano filesystem storage

Buy Me A Coffee

A minimalistic, secure, type-safe, zero-dependencies, persistent data store.

Note If you want safely use it in electron app look at electron-nano-store

Usage

import { defineStore } from "fs-nano-store";

/**
 * Declare types for you storage
 */
type Store = {
	name: string,
	role: 'admin' | 'user'
}

const { get, set, changes } = await defineStore<Store>('/path/to/storage-file.json')

get('name') // undefined
set('name', 'Alex')
get('name') // Alex

// Store is Type-safe
// TS Error: Argument of type '"wrong-role"' is not assignable to parameter of type '"admin" | "user"'.
set('role', 'wrong-role')

// fs-nano-store automatically tracks any storage-file.json changes.
// Additionally, you can addListener on the `changed` event that emits
// if the store file has been modified somehow outside defined store methods.
changes.addListener('changed', () => {
})

Note

Objects in store are immutable and will be deeply cloned on each get/set.

const obj = {}
store.set('obj', obj)
store.get('obj') === obj // false
store.get('obj') === store.get('obj') // false
store.get('obj').bar = 'baz' // will no have effect
obj.bar = 'baz' // will not affected to stored data

Custom serializer

By default, all data is serialized in JSON using global JSON. So if you want to store more complex data types like Date or Map, or want to have custom stringify/parse logic, you need to use your own serializer that supports those data types. Example with superjson:

import { defineStore } from 'fs-nano-store'
import superjson from 'superjson';

type Store = {
	date: Date,
}

const store = defineStore<Store>('store-file.json', {
	serializer: superjson
})

store.set('date', new Date)
store.get('date') // Date object

Migrating from v0.2.x to v0.3.x

In https://github.com/cawa-93/fs-nano-store/commit/bd2dfb50c92eadae68f6a12e406acf6daacd05f7 Was changed how exactly data saving to filesystem. Old store files are incompatible. You may need manually convert old data to new format by command:

const newDataStr = JSON.stringify(
  Object.entries(serializer.parse(oldDataStr))
  .map(([k,v]) => [k,serializer.stringify(v)])  
) 

More Repositories

1

vite-electron-builder

Secure boilerplate for Electron app based on Vite. TypeScript + Vue/React/Angular/Svelte/Vanilla
TypeScript
2,332
star
2

iconify-prerendered

A superset standalone icon-components for Vue with zero dependencies. Designed for ease of use and high performance.
TypeScript
84
star
3

unplugin-auto-expose

Plugins for automatic exposeInMainWorld everything you exported from preload and easily importing exposed api in renderer
TypeScript
39
star
4

play-shikimori-online

Расширение позволяет смотреть аниме онлайн и автоматическии синхронизировать его с вашим списком на Шикимори (shikimori.org и shikimori.one)
JavaScript
33
star
5

anime-library

Бесплатное Приложение для просмотра аниме онлайн
TypeScript
29
star
6

youtube-forward-timeline

Browser extension for fast-forward YouTube video to the next section.
JavaScript
18
star
7

Vk-Emoji

Replace symbols emoticons VK the corresponding HTML code
JavaScript
12
star
8

dts-for-context-bridge

Generate `.d.ts` file for Electron `contextBridge.exposeInMainWorld` calls
JavaScript
9
star
9

electron-nano-store

A minimalistic, secure, type-safe data store for Electron
TypeScript
8
star
10

vite-plugin-vue-chrome-i18n

A plugin for the Vite that extracts and scope translations from Vue Single File Component and combines them into a localization file for browser extensions.
TypeScript
2
star
11

admitad-data-studio-connector

Google Data Studio Connector for Admitad advertiser reports
TypeScript
2
star
12

anime-news-crawler-bot

Чат бот, который ищет аниме новости по выбранным франшизам
JavaScript
2
star
13

kozack.me

⁨⁨⁨Alex⁩ ⁨Kozack⁩⁩ — ⁨Full stack web developer⁩⁩ — ⁨Personal site⁩
TypeScript
1
star
14

HWL-uk-pak-debugger

TypeScript
1
star
15

HYWenHei-Extended-Font

HYWenHei (Genshin Impact) Font extendet to support additional languages
1
star
16

rollup-plugin-dynamic-macros

A minimalist Rollup and Vite plugin for creating compile-time macros.
JavaScript
1
star
17

anime-tracker-bot

TypeScript
1
star
18

shikimori-to-anitube

Скрипт для tampermonkey, що додає прямі посилання з shikimori.one на anitube.in.ua
JavaScript
1
star
19

comebackalive-paywall

Генератор посилань на захищений контент. Захищений контент буде доступний користувачу лише після пожертви у фонд Повернись Живим
Vue
1
star
20

public-accesebility-reports

1
star