Skip to content

Instantly share code, notes, and snippets.

@florianherrengt
Last active April 30, 2019 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save florianherrengt/1b624381efce948a68cac95c25058a3d to your computer and use it in GitHub Desktop.
Save florianherrengt/1b624381efce948a68cac95c25058a3d to your computer and use it in GitHub Desktop.
Sequelize existing database fields mapping
/*
How to map fields from an existing database to a Sequelize model with:
- @Table({ tableName: '...' })
- @Column({ field: '...' })
*/
import {
Sequelize,
Table,
Model,
Column,
PrimaryKey,
AllowNull,
IsInt,
IsUUID,
DataType,
Default
} from "sequelize-typescript";
@Table({ tableName: "QWFPG" })
class CustomerModel extends Model<CustomerModel> {
@PrimaryKey
@IsUUID(4)
@Default(DataType.UUIDV4)
@Column
id: string;
@IsInt
@AllowNull(false)
@Column({ field: "ARSTD" })
soldTo: number;
@AllowNull(false)
@Column({ field: "ZXCVB" })
name: string;
}
const sequelize = new Sequelize({
logging: false,
dialect: "sqlite",
storage: "./database.sqlite"
});
sequelize.addModels([CustomerModel]);
sequelize.sync().then(async () => {
console.log("model synced");
const customer = new CustomerModel({ name: "test", soldTo: 123 });
await customer.save();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment