[关闭]
@WrRan 2016-11-10T11:50:26.000000Z 字数 4005 阅读 958

waterline

Waterline Models

Models represent a structure of data which requires persistent storage. The data may live in any
datastore but is interfaced in the same way regardless of datastore. This allows your users to live in PostgreSQL and your
user preferences to live in MongoDB while you interact with the data models in the exact same way.

If you're using MySQL, a model might correspond to a table. If you're using MongoDB, it might
correspond to a collection. In either case, our goal is to provide a simple, modular way of managing
data without relying on any one type of database.

See Also

How to define a model

Model definitions contain attributes, validations, instance methods, lifecycle callbacks
and class methods. To define a model you will extend the Waterline.Collection object and add
in your own attributes and methods.

By default an attribute named id will be automatically added to your model which will contain
an auto-incrementing number unique to each record. This will be your model's primary key and
will be indexed when available. You can override this if you would like to define your own primary
key factory or attribute.

Each model will also get two timestamp attributes added by default: createdAt and updatedAt which respectively
will track when a record went into the datastore and when it was last updated.

  1. var Person = Waterline.Collection.extend({
  2. // Identity is a unique name for this model and must be in lower case
  3. identity: 'person',
  4. // Connection
  5. // A named connection which will be used to read/write to the datastore
  6. connection: 'local-postgresql',
  7. // Attributes are basic pieces of information about a model
  8. attributes: {
  9. firstName: 'string',
  10. lastName: 'string',
  11. age: 'integer',
  12. birthDate: 'date',
  13. emailAddress: 'email'
  14. }
  15. });
  16. module.exports = Person;

You can also set options for each attribute. These include validations and any indexing or unique
properties.

  1. var Person = Waterline.Collection.extend({
  2. identity: 'person',
  3. connection: 'local-postgresql',
  4. attributes: {
  5. // Don't allow two objects with the same value
  6. lastName: {
  7. type: 'string',
  8. unique: true
  9. },
  10. // Ensure a value is set
  11. age: {
  12. type: 'integer',
  13. required: true
  14. },
  15. // Set a default value if no value is set
  16. phoneNumber: {
  17. type: 'string',
  18. defaultsTo: '111-222-3333'
  19. },
  20. // Create an auto-incrementing value (not supported by all datastores)
  21. incrementMe: {
  22. type: 'integer',
  23. autoIncrement: true
  24. },
  25. // Index a value for faster queries
  26. emailAddress: {
  27. type: 'email', // Email type will get validated by the ORM
  28. index: true
  29. }
  30. }
  31. });

Using an existing database

There might be times when you want to use an existing database in your models.

It is extremely important to set the migrate property to safe in your models when working with existing databases. If you do not to this, you will very likely lose data and do other terrible things as it tries to automatically adjust the schema.

In this example, the WB Company has prefixed all of their fields with wb_. You'll notice that you can use the tableName attribute, but also columnName in the attributes object.

  1. var Widget = Waterline.Collection.extend({
  2. identity: 'wbwidget',
  3. connection: 'wb-widget-database',
  4. tableName: 'wb_widgets',
  5. attributes: {
  6. id: {
  7. type: 'integer',
  8. columnName: 'wb_id',
  9. primaryKey: true
  10. },
  11. name: {
  12. type: 'string',
  13. columnName: 'wb_name'
  14. },
  15. description: {
  16. type: 'text',
  17. columnName: 'wb_description'
  18. }
  19. },
  20. migrate: 'safe',
  21. autoPK: false,
  22. autoCreatedAt: false,
  23. autoUpdatedAt: false
  24. });

In addition, settings for automatically generating the primary key field (autoPK), the created timestamp (autoCreatedAt), and the modified timestamp (autoUpdatedAt) are disabled in this example because either the model specifies them specifically, or they are actually absent from the existing database table.

Model property summary

All of these properties can be set as global defaults, or can be individually set in each model.

Property Value Default Description
connection string - The name of the connection to use for the model.
identity string - The programmatic name for the model.
tableName string - Use a custom database table/collection name rather than inferring it from the name of the model.
migrate string alter Sets the schema to automatically alter the schema, drop the schema or make no changes (safe).
autoPK boolean true Automatically add an id attribute to the model to be the primary key.
autoCreatedAt date Current time Automatically add a createdAt date attribute to the model.
autoUpdatedAt date The created time Automatically add a updatedAt date attribute to the model.
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注