Skip to content

Commit 1d06fd4

Browse files
atkgithub-actions[bot]
authored andcommitted
Format
1 parent 31e1cfc commit 1d06fd4

2 files changed

Lines changed: 39 additions & 24 deletions

File tree

packages/db-store/dev/index.tsx

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { createDbStore, supabaseAdapter, DbRow, DbStoreError } from "../src/inde
33
import { AuthResponse, createClient, Session, SupabaseClient } from "@supabase/supabase-js";
44
import { reconcile } from "solid-js/store";
55

6-
const TodoList = (props: { client: SupabaseClient, logout: () => void }) => {
6+
const TodoList = (props: { client: SupabaseClient; logout: () => void }) => {
77
const [error, setError] = createSignal<DbStoreError<DbRow>>();
88
(globalThis as any).supabaseClient = props.client;
99
const [todos, setTodos] = createDbStore({
1010
adapter: supabaseAdapter({ client: props.client, table: "todos" }),
11-
defaultFields: ['id', 'user_id'],
11+
defaultFields: ["id", "user_id"],
1212
onError: setError,
1313
});
1414
const [edit, setEdit] = createSignal<DbRow>();
@@ -79,39 +79,37 @@ const TodoList = (props: { client: SupabaseClient, logout: () => void }) => {
7979

8080
const App: Component = () => {
8181
// these are public keys that will end up in the client in any case:
82-
const client =
83-
createClient(
84-
import.meta.env.VITE_SUPABASE_URL,
85-
import.meta.env.VITE_SUPABASE_KEY
86-
);
82+
const client = createClient(import.meta.env.VITE_SUPABASE_URL, import.meta.env.VITE_SUPABASE_KEY);
8783
const [session, setSession] = createSignal<Session>();
88-
const [error, setError] = createSignal('');
84+
const [error, setError] = createSignal("");
8985
const handleAuthPromise = ({ error, data }: AuthResponse) => {
9086
if (error) {
91-
setError(error.toString())
87+
setError(error.toString());
9288
} else {
9389
setSession(data.session ?? undefined);
9490
}
9591
};
9692
onMount(() => client.auth.refreshSession().then(handleAuthPromise));
9793
const login = () => {
9894
const email = (document.querySelector('[type="email"]') as HTMLInputElement | null)?.value;
99-
const password = (document.querySelector('[type="password"]') as HTMLInputElement | null)?.value;
95+
const password = (document.querySelector('[type="password"]') as HTMLInputElement | null)
96+
?.value;
10097
if (!email || !password) {
101-
setError('please provide an email and password');
98+
setError("please provide an email and password");
10299
return;
103100
}
104101
client.auth.signInWithPassword({ email, password }).then(handleAuthPromise);
105102
};
106103
const register = () => {
107104
const email = (document.querySelector('[type="email"]') as HTMLInputElement | null)?.value;
108-
const password = (document.querySelector('[type="password"]') as HTMLInputElement | null)?.value;
105+
const password = (document.querySelector('[type="password"]') as HTMLInputElement | null)
106+
?.value;
109107
if (!email || !password) {
110-
setError('please provide an email and password');
108+
setError("please provide an email and password");
111109
return;
112110
}
113111
client.auth.signUp({ email, password }).then(handleAuthPromise);
114-
}
112+
};
115113

116114
return (
117115
<div class="box-border flex min-h-screen w-full flex-col items-center justify-center space-y-4 bg-gray-800 p-24 text-white">
@@ -121,25 +119,34 @@ const App: Component = () => {
121119
when={session()}
122120
fallback={
123121
<>
124-
<Show when={error()}><p>{error()}</p></Show>
122+
<Show when={error()}>
123+
<p>{error()}</p>
124+
</Show>
125125
<p>
126126
<label>
127-
Email <input type="email" onInput={() => setError('')} />
127+
Email <input type="email" onInput={() => setError("")} />
128128
</label>
129129
</p>
130130
<p>
131131
<label>
132132
Password <input type="password" />
133133
</label>
134134
</p>
135-
<button class="btn" onClick={login}>sign in</button>
136-
<button class="btn" onClick={register}>sign up</button>
135+
<button class="btn" onClick={login}>
136+
sign in
137+
</button>
138+
<button class="btn" onClick={register}>
139+
sign up
140+
</button>
137141
</>
138142
}
139143
>
140144
<TodoList
141145
client={client}
142-
logout={() => { setSession(undefined); client.auth.signOut(); }}
146+
logout={() => {
147+
setSession(undefined);
148+
client.auth.signOut();
149+
}}
143150
/>
144151
</Show>
145152
</div>

packages/db-store/src/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ const supabaseHandleError =
6969
)
7070
: Promise.resolve();
7171

72-
export const supabaseAdapter = <Row extends DbRow>(opts: DbAdapterOptions<Row, { schema?: string }>): DbAdapter<Row> => {
72+
export const supabaseAdapter = <Row extends DbRow>(
73+
opts: DbAdapterOptions<Row, { schema?: string }>,
74+
): DbAdapter<Row> => {
7375
const [insertSignal, setInsertSignal] = createSignal<DbAdapterUpdate<Row>>();
7476
const [updateSignal, setUpdateSignal] = createSignal<DbAdapterUpdate<Row>>();
7577
const [deleteSignal, setDeleteSignal] = createSignal<DbAdapterUpdate<Row>>();
@@ -119,7 +121,7 @@ export const supabaseAdapter = <Row extends DbRow>(opts: DbAdapterOptions<Row, {
119121
export type DbStoreOptions<Row extends DbRow> = {
120122
adapter: DbAdapter<Row>;
121123
init?: Row[];
122-
defaultFields?: readonly string[];
124+
defaultFields?: readonly string[];
123125
equals?: (a: unknown, b: unknown) => boolean;
124126
onError?: (err: DbStoreError<Row>) => void;
125127
};
@@ -131,7 +133,7 @@ export const createDbStore = <Row extends DbRow>(
131133
const [dbStore, setDbStore] = createStore<Row[]>(opts.init || []);
132134
const [dbInit, { refetch }] = createResource(opts.adapter.init);
133135
const equals = opts.equals || ((a, b) => a === b);
134-
const defaultFields = opts.defaultFields || ['id'];
136+
const defaultFields = opts.defaultFields || ["id"];
135137
const onError = (error: DbStoreError<Row>) => {
136138
if (typeof opts.onError === "function") {
137139
opts.onError(error);
@@ -152,11 +154,17 @@ export const createDbStore = <Row extends DbRow>(
152154
on(opts.adapter.insertSignal, inserted => {
153155
if (!inserted?.new?.id) return;
154156
for (const row of insertions.values()) {
155-
if (Object.entries(inserted.new).some(([key, value]) => !defaultFields.includes(key) && row[key] !== value))
157+
if (
158+
Object.entries(inserted.new).some(
159+
([key, value]) => !defaultFields.includes(key) && row[key] !== value,
160+
)
161+
)
156162
continue;
157163
const index = untrack(() =>
158164
dbStore.findIndex(cand =>
159-
Object.entries(cand).every(([key, value]) => defaultFields.includes(key) || row[key] == value),
165+
Object.entries(cand).every(
166+
([key, value]) => defaultFields.includes(key) || row[key] == value,
167+
),
160168
),
161169
);
162170
if (index !== -1) {

0 commit comments

Comments
 (0)