@signaldb/localstorage 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/dist/.vite/manifest.json +8 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.mjs +38 -0
- package/dist/index.mjs.map +1 -0
- package/dist/index.umd.js +2 -0
- package/dist/index.umd.js.map +1 -0
- package/package.json +56 -0
package/README.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# @signaldb/localstorage
|
|
2
|
+
|
|
3
|
+
This is the `localStorage` persistence adapter for [SignalDB](https://github.com/maxnowack/signaldb). SignalDB is a local-first JavaScript database with real-time sync, enabling optimistic UI with signal-based reactivity across multiple frameworks.
|
|
4
|
+
|
|
5
|
+
See https://signaldb.js.org/reference/localstorage/ for more information.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a persistence adapter for managing a SignalDB collection using browser `localStorage`.
|
|
3
|
+
* This adapter reads and writes data to `localStorage`, with customizable serialization and deserialization.
|
|
4
|
+
* @template T - The type of the items in the collection.
|
|
5
|
+
* @template I - The type of the unique identifier for the items.
|
|
6
|
+
* @param name - A unique name for the collection, used as the key in `localStorage`.
|
|
7
|
+
* @param options - Optional configuration for serialization and deserialization.
|
|
8
|
+
* @param options.serialize - A function to serialize items to a string (default: `JSON.stringify`).
|
|
9
|
+
* @param options.deserialize - A function to deserialize a string into items (default: `JSON.parse`).
|
|
10
|
+
* @returns A SignalDB persistence adapter for managing data in `localStorage`.
|
|
11
|
+
* @example
|
|
12
|
+
* import createLocalStorageAdapter from './createLocalStorageAdapter';
|
|
13
|
+
*
|
|
14
|
+
* const adapter = createLocalStorageAdapter('myCollection', {
|
|
15
|
+
* serialize: (items) => JSON.stringify(items, null, 2), // Pretty-print JSON
|
|
16
|
+
* deserialize: (itemsString) => JSON.parse(itemsString), // Default JSON parse
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* const collection = new Collection({
|
|
20
|
+
* persistence: adapter,
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Perform operations on the collection, and changes will be reflected in local storage.
|
|
24
|
+
*/
|
|
25
|
+
export default function createLocalStorageAdapter<T extends {
|
|
26
|
+
id: I;
|
|
27
|
+
} & Record<string, any>, I>(name: string, options?: {
|
|
28
|
+
serialize?: (items: T[]) => string;
|
|
29
|
+
deserialize?: (itemsString: string) => T[];
|
|
30
|
+
}): import("@signaldb/core").PersistenceAdapter<T, I>;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createPersistenceAdapter as m } from "@signaldb/core";
|
|
2
|
+
function h(c, a) {
|
|
3
|
+
const { serialize: d = JSON.stringify, deserialize: l = JSON.parse } = a || {}, n = `signaldb-collection-${c}`;
|
|
4
|
+
function s() {
|
|
5
|
+
return l(localStorage.getItem(n) || "[]");
|
|
6
|
+
}
|
|
7
|
+
return m({
|
|
8
|
+
async load() {
|
|
9
|
+
const i = s();
|
|
10
|
+
return Promise.resolve({ items: i });
|
|
11
|
+
},
|
|
12
|
+
async save(i, { added: f, modified: u, removed: I }) {
|
|
13
|
+
const r = s();
|
|
14
|
+
return f.forEach((e) => {
|
|
15
|
+
r.push(e);
|
|
16
|
+
}), u.forEach((e) => {
|
|
17
|
+
const t = r.findIndex(({ id: o }) => o === e.id);
|
|
18
|
+
/* istanbul ignore if -- @preserve */
|
|
19
|
+
if (t === -1)
|
|
20
|
+
throw new Error(`Item with ID ${e.id} not found`);
|
|
21
|
+
r[t] = e;
|
|
22
|
+
}), I.forEach((e) => {
|
|
23
|
+
const t = r.findIndex(({ id: o }) => o === e.id);
|
|
24
|
+
/* istanbul ignore if -- @preserve */
|
|
25
|
+
if (t === -1)
|
|
26
|
+
throw new Error(`Item with ID ${e.id} not found`);
|
|
27
|
+
r.splice(t, 1);
|
|
28
|
+
}), localStorage.setItem(n, d(r)), Promise.resolve();
|
|
29
|
+
},
|
|
30
|
+
async register() {
|
|
31
|
+
return Promise.resolve();
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
export {
|
|
36
|
+
h as default
|
|
37
|
+
};
|
|
38
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["import { createPersistenceAdapter } from '@signaldb/core';\n/**\n * Creates a persistence adapter for managing a SignalDB collection using browser `localStorage`.\n * This adapter reads and writes data to `localStorage`, with customizable serialization and deserialization.\n * @template T - The type of the items in the collection.\n * @template I - The type of the unique identifier for the items.\n * @param name - A unique name for the collection, used as the key in `localStorage`.\n * @param options - Optional configuration for serialization and deserialization.\n * @param options.serialize - A function to serialize items to a string (default: `JSON.stringify`).\n * @param options.deserialize - A function to deserialize a string into items (default: `JSON.parse`).\n * @returns A SignalDB persistence adapter for managing data in `localStorage`.\n * @example\n * import createLocalStorageAdapter from './createLocalStorageAdapter';\n *\n * const adapter = createLocalStorageAdapter('myCollection', {\n * serialize: (items) => JSON.stringify(items, null, 2), // Pretty-print JSON\n * deserialize: (itemsString) => JSON.parse(itemsString), // Default JSON parse\n * });\n *\n * const collection = new Collection({\n * persistence: adapter,\n * });\n *\n * // Perform operations on the collection, and changes will be reflected in local storage.\n */\nexport default function createLocalStorageAdapter(name, options) {\n const { serialize = JSON.stringify, deserialize = JSON.parse } = options || {};\n const collectionId = `signaldb-collection-${name}`;\n function getItems() {\n return deserialize(localStorage.getItem(collectionId) || '[]');\n }\n return createPersistenceAdapter({\n async load() {\n const items = getItems();\n return Promise.resolve({ items });\n },\n async save(items, { added, modified, removed }) {\n const currentItems = getItems();\n added.forEach((item) => {\n currentItems.push(item);\n });\n modified.forEach((item) => {\n const index = currentItems.findIndex(({ id }) => id === item.id);\n /* istanbul ignore if -- @preserve */\n if (index === -1)\n throw new Error(`Item with ID ${item.id} not found`);\n currentItems[index] = item;\n });\n removed.forEach((item) => {\n const index = currentItems.findIndex(({ id }) => id === item.id);\n /* istanbul ignore if -- @preserve */\n if (index === -1)\n throw new Error(`Item with ID ${item.id} not found`);\n currentItems.splice(index, 1);\n });\n localStorage.setItem(collectionId, serialize(currentItems));\n return Promise.resolve();\n },\n async register() {\n return Promise.resolve();\n },\n });\n}\n"],"names":["createLocalStorageAdapter","name","options","serialize","deserialize","collectionId","getItems","createPersistenceAdapter","items","added","modified","removed","currentItems","item","index","id"],"mappings":";AAyBwB,SAAAA,EAA0BC,GAAMC,GAAS;AACvD,QAAA,EAAE,WAAAC,IAAY,KAAK,WAAW,aAAAC,IAAc,KAAK,UAAUF,KAAW,CAAC,GACvEG,IAAe,uBAAuBJ,CAAI;AAChD,WAASK,IAAW;AAChB,WAAOF,EAAY,aAAa,QAAQC,CAAY,KAAK,IAAI;AAAA,EAAA;AAEjE,SAAOE,EAAyB;AAAA,IAC5B,MAAM,OAAO;AACT,YAAMC,IAAQF,EAAS;AACvB,aAAO,QAAQ,QAAQ,EAAE,OAAAE,GAAO;AAAA,IACpC;AAAA,IACA,MAAM,KAAKA,GAAO,EAAE,OAAAC,GAAO,UAAAC,GAAU,SAAAC,KAAW;AAC5C,YAAMC,IAAeN,EAAS;AACxB,aAAAG,EAAA,QAAQ,CAACI,MAAS;AACpB,QAAAD,EAAa,KAAKC,CAAI;AAAA,MAAA,CACzB,GACQH,EAAA,QAAQ,CAACG,MAAS;AACjB,cAAAC,IAAQF,EAAa,UAAU,CAAC,EAAE,IAAAG,QAASA,MAAOF,EAAK,EAAE;AAAA,QAAA;AAE/D,YAAIC,MAAU;AACV,gBAAM,IAAI,MAAM,gBAAgBD,EAAK,EAAE,YAAY;AACvD,QAAAD,EAAaE,CAAK,IAAID;AAAA,MAAA,CACzB,GACOF,EAAA,QAAQ,CAACE,MAAS;AAChB,cAAAC,IAAQF,EAAa,UAAU,CAAC,EAAE,IAAAG,QAASA,MAAOF,EAAK,EAAE;AAAA,QAAA;AAE/D,YAAIC,MAAU;AACV,gBAAM,IAAI,MAAM,gBAAgBD,EAAK,EAAE,YAAY;AAC1C,QAAAD,EAAA,OAAOE,GAAO,CAAC;AAAA,MAAA,CAC/B,GACD,aAAa,QAAQT,GAAcF,EAAUS,CAAY,CAAC,GACnD,QAAQ,QAAQ;AAAA,IAC3B;AAAA,IACA,MAAM,WAAW;AACb,aAAO,QAAQ,QAAQ;AAAA,IAAA;AAAA,EAC3B,CACH;AACL;"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
(function(n,r){typeof exports=="object"&&typeof module<"u"?module.exports=r(require("@signaldb/core")):typeof define=="function"&&define.amd?define(["@signaldb/core"],r):(n=typeof globalThis<"u"?globalThis:n||self,n.SignalDB=r(n.core))})(this,function(n){"use strict";function r(a,f){const{serialize:l=JSON.stringify,deserialize:u=JSON.parse}=f||{},s=`signaldb-collection-${a}`;function c(){return u(localStorage.getItem(s)||"[]")}return n.createPersistenceAdapter({async load(){const d=c();return Promise.resolve({items:d})},async save(d,{added:h,modified:m,removed:p}){const t=c();return h.forEach(e=>{t.push(e)}),m.forEach(e=>{const o=t.findIndex(({id:i})=>i===e.id);/* istanbul ignore if -- @preserve */if(o===-1)throw new Error(`Item with ID ${e.id} not found`);t[o]=e}),p.forEach(e=>{const o=t.findIndex(({id:i})=>i===e.id);/* istanbul ignore if -- @preserve */if(o===-1)throw new Error(`Item with ID ${e.id} not found`);t.splice(o,1)}),localStorage.setItem(s,l(t)),Promise.resolve()},async register(){return Promise.resolve()}})}return r});
|
|
2
|
+
//# sourceMappingURL=index.umd.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../src/index.ts"],"sourcesContent":["import { createPersistenceAdapter } from '@signaldb/core';\n/**\n * Creates a persistence adapter for managing a SignalDB collection using browser `localStorage`.\n * This adapter reads and writes data to `localStorage`, with customizable serialization and deserialization.\n * @template T - The type of the items in the collection.\n * @template I - The type of the unique identifier for the items.\n * @param name - A unique name for the collection, used as the key in `localStorage`.\n * @param options - Optional configuration for serialization and deserialization.\n * @param options.serialize - A function to serialize items to a string (default: `JSON.stringify`).\n * @param options.deserialize - A function to deserialize a string into items (default: `JSON.parse`).\n * @returns A SignalDB persistence adapter for managing data in `localStorage`.\n * @example\n * import createLocalStorageAdapter from './createLocalStorageAdapter';\n *\n * const adapter = createLocalStorageAdapter('myCollection', {\n * serialize: (items) => JSON.stringify(items, null, 2), // Pretty-print JSON\n * deserialize: (itemsString) => JSON.parse(itemsString), // Default JSON parse\n * });\n *\n * const collection = new Collection({\n * persistence: adapter,\n * });\n *\n * // Perform operations on the collection, and changes will be reflected in local storage.\n */\nexport default function createLocalStorageAdapter(name, options) {\n const { serialize = JSON.stringify, deserialize = JSON.parse } = options || {};\n const collectionId = `signaldb-collection-${name}`;\n function getItems() {\n return deserialize(localStorage.getItem(collectionId) || '[]');\n }\n return createPersistenceAdapter({\n async load() {\n const items = getItems();\n return Promise.resolve({ items });\n },\n async save(items, { added, modified, removed }) {\n const currentItems = getItems();\n added.forEach((item) => {\n currentItems.push(item);\n });\n modified.forEach((item) => {\n const index = currentItems.findIndex(({ id }) => id === item.id);\n /* istanbul ignore if -- @preserve */\n if (index === -1)\n throw new Error(`Item with ID ${item.id} not found`);\n currentItems[index] = item;\n });\n removed.forEach((item) => {\n const index = currentItems.findIndex(({ id }) => id === item.id);\n /* istanbul ignore if -- @preserve */\n if (index === -1)\n throw new Error(`Item with ID ${item.id} not found`);\n currentItems.splice(index, 1);\n });\n localStorage.setItem(collectionId, serialize(currentItems));\n return Promise.resolve();\n },\n async register() {\n return Promise.resolve();\n },\n });\n}\n"],"names":["createLocalStorageAdapter","name","options","serialize","deserialize","collectionId","getItems","createPersistenceAdapter","items","added","modified","removed","currentItems","item","index","id"],"mappings":"4QAyBwB,SAAAA,EAA0BC,EAAMC,EAAS,CACvD,KAAA,CAAE,UAAAC,EAAY,KAAK,UAAW,YAAAC,EAAc,KAAK,OAAUF,GAAW,CAAC,EACvEG,EAAe,uBAAuBJ,CAAI,GAChD,SAASK,GAAW,CAChB,OAAOF,EAAY,aAAa,QAAQC,CAAY,GAAK,IAAI,CAAA,CAEjE,OAAOE,2BAAyB,CAC5B,MAAM,MAAO,CACT,MAAMC,EAAQF,EAAS,EACvB,OAAO,QAAQ,QAAQ,CAAE,MAAAE,EAAO,CACpC,EACA,MAAM,KAAKA,EAAO,CAAE,MAAAC,EAAO,SAAAC,EAAU,QAAAC,GAAW,CAC5C,MAAMC,EAAeN,EAAS,EACxB,OAAAG,EAAA,QAASI,GAAS,CACpBD,EAAa,KAAKC,CAAI,CAAA,CACzB,EACQH,EAAA,QAASG,GAAS,CACjB,MAAAC,EAAQF,EAAa,UAAU,CAAC,CAAE,GAAAG,KAASA,IAAOF,EAAK,EAAE,EAAA,qCAE/D,GAAIC,IAAU,GACV,MAAM,IAAI,MAAM,gBAAgBD,EAAK,EAAE,YAAY,EACvDD,EAAaE,CAAK,EAAID,CAAA,CACzB,EACOF,EAAA,QAASE,GAAS,CAChB,MAAAC,EAAQF,EAAa,UAAU,CAAC,CAAE,GAAAG,KAASA,IAAOF,EAAK,EAAE,EAAA,qCAE/D,GAAIC,IAAU,GACV,MAAM,IAAI,MAAM,gBAAgBD,EAAK,EAAE,YAAY,EAC1CD,EAAA,OAAOE,EAAO,CAAC,CAAA,CAC/B,EACD,aAAa,QAAQT,EAAcF,EAAUS,CAAY,CAAC,EACnD,QAAQ,QAAQ,CAC3B,EACA,MAAM,UAAW,CACb,OAAO,QAAQ,QAAQ,CAAA,CAC3B,CACH,CACL"}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@signaldb/localstorage",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "rimraf dist && vite build"
|
|
7
|
+
},
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/maxnowack/signaldb.git"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://signaldb.js.org",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"local-storage",
|
|
15
|
+
"client-database",
|
|
16
|
+
"client",
|
|
17
|
+
"database",
|
|
18
|
+
"local-database",
|
|
19
|
+
"offline-first",
|
|
20
|
+
"optimistic-ui",
|
|
21
|
+
"plugin",
|
|
22
|
+
"reactive",
|
|
23
|
+
"reactivity",
|
|
24
|
+
"solid",
|
|
25
|
+
"synchronization",
|
|
26
|
+
"typescript"
|
|
27
|
+
],
|
|
28
|
+
"author": "Max Nowack <max.nowack@gmail.com>",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"sideEffects": false,
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"import": "./dist/index.mjs",
|
|
35
|
+
"require": "./dist/index.umd.js",
|
|
36
|
+
"default": "./dist/index.umd.js"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"main": "./dist/index.umd.js",
|
|
40
|
+
"module": "./dist/index.mjs",
|
|
41
|
+
"types": "./dist/index.d.ts",
|
|
42
|
+
"typesVersions": {
|
|
43
|
+
"*": {
|
|
44
|
+
"*": [
|
|
45
|
+
"./dist/*",
|
|
46
|
+
"./dist/index.d.ts"
|
|
47
|
+
]
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"files": [
|
|
51
|
+
"dist"
|
|
52
|
+
],
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"@signaldb/core": "1.x"
|
|
55
|
+
}
|
|
56
|
+
}
|