@fidscript/instant-solidjs 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.
Files changed (43) hide show
  1. package/.tshy/build.json +8 -0
  2. package/.tshy/commonjs.json +16 -0
  3. package/.tshy/esm.json +15 -0
  4. package/dist/commonjs/InstantSolidDatabase.d.ts +185 -0
  5. package/dist/commonjs/InstantSolidDatabase.d.ts.map +1 -0
  6. package/dist/commonjs/InstantSolidDatabase.js +329 -0
  7. package/dist/commonjs/InstantSolidDatabase.js.map +1 -0
  8. package/dist/commonjs/InstantSolidRoom.d.ts +97 -0
  9. package/dist/commonjs/InstantSolidRoom.d.ts.map +1 -0
  10. package/dist/commonjs/InstantSolidRoom.js +209 -0
  11. package/dist/commonjs/InstantSolidRoom.js.map +1 -0
  12. package/dist/commonjs/index.d.ts +6 -0
  13. package/dist/commonjs/index.d.ts.map +1 -0
  14. package/dist/commonjs/index.js +23 -0
  15. package/dist/commonjs/index.js.map +1 -0
  16. package/dist/commonjs/package.json +3 -0
  17. package/dist/commonjs/version.d.ts +3 -0
  18. package/dist/commonjs/version.d.ts.map +1 -0
  19. package/dist/commonjs/version.js +5 -0
  20. package/dist/commonjs/version.js.map +1 -0
  21. package/dist/esm/InstantSolidDatabase.d.ts +185 -0
  22. package/dist/esm/InstantSolidDatabase.d.ts.map +1 -0
  23. package/dist/esm/InstantSolidDatabase.js +321 -0
  24. package/dist/esm/InstantSolidDatabase.js.map +1 -0
  25. package/dist/esm/InstantSolidRoom.d.ts +97 -0
  26. package/dist/esm/InstantSolidRoom.d.ts.map +1 -0
  27. package/dist/esm/InstantSolidRoom.js +200 -0
  28. package/dist/esm/InstantSolidRoom.js.map +1 -0
  29. package/dist/esm/index.d.ts +6 -0
  30. package/dist/esm/index.d.ts.map +1 -0
  31. package/dist/esm/index.js +18 -0
  32. package/dist/esm/index.js.map +1 -0
  33. package/dist/esm/package.json +3 -0
  34. package/dist/esm/version.d.ts +3 -0
  35. package/dist/esm/version.d.ts.map +1 -0
  36. package/dist/esm/version.js +3 -0
  37. package/dist/esm/version.js.map +1 -0
  38. package/package.json +47 -0
  39. package/src/InstantSolidDatabase.ts +463 -0
  40. package/src/InstantSolidRoom.ts +330 -0
  41. package/src/index.ts +204 -0
  42. package/src/version.ts +2 -0
  43. package/tsconfig.json +45 -0
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "../src",
5
+ "module": "nodenext",
6
+ "moduleResolution": "nodenext"
7
+ }
8
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "extends": "./build.json",
3
+ "include": [
4
+ "../src/**/*.ts",
5
+ "../src/**/*.cts",
6
+ "../src/**/*.tsx",
7
+ "../src/**/*.json"
8
+ ],
9
+ "exclude": [
10
+ "../src/**/*.mts",
11
+ "../src/package.json"
12
+ ],
13
+ "compilerOptions": {
14
+ "outDir": "../.tshy-build/commonjs"
15
+ }
16
+ }
package/.tshy/esm.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "./build.json",
3
+ "include": [
4
+ "../src/**/*.ts",
5
+ "../src/**/*.mts",
6
+ "../src/**/*.tsx",
7
+ "../src/**/*.json"
8
+ ],
9
+ "exclude": [
10
+ "../src/package.json"
11
+ ],
12
+ "compilerOptions": {
13
+ "outDir": "../.tshy-build/esm"
14
+ }
15
+ }
@@ -0,0 +1,185 @@
1
+ import { type AuthState, type User, type ConnectionStatus, type TransactionChunk, type RoomSchemaShape, type InstaQLOptions, type InstantConfig, type PageInfoResponse, type InstaQLLifecycleState, type InstaQLResponse, type ValidQuery, Auth, Storage, Streams, InstantCoreDatabase, InstantSchemaDef, RoomsOf, IInstantDatabase } from '@fidscript/instant-sdk';
2
+ import type { Accessor } from 'solid-js';
3
+ import { InstantSolidRoom } from './InstantSolidRoom.js';
4
+ export type InfiniteQueryState<Schema extends InstantSchemaDef<any, any, any>, Q extends ValidQuery<Q, Schema>, UseDates extends boolean> = {
5
+ isLoading: boolean;
6
+ error: {
7
+ message: string;
8
+ } | undefined;
9
+ data: InstaQLResponse<Schema, Q, UseDates> | undefined;
10
+ canLoadNextPage: boolean;
11
+ loadNextPage: () => void;
12
+ };
13
+ export declare class InstantSolidDatabase<Schema extends InstantSchemaDef<any, any, any>, UseDates extends boolean = false, Rooms extends RoomSchemaShape = RoomsOf<Schema>> implements IInstantDatabase<Schema> {
14
+ tx: import("../node_modules/@fidscript/instant-sdk/dist/commonjs/index.js", { with: { "resolution-mode": "import" } }).TxChunk<Schema>;
15
+ auth: Auth;
16
+ storage: Storage;
17
+ streams: Streams;
18
+ core: InstantCoreDatabase<Schema, UseDates>;
19
+ constructor(core: InstantCoreDatabase<Schema, UseDates>);
20
+ /**
21
+ * Returns a unique ID for a given `name`. It's stored in local storage,
22
+ * so you will get the same ID across sessions.
23
+ *
24
+ * @example
25
+ * const deviceId = await db.getLocalId('device');
26
+ */
27
+ getLocalId: (name: string) => Promise<string>;
28
+ /**
29
+ * Use this to write data! You can create, update, delete, and link objects
30
+ *
31
+ * @see https://instantdb.com/docs/instaml
32
+ *
33
+ * @example
34
+ * const goalId = id();
35
+ * db.transact(db.tx.goals[goalId].update({title: "Get fit"}))
36
+ */
37
+ transact: (chunks: TransactionChunk<any, any> | TransactionChunk<any, any>[]) => Promise<import("../node_modules/@fidscript/instant-sdk/dist/commonjs/index.js", { with: { "resolution-mode": "import" } }).TransactionResult>;
38
+ /**
39
+ * One time query for the logged in state.
40
+ *
41
+ * @see https://instantdb.com/docs/auth
42
+ * @example
43
+ * const user = await db.getAuth();
44
+ * console.log('logged in as', user.email)
45
+ */
46
+ getAuth(): Promise<User | null>;
47
+ /**
48
+ * Use this for one-off queries.
49
+ * Returns local data if available, otherwise fetches from the server.
50
+ *
51
+ * @see https://instantdb.com/docs/instaql
52
+ *
53
+ * @example
54
+ * const resp = await db.queryOnce({ goals: {} });
55
+ * console.log(resp.data.goals)
56
+ */
57
+ queryOnce: <Q extends ValidQuery<Q, Schema>>(query: Q, opts?: InstaQLOptions) => Promise<{
58
+ data: InstaQLResponse<Schema, Q, UseDates>;
59
+ pageInfo: PageInfoResponse<Q>;
60
+ }>;
61
+ /**
62
+ * Use this to query your data!
63
+ *
64
+ * @see https://instantdb.com/docs/instaql
65
+ *
66
+ * @example
67
+ * const state = db.useQuery({ goals: {} });
68
+ * // state().isLoading, state().error, state().data
69
+ */
70
+ useQuery: <Q extends ValidQuery<Q, Schema>>(query: (() => null | Q) | null | Q, opts?: InstaQLOptions) => Accessor<InstaQLLifecycleState<Schema, Q, UseDates>>;
71
+ /**
72
+ * Subscribe to a query and incrementally load more items.
73
+ *
74
+ * Only one top level namespace in the query is allowed.
75
+ *
76
+ * @see https://instantdb.com/docs/instaql
77
+ *
78
+ * @example
79
+ * const state = db.useInfiniteQuery({
80
+ * posts: {
81
+ * $: {
82
+ * limit: 20,
83
+ * order: { createdAt: 'desc' },
84
+ * },
85
+ * },
86
+ * });
87
+ * // state().data, state().isLoading, state().error,
88
+ * // state().canLoadNextPage, state().loadNextPage()
89
+ */
90
+ useInfiniteQuery: <Q extends ValidQuery<Q, Schema>>(query: (() => null | Q) | null | Q, opts?: InstaQLOptions) => Accessor<InfiniteQueryState<Schema, Q, UseDates>>;
91
+ /**
92
+ * Listen for the logged in state. This is useful
93
+ * for deciding when to show a login screen.
94
+ *
95
+ * @see https://instantdb.com/docs/auth
96
+ * @example
97
+ * function App() {
98
+ * const auth = db.useAuth();
99
+ * // auth().isLoading, auth().user, auth().error
100
+ * }
101
+ */
102
+ useAuth: () => Accessor<AuthState>;
103
+ /**
104
+ * Subscribe to the currently logged in user.
105
+ * If the user is not logged in, this will throw an Error.
106
+ *
107
+ * @see https://instantdb.com/docs/auth
108
+ * @example
109
+ * function UserDisplay() {
110
+ * const user = db.useUser();
111
+ * return <div>Logged in as: {user().email}</div>
112
+ * }
113
+ */
114
+ useUser: () => Accessor<User>;
115
+ /**
116
+ * Listen for connection status changes to Instant.
117
+ *
118
+ * @see https://www.instantdb.com/docs/patterns#connection-status
119
+ * @example
120
+ * function App() {
121
+ * const status = db.useConnectionStatus();
122
+ * return <div>Connection state: {status()}</div>
123
+ * }
124
+ */
125
+ useConnectionStatus: () => Accessor<ConnectionStatus>;
126
+ /**
127
+ * A hook that returns a unique ID for a given `name`. localIds are
128
+ * stored in local storage, so you will get the same ID across sessions.
129
+ *
130
+ * Initially returns `null`, and then loads the localId.
131
+ *
132
+ * @example
133
+ * const deviceId = db.useLocalId('device');
134
+ * // deviceId() is null initially, then the ID string
135
+ */
136
+ useLocalId: (name: string) => Accessor<string | null>;
137
+ /**
138
+ * Obtain a handle to a room, which allows you to listen to topics and presence data
139
+ *
140
+ * @see https://instantdb.com/docs/presence-and-topics
141
+ *
142
+ * @example
143
+ * const room = db.room('chat', roomId);
144
+ * const presence = db.rooms.usePresence(room);
145
+ */
146
+ room<RoomType extends keyof Rooms>(type?: RoomType, id?: string): InstantSolidRoom<Schema, Rooms, RoomType>;
147
+ /**
148
+ * Hooks for working with rooms
149
+ *
150
+ * @see https://instantdb.com/docs/presence-and-topics
151
+ *
152
+ * @example
153
+ * const room = db.room('chat', roomId);
154
+ * const presence = db.rooms.usePresence(room);
155
+ * const publish = db.rooms.usePublishTopic(room, 'emoji');
156
+ */
157
+ rooms: {
158
+ useTopicEffect: typeof import("./InstantSolidRoom.js").useTopicEffect;
159
+ usePublishTopic: typeof import("./InstantSolidRoom.js").usePublishTopic;
160
+ usePresence: typeof import("./InstantSolidRoom.js").usePresence;
161
+ useSyncPresence: typeof import("./InstantSolidRoom.js").useSyncPresence;
162
+ useTypingIndicator: typeof import("./InstantSolidRoom.js").useTypingIndicator;
163
+ };
164
+ }
165
+ /**
166
+ * The first step: init your application!
167
+ *
168
+ * Visit https://instantdb.com/dash to get your `appId` :)
169
+ *
170
+ * @example
171
+ * import { init } from "@instantdb/solidjs"
172
+ *
173
+ * const db = init({ appId: "my-app-id" })
174
+ *
175
+ * // You can also provide a schema for type safety and editor autocomplete!
176
+ *
177
+ * import { init } from "@instantdb/solidjs"
178
+ * import schema from "../instant.schema.ts";
179
+ *
180
+ * const db = init({ appId: "my-app-id", schema })
181
+ */
182
+ export declare function init<Schema extends InstantSchemaDef<any, any, any>, UseDates extends boolean = false>(config: Omit<InstantConfig<Schema, UseDates>, 'useDateObjects'> & {
183
+ useDateObjects?: UseDates;
184
+ }): InstantSolidDatabase<Schema, UseDates>;
185
+ //# sourceMappingURL=InstantSolidDatabase.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"InstantSolidDatabase.d.ts","sourceRoot":"","sources":["../../src/InstantSolidDatabase.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,SAAS,EACd,KAAK,IAAI,EACT,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,qBAAqB,EAC1B,KAAK,eAAe,EAEpB,KAAK,UAAU,EAEf,IAAI,EACJ,OAAO,EACP,OAAO,EAEP,mBAAmB,EAInB,gBAAgB,EAChB,OAAO,EAEP,gBAAgB,EACjB,MAAM,wBAAwB,CAAC;AAGhC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEzC,OAAO,EAAE,gBAAgB,EAAS,MAAM,uBAAuB,CAAC;AAUhE,MAAM,MAAM,kBAAkB,CAC5B,MAAM,SAAS,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAC9C,CAAC,SAAS,UAAU,CAAC,CAAC,EAAE,MAAM,CAAC,EAC/B,QAAQ,SAAS,OAAO,IACtB;IACF,SAAS,EAAE,OAAO,CAAC;IACnB,KAAK,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,CAAC;IACvC,IAAI,EAAE,eAAe,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ,CAAC,GAAG,SAAS,CAAC;IACvD,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EAAE,QAAQ,CAAC,CAKxC"}
@@ -0,0 +1,329 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.InstantSolidDatabase = void 0;
7
+ exports.init = init;
8
+ const instant_sdk_1 = require("@fidscript/instant-sdk");
9
+ const solid_js_1 = require("solid-js");
10
+ const InstantSolidRoom_js_1 = require("./InstantSolidRoom.js");
11
+ const version_js_1 = __importDefault(require("./version.js"));
12
+ const defaultState = {
13
+ isLoading: true,
14
+ data: undefined,
15
+ pageInfo: undefined,
16
+ error: undefined,
17
+ };
18
+ const defaultAuthState = {
19
+ isLoading: true,
20
+ user: undefined,
21
+ error: undefined,
22
+ };
23
+ function stateForResult(result) {
24
+ return {
25
+ isLoading: !Boolean(result),
26
+ data: undefined,
27
+ pageInfo: undefined,
28
+ error: undefined,
29
+ ...(result ? result : {}),
30
+ };
31
+ }
32
+ class InstantSolidDatabase {
33
+ tx = (0, instant_sdk_1.txInit)();
34
+ auth;
35
+ storage;
36
+ streams;
37
+ core;
38
+ constructor(core) {
39
+ this.core = core;
40
+ this.auth = this.core.auth;
41
+ this.storage = this.core.storage;
42
+ this.streams = this.core.streams;
43
+ }
44
+ /**
45
+ * Returns a unique ID for a given `name`. It's stored in local storage,
46
+ * so you will get the same ID across sessions.
47
+ *
48
+ * @example
49
+ * const deviceId = await db.getLocalId('device');
50
+ */
51
+ getLocalId = (name) => {
52
+ return this.core.getLocalId(name);
53
+ };
54
+ /**
55
+ * Use this to write data! You can create, update, delete, and link objects
56
+ *
57
+ * @see https://instantdb.com/docs/instaml
58
+ *
59
+ * @example
60
+ * const goalId = id();
61
+ * db.transact(db.tx.goals[goalId].update({title: "Get fit"}))
62
+ */
63
+ transact = (chunks) => {
64
+ return this.core.transact(chunks);
65
+ };
66
+ /**
67
+ * One time query for the logged in state.
68
+ *
69
+ * @see https://instantdb.com/docs/auth
70
+ * @example
71
+ * const user = await db.getAuth();
72
+ * console.log('logged in as', user.email)
73
+ */
74
+ getAuth() {
75
+ return this.core.getAuth();
76
+ }
77
+ /**
78
+ * Use this for one-off queries.
79
+ * Returns local data if available, otherwise fetches from the server.
80
+ *
81
+ * @see https://instantdb.com/docs/instaql
82
+ *
83
+ * @example
84
+ * const resp = await db.queryOnce({ goals: {} });
85
+ * console.log(resp.data.goals)
86
+ */
87
+ queryOnce = (query, opts) => {
88
+ return this.core.queryOnce(query, opts);
89
+ };
90
+ // -----------
91
+ // Solid reactive hooks
92
+ /**
93
+ * Use this to query your data!
94
+ *
95
+ * @see https://instantdb.com/docs/instaql
96
+ *
97
+ * @example
98
+ * const state = db.useQuery({ goals: {} });
99
+ * // state().isLoading, state().error, state().data
100
+ */
101
+ useQuery = (query, opts) => {
102
+ const [state, setState] = (0, solid_js_1.createSignal)(defaultState);
103
+ (0, solid_js_1.createEffect)(() => {
104
+ const resolvedQuery = typeof query === 'function' ? query() : query;
105
+ if (!resolvedQuery) {
106
+ setState(() => defaultState);
107
+ return;
108
+ }
109
+ let q = resolvedQuery;
110
+ if (opts && 'ruleParams' in opts) {
111
+ q = { $$ruleParams: opts['ruleParams'], ...q };
112
+ }
113
+ const coerced = (0, instant_sdk_1.coerceQuery)(q);
114
+ const prev = this.core._reactor.getPreviousResult(coerced);
115
+ if (prev) {
116
+ setState(() => stateForResult(prev));
117
+ }
118
+ const unsub = this.core.subscribeQuery(coerced, (result) => {
119
+ setState(() => Object.assign({
120
+ isLoading: false,
121
+ data: undefined,
122
+ pageInfo: undefined,
123
+ error: undefined,
124
+ }, result));
125
+ });
126
+ (0, solid_js_1.onCleanup)(unsub);
127
+ });
128
+ return state;
129
+ };
130
+ /**
131
+ * Subscribe to a query and incrementally load more items.
132
+ *
133
+ * Only one top level namespace in the query is allowed.
134
+ *
135
+ * @see https://instantdb.com/docs/instaql
136
+ *
137
+ * @example
138
+ * const state = db.useInfiniteQuery({
139
+ * posts: {
140
+ * $: {
141
+ * limit: 20,
142
+ * order: { createdAt: 'desc' },
143
+ * },
144
+ * },
145
+ * });
146
+ * // state().data, state().isLoading, state().error,
147
+ * // state().canLoadNextPage, state().loadNextPage()
148
+ */
149
+ useInfiniteQuery = (query, opts) => {
150
+ let sub = null;
151
+ const loadNextPage = () => sub?.loadNextPage();
152
+ const [state, setState] = (0, solid_js_1.createSignal)({
153
+ isLoading: true,
154
+ error: undefined,
155
+ data: undefined,
156
+ canLoadNextPage: false,
157
+ loadNextPage,
158
+ });
159
+ (0, solid_js_1.createEffect)(() => {
160
+ const resolvedQuery = typeof query === 'function' ? query() : query;
161
+ if (!resolvedQuery) {
162
+ sub = null;
163
+ setState(() => ({
164
+ isLoading: true,
165
+ error: undefined,
166
+ data: undefined,
167
+ canLoadNextPage: false,
168
+ loadNextPage,
169
+ }));
170
+ return;
171
+ }
172
+ const snapshot = (0, instant_sdk_1.getInfiniteQueryInitialSnapshot)(this.core, resolvedQuery, opts);
173
+ setState(() => ({
174
+ ...snapshot,
175
+ isLoading: !snapshot.data && !snapshot.error,
176
+ loadNextPage,
177
+ }));
178
+ sub = this.core.subscribeInfiniteQuery(resolvedQuery, (resp) => {
179
+ setState(() => ({
180
+ ...resp,
181
+ isLoading: false,
182
+ loadNextPage,
183
+ }));
184
+ }, opts);
185
+ (0, solid_js_1.onCleanup)(() => {
186
+ sub?.unsubscribe();
187
+ sub = null;
188
+ });
189
+ });
190
+ return state;
191
+ };
192
+ /**
193
+ * Listen for the logged in state. This is useful
194
+ * for deciding when to show a login screen.
195
+ *
196
+ * @see https://instantdb.com/docs/auth
197
+ * @example
198
+ * function App() {
199
+ * const auth = db.useAuth();
200
+ * // auth().isLoading, auth().user, auth().error
201
+ * }
202
+ */
203
+ useAuth = () => {
204
+ const [state, setState] = (0, solid_js_1.createSignal)(this.core._reactor._currentUserCached ?? defaultAuthState);
205
+ (0, solid_js_1.createEffect)(() => {
206
+ const unsub = this.core.subscribeAuth((auth) => {
207
+ setState({ isLoading: false, ...auth });
208
+ });
209
+ (0, solid_js_1.onCleanup)(unsub);
210
+ });
211
+ return state;
212
+ };
213
+ /**
214
+ * Subscribe to the currently logged in user.
215
+ * If the user is not logged in, this will throw an Error.
216
+ *
217
+ * @see https://instantdb.com/docs/auth
218
+ * @example
219
+ * function UserDisplay() {
220
+ * const user = db.useUser();
221
+ * return <div>Logged in as: {user().email}</div>
222
+ * }
223
+ */
224
+ useUser = () => {
225
+ const auth = this.useAuth();
226
+ return (0, solid_js_1.createMemo)(() => {
227
+ const { user } = auth();
228
+ if (!user) {
229
+ throw new instant_sdk_1.InstantError('useUser must be used within an auth-protected route');
230
+ }
231
+ return user;
232
+ });
233
+ };
234
+ /**
235
+ * Listen for connection status changes to Instant.
236
+ *
237
+ * @see https://www.instantdb.com/docs/patterns#connection-status
238
+ * @example
239
+ * function App() {
240
+ * const status = db.useConnectionStatus();
241
+ * return <div>Connection state: {status()}</div>
242
+ * }
243
+ */
244
+ useConnectionStatus = () => {
245
+ const [status, setStatus] = (0, solid_js_1.createSignal)(this.core._reactor.status);
246
+ (0, solid_js_1.createEffect)(() => {
247
+ const unsub = this.core.subscribeConnectionStatus((newStatus) => {
248
+ setStatus(() => newStatus);
249
+ });
250
+ (0, solid_js_1.onCleanup)(unsub);
251
+ });
252
+ return status;
253
+ };
254
+ /**
255
+ * A hook that returns a unique ID for a given `name`. localIds are
256
+ * stored in local storage, so you will get the same ID across sessions.
257
+ *
258
+ * Initially returns `null`, and then loads the localId.
259
+ *
260
+ * @example
261
+ * const deviceId = db.useLocalId('device');
262
+ * // deviceId() is null initially, then the ID string
263
+ */
264
+ useLocalId = (name) => {
265
+ const [localId, setLocalId] = (0, solid_js_1.createSignal)(null);
266
+ (0, solid_js_1.createEffect)(() => {
267
+ let mounted = true;
268
+ this.getLocalId(name).then((id) => {
269
+ if (mounted) {
270
+ setLocalId(() => id);
271
+ }
272
+ });
273
+ (0, solid_js_1.onCleanup)(() => {
274
+ mounted = false;
275
+ });
276
+ });
277
+ return localId;
278
+ };
279
+ /**
280
+ * Obtain a handle to a room, which allows you to listen to topics and presence data
281
+ *
282
+ * @see https://instantdb.com/docs/presence-and-topics
283
+ *
284
+ * @example
285
+ * const room = db.room('chat', roomId);
286
+ * const presence = db.rooms.usePresence(room);
287
+ */
288
+ room(type = '_defaultRoomType', id = '_defaultRoomId') {
289
+ return new InstantSolidRoom_js_1.InstantSolidRoom(this.core, type, id);
290
+ }
291
+ /**
292
+ * Hooks for working with rooms
293
+ *
294
+ * @see https://instantdb.com/docs/presence-and-topics
295
+ *
296
+ * @example
297
+ * const room = db.room('chat', roomId);
298
+ * const presence = db.rooms.usePresence(room);
299
+ * const publish = db.rooms.usePublishTopic(room, 'emoji');
300
+ */
301
+ rooms = InstantSolidRoom_js_1.rooms;
302
+ }
303
+ exports.InstantSolidDatabase = InstantSolidDatabase;
304
+ // -----------
305
+ // init
306
+ /**
307
+ * The first step: init your application!
308
+ *
309
+ * Visit https://instantdb.com/dash to get your `appId` :)
310
+ *
311
+ * @example
312
+ * import { init } from "@instantdb/solidjs"
313
+ *
314
+ * const db = init({ appId: "my-app-id" })
315
+ *
316
+ * // You can also provide a schema for type safety and editor autocomplete!
317
+ *
318
+ * import { init } from "@instantdb/solidjs"
319
+ * import schema from "../instant.schema.ts";
320
+ *
321
+ * const db = init({ appId: "my-app-id", schema })
322
+ */
323
+ function init(config) {
324
+ const coreDb = (0, instant_sdk_1.init)(config, undefined, undefined, {
325
+ '@instantdb/solidjs': version_js_1.default,
326
+ });
327
+ return new InstantSolidDatabase(coreDb);
328
+ }
329
+ //# sourceMappingURL=InstantSolidDatabase.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"InstantSolidDatabase.js","sourceRoot":"","sources":["../../src/InstantSolidDatabase.ts"],"names":[],"mappings":";;;;;;AAkcA,oBAYC;AA9cD,wDA2BgC;AAEhC,uCAA6E;AAG7E,+DAAgE;AAChE,8DAAmC;AAEnC,MAAM,YAAY,GAAG;IACnB,S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AE;gBACb,OAAO,GAAG,KAAK,CAAC;YAClB,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,OAAO,OAAO,CAAC;IACjB,CAAC,CAAC;IAEF;;;;;;;;OAQG;IACH,IAAI,CACF,OAAiB,kBAA8B,EAC/C,KAAa,gBAAgB;QAE7B,OAAO,IAAI,sCAAgB,CAA0B,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;IAC5E,CAAC;IAED;;;;;;;;;OASG;IACH,KAAK,GAAG,2BAAK,CAAC;CACf;AAtWD,oDAsWC;AAED,cAAc;AACd,OAAO;AAEP;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,IAAI,CAIlB,MAEC;IAED,MAAM,MAAM,GAAG,IAAA,kBAAS,EAAmB,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE;QACvE,oBAAoB,EAAE,oBAAO;KAC9B,CAAC,CAAC;IACH,OAAO,IAAI,oBAAoB,CAAmB,MAAM,CAAC,CAAC;AAC5D,CAAC","sourcesContent":["import {\n // types\n type AuthState,\n type User,\n type ConnectionStatus,\n type TransactionChunk,\n type RoomSchemaShape,\n type InstaQLOptions,\n type InstantConfig,\n type PageInfoResponse,\n type InstaQLLifecycleState,\n type InstaQLResponse,\n type InfiniteQuerySubscription,\n type ValidQuery,\n // classes\n Auth,\n Storage,\n Streams,\n txInit,\n InstantCoreDatabase,\n init as core_init,\n coerceQuery,\n getInfiniteQueryInitialSnapshot,\n InstantSchemaDef,\n RoomsOf,\n InstantError,\n IInstantDatabase,\n} from '@fidscript/instant-sdk';\n\nimport { createSignal, createEffect, onCleanup, createMemo } from 'solid-js';\nimport type { Accessor } from 'solid-js';\n\nimport { InstantSolidRoom, rooms } from './InstantSolidRoom.js';\nimport version from './version.js';\n\nconst defaultState = {\n isLoading: true,\n data: undefined,\n pageInfo: undefined,\n error: undefined,\n} as const;\n\nexport type InfiniteQueryState<\n Schema extends InstantSchemaDef<any, any, any>,\n Q extends ValidQuery<Q, Schema>,\n UseDates extends boolean,\n> = {\n isLoading: boolean;\n error: { message: string } | undefined;\n data: InstaQLResponse<Schema, Q, UseDates> | undefined;\n canLoadNextPage: boolean;\n loadNextPage: () => void;\n};\n\nconst defaultAuthState: AuthState = {\n isLoading: true,\n user: undefined,\n error: undefined,\n};\n\nfunction stateForResult(result: any) {\n return {\n isLoading: !Boolean(result),\n data: undefined,\n pageInfo: undefined,\n error: undefined,\n ...(result ? result : {}),\n };\n}\n\nexport class InstantSolidDatabase<\n Schema extends InstantSchemaDef<any, any, any>,\n UseDates extends boolean = false,\n Rooms extends RoomSchemaShape = RoomsOf<Schema>,\n> implements IInstantDatabase<Schema>\n{\n public tx = txInit<Schema>();\n\n public auth: Auth;\n public storage: Storage;\n public streams: Streams;\n public core: InstantCoreDatabase<Schema, UseDates>;\n\n constructor(core: InstantCoreDatabase<Schema, UseDates>) {\n this.core = core;\n this.auth = this.core.auth;\n this.storage = this.core.storage;\n this.streams = this.core.streams;\n }\n\n /**\n * Returns a unique ID for a given `name`. It's stored in local storage,\n * so you will get the same ID across sessions.\n *\n * @example\n * const deviceId = await db.getLocalId('device');\n */\n getLocalId = (name: string): Promise<string> => {\n return this.core.getLocalId(name);\n };\n\n /**\n * Use this to write data! You can create, update, delete, and link objects\n *\n * @see https://instantdb.com/docs/instaml\n *\n * @example\n * const goalId = id();\n * db.transact(db.tx.goals[goalId].update({title: \"Get fit\"}))\n */\n transact = (\n chunks: TransactionChunk<any, any> | TransactionChunk<any, any>[],\n ) => {\n return this.core.transact(chunks);\n };\n\n /**\n * One time query for the logged in state.\n *\n * @see https://instantdb.com/docs/auth\n * @example\n * const user = await db.getAuth();\n * console.log('logged in as', user.email)\n */\n getAuth(): Promise<User | null> {\n return this.core.getAuth();\n }\n\n /**\n * Use this for one-off queries.\n * Returns local data if available, otherwise fetches from the server.\n *\n * @see https://instantdb.com/docs/instaql\n *\n * @example\n * const resp = await db.queryOnce({ goals: {} });\n * console.log(resp.data.goals)\n */\n queryOnce = <Q extends ValidQuery<Q, Schema>>(\n query: Q,\n opts?: InstaQLOptions,\n ): Promise<{\n data: InstaQLResponse<Schema, Q, UseDates>;\n pageInfo: PageInfoResponse<Q>;\n }> => {\n return this.core.queryOnce(query, opts);\n };\n\n // -----------\n // Solid reactive hooks\n\n /**\n * Use this to query your data!\n *\n * @see https://instantdb.com/docs/instaql\n *\n * @example\n * const state = db.useQuery({ goals: {} });\n * // state().isLoading, state().error, state().data\n */\n useQuery = <Q extends ValidQuery<Q, Schema>>(\n query: (() => null | Q) | null | Q,\n opts?: InstaQLOptions,\n ): Accessor<InstaQLLifecycleState<Schema, Q, UseDates>> => {\n const [state, setState] = createSignal<\n InstaQLLifecycleState<Schema, Q, UseDates>\n >(defaultState as InstaQLLifecycleState<Schema, Q, UseDates>);\n\n createEffect(() => {\n const resolvedQuery = typeof query === 'function' ? query() : query;\n\n if (!resolvedQuery) {\n setState(\n () => defaultState as InstaQLLifecycleState<Schema, Q, UseDates>,\n );\n return;\n }\n\n let q = resolvedQuery;\n if (opts && 'ruleParams' in opts) {\n q = { $$ruleParams: (opts as any)['ruleParams'], ...q };\n }\n\n const coerced = coerceQuery(q);\n const prev = this.core._reactor.getPreviousResult(coerced);\n if (prev) {\n setState(\n () =>\n stateForResult(prev) as InstaQLLifecycleState<Schema, Q, UseDates>,\n );\n }\n\n const unsub = this.core.subscribeQuery<Q, UseDates>(coerced, (result) => {\n setState(\n () =>\n Object.assign(\n {\n isLoading: false,\n data: undefined,\n pageInfo: undefined,\n error: undefined,\n },\n result,\n ) as InstaQLLifecycleState<Schema, Q, UseDates>,\n );\n });\n\n onCleanup(unsub);\n });\n\n return state;\n };\n\n /**\n * Subscribe to a query and incrementally load more items.\n *\n * Only one top level namespace in the query is allowed.\n *\n * @see https://instantdb.com/docs/instaql\n *\n * @example\n * const state = db.useInfiniteQuery({\n * posts: {\n * $: {\n * limit: 20,\n * order: { createdAt: 'desc' },\n * },\n * },\n * });\n * // state().data, state().isLoading, state().error,\n * // state().canLoadNextPage, state().loadNextPage()\n */\n useInfiniteQuery = <Q extends ValidQuery<Q, Schema>>(\n query: (() => null | Q) | null | Q,\n opts?: InstaQLOptions,\n ): Accessor<InfiniteQueryState<Schema, Q, UseDates>> => {\n let sub: InfiniteQuerySubscription | null = null;\n const loadNextPage = () => sub?.loadNextPage();\n\n const [state, setState] = createSignal<\n InfiniteQueryState<Schema, Q, UseDates>\n >({\n isLoading: true,\n error: undefined,\n data: undefined,\n canLoadNextPage: false,\n loadNextPage,\n });\n\n createEffect(() => {\n const resolvedQuery = typeof query === 'function' ? query() : query;\n\n if (!resolvedQuery) {\n sub = null;\n setState(() => ({\n isLoading: true,\n error: undefined,\n data: undefined,\n canLoadNextPage: false,\n loadNextPage,\n }));\n return;\n }\n\n const snapshot = getInfiniteQueryInitialSnapshot<Schema, Q, UseDates>(\n this.core,\n resolvedQuery,\n opts,\n );\n setState(() => ({\n ...snapshot,\n isLoading: !snapshot.data && !snapshot.error,\n loadNextPage,\n }));\n\n sub = this.core.subscribeInfiniteQuery<Q>(\n resolvedQuery,\n (resp) => {\n setState(() => ({\n ...resp,\n isLoading: false,\n loadNextPage,\n }));\n },\n opts,\n );\n\n onCleanup(() => {\n sub?.unsubscribe();\n sub = null;\n });\n });\n\n return state;\n };\n\n /**\n * Listen for the logged in state. This is useful\n * for deciding when to show a login screen.\n *\n * @see https://instantdb.com/docs/auth\n * @example\n * function App() {\n * const auth = db.useAuth();\n * // auth().isLoading, auth().user, auth().error\n * }\n */\n useAuth = (): Accessor<AuthState> => {\n const [state, setState] = createSignal<AuthState>(\n this.core._reactor._currentUserCached ?? defaultAuthState,\n );\n\n createEffect(() => {\n const unsub = this.core.subscribeAuth((auth) => {\n setState({ isLoading: false, ...auth });\n });\n\n onCleanup(unsub);\n });\n\n return state;\n };\n\n /**\n * Subscribe to the currently logged in user.\n * If the user is not logged in, this will throw an Error.\n *\n * @see https://instantdb.com/docs/auth\n * @example\n * function UserDisplay() {\n * const user = db.useUser();\n * return <div>Logged in as: {user().email}</div>\n * }\n */\n useUser = (): Accessor<User> => {\n const auth = this.useAuth();\n return createMemo(() => {\n const { user } = auth();\n if (!user) {\n throw new InstantError(\n 'useUser must be used within an auth-protected route',\n );\n }\n return user;\n });\n };\n\n /**\n * Listen for connection status changes to Instant.\n *\n * @see https://www.instantdb.com/docs/patterns#connection-status\n * @example\n * function App() {\n * const status = db.useConnectionStatus();\n * return <div>Connection state: {status()}</div>\n * }\n */\n useConnectionStatus = (): Accessor<ConnectionStatus> => {\n const [status, setStatus] = createSignal<ConnectionStatus>(\n this.core._reactor.status as ConnectionStatus,\n );\n\n createEffect(() => {\n const unsub = this.core.subscribeConnectionStatus((newStatus) => {\n setStatus(() => newStatus);\n });\n\n onCleanup(unsub);\n });\n\n return status;\n };\n\n /**\n * A hook that returns a unique ID for a given `name`. localIds are\n * stored in local storage, so you will get the same ID across sessions.\n *\n * Initially returns `null`, and then loads the localId.\n *\n * @example\n * const deviceId = db.useLocalId('device');\n * // deviceId() is null initially, then the ID string\n */\n useLocalId = (name: string): Accessor<string | null> => {\n const [localId, setLocalId] = createSignal<string | null>(null);\n\n createEffect(() => {\n let mounted = true;\n this.getLocalId(name).then((id) => {\n if (mounted) {\n setLocalId(() => id);\n }\n });\n onCleanup(() => {\n mounted = false;\n });\n });\n\n return localId;\n };\n\n /**\n * Obtain a handle to a room, which allows you to listen to topics and presence data\n *\n * @see https://instantdb.com/docs/presence-and-topics\n *\n * @example\n * const room = db.room('chat', roomId);\n * const presence = db.rooms.usePresence(room);\n */\n room<RoomType extends keyof Rooms>(\n type: RoomType = '_defaultRoomType' as RoomType,\n id: string = '_defaultRoomId',\n ) {\n return new InstantSolidRoom<Schema, Rooms, RoomType>(this.core, type, id);\n }\n\n /**\n * Hooks for working with rooms\n *\n * @see https://instantdb.com/docs/presence-and-topics\n *\n * @example\n * const room = db.room('chat', roomId);\n * const presence = db.rooms.usePresence(room);\n * const publish = db.rooms.usePublishTopic(room, 'emoji');\n */\n rooms = rooms;\n}\n\n// -----------\n// init\n\n/**\n * The first step: init your application!\n *\n * Visit https://instantdb.com/dash to get your `appId` :)\n *\n * @example\n * import { init } from \"@instantdb/solidjs\"\n *\n * const db = init({ appId: \"my-app-id\" })\n *\n * // You can also provide a schema for type safety and editor autocomplete!\n *\n * import { init } from \"@instantdb/solidjs\"\n * import schema from \"../instant.schema.ts\";\n *\n * const db = init({ appId: \"my-app-id\", schema })\n */\nexport function init<\n Schema extends InstantSchemaDef<any, any, any>,\n UseDates extends boolean = false,\n>(\n config: Omit<InstantConfig<Schema, UseDates>, 'useDateObjects'> & {\n useDateObjects?: UseDates;\n },\n): InstantSolidDatabase<Schema, UseDates> {\n const coreDb = core_init<Schema, UseDates>(config, undefined, undefined, {\n '@instantdb/solidjs': version,\n });\n return new InstantSolidDatabase<Schema, UseDates>(coreDb);\n}\n"]}