@legendapp/state 3.0.0-alpha.32 → 3.0.0-alpha.33
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/package.json
CHANGED
|
@@ -2,7 +2,8 @@ import { Observable } from '@legendapp/state';
|
|
|
2
2
|
import { SyncedOptions, SyncedOptionsGlobal, SyncedGetParams } from '@legendapp/state/sync';
|
|
3
3
|
import { SyncedCrudPropsBase, CrudAsOption, SyncedCrudReturnType, SyncedCrudPropsMany } from '@legendapp/state/sync-plugins/crud';
|
|
4
4
|
import { PostgrestQueryBuilder, PostgrestFilterBuilder } from '@supabase/postgrest-js';
|
|
5
|
-
import { SupabaseClient } from '@supabase/supabase-js';
|
|
5
|
+
import { SupabaseClient, PostgrestSingleResponse } from '@supabase/supabase-js';
|
|
6
|
+
import { FunctionsResponse } from '@supabase/functions-js';
|
|
6
7
|
|
|
7
8
|
type DatabaseOf<Client extends SupabaseClient> = Client extends SupabaseClient<infer TDB> ? TDB : never;
|
|
8
9
|
type SchemaNameOf<Client extends SupabaseClient> = keyof DatabaseOf<Client>;
|
|
@@ -24,7 +25,7 @@ interface SyncedSupabaseConfiguration extends Omit<SyncedSupabaseConfig<{
|
|
|
24
25
|
enabled?: Observable<boolean>;
|
|
25
26
|
as?: Exclude<CrudAsOption, 'value'>;
|
|
26
27
|
}
|
|
27
|
-
interface SyncedSupabaseProps<Client extends SupabaseClient<any, any>, Collection extends SupabaseCollectionOf<Client, SchemaName>, SchemaName extends SchemaNameOf<Client>, TOption extends CrudAsOption = 'object', TRemote extends SupabaseRowOf<Client, Collection, SchemaName> = SupabaseRowOf<Client, Collection, SchemaName>, TLocal = TRemote> extends SyncedSupabaseConfig<TRemote, TLocal>, SyncedCrudPropsMany<TRemote, TRemote, TOption> {
|
|
28
|
+
interface SyncedSupabaseProps<Client extends SupabaseClient<any, any>, Collection extends SupabaseCollectionOf<Client, SchemaName>, SchemaName extends SchemaNameOf<Client>, TOption extends CrudAsOption = 'object', TRemote extends SupabaseRowOf<Client, Collection, SchemaName> = SupabaseRowOf<Client, Collection, SchemaName>, TLocal = TRemote> extends SyncedSupabaseConfig<TRemote, TLocal>, Omit<SyncedCrudPropsMany<TRemote, TRemote, TOption>, 'list'> {
|
|
28
29
|
supabase: Client;
|
|
29
30
|
collection: Collection;
|
|
30
31
|
schema?: SchemaName;
|
|
@@ -36,6 +37,10 @@ interface SyncedSupabaseProps<Client extends SupabaseClient<any, any>, Collectio
|
|
|
36
37
|
filter?: string;
|
|
37
38
|
};
|
|
38
39
|
stringifyDates?: boolean;
|
|
40
|
+
list?: (...params: Parameters<Required<SyncedCrudPropsMany<TRemote, TLocal, TOption>>['list']>) => PromiseLike<PostgrestSingleResponse<TRemote[]>> | Promise<FunctionsResponse<NoInfer<TRemote>[]>>;
|
|
41
|
+
create?: (...params: Parameters<Required<SyncedCrudPropsBase<TRemote>>['create']>) => PromiseLike<PostgrestSingleResponse<TRemote>> | Promise<FunctionsResponse<NoInfer<TRemote>>>;
|
|
42
|
+
update?: (...params: Parameters<Required<SyncedCrudPropsBase<TRemote>>['update']>) => PromiseLike<PostgrestSingleResponse<TRemote>> | Promise<FunctionsResponse<NoInfer<TRemote>>>;
|
|
43
|
+
delete?: (...params: Parameters<Required<SyncedCrudPropsBase<TRemote>>['delete']>) => PromiseLike<PostgrestSingleResponse<TRemote>> | Promise<FunctionsResponse<NoInfer<TRemote>>>;
|
|
39
44
|
}
|
|
40
45
|
declare function getSyncedSupabaseConfiguration(): SyncedSupabaseConfiguration;
|
|
41
46
|
declare function configureSyncedSupabase(config: SyncedSupabaseConfiguration): void;
|
|
@@ -2,7 +2,8 @@ import { Observable } from '@legendapp/state';
|
|
|
2
2
|
import { SyncedOptions, SyncedOptionsGlobal, SyncedGetParams } from '@legendapp/state/sync';
|
|
3
3
|
import { SyncedCrudPropsBase, CrudAsOption, SyncedCrudReturnType, SyncedCrudPropsMany } from '@legendapp/state/sync-plugins/crud';
|
|
4
4
|
import { PostgrestQueryBuilder, PostgrestFilterBuilder } from '@supabase/postgrest-js';
|
|
5
|
-
import { SupabaseClient } from '@supabase/supabase-js';
|
|
5
|
+
import { SupabaseClient, PostgrestSingleResponse } from '@supabase/supabase-js';
|
|
6
|
+
import { FunctionsResponse } from '@supabase/functions-js';
|
|
6
7
|
|
|
7
8
|
type DatabaseOf<Client extends SupabaseClient> = Client extends SupabaseClient<infer TDB> ? TDB : never;
|
|
8
9
|
type SchemaNameOf<Client extends SupabaseClient> = keyof DatabaseOf<Client>;
|
|
@@ -24,7 +25,7 @@ interface SyncedSupabaseConfiguration extends Omit<SyncedSupabaseConfig<{
|
|
|
24
25
|
enabled?: Observable<boolean>;
|
|
25
26
|
as?: Exclude<CrudAsOption, 'value'>;
|
|
26
27
|
}
|
|
27
|
-
interface SyncedSupabaseProps<Client extends SupabaseClient<any, any>, Collection extends SupabaseCollectionOf<Client, SchemaName>, SchemaName extends SchemaNameOf<Client>, TOption extends CrudAsOption = 'object', TRemote extends SupabaseRowOf<Client, Collection, SchemaName> = SupabaseRowOf<Client, Collection, SchemaName>, TLocal = TRemote> extends SyncedSupabaseConfig<TRemote, TLocal>, SyncedCrudPropsMany<TRemote, TRemote, TOption> {
|
|
28
|
+
interface SyncedSupabaseProps<Client extends SupabaseClient<any, any>, Collection extends SupabaseCollectionOf<Client, SchemaName>, SchemaName extends SchemaNameOf<Client>, TOption extends CrudAsOption = 'object', TRemote extends SupabaseRowOf<Client, Collection, SchemaName> = SupabaseRowOf<Client, Collection, SchemaName>, TLocal = TRemote> extends SyncedSupabaseConfig<TRemote, TLocal>, Omit<SyncedCrudPropsMany<TRemote, TRemote, TOption>, 'list'> {
|
|
28
29
|
supabase: Client;
|
|
29
30
|
collection: Collection;
|
|
30
31
|
schema?: SchemaName;
|
|
@@ -36,6 +37,10 @@ interface SyncedSupabaseProps<Client extends SupabaseClient<any, any>, Collectio
|
|
|
36
37
|
filter?: string;
|
|
37
38
|
};
|
|
38
39
|
stringifyDates?: boolean;
|
|
40
|
+
list?: (...params: Parameters<Required<SyncedCrudPropsMany<TRemote, TLocal, TOption>>['list']>) => PromiseLike<PostgrestSingleResponse<TRemote[]>> | Promise<FunctionsResponse<NoInfer<TRemote>[]>>;
|
|
41
|
+
create?: (...params: Parameters<Required<SyncedCrudPropsBase<TRemote>>['create']>) => PromiseLike<PostgrestSingleResponse<TRemote>> | Promise<FunctionsResponse<NoInfer<TRemote>>>;
|
|
42
|
+
update?: (...params: Parameters<Required<SyncedCrudPropsBase<TRemote>>['update']>) => PromiseLike<PostgrestSingleResponse<TRemote>> | Promise<FunctionsResponse<NoInfer<TRemote>>>;
|
|
43
|
+
delete?: (...params: Parameters<Required<SyncedCrudPropsBase<TRemote>>['delete']>) => PromiseLike<PostgrestSingleResponse<TRemote>> | Promise<FunctionsResponse<NoInfer<TRemote>>>;
|
|
39
44
|
}
|
|
40
45
|
declare function getSyncedSupabaseConfiguration(): SyncedSupabaseConfiguration;
|
|
41
46
|
declare function configureSyncedSupabase(config: SyncedSupabaseConfiguration): void;
|
package/sync-plugins/supabase.js
CHANGED
|
@@ -18,6 +18,15 @@ function configureSyncedSupabase(config) {
|
|
|
18
18
|
}
|
|
19
19
|
Object.assign(supabaseConfig, sync.removeNullUndefined(rest));
|
|
20
20
|
}
|
|
21
|
+
function wrapSupabaseFn(fn) {
|
|
22
|
+
return async (...args) => {
|
|
23
|
+
const { data, error } = await fn(...args);
|
|
24
|
+
if (error) {
|
|
25
|
+
throw new Error(error.message);
|
|
26
|
+
}
|
|
27
|
+
return data;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
21
30
|
function syncedSupabase(props) {
|
|
22
31
|
props = { ...supabaseConfig, ...props };
|
|
23
32
|
const {
|
|
@@ -38,12 +47,16 @@ function syncedSupabase(props) {
|
|
|
38
47
|
waitForSet,
|
|
39
48
|
generateId,
|
|
40
49
|
mode,
|
|
50
|
+
list: listParam,
|
|
51
|
+
create: createParam,
|
|
52
|
+
update: updateParam,
|
|
53
|
+
delete: deleteParam,
|
|
41
54
|
...rest
|
|
42
55
|
} = props;
|
|
43
56
|
const fieldCreatedAt = fieldCreatedAtParam || (changesSince === "last-sync" ? "created_at" : void 0);
|
|
44
57
|
const fieldUpdatedAt = fieldUpdatedAtParam || (changesSince === "last-sync" ? "updated_at" : void 0);
|
|
45
58
|
const fieldDeleted = fieldDeletedParam || (changesSince === "last-sync" ? "deleted" : void 0);
|
|
46
|
-
const list = !actions || actions.includes("read") ? async (params) => {
|
|
59
|
+
const list = !actions || actions.includes("read") ? listParam ? wrapSupabaseFn(listParam) : async (params) => {
|
|
47
60
|
const { lastSync } = params;
|
|
48
61
|
const clientSchema = schema ? client.schema(schema) : client;
|
|
49
62
|
const from = clientSchema.from(collection);
|
|
@@ -61,7 +74,7 @@ function syncedSupabase(props) {
|
|
|
61
74
|
}
|
|
62
75
|
return data || [];
|
|
63
76
|
} : void 0;
|
|
64
|
-
const create = !actions || actions.includes("create") ? async (input) => {
|
|
77
|
+
const create = createParam ? wrapSupabaseFn(createParam) : !actions || actions.includes("create") ? async (input) => {
|
|
65
78
|
const res = await client.from(collection).insert(input).select();
|
|
66
79
|
const { data, error } = res;
|
|
67
80
|
if (data) {
|
|
@@ -71,7 +84,7 @@ function syncedSupabase(props) {
|
|
|
71
84
|
throw new Error(error == null ? void 0 : error.message);
|
|
72
85
|
}
|
|
73
86
|
} : void 0;
|
|
74
|
-
const update = !actions || actions.includes("update") ? async (input) => {
|
|
87
|
+
const update = !actions || actions.includes("update") ? updateParam ? wrapSupabaseFn(updateParam) : async (input) => {
|
|
75
88
|
const res = await client.from(collection).update(input).eq("id", input.id).select();
|
|
76
89
|
const { data, error } = res;
|
|
77
90
|
if (data) {
|
|
@@ -81,7 +94,7 @@ function syncedSupabase(props) {
|
|
|
81
94
|
throw new Error(error == null ? void 0 : error.message);
|
|
82
95
|
}
|
|
83
96
|
} : void 0;
|
|
84
|
-
const deleteFn = !fieldDeleted && (!actions || actions.includes("delete")) ? async (input) => {
|
|
97
|
+
const deleteFn = !fieldDeleted && (!actions || actions.includes("delete")) ? deleteParam ? wrapSupabaseFn(deleteParam) : async (input) => {
|
|
85
98
|
const id = input.id;
|
|
86
99
|
const res = await client.from(collection).delete().eq("id", id).select();
|
|
87
100
|
const { data, error } = res;
|
|
@@ -16,6 +16,15 @@ function configureSyncedSupabase(config) {
|
|
|
16
16
|
}
|
|
17
17
|
Object.assign(supabaseConfig, removeNullUndefined(rest));
|
|
18
18
|
}
|
|
19
|
+
function wrapSupabaseFn(fn) {
|
|
20
|
+
return async (...args) => {
|
|
21
|
+
const { data, error } = await fn(...args);
|
|
22
|
+
if (error) {
|
|
23
|
+
throw new Error(error.message);
|
|
24
|
+
}
|
|
25
|
+
return data;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
19
28
|
function syncedSupabase(props) {
|
|
20
29
|
props = { ...supabaseConfig, ...props };
|
|
21
30
|
const {
|
|
@@ -36,12 +45,16 @@ function syncedSupabase(props) {
|
|
|
36
45
|
waitForSet,
|
|
37
46
|
generateId,
|
|
38
47
|
mode,
|
|
48
|
+
list: listParam,
|
|
49
|
+
create: createParam,
|
|
50
|
+
update: updateParam,
|
|
51
|
+
delete: deleteParam,
|
|
39
52
|
...rest
|
|
40
53
|
} = props;
|
|
41
54
|
const fieldCreatedAt = fieldCreatedAtParam || (changesSince === "last-sync" ? "created_at" : void 0);
|
|
42
55
|
const fieldUpdatedAt = fieldUpdatedAtParam || (changesSince === "last-sync" ? "updated_at" : void 0);
|
|
43
56
|
const fieldDeleted = fieldDeletedParam || (changesSince === "last-sync" ? "deleted" : void 0);
|
|
44
|
-
const list = !actions || actions.includes("read") ? async (params) => {
|
|
57
|
+
const list = !actions || actions.includes("read") ? listParam ? wrapSupabaseFn(listParam) : async (params) => {
|
|
45
58
|
const { lastSync } = params;
|
|
46
59
|
const clientSchema = schema ? client.schema(schema) : client;
|
|
47
60
|
const from = clientSchema.from(collection);
|
|
@@ -59,7 +72,7 @@ function syncedSupabase(props) {
|
|
|
59
72
|
}
|
|
60
73
|
return data || [];
|
|
61
74
|
} : void 0;
|
|
62
|
-
const create = !actions || actions.includes("create") ? async (input) => {
|
|
75
|
+
const create = createParam ? wrapSupabaseFn(createParam) : !actions || actions.includes("create") ? async (input) => {
|
|
63
76
|
const res = await client.from(collection).insert(input).select();
|
|
64
77
|
const { data, error } = res;
|
|
65
78
|
if (data) {
|
|
@@ -69,7 +82,7 @@ function syncedSupabase(props) {
|
|
|
69
82
|
throw new Error(error == null ? void 0 : error.message);
|
|
70
83
|
}
|
|
71
84
|
} : void 0;
|
|
72
|
-
const update = !actions || actions.includes("update") ? async (input) => {
|
|
85
|
+
const update = !actions || actions.includes("update") ? updateParam ? wrapSupabaseFn(updateParam) : async (input) => {
|
|
73
86
|
const res = await client.from(collection).update(input).eq("id", input.id).select();
|
|
74
87
|
const { data, error } = res;
|
|
75
88
|
if (data) {
|
|
@@ -79,7 +92,7 @@ function syncedSupabase(props) {
|
|
|
79
92
|
throw new Error(error == null ? void 0 : error.message);
|
|
80
93
|
}
|
|
81
94
|
} : void 0;
|
|
82
|
-
const deleteFn = !fieldDeleted && (!actions || actions.includes("delete")) ? async (input) => {
|
|
95
|
+
const deleteFn = !fieldDeleted && (!actions || actions.includes("delete")) ? deleteParam ? wrapSupabaseFn(deleteParam) : async (input) => {
|
|
83
96
|
const id = input.id;
|
|
84
97
|
const res = await client.from(collection).delete().eq("id", id).select();
|
|
85
98
|
const { data, error } = res;
|