@iebh/tera-fy 2.3.9 → 2.3.10

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.
@@ -53,18 +53,18 @@ interface NamespaceRow {
53
53
  interface SyncroEntityConfig {
54
54
  singular: string;
55
55
  initState: (args: {
56
- HYPERDRIVE: PostgresSql;
57
- supabasey: BoundSupabaseyFunction;
58
- id: string; // Primary ID for the entity
59
- relation?: string; // Optional relation identifier (for namespaces, libraries)
56
+ HYPERDRIVE: PostgresSql;
57
+ supabasey: BoundSupabaseyFunction;
58
+ id: string; // Primary ID for the entity
59
+ relation?: string; // Optional relation identifier (for namespaces, libraries)
60
60
  }) => Promise<any>;
61
61
  flushState: (args: {
62
- HYPERDRIVE?: PostgresSql;
63
- supabasey: BoundSupabaseyFunction;
64
- state: any; // The state object to flush
65
- id?: string; // Primary ID (used in some lookups like namespaces)
66
- fsId?: string; // ID often passed to Supabase RPCs (might be same as 'id')
67
- relation?: string; // Optional relation identifier
62
+ HYPERDRIVE: PostgresSql;
63
+ supabasey?: BoundSupabaseyFunction;
64
+ state: any; // The state object to flush
65
+ id?: string; // Primary ID (used in some lookups like namespaces)
66
+ fsId?: string; // ID often passed to Supabase RPCs (might be same as 'id')
67
+ relation?: string; // Optional relation identifier
68
68
  }) => Promise<any>; // Return type signifies completion/result of flush
69
69
  }
70
70
 
@@ -79,45 +79,46 @@ type SyncroConfig = Record<string, SyncroEntityConfig>;
79
79
  *
80
80
  * @property {String} singular The singular noun for the item
81
81
  * @property {Function} initState Function called to initialize state when Firestore has no existing document. Called as `({HYPERDRIVE:PostgresSql, supabasey:BoundSupabaseyFunction, id:String, relation?:string})` and expected to return the initial data object state
82
- * @property {Function} flushState Function called to flush state from Firebase to Supabase. Called the same as `initState` + `{state:Object}`
82
+ * @property {Function} flushState Function called to flush state from Firebase to Postgres. Called the same as `initState` + `{state:Object}`
83
83
  */
84
84
  const syncroConfig: SyncroConfig = {
85
85
  institutes: { // {{{
86
86
  singular: 'institute',
87
87
  async initState({HYPERDRIVE, id}: {HYPERDRIVE: PostgresSql, id: string}) {
88
- let institute = await HYPERDRIVE`
88
+ let rows = await HYPERDRIVE`
89
89
  SELECT data
90
90
  FROM institutes
91
91
  WHERE id = ${id}
92
92
  LIMIT 1
93
93
  `;
94
- if (institute.length > 0) {
95
- return institute[0].data; // institute is valid and already exists
94
+ if (rows.length > 0) {
95
+ return rows[0].data; // institute is valid and already exists
96
96
  } else {
97
97
  throw new Error(`Syncro institute "${id}" not found`);
98
98
  }
99
99
  },
100
- flushState({supabasey, state, id}) {
101
- // FIXME: Better to reuse `env.HYPERDRIVE` instead of supabasey here in future
102
- return supabasey((supabase) => supabase.rpc('syncro_merge_data', {
103
- table_name: 'institutes',
104
- entity_id: id,
105
- new_data: state,
106
- }));
100
+ flushState({HYPERDRIVE, state, id}) {
101
+ return HYPERDRIVE`
102
+ SELECT syncro_merge_data(
103
+ table_name => 'institutes',
104
+ entity_id => ${id}::UUID,
105
+ new_data => ${HYPERDRIVE.json(state)}::JSONB
106
+ )
107
+ `;
107
108
  },
108
109
  }, // }}}
109
110
  projects: { // {{{
110
111
  singular: 'project',
111
112
  async initState({HYPERDRIVE, supabasey, id}: {HYPERDRIVE: PostgresSql, supabasey: BoundSupabaseyFunction, id: string}) {
112
- let projects = await HYPERDRIVE`
113
+ let rows = await HYPERDRIVE`
113
114
  SELECT data
114
115
  FROM projects
115
116
  WHERE id = ${id}
116
117
  LIMIT 1
117
118
  `;
118
- if (projects.length == 0) throw new Error(`Syncro project "${id}" not found`);
119
+ if (rows.length == 0) throw new Error(`Syncro project "${id}" not found`);
119
120
 
120
- const data = projects[0].data;
121
+ const data = rows[0].data;
121
122
 
122
123
  // MIGRATION - Move data.temp{} into Supabase files + add pointer to filename {{{
123
124
  if (
@@ -177,13 +178,14 @@ const syncroConfig: SyncroConfig = {
177
178
 
178
179
  return data;
179
180
  },
180
- flushState({supabasey, state, fsId}) {
181
- // FIXME: Better to reuse `env.HYPERDRIVE` instead of supabasey here in future
182
- return supabasey((supabase) => supabase.rpc('syncro_merge_data', {
183
- table_name: 'projects',
184
- entity_id: fsId,
185
- new_data: state,
186
- }));
181
+ flushState({HYPERDRIVE, state, fsId}) {
182
+ return HYPERDRIVE`
183
+ SELECT syncro_merge_data(
184
+ table_name => 'projects',
185
+ entity_id => ${fsId}::UUID,
186
+ new_data => ${HYPERDRIVE.json(state)}::JSONB
187
+ )
188
+ `;
187
189
  },
188
190
  }, // }}}
189
191
  project_libraries: { // {{{
@@ -250,25 +252,26 @@ const syncroConfig: SyncroConfig = {
250
252
  throw new Error(`Syncro test "${id}" not found`);
251
253
  }
252
254
  },
253
- flushState({supabasey, state, fsId}) {
254
- // FIXME: Better to reuse `env.HYPERDRIVE` instead of supabasey here in future
255
- return supabasey((supabase) => supabase.rpc('syncro_merge_data', {
256
- table_name: 'test',
257
- entity_id: fsId,
258
- new_data: state,
259
- }));
255
+ flushState({HYPERDRIVE, state, fsId}) {
256
+ return HYPERDRIVE`
257
+ SELECT syncro_merge_data(
258
+ table_name => 'test',
259
+ entity_id => ${fsId}::UUID,
260
+ new_data => ${HYPERDRIVE.json(state)}::JSONB
261
+ )
262
+ `;
260
263
  },
261
264
  }, // }}}
262
265
  users: { // {{{
263
266
  singular: 'user',
264
267
  async initState({HYPERDRIVE, id}: {HYPERDRIVE: PostgresSql, id: string}) {
265
- let user = await HYPERDRIVE`
268
+ let rows = await HYPERDRIVE`
266
269
  SELECT data
267
270
  FROM users
268
271
  WHERE id = ${id}
269
272
  LIMIT 1
270
273
  `;
271
- if (user.length > 0) return user[0].data; // User is valid and already exists
274
+ if (rows.length > 0) return rows[0].data; // User is valid and already exists
272
275
 
273
276
  // User row doesn't already exist - this shouldn't happen if the user has correctly gone through the onboarding process
274
277
  // but... *shrugs*, who knows
@@ -290,13 +293,14 @@ const syncroConfig: SyncroConfig = {
290
293
  return newUser[0].data; // Return back the data that eventually got created - allowing for database triggers, default field values etc.
291
294
 
292
295
  },
293
- flushState({supabasey, state, fsId}) {
294
- // FIXME: Better to reuse `env.HYPERDRIVE` instead of supabasey here in future
295
- return supabasey((supabase) => supabase.rpc('syncro_merge_data', {
296
- table_name: 'users',
297
- entity_id: fsId,
298
- new_data: state,
299
- }));
296
+ flushState({HYPERDRIVE, state, fsId}) {
297
+ return HYPERDRIVE`
298
+ SELECT syncro_merge_data(
299
+ table_name => 'users',
300
+ entity_id => ${fsId}::UUID,
301
+ new_data => ${HYPERDRIVE.json(state)}::JSONB
302
+ )
303
+ `;
300
304
  },
301
305
  }, // }}}
302
306
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iebh/tera-fy",
3
- "version": "2.3.9",
3
+ "version": "2.3.10",
4
4
  "description": "TERA website worker",
5
5
  "scripts": {
6
6
  "dev": "esbuild --platform=browser --format=esm --bundle lib/terafy.client.ts --outfile=dist/terafy.js --minify --serve --servedir=.",