不要怂,就是干,撸起袖子干!

Commit b4502a0c by Mick Hansen

[ci skip] docs on docs on docs

1 parent fb135808
While out of the box Sequelize will seem a bit opinionated it's trivial to both legacy and forward proof your application by defining (otherwise generated) table and field names. While out of the box Sequelize will seem a bit opinionated it's trivial to both legacy and forward proof your application by defining (otherwise generated) table and field names.
## Tables ## Tables
```js
sequelize.define('User', {
}, {
tableName: 'users'
});
```
## Fields ## Fields
sequelize.define('ModelName', {
userId: {
type: Sequelize.INTEGER,
field: 'user_id'
}
});
```
## Primary keys
Sequelize will assume your table has a `id` primary key property by default.
To define your own primary key:
```js
sequelize.define('collection', {
uid: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true // Automatically gets converted to SERIAL for postgres
}
});
sequelize.define('collection', {
uuid: {
type: Sequelize.UUID,
primaryKey: true
}
});
```
And if your model has no primary key at all you can use `Model.removeAttribute('id');`
## Foreign keys ## Foreign keys
```js
// 1:1
User.belongsTo(Organization, {foreignKey: 'organization_id'});
// 1:M
Project.hasMany(Task, {foreignkey: 'tasks_pk'});
## Join tables // N:M
\ No newline at end of file User.hasMany(Role, {through: 'user_has_roles', foreignKey: 'user_role_user_id'});
Role.hasMany(User, {through: 'user_has_roles', foreignKey: 'roles_identifier'});
```
\ No newline at end of file
## sequelize.sync() ## Syncing
`sequelize.sync()` will, based on your model definitions, create any missing tables. If `force: true` it will first drop tables before recreating them. `sequelize.sync()` will, based on your model definitions, create any missing tables.
If `force: true` it will first drop tables before recreating them.
## Migrations / Manual schema changes ## Migrations / Manual schema changes
Sequelize has a [sister library](https://github.com/sequelize/umzug) for handling execution and logging of migration tasks. Sequelize has a [sister library](https://github.com/sequelize/umzug) for handling execution and logging of migration tasks.
Sequelize provides a list of ways to programmaticaly create or change a table schema. Sequelize provides a list of ways to programmaticaly create or change a table schema.
### createTable ### createTable
......
...@@ -15,12 +15,13 @@ return sequelize.transaction(t) { ...@@ -15,12 +15,13 @@ return sequelize.transaction(t) {
firstName: 'John', firstName: 'John',
lastName: 'Boothe' lastName: 'Boothe'
}, {transction: t}); }, {transction: t});
}).then(function () {
// Transaction has been committed
}).catch(function (err) {
// Transaction has been rolled back
// err is whatever rejected the promise chain returned to the transaction callback
}); });
}).then(function (result) {
// Transaction has been committed
// result is whatever the result of the promise chain returned to the transaction callback is
}).catch(function (err) {
// Transaction has been rolled back
// err is whatever rejected the promise chain returned to the transaction callback is
}); });
``` ```
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!