Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Sequelize from 'sequelize';
- import _ from 'lodash';
- import Faker from 'faker';
- const Conn = new Sequelize(
- 'elzzleDB',
- 'root',
- 'password',
- {
- dialect: 'mysql',
- host: 'localhost',
- port: 3306
- }
- );
- //start creating objects here
- const Person = Conn.define('person', {
- firstName: {
- type: Sequelize.STRING,
- allowNull: false
- },
- lastName: {
- type: Sequelize.STRING,
- allowNull: false
- },
- email: {
- type: Sequelize.STRING,
- allowNull: false,
- validate: {
- isEmail: true
- }
- }
- });
- const Listing = Conn.define('listing', {
- thumbnailUrl: {
- type: Sequelize.STRING,
- allowNull: false
- },
- title: {
- type: Sequelize.STRING,
- allowNull: false
- },
- description: {
- type: Sequelize.TEXT("medium"),
- allowNull: false
- },
- allowRent: {
- type: Sequelize.BOOLEAN,
- allowNull: false
- },
- allowBuy: {
- type: Sequelize.BOOLEAN,
- allowNull: false
- },
- rentPrice: {
- type: Sequelize.INTEGER,
- allowNull: true,
- },
- buyPrice: {
- type: Sequelize.INTEGER,
- allowNull: true
- }
- });
- //Relationships
- Person.hasMany(Listing);
- Listing.belongsTo(Person);
- //WARNING: Will forcefully override tables in db
- Conn.sync({force: true}).then(() => {
- _.times(10, () => {
- var row = Person.create({
- firstName: Faker.name.firstName(),
- lastName: Faker.name.lastName(),
- email: Faker.internet.email()
- }).then(person => {
- let price = Faker.random.number(100);
- return _.times(10, () => {
- person.createListing({
- thumbnailUrl: Faker.image.imageUrl(),
- title: Faker.commerce.productName(),
- description: Faker.lorem.sentences(),
- allowRent: Faker.random.boolean(),
- allowBuy: Faker.random.boolean(),
- rentPrice: price,
- buyPrice: price * 10 + Faker.random.number(100)
- })
- });
- });
- });
- });
- export default Conn;
Add Comment
Please, Sign In to add comment