@storion/storion 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/LICENSE +21 -0
- package/README.md +297 -0
- package/dist/Database.js +471 -0
- package/dist/changeListener.js +73 -0
- package/dist/index.d.ts +142 -0
- package/dist/index.js +26 -0
- package/dist/queryEngine.js +299 -0
- package/dist/schema.js +121 -0
- package/dist/storage/adapters.js +197 -0
- package/docs/API.md +216 -0
- package/docs/CONFIG_FORMAT.md +117 -0
- package/docs/QUERY_LANGUAGE.md +73 -0
- package/package.json +54 -0
- package/types/index.d.ts +142 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Storion
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
# storion
|
|
2
|
+
|
|
3
|
+
Framework-agnostic client-side database for the browser. Use it with **React**, **Vue**, **Angular**, or vanilla JS. No framework-specific code—just create databases, tables, save/fetch records, and run JSON queries on **localStorage**, **sessionStorage**, or **IndexedDB**.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Framework-agnostic** – Works with any frontend (React, Vue, Angular, Svelte, etc.)
|
|
8
|
+
- **Multiple stores** – Create databases in `localStorage`, `sessionStorage`, or `indexedDB`
|
|
9
|
+
- **Tables** – Define tables with columns (int, float, boolean, string) and optional foreign keys
|
|
10
|
+
- **CRUD** – Insert, fetch, update, and delete records
|
|
11
|
+
- **Query engine** – Run JSON queries (where, orderBy, limit, offset) directly on tables
|
|
12
|
+
- **Config from file** – Create a database from a config object or load config from a URL/file
|
|
13
|
+
- **Change subscription** – Subscribe to table or row changes so multiple components stay in sync when data is updated (no polling)
|
|
14
|
+
- **Cross-context ready** – Optional broadcaster + listener helpers so an extension, background script, or another tab can stream change events to your UI
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @storion/storion
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Quick start
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import { createDatabase } from '@storion/storion';
|
|
26
|
+
|
|
27
|
+
// Create a database in localStorage (or sessionStorage / indexedDB)
|
|
28
|
+
const db = await createDatabase({
|
|
29
|
+
name: 'myapp',
|
|
30
|
+
storage: 'localStorage'
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Create a table
|
|
34
|
+
await db.createTable('users', [
|
|
35
|
+
{ name: 'id', type: 'int' },
|
|
36
|
+
{ name: 'email', type: 'string' },
|
|
37
|
+
{ name: 'name', type: 'string' },
|
|
38
|
+
{ name: 'active', type: 'boolean' }
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
// Insert rows
|
|
42
|
+
await db.insert('users', { email: 'alice@example.com', name: 'Alice', active: true });
|
|
43
|
+
await db.insert('users', { email: 'bob@example.com', name: 'Bob', active: false });
|
|
44
|
+
|
|
45
|
+
// Fetch all or with options
|
|
46
|
+
const all = await db.fetch('users');
|
|
47
|
+
const active = await db.fetch('users', { filter: { active: true }, sortBy: 'name', limit: 10 });
|
|
48
|
+
|
|
49
|
+
// Run a query (where, orderBy, limit, offset)
|
|
50
|
+
const { rows, totalCount } = await db.query('users', {
|
|
51
|
+
where: { field: 'active', op: 'eq', value: true },
|
|
52
|
+
orderBy: [{ field: 'name', direction: 'asc' }],
|
|
53
|
+
limit: 20,
|
|
54
|
+
offset: 0
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Update and delete
|
|
58
|
+
await db.update('users', 1, { name: 'Alice Smith' });
|
|
59
|
+
await db.delete('users', 2);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Create database from config
|
|
63
|
+
|
|
64
|
+
You can create a database and its tables from a **config object** (e.g. from a JSON file).
|
|
65
|
+
|
|
66
|
+
### Config in code
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
const config = {
|
|
70
|
+
tables: {
|
|
71
|
+
users: {
|
|
72
|
+
columns: [
|
|
73
|
+
{ name: 'id', type: 'int' },
|
|
74
|
+
{ name: 'email', type: 'string' },
|
|
75
|
+
{ name: 'active', type: 'boolean' }
|
|
76
|
+
]
|
|
77
|
+
},
|
|
78
|
+
posts: {
|
|
79
|
+
columns: [
|
|
80
|
+
{ name: 'id', type: 'int' },
|
|
81
|
+
{ name: 'title', type: 'string' },
|
|
82
|
+
{ name: 'user_id', type: 'int', references: { table: 'users', column: 'id' } }
|
|
83
|
+
]
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const db = await createDatabase({
|
|
89
|
+
name: 'myapp',
|
|
90
|
+
storage: 'localStorage',
|
|
91
|
+
config
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Load config from URL
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
import { createDatabase, loadConfigFromUrl } from '@storion/storion';
|
|
99
|
+
|
|
100
|
+
const config = await loadConfigFromUrl('/config/db.json');
|
|
101
|
+
const db = await createDatabase({
|
|
102
|
+
name: 'myapp',
|
|
103
|
+
storage: 'localStorage',
|
|
104
|
+
config
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Load config from file (e.g. file input)
|
|
109
|
+
|
|
110
|
+
```js
|
|
111
|
+
import { createDatabase, loadConfigFromFile } from '@storion/storion';
|
|
112
|
+
|
|
113
|
+
// <input type="file" id="configFile" accept=".json" />
|
|
114
|
+
const file = document.getElementById('configFile').files[0];
|
|
115
|
+
const config = await loadConfigFromFile(file);
|
|
116
|
+
const db = await createDatabase({
|
|
117
|
+
name: 'imported',
|
|
118
|
+
storage: 'localStorage',
|
|
119
|
+
config
|
|
120
|
+
});
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Config format and options are described in [docs/CONFIG_FORMAT.md](docs/CONFIG_FORMAT.md).
|
|
124
|
+
|
|
125
|
+
## Query language
|
|
126
|
+
|
|
127
|
+
Use `db.query(tableName, query)` with a JSON query:
|
|
128
|
+
|
|
129
|
+
- **where** – Filter: `{ field, op, value }` or `{ and: [...] }` / `{ or: [...] }`
|
|
130
|
+
- **orderBy** – Sort: `[{ field, direction: 'asc' | 'desc' }]`
|
|
131
|
+
- **limit** / **offset** – Pagination
|
|
132
|
+
|
|
133
|
+
Operators: `eq`, `ne`, `gt`, `gte`, `lt`, `lte`, `contains`, `startsWith`, `endsWith`, `in`, `notIn`, `isNull`, `isNotNull`.
|
|
134
|
+
|
|
135
|
+
Example:
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
const { rows, totalCount } = await db.query('users', {
|
|
139
|
+
where: {
|
|
140
|
+
and: [
|
|
141
|
+
{ field: 'status', op: 'eq', value: 'active' },
|
|
142
|
+
{ field: 'name', op: 'contains', value: 'smith' }
|
|
143
|
+
]
|
|
144
|
+
},
|
|
145
|
+
orderBy: [{ field: 'created_at', direction: 'desc' }],
|
|
146
|
+
limit: 10,
|
|
147
|
+
offset: 0
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Full reference: [docs/QUERY_LANGUAGE.md](docs/QUERY_LANGUAGE.md).
|
|
152
|
+
|
|
153
|
+
## Subscribing to changes
|
|
154
|
+
|
|
155
|
+
When multiple components share the same `Database` instance, they can subscribe to change events so that when one component inserts, updates, or deletes data, the others receive an event and can refresh or react—without polling.
|
|
156
|
+
|
|
157
|
+
```js
|
|
158
|
+
const db = await createDatabase({ name: 'myapp', storage: 'localStorage' });
|
|
159
|
+
|
|
160
|
+
// Subscribe to all changes in a table
|
|
161
|
+
const unsubscribe = db.subscribe('todos', (event) => {
|
|
162
|
+
console.log(event.type, event.row); // 'insert' | 'update' | 'delete' | 'tableCreated' | 'tableDeleted'
|
|
163
|
+
// Refresh your UI or state here
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// Or subscribe to all tables: db.subscribe((event) => { ... })
|
|
167
|
+
// Or subscribe to one row: db.subscribe('todos', 1, (event) => { ... })
|
|
168
|
+
|
|
169
|
+
// When done: unsubscribe();
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Every matching subscriber receives the event (multiple components can subscribe to the same table or row). If no one subscribes, the database behaves as before. Full details: [docs/API.md#dbsubscribe](docs/API.md).
|
|
173
|
+
|
|
174
|
+
## Cross-context sync (extension ↔ webapp)
|
|
175
|
+
|
|
176
|
+
Storion can also be used as the **source of truth in one context** (e.g. a Chrome extension or background script) and stream change events to another context (e.g. a webapp UI) using a broadcaster + listener pattern.
|
|
177
|
+
|
|
178
|
+
```js
|
|
179
|
+
import { createDatabase, createChangeListener } from '@storion/storion';
|
|
180
|
+
|
|
181
|
+
// 1) Producer side (e.g. extension popup/background)
|
|
182
|
+
const db = await createDatabase({ name: 'myapp', storage: 'localStorage' });
|
|
183
|
+
|
|
184
|
+
// Forward normalized StorionChangeEvent payloads to the active tab.
|
|
185
|
+
db.setChangeBroadcaster({
|
|
186
|
+
async broadcastChange(event) {
|
|
187
|
+
if (typeof chrome !== 'undefined' && chrome.tabs && chrome.tabs.query) {
|
|
188
|
+
// Chrome extension context: send to the active tab's content script
|
|
189
|
+
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
|
190
|
+
const tab = tabs && tabs[0];
|
|
191
|
+
if (!tab || !tab.id) return;
|
|
192
|
+
chrome.tabs.sendMessage(tab.id, { action: 'storionChangeEvent', event });
|
|
193
|
+
});
|
|
194
|
+
} else {
|
|
195
|
+
// Fallback: same-window postMessage (e.g. two iframes or tabs using BroadcastChannel)
|
|
196
|
+
window.postMessage({ source: 'storion-change', payload: event }, '*');
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// 2) Consumer side (e.g. webapp page or another tab)
|
|
202
|
+
const transport = {
|
|
203
|
+
onMessage(handler) {
|
|
204
|
+
function listener(ev) {
|
|
205
|
+
if (!ev.data || ev.data.source !== 'storion-change') return;
|
|
206
|
+
handler(ev.data.payload);
|
|
207
|
+
}
|
|
208
|
+
window.addEventListener('message', listener);
|
|
209
|
+
return () => window.removeEventListener('message', listener);
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const stop = createChangeListener(transport, (event) => {
|
|
214
|
+
// React to inserts/updates/deletes coming from another context
|
|
215
|
+
console.log('Cross-context change:', event.type, event.tableName, event.row);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// later, when you no longer need updates:
|
|
219
|
+
// stop();
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
The same `StorionChangeEvent` payload is delivered to **local subscribers** and to the **broadcaster**, and `createChangeListener` normalizes incoming messages on the receiving side. See [docs/API.md#createChangeListenertransport-onchange](docs/API.md) for details and Chrome-extension-specific wiring in the Storion Studio README.
|
|
223
|
+
|
|
224
|
+
## API overview
|
|
225
|
+
|
|
226
|
+
| Method | Description |
|
|
227
|
+
|--------|-------------|
|
|
228
|
+
| `createDatabase(options)` | Create or connect to a DB (name, storage, optional config). |
|
|
229
|
+
| `loadConfigFromUrl(url)` | Fetch config JSON from a URL. |
|
|
230
|
+
| `loadConfigFromFile(file)` | Read config from a File (e.g. file input). |
|
|
231
|
+
| `db.createTable(name, columns)` | Create a table. |
|
|
232
|
+
| `db.listTables()` | List table names. |
|
|
233
|
+
| `db.getTable(name)` | Get table structure and rows. |
|
|
234
|
+
| `db.insert(table, row)` | Insert a row (id auto if omitted). |
|
|
235
|
+
| `db.fetch(table, options?)` | Fetch rows (optional filter, sort, limit). |
|
|
236
|
+
| `db.query(table, query)` | Run JSON query; returns `{ rows, totalCount }`. |
|
|
237
|
+
| `db.update(table, id, data)` | Update a row by id. |
|
|
238
|
+
| `db.delete(table, id)` | Delete a row by id. |
|
|
239
|
+
| `db.deleteTable(name)` | Delete a table. |
|
|
240
|
+
| `db.exportConfig()` | Export DB as config-like object. |
|
|
241
|
+
| `db.subscribe(callback)` / `db.subscribe(table, callback)` / `db.subscribe(table, rowId, callback)` | Subscribe to change events; returns `unsubscribe()`. |
|
|
242
|
+
| `db.unsubscribe(id)` | Remove a subscription by id. |
|
|
243
|
+
| `db.setChangeBroadcaster(broadcaster)` | Optional: broadcast changes to another context (e.g. extension ↔ page). |
|
|
244
|
+
| `createChangeListener(transport, onChange)` | Listen for change events coming from another context via a custom transport. |
|
|
245
|
+
|
|
246
|
+
Full API: [docs/API.md](docs/API.md).
|
|
247
|
+
|
|
248
|
+
## Storage backends
|
|
249
|
+
|
|
250
|
+
- **localStorage** – Persists across sessions; same origin; ~5MB typical.
|
|
251
|
+
- **sessionStorage** – Cleared when the tab/window closes; same origin.
|
|
252
|
+
- **indexedDB** – Async; larger quota; good for bigger datasets.
|
|
253
|
+
|
|
254
|
+
All data for a given storage key is stored in one place (default key: `__LS_DB__`). Multiple logical databases (different `name`s) can coexist under the same key.
|
|
255
|
+
|
|
256
|
+
## Usage with React / Vue / Angular
|
|
257
|
+
|
|
258
|
+
Use the same API in any framework. Share one `Database` instance (e.g. via context, service, or singleton) so that `db.subscribe()` keeps all components in sync when data changes. Example with React:
|
|
259
|
+
|
|
260
|
+
```js
|
|
261
|
+
import { createDatabase } from '@storion/storion';
|
|
262
|
+
import { useEffect, useState } from 'react';
|
|
263
|
+
|
|
264
|
+
function UserList() {
|
|
265
|
+
const [db, setDb] = useState(null);
|
|
266
|
+
const [users, setUsers] = useState([]);
|
|
267
|
+
|
|
268
|
+
useEffect(() => {
|
|
269
|
+
let unsubscribe;
|
|
270
|
+
createDatabase({ name: 'myapp', storage: 'localStorage' }).then(async (database) => {
|
|
271
|
+
setDb(database);
|
|
272
|
+
const { rows } = await database.query('users', { limit: 50 });
|
|
273
|
+
setUsers(rows);
|
|
274
|
+
unsubscribe = database.subscribe('users', async () => {
|
|
275
|
+
const { rows: next } = await database.query('users', { limit: 50 });
|
|
276
|
+
setUsers(next);
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
return () => unsubscribe?.();
|
|
280
|
+
}, []);
|
|
281
|
+
|
|
282
|
+
if (!db) return <div>Loading...</div>;
|
|
283
|
+
return (
|
|
284
|
+
<ul>
|
|
285
|
+
{users.map((u) => (
|
|
286
|
+
<li key={u.id}>{u.name}</li>
|
|
287
|
+
))}
|
|
288
|
+
</ul>
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
No framework-specific bindings—just call the async API and set state as needed.
|
|
294
|
+
|
|
295
|
+
## License
|
|
296
|
+
|
|
297
|
+
MIT
|