55 lines
1.9 KiB
JavaScript
55 lines
1.9 KiB
JavaScript
const { fakerLV : faker } = require('@faker-js/faker');
|
|
const { CATEGORIES } = require('./categories.js');
|
|
const { randomUUID } = require('crypto');
|
|
const PLACE_COUNT = 1000;
|
|
const BBOX_RIGA = [23.755875,56.815914,24.466553,57.067617];
|
|
const [ BBOX_MIN_LAT, BBOX_MIN_LON, BBOX_MAX_LAT, BBOX_MAX_LON ] = BBOX_RIGA;
|
|
|
|
/**
|
|
* @param { import("knex").Knex } knex
|
|
* @returns { Promise<void> }
|
|
*/
|
|
exports.seed = async function(knex) {
|
|
|
|
const createPlace = () => {
|
|
const coords = [
|
|
faker.location.latitude({ min: BBOX_MIN_LAT, max: BBOX_MAX_LAT }),
|
|
faker.location.longitude({ min: BBOX_MIN_LON, max: BBOX_MAX_LON })
|
|
];
|
|
|
|
return {
|
|
id: randomUUID(),
|
|
name: faker.company.name(),
|
|
coordinates: knex.raw('ST_SetSRID(ST_MakePoint(?, ?), 4326)', coords),
|
|
country: 'LV',
|
|
county: faker.helpers.arrayElement(['Kurzemes', 'Latgales', 'Rīgas', 'Vidzemes', 'Zemgales']),
|
|
district: faker.location.state(),
|
|
city: faker.location.city(),
|
|
street: faker.location.street(),
|
|
house: faker.helpers.maybe(() => faker.person.lastName(), { probability: 0.1 }) || faker.location.buildingNumber(),
|
|
postalCode: faker.location.zipCode(),
|
|
category_id: faker.helpers.arrayElement(CATEGORIES.map(c => c.id)),
|
|
}
|
|
};
|
|
|
|
const places = new Array(PLACE_COUNT).fill(() => createPlace()).map((fn, i) => fn(i));
|
|
|
|
const placeSubcategories = places.flatMap(place => {
|
|
const options = CATEGORIES.find(category => category.id === place.category_id).subcategories;
|
|
const subcategoryCount = faker.number.int({ min: 0, max: 4 });
|
|
const subcategories = new Set();
|
|
|
|
for (let i = 0; i <= subcategoryCount; i++) {
|
|
subcategories.add(faker.helpers.arrayElement(options));
|
|
}
|
|
|
|
return Array.from(subcategories).map(subcategory => ({
|
|
place_id: place.id,
|
|
subcategory_id: subcategory.id
|
|
}));
|
|
});
|
|
|
|
await knex('places').insert(places);
|
|
await knex('place_subcategories').insert(placeSubcategories);
|
|
};
|