AppReports/seeds/fake.js

48 lines
1.0 KiB
JavaScript
Raw Normal View History

2020-08-19 12:33:09 +02:00
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of appreports which is released under GPL-3.0-or-later.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, August 2020
*/
2020-07-29 20:56:03 +02:00
'use strict';
const faker = require ('faker');
let last_t = 0;
let last_h = 0;
function create_log (timestamp) {
2020-08-24 21:36:33 +02:00
last_t = faker.random.number (2) + 22;
last_h = faker.random.number (10) + 38;
2020-07-30 18:39:37 +02:00
const data = {
light: faker.random.number (10),
temperature: last_t,
humidity: last_h
2020-07-30 18:39:37 +02:00
};
2020-07-29 20:56:03 +02:00
return {
2020-08-25 15:24:20 +02:00
app_id: 1,
2020-07-29 20:56:03 +02:00
message: faker.random.words (),
2020-07-30 18:39:37 +02:00
data: JSON.stringify (data),
timestamp
2020-07-29 20:56:03 +02:00
};
}
async function seed (knex) {
2020-08-25 15:24:20 +02:00
await knex ('log')
.del ();
2020-08-15 15:19:52 +02:00
// eslint-disable-next-line no-console
2020-07-29 20:56:03 +02:00
console.log ('creating seeds');
2020-08-16 11:48:06 +02:00
2020-08-25 15:24:20 +02:00
const log = (Array (10000))
2020-07-30 18:39:37 +02:00
.fill (() => null)
2020-08-25 15:24:20 +02:00
.map (() => faker.date.recent (60))
.sort ()
.map ((t) => create_log (t));
2020-07-30 18:39:37 +02:00
await knex.batchInsert ('log', log, 10);
2020-07-29 20:56:03 +02:00
}
module.exports = { seed };