Skip to content

Commit

Permalink
fix: make js operators work on schema-object
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Aug 22, 2024
1 parent 734a389 commit 4cf0ce1
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions packages/schema-record/src/-private/managed-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,63 @@ export class ManagedObject {
const transaction = false;

const proxy = new Proxy(this[SOURCE], {
ownKeys() {
if (isSchemaObject) {
const fields = schema.fields({ type: field.type! });
return Array.from(fields.keys());
}

return Object.keys(self[SOURCE]);
},

has(target: unknown, prop: string | number | symbol) {
if (isSchemaObject) {
const fields = schema.fields({ type: field.type! });
return fields.has(prop as string);
}

return prop in self[SOURCE];
},

getOwnPropertyDescriptor(target, prop) {
if (!isSchemaObject) {
return {
writable: false,
enumerable: true,
configurable: true,
};
}
const fields = schema.fields({ type: field.type! });
if (!fields.has(prop as string)) {
throw new Error(`No field named ${String(prop)} on ${field.type}`);
}
const schemaForField = fields.get(prop as string)!;
switch (schemaForField.kind) {
case 'derived':
return {
writable: false,
enumerable: true,
configurable: true,
};
case '@local':
case 'field':
case 'attribute':
case 'resource':
case 'belongsTo':
case 'hasMany':
case 'collection':
case 'schema-array':
case 'array':
case 'schema-object':
case 'object':
return {
writable: false, // IS_EDITABLE,
enumerable: true,
configurable: true,
};
}
},

get<R extends typeof Proxy<object>>(target: object, prop: keyof R, receiver: R) {
if (prop === OBJECT_SIGNAL) {
return _SIGNAL;
Expand Down

0 comments on commit 4cf0ce1

Please sign in to comment.