@ichibase/client 0.1.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 +130 -0
- package/dist/index.cjs +1077 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +742 -0
- package/dist/index.d.ts +742 -0
- package/dist/index.js +1066 -0
- package/dist/index.js.map +1 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ichibase
|
|
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,130 @@
|
|
|
1
|
+
# @ichibase/client
|
|
2
|
+
|
|
3
|
+
The official **client-side** SDK for [ichibase](https://ichibase.com) — Postgres,
|
|
4
|
+
MongoDB, Auth, and Realtime from a single client. Built for the browser,
|
|
5
|
+
React Native, Deno, Node 22+, and Bun. **Anon key only** — depends solely on
|
|
6
|
+
global `fetch` + `WebSocket`, zero runtime dependencies.
|
|
7
|
+
|
|
8
|
+
> Building a server, edge function, or admin tool with the **service** key? Use
|
|
9
|
+
> the JSR SDKs (`@ichibase/postgrest`, `@ichibase/auth`, …) instead. This package
|
|
10
|
+
> refuses `ich_admin_` keys by design.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @ichibase/client
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Quick start
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { createClient } from '@ichibase/client';
|
|
22
|
+
|
|
23
|
+
const ichi = createClient(
|
|
24
|
+
'https://<project>.ichibase.net',
|
|
25
|
+
'ich_pub_…', // your project's publishable (anon) key — safe to ship
|
|
26
|
+
);
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Database (PostgREST)
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
// Read (role = anon until a user logs in)
|
|
33
|
+
const { data, error } = await ichi.from('posts').select('*').eq('published', true);
|
|
34
|
+
|
|
35
|
+
// Insert / update / delete
|
|
36
|
+
await ichi.from('posts').insert({ title: 'Hello' });
|
|
37
|
+
await ichi.from('posts').update({ title: 'Edited' }).eq('id', 1);
|
|
38
|
+
await ichi.from('posts').delete().eq('id', 1);
|
|
39
|
+
|
|
40
|
+
// RPC
|
|
41
|
+
const { data: total } = await ichi.rpc('count_posts', { author: 'me' });
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Auth + per-user access
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
await ichi.auth.signup({ email, password });
|
|
48
|
+
await ichi.auth.login({ email, password });
|
|
49
|
+
|
|
50
|
+
// After login, data calls automatically use the user's access token, so your
|
|
51
|
+
// RLS policies and realtime rules see `auth.uid()` etc.
|
|
52
|
+
await ichi.from('posts').insert({ title: 'mine' });
|
|
53
|
+
|
|
54
|
+
const user = await ichi.auth.getUser();
|
|
55
|
+
await ichi.auth.logout();
|
|
56
|
+
|
|
57
|
+
// React to session changes (e.g. update UI)
|
|
58
|
+
ichi.onAuthStateChange((event, session) => {
|
|
59
|
+
console.log(event, session?.user?.email);
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
#### Persisting the session
|
|
64
|
+
|
|
65
|
+
The session lives in memory by default. Pass a storage adapter to keep users
|
|
66
|
+
logged in across reloads:
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
// Browser
|
|
70
|
+
const ichi = createClient(url, anonKey, { storage: window.localStorage });
|
|
71
|
+
|
|
72
|
+
// React Native (expo-secure-store / AsyncStorage). Async adapters need an
|
|
73
|
+
// explicit hydrate at startup:
|
|
74
|
+
const ichi = createClient(url, anonKey, { storage: SecureStoreAdapter });
|
|
75
|
+
await ichi.auth.loadSession();
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Storage
|
|
79
|
+
|
|
80
|
+
Storage is **not** on the client. Read/upload tokens are minted server-side by
|
|
81
|
+
the project owner (an Edge Function using the service key) and handed to your
|
|
82
|
+
app. Public files are read directly from
|
|
83
|
+
`https://cdn.ichibase.net/<project>/public/<path>`. See the
|
|
84
|
+
[Storage docs](https://ichibase.com/docs/storage).
|
|
85
|
+
|
|
86
|
+
### Mongo
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
await ichi.mongo.collection('orders').insertOne({ total: 42 });
|
|
90
|
+
const docs = await ichi.mongo.collection('orders').find({ total: { $gt: 10 } });
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Realtime
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
// Postgres row changes
|
|
97
|
+
const sub = ichi.realtime.subscribe(
|
|
98
|
+
{ kind: 'postgres', table: 'messages', events: ['INSERT'] },
|
|
99
|
+
(msg) => console.log(msg.event, msg.record),
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
// Broadcast + presence
|
|
103
|
+
const room = ichi.realtime.subscribe(
|
|
104
|
+
{ kind: 'broadcast', channel: 'room:42', presence: true },
|
|
105
|
+
(msg) => console.log(msg),
|
|
106
|
+
);
|
|
107
|
+
room.send('chat', { text: 'hi' });
|
|
108
|
+
room.track({ typing: true });
|
|
109
|
+
|
|
110
|
+
sub.unsubscribe();
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Web vs. native: CORS
|
|
114
|
+
|
|
115
|
+
Native apps (React Native, Node, servers) are unaffected by CORS. **Browser**
|
|
116
|
+
apps must be allow-listed: in your ichibase dashboard → project → Settings →
|
|
117
|
+
CORS, add your origin (e.g. `https://yourapp.com`, `http://localhost:5173`).
|
|
118
|
+
Until you do, browsers refuse cross-origin calls (default-deny).
|
|
119
|
+
|
|
120
|
+
## Security model
|
|
121
|
+
|
|
122
|
+
The anon key is **publishable** — it's meant to be shipped in clients. Access is
|
|
123
|
+
gated by your **Row-Level Security policies** (Postgres) and **collection
|
|
124
|
+
policies** (Mongo), not by hiding the key. A table with RLS disabled (or enabled
|
|
125
|
+
with no matching policy) is open to anyone holding the anon key — so enable RLS
|
|
126
|
+
on everything you expose. Never put an `ich_admin_` (service) key in a client.
|
|
127
|
+
|
|
128
|
+
## License
|
|
129
|
+
|
|
130
|
+
MIT
|