54 lines
2.2 KiB
TypeScript
54 lines
2.2 KiB
TypeScript
import Database from 'better-sqlite3';
|
|
import { readFileSync } from 'fs';
|
|
import { join } from 'path';
|
|
|
|
/** 读取 ETL 的 schema.sql 并在给定 db 上建表 + 注入测试数据。*/
|
|
export function seedDatabase(db: Database.Database): void {
|
|
const schemaPath = join(
|
|
__dirname,
|
|
'..',
|
|
'..',
|
|
'..',
|
|
'..',
|
|
'app',
|
|
'etl',
|
|
'schema.sql',
|
|
);
|
|
db.exec(readFileSync(schemaPath, 'utf-8'));
|
|
|
|
const cat = db.prepare(
|
|
'INSERT INTO category(name, subcat) VALUES (?, ?)',
|
|
);
|
|
const elec = cat.run('电力机车', '').lastInsertRowid as number;
|
|
const emu = cat.run('动车组', '复兴号').lastInsertRowid as number;
|
|
const insp = cat.run('检测车', '普速检测').lastInsertRowid as number;
|
|
|
|
const model = db.prepare(
|
|
`INSERT INTO model(category_id, series, model_code, full_name, manufacturer,
|
|
country, country_type, first_year, last_year, status, usage,
|
|
max_speed_value, max_speed_unit, weight_value, weight_unit, raw_json)
|
|
VALUES (@category_id, @series, @model_code, @full_name, @manufacturer,
|
|
@country, @country_type, @first_year, @last_year, @status, @usage,
|
|
@max_speed_value, @max_speed_unit, @weight_value, @weight_unit, @raw_json)`,
|
|
);
|
|
model.run({
|
|
category_id: elec, series: '和谐', model_code: 'HXD1型', full_name: '',
|
|
manufacturer: '中车株洲', country: '中国', country_type: '国产',
|
|
first_year: 2006, last_year: null, status: '现役', usage: '干线货运',
|
|
max_speed_value: 120, max_speed_unit: 'km/h', weight_value: 150,
|
|
weight_unit: 't', raw_json: JSON.stringify({ model_code: 'HXD1型' }),
|
|
});
|
|
model.run({
|
|
category_id: emu, series: '', model_code: 'CR400AF', full_name: '复兴号',
|
|
manufacturer: '中车四方', country: '中国', country_type: '国产',
|
|
first_year: 2017, last_year: null, status: '现役', usage: '高速客运',
|
|
max_speed_value: 350, max_speed_unit: 'km/h', weight_value: null,
|
|
weight_unit: '', raw_json: JSON.stringify({ model_code: 'CR400AF' }),
|
|
});
|
|
|
|
db.prepare(
|
|
`INSERT INTO unit(category_id, car_number, model_name, function, depot, status)
|
|
VALUES (?, ?, ?, ?, ?, ?)`,
|
|
).run(insp, 'SYJZ-0001', '移动式线路动态加载试验车', '试验车', '国铁', '半封存');
|
|
}
|