breeze-client / MetadataStore
Class: MetadataStore
Defined in: src/metadata/entity-metadata.ts:76
An instance of the MetadataStore contains all of the metadata about a collection of EntityType's. MetadataStores may be shared across EntityManager's. If an EntityManager is created without an explicit MetadataStore, the MetadataStore from the MetadataStore.defaultInstance property will be used.
Constructors
Constructor
new MetadataStore(
config?):MetadataStore
Defined in: src/metadata/entity-metadata.ts:174
Constructs a new MetadataStore.
const ms = new MetadataStore();The store can then be associated with an EntityManager
const entityManager = new EntityManager({
serviceName: "breeze/NorthwindIBModel",
metadataStore: ms
});or for an existing EntityManager
// Assume em1 is an existing EntityManager
em1.setProperties({ metadataStore: ms });Parameters
config?
Configuration settings .
- namingConvention - (default=NamingConvention.defaultInstance) NamingConvention to be used in mapping property names between client and server. Uses the NamingConvention.defaultInstance if not specified.
- localQueryComparisonOptions - (default=LocalQueryComparisonOptions.defaultInstance) The LocalQueryComparisonOptions to be used when performing "local queries" in order to match the semantics of queries against a remote service. Options passed here, or made the default with setAsDefault(), win over any that imported metadata names.
- serializerFn - A function that is used to mediate the serialization of instances of this type.
Returns
MetadataStore
Properties
dataServices
dataServices:
DataService[]
Defined in: src/metadata/entity-metadata.ts:95
The DataServices this store has metadata for, one per service name. Entries are added by MetadataStore.fetchMetadata, MetadataStore.importMetadata and MetadataStore.addDataService. Read Only
localQueryComparisonOptions
localQueryComparisonOptions:
LocalQueryComparisonOptions
Defined in: src/metadata/entity-metadata.ts:100
The LocalQueryComparisonOptions associated with this MetadataStore. Read Only
metadataFetched
metadataFetched:
BreezeEvent<MetadataFetchedEventArgs>
Defined in: src/metadata/entity-metadata.ts:123
An BreezeEvent that fires after a MetadataStore has completed fetching metadata from a remote service.
Event Args
- metadataStore - The MetadataStore into which the metadata was fetched.
- dataService - The DataService that metadata was fetched from.
- rawMetadata - {Object} The raw metadata returned from the service. (It will have already been processed by this point).
const ms = myEntityManager.metadataStore;
ms.metadataFetched.subscribe(args => {
const metadataStore = args.metadataStore;
const dataService = args.dataService;
});Event
name
name:
string
Defined in: src/metadata/entity-metadata.ts:91
An optional name for this MetadataStore, set with MetadataStore.setProperties. MetadataStore.exportMetadata writes it, but MetadataStore.importMetadata does not restore it. When EntityManager.exportEntities leaves out the metadata it records this name, and EntityManager.importEntities passes it to ImportConfig.metadataVersionFn.
namingConvention
namingConvention:
NamingConvention
Defined in: src/metadata/entity-metadata.ts:98
The NamingConvention associated with this MetadataStore. Read Only
serializerFn?
optionalserializerFn?: (prop,val) =>any
Defined in: src/metadata/entity-metadata.ts:106
A function that customizes how data property values are serialized, both when entities are sent to the server in a save and when EntityManager.exportEntities writes them. It is called as serializerFn(property, value); its return value is used in place of value, and returning undefined leaves the property out. Applies to every type in the store that has no serializerFn of its own (see EntityType.serializerFn).
Parameters
prop
val
any
Returns
any
metadataVersion
staticmetadataVersion:string='1.0.5'
Defined in: src/metadata/entity-metadata.ts:85
The version of any MetadataStores created by this class
normalizeTypeName
staticnormalizeTypeName:any
Defined in: src/metadata/entity-metadata.ts:204
Dev Only - for use when writing a DataServiceAdapter or JsonResultsAdapter. Converts a type name as a server sends it, such as the .NET "Northwind.Models.Customer, Northwind", into Breeze's qualified form, "Customer:#Northwind.Models". Results are cached; a null or empty name is returned unchanged.
Methods
addDataService()
addDataService(
dataService,shouldOverwrite?):void
Defined in: src/metadata/entity-metadata.ts:235
Adds a DataService to this MetadataStore. If a DataService with the same serviceName is already in the MetadataStore an exception will be thrown.
Parameters
dataService
The DataService to add
shouldOverwrite?
boolean
(default=false) Permit overwrite of existing DataService rather than throw exception
Returns
void
addEntityType()
addEntityType(
stype):void
Defined in: src/metadata/entity-metadata.ts:263
Adds an EntityType or ComplexType to this MetadataStore. Properties can still be added to the type afterwards, until the first entity of that type is attached to an EntityManager - after that the type is frozen.
Parameters
stype
StructuralType | EntityTypeConfig | ComplexTypeConfig
The EntityType or ComplexType to add
Returns
void
exportMetadata()
exportMetadata():
string
Defined in: src/metadata/entity-metadata.ts:334
Exports this MetadataStore to a serialized string appropriate for local storage. This operation is also called internally when exporting an EntityManager.
// assume ms is a previously created MetadataStore
const metadataAsString = ms.exportMetadata();
window.localStorage.setItem("metadata", metadataAsString);
// and later, usually in a different session imported
const metadataFromStorage = window.localStorage.getItem("metadata");
const newMetadataStore = new MetadataStore();
newMetadataStore.importMetadata(metadataFromStorage);Returns
string
A serialized version of this MetadataStore that may be stored locally and later restored.
fetchMetadata()
Fetches the metadata for a specified 'service'. This method is automatically called internally by an EntityManager before its first query against a new service. Async
Usually you will not actually process the results of a fetchMetadata call directly, but will instead ask for the metadata from the EntityManager after the fetchMetadata call returns.
const ms = new MetadataStore();
// or more commonly
// const ms = anEntityManager.metadataStore;
const rawMetadata = await ms.fetchMetadata("breeze/NorthwindIBModel");
// do something with the metadataParam
dataService
Either a DataService or just the name of the DataService to fetch metadata for.
Param
callback
Deprecated. Function called on success.
Param
errorCallback
Deprecated. Function called on failure.
Call Signature
fetchMetadata(
dataService):Promise<any>
Defined in: src/metadata/entity-metadata.ts:476
Parameters
dataService
string | DataService
Returns
Promise<any>
Call Signature
fetchMetadata(
dataService,callback?,errorCallback?):Promise<any>
Defined in: src/metadata/entity-metadata.ts:478
Parameters
dataService
string | DataService
callback?
(schema) => void
errorCallback?
Returns
Promise<any>
Deprecated
Await the returned promise instead of passing callbacks.
getAsComplexType()
getAsComplexType(
typeName,okIfNotFound?):ComplexType|null
Defined in: src/metadata/entity-metadata.ts:660
Returns an EntityType or null given its name.
// assume em1 is a preexisting EntityManager
const locType = em1.metadataStore.getAsComplexType("Location");or to throw an error if the type is not found
const badType = em1.metadataStore.getAsComplexType("Foo", false);
// badType will not get set and an exception will be thrown.Parameters
typeName
string
Either the fully qualified name or a short name may be used. If a short name is specified and multiple types share that same short name an exception will be thrown.
okIfNotFound?
boolean = false
(default=false) Whether to throw an error if the specified EntityType is not found.
Returns
ComplexType | null
The EntityType. ComplexType or 'null' if not not found.
getAsEntityType()
Returns an EntityType given a registered class or a type name, or null if there is none and okIfNotFound is true.
// assume em1 is a preexisting EntityManager, and OrderDetail is registered with its store
const odType = em1.metadataStore.getAsEntityType(OrderDetail);
const sameType = em1.metadataStore.getAsEntityType("OrderDetail");or to throw an error if the type is not found
const badType = em1.metadataStore.getAsEntityType("Foo", false);
// badType will not get set and an exception will be thrown.Param
type
A class registered with MetadataStore.registerEntityTypeCtor, or a type name: either the fully qualified name or a short name. If a short name is specified and multiple types share that same short name an exception will be thrown.
Param
okIfNotFound
(default=false) Whether to return null, rather than throw, if the EntityType is not found.
Call Signature
getAsEntityType(
type,okIfNotFound?):EntityType
Defined in: src/metadata/entity-metadata.ts:606
Parameters
type
string | (() => Entity)
okIfNotFound?
false
Returns
Call Signature
getAsEntityType(
type,okIfNotFound):EntityType|null
Defined in: src/metadata/entity-metadata.ts:607
Parameters
type
string | (() => Entity)
okIfNotFound
boolean
Returns
EntityType | null
getDataService()
getDataService(
serviceName):DataService
Defined in: src/metadata/entity-metadata.ts:467
Returns the DataService for a specified service name
// Assume em1 is an existing EntityManager.
const ds = em1.metadataStore.getDataService("breeze/NorthwindIBModel");
const adapterName = ds.adapterName; // may be nullParameters
serviceName
string
The service name.
Returns
The DataService with the specified name.
getEntityType()
getEntityType(
typeName,okIfNotFound?):StructuralType|null
Defined in: src/metadata/entity-metadata.ts:683
Returns an EntityType or a ComplexType given its name.
Parameters
typeName
string
Either the fully qualified name or a short name may be used. If a short name is specified and multiple types share that same short name an exception will be thrown.
okIfNotFound?
boolean = false
(default=false) Whether to throw an error if the specified EntityType is not found.
Returns
StructuralType | null
The EntityType. ComplexType or 'null' if not not found.
Deprecated
Misleading name: it returns an EntityType or a ComplexType. Use MetadataStore.getAsEntityType or MetadataStore.getAsComplexType when you know which kind you expect, and MetadataStore.getStructuralType when either will do.
getEntityTypeNameForResourceName()
getEntityTypeNameForResourceName(
resourceName):string
Defined in: src/metadata/entity-metadata.ts:748
Returns a fully qualified entityTypeName for a specified resource name. The reverse of this operation can be obtained via the EntityType.defaultResourceName property
Parameters
resourceName
string
Returns
string
getEntityTypes()
getEntityTypes():
StructuralType[]
Defined in: src/metadata/entity-metadata.ts:731
Returns an array containing all of the EntityTypes or ComplexTypes in this MetadataStore.
// assume em1 is a preexisting EntityManager
const allTypes = em1.metadataStore.getEntityTypes();Returns
getIncompleteNavigationProperties()
getIncompleteNavigationProperties():
NavigationProperty[][]
Defined in: src/metadata/entity-metadata.ts:740
Returns the navigation properties whose target EntityType has not been added to this store yet, as one array per missing type. An empty result means every navigation property has been resolved; useful for checking metadata that is built by hand or imported in pieces.
Returns
getStructuralType()
getStructuralType(
typeName,okIfNotFound?):StructuralType|null
Defined in: src/metadata/entity-metadata.ts:706
Returns an EntityType or a ComplexType given its name.
// assume em1 is a preexisting EntityManager
const odType = em1.metadataStore.getStructuralType("OrderDetail");or to throw an error if the type is not found
const badType = em1.metadataStore.getStructuralType("Foo", false);
// badType will not get set and an exception will be thrown.Prefer MetadataStore.getAsEntityType or MetadataStore.getAsComplexType when you know which kind you expect. This is the lookup for when you do not.
Parameters
typeName
string
Either the fully qualified name or a short name may be used. If a short name is specified and multiple types share that same short name an exception will be thrown.
okIfNotFound?
boolean = false
(default=false) Whether to throw an error if the specified EntityType is not found.
Returns
StructuralType | null
The EntityType. ComplexType or 'null' if not not found.
hasMetadataFor()
hasMetadataFor(
serviceName):boolean
Defined in: src/metadata/entity-metadata.ts:453
Returns whether Metadata has been retrieved for a specified service name.
// Assume em1 is an existing EntityManager.
if (!em1.metadataStore.hasMetadataFor("breeze/NorthwindIBModel")) {
// do something interesting
}Parameters
serviceName
string
The service name.
Returns
boolean
Whether metadata has already been retrieved for the specified service name.
importMetadata()
importMetadata(
exportedMetadata,allowMerge?):MetadataStore
Defined in: src/metadata/entity-metadata.ts:369
Imports a previously exported serialized MetadataStore into this MetadataStore.
// assume ms is a previously created MetadataStore
const metadataAsString = ms.exportMetadata();
window.localStorage.setItem("metadata", metadataAsString);
// and later, usually in a different session
const metadataFromStorage = window.localStorage.getItem("metadata");
const newMetadataStore = new MetadataStore();
newMetadataStore.importMetadata(metadataFromStorage);A type that is already in this store is left as it is unless 'allowMerge' is set, so importing the same metadata twice is harmless. A type that is not in the store is created, with or without 'allowMerge', and its metadata must include 'dataProperties'. The 'localQueryComparisonOptions' the metadata names are adopted only by an empty store whose own options were not chosen on the client (see the constructor).
Parameters
exportedMetadata
string | Object
A previously exported MetadataStore.
allowMerge?
boolean = false
Allows custom metadata to be merged into existing metadata types.
Returns
MetadataStore
This MetadataStore.
Chainable
isEmpty()
isEmpty():
boolean
Defined in: src/metadata/entity-metadata.ts:601
Returns whether this MetadataStore contains any metadata yet.
// assume em1 is a preexisting EntityManager;
if (em1.metadataStore.isEmpty()) {
// do something interesting
}Returns
boolean
registerEntityTypeCtor()
registerEntityTypeCtor(
structuralTypeName,aCtor?,initFn?,noTrackingFn?):void
Defined in: src/metadata/entity-metadata.ts:568
Provides a mechanism to register a 'custom' constructor to be used when creating new instances of the specified entity type. If this call is not made, a default constructor is created for the entity as needed. This call may be made before or after the corresponding EntityType has been discovered via Metadata discovery.
class Customer {
miscData = "asdf"; // an unmapped property
doFoo() {
// ...
}
}
// assume em1 is a preexisting EntityManager;
em1.metadataStore.registerEntityTypeCtor("Customer", Customer);
// any queries or createEntity calls from this point on will call the Customer constructor
// registered above.Parameters
structuralTypeName
string
The name of the EntityType or ComplexType.
aCtor?
any
The constructor for this EntityType or ComplexType; may be null if all you want to do is set the next parameter.
initFn?
string | Function
A function or the name of a function on the entity that is to be executed immediately after the entity has been created and populated with any initial values. Called with 'initFn(entity)'
noTrackingFn?
Function
A function that is executed immediately after a noTracking entity has been created and whose return value will be used in place of the noTracking entity.
Returns
void
setEntityTypeForResourceName()
setEntityTypeForResourceName(
resourceName,entityTypeOrName):void
Defined in: src/metadata/entity-metadata.ts:763
Associates a resourceName with an entityType.
This method is only needed in those cases where multiple resources return the same entityType. In this case Metadata discovery will only determine a single resource name for each entityType.
Parameters
resourceName
string
The resource name
entityTypeOrName
string | EntityType
If passing a string either the fully qualified name or a short name may be used. If a short name is specified and multiple types share that same short name an exception will be thrown. If the entityType has not yet been discovered then a fully qualified name must be used.
Returns
void
setProperties()
setProperties(
config):void
Defined in: src/metadata/entity-metadata.ts:222
General purpose property set method
// assume em1 is an EntityManager containing a number of existing entities.
em1.metadataStore.setProperties({
name: "Northwind v6.1.3",
serializerFn: (prop, value) => prop.isUnmapped ? undefined : value
});Parameters
config
An object containing the selected properties and values to set.
Returns
void
trackUnmappedType()
trackUnmappedType(
entityCtor,interceptor):void
Defined in: src/metadata/entity-metadata.ts:535
Used to register a constructor for an EntityType that is not known via standard Metadata discovery; i.e. an unmapped type.
Parameters
entityCtor
any
The constructor function for the 'unmapped' type.
interceptor
any
An interceptor function
Returns
void
importMetadata()
staticimportMetadata(exportedString):MetadataStore
Defined in: src/metadata/entity-metadata.ts:436
Creates a new MetadataStore from a previously exported serialized MetadataStore
// assume ms is a previously created MetadataStore
const metadataAsString = ms.exportMetadata();
window.localStorage.setItem("metadata", metadataAsString);
// and later, usually in a different session
const metadataFromStorage = window.localStorage.getItem("metadata");
const newMetadataStore = MetadataStore.importMetadata(metadataFromStorage);Parameters
exportedString
string
A previously exported MetadataStore.
Returns
MetadataStore
A new MetadataStore.
makeTypeHash()
staticmakeTypeHash(shortName,ns?):object
Defined in: src/metadata/entity-metadata.ts:806
Dev Only - for use when creating a new MetadataParserAdapter
Parameters
shortName
string
ns?
string
Returns
namespace
namespace:
string|undefined=ns
The namespace, such as "Northwind.Models"; undefined if there is none.
shortTypeName
shortTypeName:
string=shortName
The unqualified name, such as "Customer".
typeName
typeName:
string
The qualified name, such as "Customer:#Northwind.Models".
parseTypeName()
staticparseTypeName(entityTypeName):object
Defined in: src/metadata/entity-metadata.ts:782
Dev Only - for use when creating a new MetadataParserAdapter
Parameters
entityTypeName
string
Returns
namespace
namespace:
string|undefined=ns
The namespace, such as "Northwind.Models"; undefined if there is none.
shortTypeName
shortTypeName:
string=shortName
The unqualified name, such as "Customer".
typeName
typeName:
string
The qualified name, such as "Customer:#Northwind.Models".