PageSourceSearch

https://sequelize.org/assets/js/945d739e.88926ab9.js

js sequelize.org collected 2026-09-24 08:28:22 UTC 5,905 bytes, 1 lines download raw bytes

1"use strict";(globalThis.webpackChunk=globalThis.webpackChunk||[]).push([[4131],{70100(e,o,s){s.r(o),s.d(o,{assets:()=>c,contentTitle:()=>d,default:()=>p,frontMatter:()=>r,metadata:()=>n,toc:()=>a});const n=JSON.parse('{"id":"moved/models-definition","title":"Models Definition","description":"The contents of this page were moved to Model Basics.","source":"@site/versioned_docs/version-6.x.x/moved/models-definition.md","sourceDirName":"moved","slug":"/moved/models-definition","permalink":"/docs/v6/moved/models-definition","draft":false,"unlisted":false,"editUrl":"https://github.com/sequelize/website/tree/main/versioned_docs/version-6.x.x/moved/models-definition.md","tags":[],"version":"6.x.x","lastUpdatedBy":"renovate[bot]","lastUpdatedAt":1775795365000,"frontMatter":{"title":"Models Definition"},"sidebar":"tutorialSidebar","previous":{"title":"Data Types","permalink":"/docs/v6/moved/data-types"},"next":{"title":"Models Usage","permalink":"/docs/v6/moved/models-usage"}}');var t=s(74848),i=s(28453);const r={title:"Models Definition"},d=void 0,c={},a=[{value:"Deprecated: <code>sequelize.import</code>",id:"deprecated-sequelizeimport",level:2}];function l(e){const o={a:"a",blockquote:"blockquote",code:"code",em:"em",h2:"h2",hr:"hr",p:"p",pre:"pre",strong:"strong",...(0,i.R)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(o.p,{children:["The contents of this page were moved to ",(0,t.jsx)(o.a,{href:"/docs/v6/core-concepts/model-basics",children:"Model Basics"}),"."]}),"\n",(0,t.jsxs)(o.p,{children:["The only exception is the guide on ",(0,t.jsx)(o.code,{children:"sequelize.import"}),", which is deprecated and was removed from the docs. However, if you really need it, it was kept here."]}),"\n",(0,t.jsx)(o.hr,{}),"\n",(0,t.jsxs)(o.h2,{id:"deprecated-sequelizeimport",children:["Deprecated: ",(0,t.jsx)(o.code,{children:"sequelize.import"})]}),"\n",(0,t.jsxs)(o.blockquote,{children:["\n",(0,t.jsx)(o.p,{children:(0,t.jsxs)(o.em,{children:[(0,t.jsx)(o.strong,{children:"Note:"})," You should not use ",(0,t.jsx)(o.code,{children:"sequelize.import"}),". Please just use ",(0,t.jsx)(o.a,{href:"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import",children:(0,t.jsx)(o.code,{children:"import"})}),", ",(0,t.jsx)(o.a,{href:"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#dynamic_imports",children:(0,t.jsx)(o.code,{children:"import()"})}),", or ",(0,t.jsx)(o.a,{href:"https://nodejs.org/api/modules.html#requireid",children:(0,t.jsx)(o.code,{children:"require"})})," instead."]})}),"\n",(0,t.jsx)(o.p,{children:(0,t.jsx)(o.em,{children:"This documentation has been kept just in case you really need to maintain old code that uses it."})}),"\n"]}),"\n",(0,t.jsxs)(o.p,{children:[(0,t.jsx)(o.code,{children:"sequelize.import"})," can only load ",(0,t.jsx)(o.a,{href:"https://nodejs.org/api/modules.html",children:"CommonJS"})," files, and is not capable of loading ",(0,t.jsx)(o.a,{href:"https://nodejs.org/api/esm.html",children:(0,t.jsx)(o.code,{children:"ecmascript modules"})}),". Use native ",(0,t.jsx)(o.code,{children:"import"})," if you need to load ecmascript modules."]}),"\n",(0,t.jsxs)(o.p,{children:["You can store your model definitions in a single file using the ",(0,t.jsx)(o.code,{children:"sequelize.import"})," method. The returned object is exactly the same as defined in the imported file's function. The import is cached, just like ",(0,t.jsx)(o.code,{children:"require"}),", so you won't run into trouble if importing a file more than once."]}),"\n",(0,t.jsx)(o.pre,{children:(0,t.jsx)(o.code,{className:"language-js",children:"// in your server file - e.g. app.js\nconst Project = sequelize.import(__dirname + '/path/to/models/project');\n\n// The model definition is done in /path/to/models/project.js\nmodule.exports = (sequelize, DataTypes) => {\n  return sequelize.define('project', {\n    name: DataTypes.STRING,\n    description: DataTypes.TEXT,\n  });\n};\n"})}),"\n",(0,t.jsxs)(o.p,{children:["The ",(0,t.jsx)(o.code,{children:"import"})," method can also accept a callback as an argument."]}),"\n",(0,t.jsx)(o.pre,{children:(0,t.jsx)(o.code,{className:"language-js",children:"sequelize.import('project', (sequelize, DataTypes) => {\n  return sequelize.define('project', {\n    name: DataTypes.STRING,\n    description: DataTypes.TEXT,\n  });\n});\n"})}),"\n",(0,t.jsxs)(o.p,{children:["This extra capability is useful when, for example, ",(0,t.jsx)(o.code,{children:"Error: Cannot find module"})," is thrown even though ",(0,t.jsx)(o.code,{children:"/path/to/models/project"})," seems to be correct. Some frameworks, such as Meteor, overload ",(0,t.jsx)(o.code,{children:"require"}),", and might raise an error such as:"]}),"\n",(0,t.jsx)(o.pre,{children:(0,t.jsx)(o.code,{className:"language-text",children:"Error: Cannot find module '/home/you/meteorAp
1p/.meteor/local/build/programs/server/app/path/to/models/project.js'\n"})}),"\n",(0,t.jsxs)(o.p,{children:["This can be worked around by passing in Meteor's version of ",(0,t.jsx)(o.code,{children:"require"}),":"]}),"\n",(0,t.jsx)(o.pre,{children:(0,t.jsx)(o.code,{className:"language-js",children:"// If this fails...\nconst AuthorModel = db.import('./path/to/models/project');\n\n// Try this instead!\nconst AuthorModel = db.import('project', require('./path/to/models/project'));\n"})})]})}function p(e={}){const{wrapper:o}={...(0,i.R)(),...e.components};return o?(0,t.jsx)(o,{...e,children:(0,t.jsx)(l,{...e})}):l(e)}},28453(e,o,s){s.d(o,{R:()=>r,x:()=>d});var n=s(96540);const t={},i=n.createContext(t);function r(e){const o=n.useContext(i);return n.useMemo(function(){return"function"==typeof e?e(o):{...o,...e}},[o,e])}function d(e){let o;return o=e.disableParentContext?"function"==typeof e.components?e.components(t):e.components||t:r(e.components),n.createElement(i.Provider,{value:o},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.