Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('adds belongsTo metadata', () => {
const meta =
MetadataInspector.getAllPropertyMetadata(
RELATIONS_KEY,
Order.prototype,
) ?? /* istanbul ignore next */ {};
const relationDef = meta.customerId;
expect(relationDef).to.containEql({
type: RelationType.belongsTo,
name: 'customer',
target: () => Customer,
keyFrom: 'customerId',
});
expect(relationDef.source).to.be.exactly(Order);
expect(relationDef.target()).to.be.exactly(Customer);
});
it('"@property.array" adds array metadata', () => {
@model()
class TestModel {
@property.array(Product)
items: Product[];
}
const meta =
MetadataInspector.getAllPropertyMetadata(
MODEL_PROPERTIES_KEY,
TestModel.prototype,
) ?? /* istanbul ignore next */ {};
expect(meta.items).to.eql({type: Array, itemType: Product});
});
) {
// Check if the definition for this class has been built (not from the super
// class)
const baseClass = Object.getPrototypeOf(target);
if (
!def &&
target.definition &&
baseClass &&
target.definition !== baseClass.definition
) {
return target.definition;
}
const modelDef = new ModelDefinition(def ?? {name: target.name});
const prototype = target.prototype;
const propertyMap: PropertyMap =
MetadataInspector.getAllPropertyMetadata(MODEL_PROPERTIES_KEY, prototype) ??
{};
for (const p in propertyMap) {
const propertyDef = propertyMap[p];
const designType = MetadataInspector.getDesignTypeForProperty(prototype, p);
if (!propertyDef.type) {
propertyDef.type = designType;
}
modelDef.addProperty(p, propertyDef);
}
target.definition = modelDef;
const relationMeta: RelationDefinitionMap =
MetadataInspector.getAllPropertyMetadata(RELATIONS_KEY, prototype) ?? {};
const relations: RelationDefinitionMap = {};
// Build an object keyed by relation names
Object.values(relationMeta).forEach(r => {
relations[r.name] = r;
const modelDef = new ModelDefinition(def ?? {name: target.name});
const prototype = target.prototype;
const propertyMap: PropertyMap =
MetadataInspector.getAllPropertyMetadata(MODEL_PROPERTIES_KEY, prototype) ??
{};
for (const p in propertyMap) {
const propertyDef = propertyMap[p];
const designType = MetadataInspector.getDesignTypeForProperty(prototype, p);
if (!propertyDef.type) {
propertyDef.type = designType;
}
modelDef.addProperty(p, propertyDef);
}
target.definition = modelDef;
const relationMeta: RelationDefinitionMap =
MetadataInspector.getAllPropertyMetadata(RELATIONS_KEY, prototype) ?? {};
const relations: RelationDefinitionMap = {};
// Build an object keyed by relation names
Object.values(relationMeta).forEach(r => {
relations[r.name] = r;
});
target.definition.relations = relations;
return modelDef;
}
it('creates juggler property metadata', () => {
@model()
class AddressBook extends Entity {
@property({id: true})
id: number;
}
@model()
class Address extends Entity {
@belongsTo(() => AddressBook)
addressBookId: number;
}
const jugglerMeta = MetadataInspector.getAllPropertyMetadata(
MODEL_PROPERTIES_KEY,
Address.prototype,
);
expect(jugglerMeta).to.eql({
addressBookId: {
type: Number,
},
});
expect(Address.definition.relations).to.containDeep({
addressBook: {
keyFrom: 'addressBookId',
name: 'addressBook',
type: 'belongsTo',
},
});
});