Skip to content

Instantly share code, notes, and snippets.

@warderer
Last active April 4, 2025 03:34
Show Gist options
  • Select an option

  • Save warderer/c347bd174bc58ee79743aeea02fa609c to your computer and use it in GitHub Desktop.

Select an option

Save warderer/c347bd174bc58ee79743aeea02fa609c to your computer and use it in GitHub Desktop.
[Seeds Knex Homes y Users] Para ejemplo en clase de código de Knex con JS. #devf
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
* author: César Guerra
*/
exports.seed = async function (knex) {
// Deletes ALL existing entries
// Se necesita borrar las tablas en un orden específico para evitar errores de llave foránea. Y es necesario reiniciar la secuencia de las tablas para que los nuevos registros tengan un id consecutivo que empiece en 1 cada vez que se ejecuta el seed.
await knex.raw('TRUNCATE homes RESTART IDENTITY CASCADE')
await knex.raw('TRUNCATE users RESTART IDENTITY CASCADE')
// await knex('users').del()
await knex('users').insert([
{
name: 'Clark',
last_name: 'Kent',
email: 'clarkkent@dc.com',
phone: '1234567890',
description: 'The superhero known as Superman',
password: 'kryptonite'
},
{
name: 'Bruce',
last_name: 'Wayne',
email: 'brucewayne@dc.com',
phone: '2345678901',
description: 'The billionaire playboy who fights crime as Batman',
password: 'batsignal'
},
{
name: 'Diana',
last_name: 'Prince',
email: 'dianaprince@dc.com',
phone: '3456789012',
description: 'The Amazonian princess who is Wonder Woman',
password: 'lassooftruth'
},
{
name: 'Barry',
last_name: 'Allen',
email: 'barryallen@dc.com',
phone: '4567890123',
description: 'The fastest man alive, also known as The Flash',
password: 'speedforce'
},
{
name: 'Arthur',
last_name: 'Curry',
email: 'arthurcurry@dc.com',
phone: '5678901234',
description: 'The king of Atlantis and superhero Aquaman',
password: 'trident'
},
{
name: 'Hal',
last_name: 'Jordan',
email: 'haljordan@dc.com',
phone: '6789012345',
description: 'A member of the Green Lantern Corps',
password: 'powerring'
},
{
name: 'Peter',
last_name: 'Parker',
email: 'peterparker@marvel.com',
phone: '7890123456',
description: 'The friendly neighborhood Spider-Man',
password: 'spiderweb'
},
{
name: 'Steve',
last_name: 'Rogers',
email: 'steverogers@marvel.com',
phone: '8901234567',
description: 'The patriotic super-soldier Captain America',
password: 'shield'
},
{
name: 'Tony',
last_name: 'Stark',
email: 'tonystark@marvel.com',
phone: '9012345678',
description: 'The genius billionaire inventor who becomes Iron Man',
password: 'arcreactor'
},
{
name: 'Natasha',
last_name: 'Romanoff',
email: 'natasharomanoff@marvel.com',
phone: '0124567590',
description: 'The highly skilled spy and Avenger Black Widow',
password: 'redroom'
},
{
name: 'Thor',
last_name: 'Odinson',
email: 'thorodinson@marvel.com',
phone: '6534467890',
description: 'The Asgardian god of thunder and member of the Avengers',
password: 'mjolnir'
},
{
name: 'Bruce',
last_name: 'Banner',
email: 'brucebanner@marvel.com',
phone: '7345678901',
description: 'The brilliant scientist who transforms into the Hulk',
password: 'smash'
},
{
name: 'Scott',
last_name: 'Lang',
email: 'scottlang@marvel.com',
phone: '5456789012',
description: 'The thief turned hero who becomes Ant-Man',
password: 'shrinking'
},
{
name: 'Wade',
last_name: 'Wilson',
email: 'wadewilson@marvel.com',
phone: '4566797123',
description: 'The mercenary with accelerated healing known as Deadpool',
password: 'chimichangas'
},
{
name: 'Carol',
last_name: 'Danvers',
email: 'caroldanvers@marvel.com',
phone: '5677901234',
description: 'The former U.S. Air Force pilot who becomes the powerful Captain Marvel',
password: 'binary'
}
])
}
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
* author: César Guerra
*/
exports.seed = async function (knex) {
// Deletes ALL existing entries
await knex('homes').insert([
{
title: 'Cozy Cottage',
description: 'A charming cottage nestled in the countryside',
guests: 4,
address: '123 Main Street',
rental_price: 1500,
fk_user: 1
},
{
title: 'Luxury Villa',
description: 'Experience pure luxury in this stunning villa',
guests: 8,
address: '456 Elm Avenue',
rental_price: 3000,
fk_user: 4
},
{
title: 'Beachfront Bungalow',
description: 'Relax and unwind in this beachfront bungalow',
guests: 2,
address: '789 Ocean Drive',
rental_price: 2000,
fk_user: 5
},
{
title: 'Mountain Retreat',
description: 'Escape to the mountains in this peaceful retreat',
guests: 6,
address: '321 Pine Street',
rental_price: 1800,
fk_user: 14
},
{
title: 'City Loft',
description: 'Stay in the heart of the city in this modern loft',
guests: 2,
address: '555 Main Boulevard',
rental_price: 2500,
fk_user: 15
},
{
title: 'Countryside Farmhouse',
description: 'Experience country living in this rustic farmhouse',
guests: 10,
address: '777 Meadow Lane',
rental_price: 3500,
fk_user: 5
},
{
title: 'Lakefront Cabin',
description: 'Enjoy lake views in this cozy cabin',
guests: 4,
address: '999 Lakeview Drive',
rental_price: 1700,
fk_user: 2
},
{
title: 'Historic Townhouse',
description: 'Step back in time in this historic townhouse',
guests: 6,
address: '222 Heritage Street',
rental_price: 2000
},
{
title: 'Secluded Cottage',
description: 'Get away from it all in this secluded cottage',
guests: 2,
address: '444 Quiet Road',
rental_price: 1600,
fk_user: 9
},
{
title: 'Modern Apartment',
description: 'Experience urban living in this sleek apartment',
guests: 4,
address: '888 Downtown Avenue',
rental_price: 2200,
fk_user: 10
},
{
title: 'Beach House',
description: 'Enjoy sun, sand, and surf in this beach house',
guests: 8,
address: '111 Shoreline Drive',
rental_price: 2800,
fk_user: 11
},
{
title: 'Mountain Chalet',
description: 'Stay in a cozy chalet surrounded by nature',
guests: 6,
address: '555 Pine Avenue',
rental_price: 1900,
fk_user: 12
},
{
title: 'City Penthouse',
description: 'Live the high life in this luxurious penthouse',
guests: 2,
address: '777 Skyscraper Road',
rental_price: 3000,
fk_user: 13
},
{
title: 'Rustic Log Cabin',
description: 'Experience rustic charm in this log cabin',
guests: 4,
address: '999 Forest Lane',
rental_price: 1500,
fk_user: 1
},
{
title: 'Elegant Mansion',
description: 'Indulge in luxury in this grand mansion',
guests: 12,
address: '222 Grand Boulevard',
rental_price: 5000,
fk_user: 7
},
{
title: 'Lakeside Retreat',
description: 'Escape to the lake in this tranquil retreat',
guests: 6,
address: '444 Lakeview Drive',
rental_price: 2200,
fk_user: 4
},
{
title: 'Historic Cottage',
description: 'Stay in a piece of history in this charming cottage',
guests: 4,
address: '888 Heritage Street',
rental_price: 1800,
fk_user: 6
},
{
title: 'Modern Townhouse',
description: 'Experience contemporary living in this stylish townhouse',
guests: 6,
address: '111 Urban Lane',
rental_price: 2500,
fk_user: 8
},
{
title: 'Mountain View Cabin',
description: 'Enjoy breathtaking mountain views in this cozy cabin',
guests: 2,
address: '555 Vista Avenue',
rental_price: 1600,
fk_user: 3
},
{
title: 'Beachfront Condo',
description: 'Wake up to stunning ocean views in this beachfront condo',
guests: 4,
address: '777 Beach Drive',
rental_price: 2000,
fk_user: 1
},
{
title: 'Charming Cottage',
description: 'Experience charm and comfort in this lovely cottage',
guests: 2,
address: '999 Cozy Road',
rental_price: 1400,
fk_user: 1
},
{
title: 'Luxury Apartment',
description: 'Indulge in luxury in this high-end apartment',
guests: 4,
address: '222 Luxury Avenue',
rental_price: 2800,
fk_user: 4
},
{
title: 'Riverside Retreat',
description: 'Escape to the river in this peaceful retreat',
guests: 6,
address: '444 Riverfront Lane',
rental_price: 2100,
fk_user: 7
},
{
title: 'Historic Farmhouse',
description: 'Step back in time in this historic farmhouse',
guests: 8,
address: '888 Farm Road',
rental_price: 3200,
fk_user: 9
},
{
title: 'City View Loft',
description: 'Enjoy panoramic city views in this modern loft',
guests: 2,
address: '111 Skyline Boulevard',
rental_price: 2400,
fk_user: 11
},
{
title: 'Secluded Cabin',
description: 'Get away from it all in this secluded cabin',
guests: 4,
address: '555 Wilderness Drive',
rental_price: 1700,
fk_user: 14
},
{
title: 'Mountain Lodge',
description: 'Stay in a cozy lodge surrounded by nature',
guests: 10,
address: '777 Mountain Lane',
rental_price: 3800,
fk_user: 14
},
{
title: 'Lake House',
description: 'Experience lakeside living in this charming house',
guests: 6,
address: '999 Lakeview Drive',
rental_price: 2000,
fk_user: 15
},
{
title: 'Historic Townhome',
description: 'Step back in time in this historic townhome',
guests: 6,
address: '222 Heritage Street',
rental_price: 2200,
fk_user: 12
},
{
title: 'Seaside Cottage',
description: 'Enjoy the seaside in this cozy cottage',
guests: 2,
address: '444 Oceanfront Road',
rental_price: 1800,
fk_user: 6
}
])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment