Once the ‘breeze-sequelize’ library has been loaded, there are three entry points, each of which returns a class constructor.
var SequelizeManager = breezeSequelize.SequelizeManager;
var SequelizeQuery = breezeSequelize.SequelizeQuery;
var SequelizeSaveHandler = breezeSequelize.SequelizeSaveHandler;
as well as access to the ‘standard’ breeze client library via
var breeze = breezeSequelize.breeze;
Note: The version of breeze that is returned is exactly that returned by the ‘breeze-client’ npm BUT with several Node/Sequelize specific methods added to various breeze classes. In general, you should use this reference instead of ‘importing’ the ‘breeze-client’ npm package directly.
You will typically create a single instance of the Breeze SequelizeManager for your service. Each query or save operation within the service will then make use of this single instance. The SequelizeManager encapsulates a single database connection along with the breeze metadata needed to translate breeze operations into the corresponding Sequelize operations.
Under the covers the SequelizeManager creates an entire collection of Sequelize models corresponding one for one with Breeze EntityTypes.
If you are already familiar with Sequelize then this might feel a bit strange. Basically, instead of creating a collection of Sequelize models to describe your interaction with the database, the Breeze SequelizeManager will create these models for you based on the breeze metadata definition. You trigger this process with the SequelizeManager.importMetadata call
If you are not familiar with Sequelize then don’t worry, you don’t actually have to know any Sequelize in order to use ‘breeze-sequelize’.
keyGenerator: An optional instance property that if defined returns an object that implements the ‘KeyGenerator’ interface. This property only needs to be set if the default Sequelize autoincrement logic is not sufficient for your needs. The keyGenerator allows you to programatically control the generation of new ids on the the Node server after a save request is recieved but before it is seen by Sequelize.
You will be creating a new SequelizeQuery instance to correspond with each HTTP get call that represents a breeze client side query. This code for this will look something like this: ( req in the code below is the HTTP request object).
// req = the HTTP request object.
var resourceName = req.params.slug; // the HTTP endpoint
var entityQuery = breeze.EntityQuery.fromUrl(req.url, resourceName);
// _sequelizeManager is a SequelizeManager that would have been
// created before the first HTTP request.
var query = new SequelizeQuery(_sequelizeManager, entityQuery);
Basically this code converts the url passed in into a ‘server side’ EntityQuery which is then converted into a SequelizeQuery which in turn can be ‘executed’ to return results.
A SequelizeSaveHandler instance is needed for each HTTP POST call that represents a client side breeze saveChanges call. You will actually execute the save via the ‘save’ method.
save(): An instance method that can be called to perform the save.
These properties will all be available from within any implementation of the beforeSaveEntity or beforeSaveEntities functions that you provide as properties of the local ‘this’ within the function. i.e. ‘this.saveOptions’