You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -1043,8 +1102,8 @@ All defined tables will be dropped and recreated.
1043
1102
A schema is defined by a JavaScript object with certain properties. For `mysql-plus`, the schema properties can be broken down into four main types:
1044
1103
1045
1104
+[Columns](#columns)
1105
+
+[Primary Key](#primary-key)
1046
1106
+[Keys](#keys)
1047
-
+[Foreign Keys](#foreign-keys)
1048
1107
+[Table Options](#table-options)
1049
1108
1050
1109
### Columns
@@ -1064,146 +1123,56 @@ Columns are defined using the `column` property which is an object where the key
1064
1123
1065
1124
See the [Column Types](#column-types) section for all possible column types and attributes that can be defined.
1066
1125
1067
-
### Keys
1068
-
1069
-
The following properties can be used to define different types of keys:
1070
-
1071
-
+[`primaryKey`](#primarykey--stringstring)
1072
-
+[`uniqueKeys`](#uniquekeys--arraystringstring)
1073
-
+[`indexes`](#indexes--arraystringstring)
1074
-
+[`spatialIndexes`](#spatialindexes--string)
1075
-
1076
-
Note that [column definitions](#columndefinition) allow these keys to be defined directly on the column. If that method of defining a key for a column is used, the key should not be defined again using one of these properties (otherwise it will be duplicated).
1077
-
1078
-
#### `primaryKey` : `string|string[]`
1079
-
1080
-
Used to define the table's primary key. Its value is the name of one or more columns that make up the primary key.
1081
-
1082
-
**Example:**
1083
-
```js
1084
-
// Single column primary key
1085
-
{
1086
-
primaryKey:'id',
1087
-
}
1088
-
1089
-
// Multi-column primary key
1090
-
{
1091
-
primaryKey: ['userID', 'videoID'],
1092
-
}
1093
-
```
1126
+
### Primary Key
1094
1127
1095
-
#### `uniqueKeys` : `Array.<string|string[]>`
1128
+
`string|string[]`
1096
1129
1097
-
Used to define the table's unique keys. Its value is an array where the elements are the names of one or more columns that make up a unique key.
1130
+
The table’s primary key can be defined with the `primaryKey` property:
1098
1131
1099
-
**Example:**
1100
1132
```js
1101
1133
{
1102
-
uniqueKeys: [
1103
-
'email', // Single column unique key
1104
-
['a', 'b'], // Multi-column unique key
1105
-
],
1134
+
columns: {
1135
+
id:pool.ColTypes.int().unsigned().notNull(),
1136
+
name:pool.ColTypes.varchar(255).notNull(),
1137
+
},
1138
+
primaryKey:'id'
1106
1139
}
1107
1140
```
1108
1141
1109
-
#### `indexes` : `Array.<string|string[]>`
1110
-
1111
-
Used to define the table's indexes. Its value is an array where the elements are the names of one or more columns that make up an index.
1142
+
An array can be used to define a multi-column primary key.
1112
1143
1113
-
**Example:**
1114
1144
```js
1115
1145
{
1116
-
indexes: [
1117
-
'points', // Single column index
1118
-
['a', 'b'], // Multi-column index
1119
-
],
1120
-
}
1121
-
```
1122
-
1123
-
#### `spatialIndexes` : `string[]`
1124
-
1125
-
Used to define the table's spatial indexes. Its value is an array where the elements are the column name for each index.
1126
-
1127
-
Note that spatial indexes may each only have 1 column and they may only be defined for [geometry-type](https://dev.mysql.com/doc/refman/5.7/en/spatial-type-overview.html) columns.
1128
-
1129
-
**Example:**
1130
-
```js
1131
-
{
1132
-
spatialIndexes: [
1133
-
'coordinates',
1134
-
'line',
1135
-
],
1146
+
columns: {
1147
+
id:pool.ColTypes.int().unsigned().notNull(),
1148
+
name:pool.ColTypes.varchar(255).notNull(),
1149
+
},
1150
+
primaryKey: ['id', 'name']
1136
1151
}
1137
1152
```
1138
1153
1139
-
### Foreign Keys
1140
-
1141
-
Foreign keys are defined using the `foreignKeys` property, which is an object that maps column names to a reference table column. The reference table column can be specified with either an object or a shorthand string.
1142
-
1143
-
If an object, it should have the following properties:
1144
-
1145
-
+`table` - The name of the reference table.
1146
-
+`column` - The name of the reference column in the reference table.
**Note:** Foreign key definitions don't define keys, but [_constraints_](https://dev.mysql.com/doc/refman/5.7/en/glossary.html#glos_foreign_key_constraint). When defining foreign key constraints, the columns that make up the constraints should also be keys.
1193
-
1194
-
Keys required for the example above:
1195
-
```js
1196
-
{
1197
-
primaryKey:'id',
1198
-
uniqueKeys: [
1199
-
'uid',
1200
-
'userID',
1201
-
],
1202
-
indexes: [
1203
-
['thingOne', 'thingTwo'],
1204
-
],
1205
-
}
1206
-
```
1175
+
See the [Key Types](#key-types) section for information on the different types of keys that can be defined.
1207
1176
1208
1177
### Table Options
1209
1178
@@ -1418,3 +1387,84 @@ Compatible types:
1418
1387
+`multilinestring`
1419
1388
+`multipolygon`
1420
1389
+`geometrycollection`
1390
+
1391
+
## Key Types
1392
+
1393
+
[`mysql.KeyTypes`](#module_mysql-plus..KeyTypes) and [`pool.KeyTypes`](#PoolPlus+KeyTypes) both expose the following methods for defining table keys:
1394
+
1395
+
+`index(columnName [, ...otherColumns])` - Creates a regular [index](https://dev.mysql.com/doc/en/create-index.html)
1396
+
+`uniqueIndex(columnName [, ...otherColumns])` - Creates a [unique index](https://dev.mysql.com/doc/en/create-index.html#create-index-unique)
1397
+
+`spatialIndex(columnName)` - Creates a [spatial index](https://dev.mysql.com/doc/en/create-index.html#create-index-spatial)
1398
+
+`foreignKey(columnName [, ...otherColumns])` - Creates a [foreign key constraint](https://dev.mysql.com/doc/en/create-table-foreign-keys.html)
All key types have a `name` method that can be used to customize the key’s name (helpful if you need to use an [index hint](https://dev.mysql.com/doc/en/index-hints.html) in a query):
**Note:** Foreign keys don't define indexes, but [_constraints_](https://dev.mysql.com/doc/en/glossary.html#glos_foreign_key_constraint). When defining foreign keys, the columns used in the key should also have an index.
0 commit comments