generated from tailored/router-template
41 lines
1.0 KiB
TypeScript
41 lines
1.0 KiB
TypeScript
import { Sequelize, Model, DataTypes, InferAttributes, InferCreationAttributes } from 'sequelize';
|
|
import path from 'path';
|
|
const dbPath = path.join(process.cwd(), './test2.db');
|
|
const sequelize = new Sequelize({
|
|
dialect: 'sqlite',
|
|
storage: dbPath,
|
|
});
|
|
console.log('dbPath', dbPath);
|
|
sequelize
|
|
.authenticate()
|
|
.then(() => {
|
|
console.log('Connection has been established successfully.');
|
|
})
|
|
.catch((err) => {
|
|
console.error('Unable to connect to the database:', err);
|
|
});
|
|
// Define a User model
|
|
class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
|
|
declare username: string | null;
|
|
declare birthday: Date | null;
|
|
}
|
|
|
|
User.init(
|
|
{
|
|
username: DataTypes.STRING,
|
|
birthday: DataTypes.DATE,
|
|
},
|
|
{ sequelize, modelName: 'user' },
|
|
);
|
|
|
|
(async () => {
|
|
await sequelize.sync({ alter: true });
|
|
const jane = await User.create({
|
|
username: 'janedoe',
|
|
birthday: new Date(1980, 6, 20),
|
|
});
|
|
console.log(jane.toJSON());
|
|
const users = await User.findAll();
|
|
console.log(users.map((item) => item.toJSON()));
|
|
})();
|