@ptenn/sequelize
v0.0.2
Published
## Installation
Downloads
3
Readme
@ptenn/sequelize
Installation
yarn add @ptenn/sequelize
Usage
Creating the Connection
// connection.ts
import { Connection } from '@ptenn/sequelize';
const connection = new Connection({
dialect: 'sqlite',
storage: ':memory:',
logging: false,
});
export { connection };
Creating Models
import { ModelAttributes } from './model';
import { CreationOptional, DATE, Sequelize, STRING } from 'sequelize';
export interface UserAttributes extends ModelAttributes {
id: CreationOptional<number>;
username: string;
password: string;
createdAt: CreationOptional<Date>;
updatedAt: CreationOptional<Date>;
}
export class UserModel extends Model<UserAttributes> {
constructor(sequelize: Sequelize) {
super(sequelize, 'users', {
username: {
type: STRING,
unique: true,
allowNull: false,
},
password: {
type: STRING,
allowNull: false,
validate: {
len: [8, 255],
},
},
createdAt: DATE,
updatedAt: DATE,
});
}
}
Setting up the database
import { Database } from '@ptenn/sequelize';
import { connection } from './connection';
import { UserModel } from './models/user';
type DatabaseShape = {
users: User,
};
const database = new Database<DatabaseShape>(connection);
database.loadThenSync((sequelize, defineModel) => {
/* eslint-disable @typescript-eslint/no-unused-vars */
const userModel = defineModel<UserModel>(new UserModel(sequelize));
/* eslint-enable @typescript-eslint/no-unused-vars */
});
export { database };