Skip to content

Commit f7216b9

Browse files
committed
feat: add app metadata
1 parent 8ebf1ce commit f7216b9

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

packages/backend/src/om/definitions/PropType.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class PropType extends AdvancedBase {
2424
new WeakConstructorTrait(),
2525
]
2626

27-
static create (context, data) {
27+
static create (context, data, k) {
2828
const chains = {};
2929
const super_type = data.from && (() => {
3030
const registry = context.get('registry');
@@ -51,7 +51,7 @@ class PropType extends AdvancedBase {
5151
}
5252

5353
return new PropType({
54-
chains,
54+
chains, name: k,
5555
});
5656
}
5757

packages/backend/src/om/entitystorage/SQLES.js

+30
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,29 @@ class SQLES extends BaseES {
194194
let value = data[col_name];
195195
value = await prop.sql_dereference(value);
196196

197+
// TODO: This is not an ideal implementation,
198+
// but this is only 6 lines of code so doing this
199+
// "properly" is not sensible at this time.
200+
//
201+
// This is here because:
202+
// - SQLES has access to the "db" object
203+
//
204+
// Writing this in `json`'s `sql_reference` method
205+
// is also not ideal because that places the concern
206+
// of supporting different database backends to
207+
// property types.
208+
//
209+
// Best solution: SQLES has a SQLRefinements by
210+
// composition. This SQLRefinements is applied
211+
// to property types for the duration of this
212+
// function.
213+
if ( prop.typ.name === 'json' ) {
214+
value = this.db.case({
215+
mysql: () => value,
216+
otherwise: () => JSON.parse(value ?? '{}'),
217+
})();
218+
}
219+
197220
entity_data[prop.name] = value;
198221
}
199222
const entity = await Entity.create({ om: this.om }, entity_data);
@@ -286,6 +309,13 @@ class SQLES extends BaseES {
286309
}
287310

288311
value = await prop.sql_reference(value);
312+
313+
// TODO: This is done here for consistency;
314+
// see the larger comment in sql_row_to_entity_
315+
// which does the reverse operation.
316+
if ( prop.typ.name === 'json' ) {
317+
value = JSON.stringify(value);
318+
}
289319

290320
if ( value && options.use_id ) {
291321
if ( value.hasOwnProperty('id') ) {

packages/backend/src/om/mappings/app.js

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ module.exports = {
5050
// so I've doubled that and rounded up
5151
maxlen: 7000,
5252
},
53+
metadata: {
54+
type: 'json',
55+
},
5356
maximize_on_start: 'flag',
5457
background: 'flag',
5558
subdomain: {

packages/backend/src/services/RegistrantService.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class RegistrantService extends BaseService {
5555
if ( data[k].from && ! seen.has(data[k].from) ) {
5656
throw new Error(`Super type "${data[k].from}" not found for property type "${k}"`);
5757
}
58-
collection.set(k, PropType.create(ctx, data[k]));
58+
collection.set(k, PropType.create(ctx, data[k], k));
5959
seen.add(k);
6060
}
6161
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE apps ADD COLUMN "metadata" JSON DEFAULT NULL;

0 commit comments

Comments
 (0)