@legendapp/state 3.0.0-alpha.31 → 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legendapp/state",
3
- "version": "3.0.0-alpha.31",
3
+ "version": "3.0.0-alpha.33",
4
4
  "description": "legend-state",
5
5
  "sideEffects": false,
6
6
  "private": false,
@@ -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;
@@ -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;
@@ -107,13 +120,18 @@ function syncedSupabase(props) {
107
120
  const { eventType, new: value, old } = payload;
108
121
  if (eventType === "INSERT" || eventType === "UPDATE") {
109
122
  const cur = (_a = value$.peek()) == null ? void 0 : _a[value.id];
110
- const curDateStr = cur && (fieldUpdatedAt && cur[fieldUpdatedAt] || fieldCreatedAt || cur[fieldCreatedAt]);
111
- const valueDateStr = fieldUpdatedAt && value[fieldUpdatedAt] || fieldCreatedAt && value[fieldCreatedAt];
112
- const valueDate = +new Date(valueDateStr);
113
- if (valueDateStr && (!curDateStr || valueDate > +new Date(curDateStr))) {
123
+ let isOk = !fieldUpdatedAt;
124
+ let lastSync = void 0;
125
+ if (!isOk) {
126
+ const curDateStr = cur && (fieldUpdatedAt && cur[fieldUpdatedAt] || fieldCreatedAt || cur[fieldCreatedAt]);
127
+ const valueDateStr = fieldUpdatedAt && value[fieldUpdatedAt] || fieldCreatedAt && value[fieldCreatedAt];
128
+ lastSync = +new Date(valueDateStr);
129
+ isOk = valueDateStr && (!curDateStr || lastSync > +new Date(curDateStr));
130
+ }
131
+ if (isOk) {
114
132
  update2({
115
133
  value: [value],
116
- lastSync: valueDate,
134
+ lastSync,
117
135
  mode: "merge"
118
136
  });
119
137
  }
@@ -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;
@@ -105,13 +118,18 @@ function syncedSupabase(props) {
105
118
  const { eventType, new: value, old } = payload;
106
119
  if (eventType === "INSERT" || eventType === "UPDATE") {
107
120
  const cur = (_a = value$.peek()) == null ? void 0 : _a[value.id];
108
- const curDateStr = cur && (fieldUpdatedAt && cur[fieldUpdatedAt] || fieldCreatedAt || cur[fieldCreatedAt]);
109
- const valueDateStr = fieldUpdatedAt && value[fieldUpdatedAt] || fieldCreatedAt && value[fieldCreatedAt];
110
- const valueDate = +new Date(valueDateStr);
111
- if (valueDateStr && (!curDateStr || valueDate > +new Date(curDateStr))) {
121
+ let isOk = !fieldUpdatedAt;
122
+ let lastSync = void 0;
123
+ if (!isOk) {
124
+ const curDateStr = cur && (fieldUpdatedAt && cur[fieldUpdatedAt] || fieldCreatedAt || cur[fieldCreatedAt]);
125
+ const valueDateStr = fieldUpdatedAt && value[fieldUpdatedAt] || fieldCreatedAt && value[fieldCreatedAt];
126
+ lastSync = +new Date(valueDateStr);
127
+ isOk = valueDateStr && (!curDateStr || lastSync > +new Date(curDateStr));
128
+ }
129
+ if (isOk) {
112
130
  update2({
113
131
  value: [value],
114
- lastSync: valueDate,
132
+ lastSync,
115
133
  mode: "merge"
116
134
  });
117
135
  }