Skip to main content

Neon SDK (Sequelize)

Babel Neon Element uses Sequelize as the default ORM framework. Sequelize is a Node.js ORM that supports various databases such as Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server. Through this article, you will learn how to interact with the Neon database using Sequelize SDK and how to read or store data.

NOTE: For information on how to create Neon Element, please refer to Neon Element

Usage

If you want to use the Neon SDK in your Babel application, you first need to import the dependency of the corresponding Neon Element in the Element. The default export of the Neon Element is the Sequelize object, which also re-exports all exports of the sequelize package:

import myNeon from "#elements/myNeon";

console.log(myNeon) // output: "Sequelize"

Examples

Raw Queries

Here's an example of a native query using the Neon SDK, for more specific usage, refer to the Sequelize Raw Queries section:

import myNeon from "#elements/myNeon";

export default async function () {
return await myNeon.query('SELECT * FROM User')
}

Model Querying

Here's an example of a Model query using the Neon SDK. The database table being operated on is User, which has two fields: id and name. For more specific usage, refer to the Sequelize Model Querying and other sections:

import myNeon, { DataTypes } from "#elements/myNeon";

export default async function () {
const User = myNeon.define('User', {
// Model attributes are defined here
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
name: {
type: DataTypes.STRING
// allowNull defaults to true
}
}, {
// Other model options go here
tableName: 'User',
createdAt: false,
updatedAt: false
});
await User.create({
id: 2,
name: "user-2"
})
return await User.findAll()
}

Reference