How to use the DataTypes function from sequelize

Find comprehensive JavaScript sequelize.DataTypes code examples handpicked from public code repositorys.

sequelize.DataTypes is an object that contains all the built-in data types that can be used in a Sequelize model definition.

38
39
40
41
42
43
44
45
46
47

await sequelizeClient.sync();

const User = sequelizeClient.define('user', {
    id: {
        type: Sequelize.DataTypes.UUID,
        defaultValue: Sequelize.DataTypes.UUIDV1,
        allowNull: false,
        primaryKey: true
    },
fork icon219
star icon593
watch icon38

+ 15 other calls in file

65
66
67
68
69
70
71
72
73
74
ul_status: {
    type: Sequelize.DataTypes.STRING,
    defaultValue: FILE_UPLOAD_STATUS.NOT_STARTED
},
dl_progress: {type: Sequelize.DataTypes.FLOAT, allowNull: false, defaultValue: 0},
ul_progress: {type: Sequelize.DataTypes.FLOAT, allowNull: false, defaultValue: 0},
expires: {type: Sequelize.DataTypes.BIGINT, allowNull: true},
dir_ul_progress: {type: Sequelize.DataTypes.FLOAT, allowNull: false, defaultValue: 0},
dir_ul_status: {
    type: Sequelize.DataTypes.STRING,
fork icon22
star icon74
watch icon9

+ 19 other calls in file

How does sequelize.DataTypes work?

sequelize.DataTypes is an object that contains all the built-in data types that can be used in a Sequelize model definition. These data types are used to define the schema of a database table and to create columns with specific data types.

The sequelize.DataTypes object provides a simple way to define data types for Sequelize models. It includes data types such as STRING, INTEGER, FLOAT, BOOLEAN, DATE, JSON, ENUM, ARRAY, and more. When defining a Sequelize model, you can specify the data type of each column by accessing the relevant data type from the sequelize.DataTypes object.

For example, to define a column named name with a STRING data type, you would use:

javascript
const { DataTypes } = require('sequelize'); const User = sequelize.define('User', { name: { type: DataTypes.STRING } });

In this example, we're requiring the DataTypes object from the sequelize package, and using it to define the name column with a STRING data type. Sequelize will then use this information to create a VARCHAR column in the database.

In addition to the built-in data types provided by sequelize.DataTypes, you can also define custom data types by extending existing data types or by creating entirely new ones using the sequelize.define method.

Overall, sequelize.DataTypes is a convenient and powerful way to define the schema of a database table with Sequelize.

13
14
15
16
17
18
19
20
21
22
}

FileMap.init(
    {
        id: {type: Sequelize.DataTypes.BIGINT, autoIncrement: true, unique: true, primaryKey: true},
        file_id: {type: Sequelize.DataTypes.STRING, allowNull: false},
        chunk_id: {type: Sequelize.DataTypes.STRING, allowNull: false},
        offset: {type: Sequelize.DataTypes.BIGINT, allowNull: false}
    },
    {
fork icon22
star icon73
watch icon9

+ 39 other calls in file

2
3
4
5
6
7
8
9
10
const Sequelize = require('sequelize');
const sequelize = require('.');

const Downtime = sequelize.define('downtime', {
    start: Sequelize.DataTypes.DATE,
    end: Sequelize.DataTypes.DATE,

    instance: {
        type: Sequelize.DataTypes.BIGINT,
fork icon20
star icon208
watch icon12

+ 5 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const { Sequelize, DataTypes } = require("sequelize");

const sequelize = new Sequelize("database", "username", "password", {
  dialect: "sqlite",
  storage: "./database.sqlite",
});

const User = sequelize.define("User", {
  name: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  email: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true,
  },
  age: {
    type: DataTypes.INTEGER,
    allowNull: true,
  },
  is_admin: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: false,
  },
});

(async () => {
  await sequelize.sync({ force: true });
  console.log("User model synced successfully");
})();

In this example, we're requiring Sequelize and DataTypes from the sequelize package. We're then creating a new sequelize instance, and using it to define a User model with four columns: name, email, age, and is_admin. For each column, we're specifying the data type by accessing it from the DataTypes object provided by Sequelize. We're also setting various options such as allowNull, unique, and defaultValue to define the column constraints. Finally, we're using the sequelize.sync method to synchronize the model with the database, and logging a success message when the sync is complete. By using sequelize.DataTypes, we've defined the schema of a database table with Sequelize. We've used the various data types provided by sequelize.DataTypes to specify the data type of each column, and set various constraints to ensure the integrity of the data. We've also used the sequelize.sync method to create the table in the database.

11
12
13
14
15
16
17
18
19
20
const reverseSequelizeColType = function(col, prefix = 'Sequelize.') 
{
    const attrName = col['type'].key
    const attrObj = col.type
    const options = (col['type']['options']) ? col['type']['options'] : {}
    const DataTypes = Sequelize.DataTypes

    switch (attrName) {
        // CHAR(length, binary)
        case DataTypes.CHAR.key:
fork icon152
star icon37
watch icon2

0
1
2
3
4
5
6
7
8
9
const Sequelize = require('sequelize')

module.exports = (sequelize) => {
  const event = sequelize.define('event', {
    id: {
      type: Sequelize.DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    title: {
fork icon1
star icon4
watch icon1

+ 13 other calls in file

45
46
47
48
49
50
51
52
53
    file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js"
)
.forEach((file) => {
  const model = require(path.join(__dirname, file))(
    sequelize,
    Sequelize.DataTypes
  );
  db[model.name] = model;
});
fork icon0
star icon0
watch icon1

+ 14 other calls in file