@iebh/tera-fy 2.0.18 → 2.0.19

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/README.md CHANGED
@@ -55,12 +55,41 @@ This module exports various plugins which are availble as `import from '@iebh/te
55
55
 
56
56
  `@iebh/tera-fy/plugins/vue2`
57
57
  ----------------------------
58
- FIXME: To add documentation
58
+ ```javascript
59
+ // Somewhere in your boot process - usually src/main.js
60
+ import TeraFy from '@iebh/tera-fy';
61
+ import TerafyVue from '@iebh/tera-fy/plugins/vue2';
62
+ let terafy = new TeraFy()
63
+ .set('devMode', false) // Uncomment this line if you want TeraFy to be chatty and use all `setIfDev` settings
64
+ .setIfDev('siteUrl', 'http://localhost:7334/embed')
65
+ .use(TerafyVue, { // Add the Vue plugin
66
+ vue: window.Vue, // Assumes Vue is available on the window object
67
+ })
68
+
69
+ // Include after app boot
70
+ const app = new Vue({ /* ... */ })
71
+ app.$mount("#app");
72
+ await terafy.init({app});
73
+ ```
59
74
 
60
75
 
61
76
  `@iebh/tera-fy/plugins/vue3`
62
77
  ----------------------------
63
- FIXME: To add documentation
78
+ ```javascript
79
+ // Somewhere in your boot process - usually src/main.js
80
+ import TeraFy from '@iebh/tera-fy';
81
+ import TerafyVue from '@iebh/tera-fy/plugins/vue';
82
+ let terafy = new TeraFy()
83
+ .set('devMode', import.meta.env.DEV) // TeraFy should be chatty and use all `setIfDev` settings
84
+ .setIfDev('siteUrl', 'http://localhost:7334/embed')
85
+ .use(TerafyVue) // Add the Vue plugin
86
+
87
+ terafy.init(); // Initialize everything
88
+
89
+ app.use(terafy.vuePlugin({
90
+ globalName: '$tera', // Install as vm.$tera into every component
91
+ }));
92
+ ```
64
93
 
65
94
 
66
95
  `@iebh/tera-fy/plugins/vite`
@@ -15,12 +15,11 @@ export default {
15
15
  projects: { // {{{
16
16
  singular: 'project',
17
17
  async initState({supabase, id}) {
18
- let data = await supabase(s => s
18
+ let data = await supabase
19
19
  .from('projects')
20
20
  .select('data')
21
21
  .maybeSingle()
22
22
  .eq('id', id)
23
- );
24
23
  if (!data) throw new Error(`Syncro project "${id}" not found`);
25
24
  data = data.data;
26
25
 
@@ -42,7 +41,7 @@ export default {
42
41
  console.log('[MIGRATION] Creating filename:', fileName);
43
42
 
44
43
  return Promise.resolve()
45
- .then(()=> supabase(s => s // Split data.temp[toolKey] -> file {{{
44
+ .then(()=> supabase // Split data.temp[toolKey] -> file {{{
46
45
  .storage
47
46
  .from('projects')
48
47
  .upload(
@@ -68,7 +67,7 @@ export default {
68
67
  upsert: true,
69
68
  },
70
69
  )
71
- )) // }}}
70
+ ) // }}}
72
71
  .then(()=> data.temp[toolKey] = fileName) // Replace data.temp[toolKey] with new filename
73
72
  .catch(e => {
74
73
  console.warn('[MIGRATION] Failed to create file', fileName, '-', e);
@@ -82,11 +81,11 @@ export default {
82
81
  return data;
83
82
  },
84
83
  flushState({supabase, state, fsId}) {
85
- return supabase(s => s.rpc('syncro_merge_data', {
84
+ return supabase.rpc('syncro_merge_data', {
86
85
  table_name: 'projects',
87
86
  entity_id: fsId,
88
87
  new_data: state,
89
- }))
88
+ })
90
89
  },
91
90
  }, // }}}
92
91
  project_libraries: { // {{{
@@ -97,17 +96,20 @@ export default {
97
96
  let fileId = relation.replace(/_\*$/, '');
98
97
 
99
98
  return Promise.resolve()
100
- .then(()=> supabase(s => s.storage
99
+ .then(()=> supabase.storage
101
100
  .from('projects')
102
101
  .list(id)
103
- ))
102
+ )
104
103
  .then(files => files.find(f => f.id == fileId))
105
104
  .then(file => file || Promise.reject(`Invalid file ID "${fileId}"`))
106
- .then(file => supabase(s => s.storage
105
+ .then(file => supabase.storage
107
106
  .from('projects')
108
107
  .download(`${id}/${file.name}`)
109
- )
110
- .then(blob => ({blob, file}))
108
+ .then(({ data: blob, error }) => {
109
+ if (error) throw error;
110
+ if (!blob) throw new Error('Failed to download file blob');
111
+ return {blob, file}
112
+ })
111
113
  )
112
114
  .then(({blob, file}) => Reflib.uploadFile({
113
115
  file: new File(
@@ -128,31 +130,37 @@ export default {
128
130
  }, // }}}
129
131
  project_namespaces: { // {{{
130
132
  singular: 'project namespace',
131
- initState({supabase, id, relation}) {
133
+ async initState({supabase, id, relation}) {
132
134
  if (!relation) throw new Error('Project namespace relation missing, path should resemble "project_namespaces::${PROJECT}::${RELATION}"');
133
- return supabase(s => s
135
+ let { data: rows, error: selectError } = await supabase
134
136
  .from('project_namespaces')
135
137
  .select('data')
136
- .limit(1)
137
138
  .eq('project', id)
138
139
  .eq('name', relation)
139
- )
140
- .then(rows => rows.length == 1
141
- ? rows[0]
142
- : supabase(s => s
143
- .from('project_namespaces') // Doesn't exist - create it
144
- .insert({
145
- project: id,
146
- name: relation,
147
- data: {},
148
- })
149
- .select('data')
150
- )
151
- )
152
- .then(item => item.data);
140
+ .limit(1);
141
+
142
+ if (selectError) throw selectError;
143
+
144
+ if (rows && rows.length == 1) {
145
+ return rows[0].data;
146
+ } else {
147
+ const { data: newItem, error: insertError } = await supabase
148
+ .from('project_namespaces') // Doesn't exist - create it
149
+ .insert({
150
+ project: id,
151
+ name: relation,
152
+ data: {},
153
+ })
154
+ .select('data')
155
+ .single(); // Assuming insert returns the single inserted row
156
+
157
+ if (insertError) throw insertError;
158
+ if (!newItem) throw new Error('Failed to create project namespace');
159
+ return newItem.data;
160
+ }
153
161
  },
154
162
  flushState({supabase, state, id, relation}) {
155
- return supabase(s => s
163
+ return supabase
156
164
  .from('project_namespaces')
157
165
  .update({
158
166
  edited_at: new Date(),
@@ -160,66 +168,65 @@ export default {
160
168
  })
161
169
  .eq('project', id)
162
170
  .eq('name', relation)
163
- )
164
171
  },
165
172
  }, // }}}
166
173
  test: { // {{{
167
174
  singular: 'test',
168
- initState({supabase, id}) {
169
- return supabase(s => s
175
+ async initState({supabase, id}) {
176
+ const { data: rows, error } = await supabase
170
177
  .from('test')
171
178
  .select('data')
172
- .limit(1)
173
179
  .eq('id', id)
174
- )
175
- .then(rows => rows.length == 1 ? rows[0] : Promise.reject(`Syncro test item "${id}" not found`))
176
- .then(item => item.data);
180
+ .limit(1);
181
+
182
+ if (error) throw error;
183
+ if (!rows || rows.length !== 1) return Promise.reject(`Syncro test item "${id}" not found`);
184
+ return rows[0].data;
177
185
  },
178
186
  flushState({supabase, state, fsId}) {
179
- return supabase(s => s.rpc('syncro_merge_data', {
187
+ return supabase.rpc('syncro_merge_data', {
180
188
  table_name: 'test',
181
189
  entity_id: fsId,
182
190
  new_data: state,
183
- }))
191
+ })
184
192
  },
185
193
  }, // }}}
186
194
  users: { // {{{
187
195
  singular: 'user',
188
- initState({supabase, id}) {
189
- return supabase(s => s
196
+ async initState({supabase, id}) {
197
+ const { data: user, error: selectError } = await supabase
190
198
  .from('users')
191
199
  .select('data')
192
- .limit(1)
193
- .maybeSingle()
194
200
  .eq('id', id)
195
- )
196
- .then(user => {
197
- if (user) return user.data; // User is valid and already exists
198
-
199
- // User row doesn't already exist - need to create stub
200
- return supabase(s => s
201
- .from('users')
202
- .insert({
203
- id,
204
- data: {
205
- id,
206
- data: { // Create user prototype data
207
- id,
208
- credits: 1000,
209
- },
210
- },
211
- })
212
- .select('data')
213
- )
214
- .then(newUser => newUser.data) // Return back the data that eventually got created - allowing for database triggers, default field values etc.
201
+ .maybeSingle();
202
+
203
+ if (selectError) throw selectError;
204
+
205
+ if (user) return user.data; // User is valid and already exists
206
+
207
+ // User row doesn't already exist - need to create stub
208
+ const { data: newUser, error: insertError } = await supabase
209
+ .from('users')
210
+ .insert({
211
+ id,
212
+ data: {
213
+ id,
214
+ credits: 1000,
215
+ },
215
216
  })
217
+ .select('data')
218
+ .single(); // Assuming insert returns the single inserted row
219
+
220
+ if (insertError) throw insertError;
221
+ if (!newUser) throw new Error('Failed to create user');
222
+ return newUser.data; // Return back the data that eventually got created - allowing for database triggers, default field values etc.
216
223
  },
217
224
  flushState({supabase, state, fsId}) {
218
- return supabase(s => s.rpc('syncro_merge_data', {
225
+ return supabase.rpc('syncro_merge_data', {
219
226
  table_name: 'users',
220
227
  entity_id: fsId,
221
228
  new_data: state,
222
- }))
229
+ })
223
230
  },
224
231
  }, // }}}
225
232
  };
@@ -42,9 +42,9 @@ export default class Syncro {
42
42
 
43
43
 
44
44
  /**
45
- * Supabasey wrapper function in use
45
+ * Supabase instance in use
46
46
  *
47
- * @type {Supabasey}
47
+ * @type {SupabaseClient}
48
48
  */
49
49
  static supabase;
50
50
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iebh/tera-fy",
3
- "version": "2.0.18",
3
+ "version": "2.0.19",
4
4
  "description": "TERA website worker",
5
5
  "scripts": {
6
6
  "dev": "esbuild --platform=browser --format=esm --bundle lib/terafy.client.js --outfile=dist/terafy.js --minify --serve --servedir=.",