Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 5a2c6a62
authored
Mar 31, 2019
by
Benoit MERIAUX
Committed by
Sushant
Mar 31, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(typings): add missing sourceKey option in HasOneOptions association type (#10651)
1 parent
bb952e89
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
64 additions
and
4 deletions
types/lib/associations/has-many.d.ts
types/lib/associations/has-one.d.ts
types/lib/query-interface.d.ts
types/test/e2e/docs-example.ts
types/lib/associations/has-many.d.ts
View file @
5a2c6a6
...
@@ -7,16 +7,21 @@ import {
...
@@ -7,16 +7,21 @@ import {
Model
,
Model
,
ModelCtor
,
ModelCtor
,
Transactionable
,
Transactionable
,
WhereOptions
,
}
from
'../model'
;
}
from
'../model'
;
import
{
Promise
}
from
'../promise'
;
import
{
Promise
}
from
'../promise'
;
import
{
Transaction
}
from
'../transaction'
;
import
{
Association
,
ManyToManyOptions
,
MultiAssociationAccessors
}
from
'./base'
;
import
{
Association
,
ManyToManyOptions
,
MultiAssociationAccessors
}
from
'./base'
;
/**
/**
* Options provided when associating models with hasMany relationship
* Options provided when associating models with hasMany relationship
*/
*/
export
interface
HasManyOptions
extends
ManyToManyOptions
{
export
interface
HasManyOptions
extends
ManyToManyOptions
{
/**
* The name of the field to use as the key for the association in the source table. Defaults to the primary
* key of the source table
*/
sourceKey
?:
string
;
/**
/**
* A string or a data type to represent the identifier in the table
* A string or a data type to represent the identifier in the table
*/
*/
...
...
types/lib/associations/has-one.d.ts
View file @
5a2c6a6
...
@@ -7,6 +7,13 @@ import { Association, AssociationOptions, SingleAssociationAccessors } from './b
...
@@ -7,6 +7,13 @@ import { Association, AssociationOptions, SingleAssociationAccessors } from './b
* Options provided when associating models with hasOne relationship
* Options provided when associating models with hasOne relationship
*/
*/
export
interface
HasOneOptions
extends
AssociationOptions
{
export
interface
HasOneOptions
extends
AssociationOptions
{
/**
* The name of the field to use as the key for the association in the source table. Defaults to the primary
* key of the source table
*/
sourceKey
?:
string
;
/**
/**
* A string or a data type to represent the identifier in the table
* A string or a data type to represent the identifier in the table
*/
*/
...
...
types/lib/query-interface.d.ts
View file @
5a2c6a6
...
@@ -56,6 +56,12 @@ export interface QueryOptions extends Logging, Transactionable {
...
@@ -56,6 +56,12 @@ export interface QueryOptions extends Logging, Transactionable {
*/
*/
instance
?:
Model
;
instance
?:
Model
;
/**
* Map returned fields to model's fields if `options.model` or `options.instance` is present.
* Mapping will occur before building the model instance.
*/
mapToModel
?:
boolean
;
retry
?:
RetryOptions
;
retry
?:
RetryOptions
;
}
}
...
...
types/test/e2e/docs-example.ts
View file @
5a2c6a6
// This file is used as example.
// This file is used as example.
import
{
Sequelize
,
Model
,
DataTypes
,
BuildOptions
}
from
'sequelize'
;
import
{
BuildOptions
,
DataTypes
,
Model
,
Sequelize
}
from
'sequelize'
;
import
{
HasManyGetAssociationsMixin
,
HasManyAddAssociationMixin
,
HasManyHasAssociationMixin
,
Association
,
HasManyCountAssociationsMixin
,
HasManyCreateAssociationMixin
}
from
'../../lib/associations'
;
import
{
Association
,
HasManyAddAssociationMixin
,
HasManyCountAssociationsMixin
,
HasManyCreateAssociationMixin
,
HasManyGetAssociationsMixin
,
HasManyHasAssociationMixin
}
from
'../../lib/associations'
;
import
QueryTypes
=
require
(
"../../lib/query-types"
);
class
User
extends
Model
{
class
User
extends
Model
{
public
id
!
:
number
;
// Note that the `null assertion` `!` is required in strict mode.
public
id
!
:
number
;
// Note that the `null assertion` `!` is required in strict mode.
...
@@ -42,6 +50,14 @@ class Project extends Model {
...
@@ -42,6 +50,14 @@ class Project extends Model {
public
readonly
updatedAt
!
:
Date
;
public
readonly
updatedAt
!
:
Date
;
}
}
class
Address
extends
Model
{
public
userId
!
:
number
;
public
address
!
:
string
;
public
readonly
createdAt
!
:
Date
;
public
readonly
updatedAt
!
:
Date
;
}
Project
.
init
({
Project
.
init
({
id
:
{
id
:
{
type
:
new
DataTypes
.
INTEGER
.
UNSIGNED
(),
// you can omit the `new` but this is discouraged
type
:
new
DataTypes
.
INTEGER
.
UNSIGNED
(),
// you can omit the `new` but this is discouraged
...
@@ -80,12 +96,29 @@ User.init({
...
@@ -80,12 +96,29 @@ User.init({
sequelize
:
sequelize
,
// this bit is important
sequelize
:
sequelize
,
// this bit is important
});
});
Address
.
init
({
userId
:
{
type
:
new
DataTypes
.
INTEGER
.
UNSIGNED
(),
},
address
:
{
type
:
new
DataTypes
.
STRING
(
128
),
allowNull
:
false
,
}
},
{
tableName
:
'users'
,
sequelize
:
sequelize
,
// this bit is important
});
// Here we associate which actually populates out pre-declared `association` static and other methods.
// Here we associate which actually populates out pre-declared `association` static and other methods.
User
.
hasMany
(
Project
,
{
User
.
hasMany
(
Project
,
{
sourceKey
:
'id'
,
foreignKey
:
'ownerId'
,
foreignKey
:
'ownerId'
,
as
:
'projects'
// this determines the name in `associations`!
as
:
'projects'
// this determines the name in `associations`!
});
});
Address
.
belongsTo
(
User
,
{
targetKey
:
'id'
});
User
.
hasOne
(
Address
,{
sourceKey
:
'id'
});
async
function
stuff
()
{
async
function
stuff
()
{
// Please note that when using async/await you lose the `bluebird` promise context
// Please note that when using async/await you lose the `bluebird` promise context
// and you fall back to native
// and you fall back to native
...
@@ -105,6 +138,15 @@ async function stuff() {
...
@@ -105,6 +138,15 @@ async function stuff() {
});
});
console
.
log
(
ourUser
.
projects
!
[
0
].
name
);
// Note the `!` null assertion since TS can't know if we included
console
.
log
(
ourUser
.
projects
!
[
0
].
name
);
// Note the `!` null assertion since TS can't know if we included
// the model or not
// the model or not
const
user
=
await
sequelize
.
query
(
'SELECT * FROM users WHERE name = :userName'
,{
type
:
QueryTypes
.
SELECT
,
replacements
:
{
userName
:
'Johnny'
},
mapToModel
:
true
,
model
:
User
})
}
}
// Legacy models
// Legacy models
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment