Change endpoint from persons to people

This commit is contained in:
xfarrow
2025-03-23 21:00:08 +01:00
parent 4ae263662c
commit d005193f63
7158 changed files with 700476 additions and 735 deletions

View File

@ -0,0 +1,137 @@
// Seeder
// -------
const path = require('path');
const { ensureDirectoryExists } = require('../util/fs');
const { writeJsFileUsingTemplate } = require('../util/template');
const { yyyymmddhhmmss } = require('../util/timestamp');
const { getMergedConfig } = require('./seeder-configuration-merger');
// The new seeds we're performing, typically called from the `knex.seed`
// interface on the main `knex` object. Passes the `knex` instance performing
// the seeds.
class Seeder {
constructor(knex) {
this.knex = knex;
this.config = this.resolveConfig(knex.client.config.seeds);
}
// Runs seed files for the given environment.
async run(config) {
this.config = this.resolveConfig(config);
const files = await this.config.seedSource.getSeeds(this.config);
return this._runSeeds(files);
}
// Creates a new seed file, with a given name.
async make(name, config) {
this.config = this.resolveConfig(config);
if (!name)
throw new Error('A name must be specified for the generated seed');
await this._ensureFolder(config);
const seedPath = await this._writeNewSeed(name);
return seedPath;
}
// Ensures a folder for the seeds exist, dependent on the
// seed config settings.
_ensureFolder() {
const dirs = this.config.seedSource._getConfigDirectories(
this.config.logger
);
const promises = dirs.map(ensureDirectoryExists);
return Promise.all(promises);
}
// Run seed files, in sequence.
async _runSeeds(seeds) {
for (const seed of seeds) {
await this._validateSeedStructure(seed);
}
return this._waterfallBatch(seeds);
}
async _validateSeedStructure(filepath) {
const seed = await this.config.seedSource.getSeed(filepath);
if (typeof seed.seed !== 'function') {
throw new Error(
`Invalid seed file: ${filepath} must have a seed function`
);
}
return filepath;
}
_getStubPath() {
return (
this.config.stub ||
path.join(__dirname, 'stub', this.config.extension + '.stub')
);
}
_getNewStubFileName(name) {
if (name[0] === '-') name = name.slice(1);
if (this.config.timestampFilenamePrefix === true) {
name = `${yyyymmddhhmmss()}_${name}`;
}
return `${name}.${this.config.extension}`;
}
_getNewStubFilePath(name) {
const fileName = this._getNewStubFileName(name);
const dirs = this.config.seedSource._getConfigDirectories(
this.config.logger
);
const dir = dirs.slice(-1)[0]; // Get last specified directory
return path.join(dir, fileName);
}
// Write a new seed to disk, using the config and generated filename,
// passing any `variables` given in the config to the template.
async _writeNewSeed(name) {
const seedPath = this._getNewStubFilePath(name);
await writeJsFileUsingTemplate(
seedPath,
this._getStubPath(),
{ variable: 'd' },
this.config.variables || {}
);
return seedPath;
}
async _listAll(config) {
this.config = this.resolveConfig(config);
return this.config.seedSource.getSeeds(this.config);
}
// Runs a batch of seed files.
async _waterfallBatch(seeds) {
const { knex } = this;
const log = [];
for (const seedPath of seeds) {
const seed = await this.config.seedSource.getSeed(seedPath);
try {
await seed.seed(knex);
log.push(seedPath);
} catch (originalError) {
const error = new Error(
`Error while executing "${seedPath}" seed: ${originalError.message}`
);
error.original = originalError;
error.stack =
error.stack.split('\n').slice(0, 2).join('\n') +
'\n' +
originalError.stack;
throw error;
}
}
return [log];
}
resolveConfig(config) {
return getMergedConfig(config, this.config, this.knex.client.logger);
}
}
module.exports = Seeder;

View File

@ -0,0 +1,13 @@
// Stub Seed:
// Used for now in browser builds, where filesystem access isn't
// available.
const StubSeed = (module.exports = function () {});
const noSuchMethod = async function () {
throw new Error('Seeds are not supported');
};
StubSeed.prototype = {
make: noSuchMethod,
run: noSuchMethod,
};

View File

@ -0,0 +1,60 @@
const { FsSeeds } = require('./sources/fs-seeds');
const Logger = require('../../logger');
const { DEFAULT_LOAD_EXTENSIONS } = require('../common/MigrationsLoader');
const defaultLogger = new Logger();
const CONFIG_DEFAULT = Object.freeze({
extension: 'js',
directory: './seeds',
loadExtensions: DEFAULT_LOAD_EXTENSIONS,
specific: null,
timestampFilenamePrefix: false,
recursive: false,
sortDirsSeparately: false,
});
function getMergedConfig(config, currentConfig, logger = defaultLogger) {
// config is the user specified config, mergedConfig has defaults and current config
// applied to it.
const mergedConfig = Object.assign(
{},
CONFIG_DEFAULT,
currentConfig || {},
config,
{
logger,
}
);
if (
config &&
// If user specifies any FS related config,
// clear specified migrationSource to avoid ambiguity
(config.directory ||
config.sortDirsSeparately !== undefined ||
config.loadExtensions)
) {
if (config.seedSource) {
logger.warn(
'FS-related option specified for seed configuration. This resets seedSource to default FsMigrations'
);
}
mergedConfig.seedSource = null;
}
// If the user has not specified any configs, we need to
// default to fs migrations to maintain compatibility
if (!mergedConfig.seedSource) {
mergedConfig.seedSource = new FsSeeds(
mergedConfig.directory,
mergedConfig.sortDirsSeparately,
mergedConfig.loadExtensions
);
}
return mergedConfig;
}
module.exports = {
getMergedConfig,
};

View File

@ -0,0 +1,65 @@
const path = require('path');
const flatten = require('lodash/flatten');
const includes = require('lodash/includes');
const { AbstractMigrationsLoader } = require('../../common/MigrationsLoader');
const { getFilepathsInFolder } = require('../../util/fs');
const filterByLoadExtensions = (extensions) => (value) => {
const extension = path.extname(value);
return includes(extensions, extension);
};
class FsSeeds extends AbstractMigrationsLoader {
_getConfigDirectories(logger) {
const directories = this.migrationsPaths;
return directories.map((directory) => {
if (!directory) {
logger.warn(
'Empty value passed as a directory for Seeder, this is not supported.'
);
}
return path.resolve(process.cwd(), directory);
});
}
async getSeeds(config) {
const { loadExtensions, recursive, specific } = config;
const seeds = flatten(
await Promise.all(
this._getConfigDirectories(config.logger).map((d) =>
getFilepathsInFolder(d, recursive)
)
)
);
// if true, each dir are already sorted
// (getFilepathsInFolderRecursively does this)
// if false, we need to sort all the seeds
let files = seeds.filter(filterByLoadExtensions(loadExtensions));
if (!this.sortDirsSeparately) {
files.sort();
}
if (specific) {
files = files.filter((file) => path.basename(file) === specific);
if (files.length === 0) {
throw new Error(
`Invalid argument provided: the specific seed "${specific}" does not exist.`
);
}
}
return files;
}
async getSeed(filepath) {
const importFile = require('../../util/import-file'); // late import
const seed = await importFile(filepath);
return seed;
}
}
module.exports = {
FsSeeds,
};

View File

@ -0,0 +1,9 @@
exports.seed = (knex) ->
knex('table_name').del()
.then () ->
# Inserts seed entries
knex('table_name').insert([
{id: 1, colName: 'rowValue'}
{id: 2, colName: 'rowValue2'}
{id: 3, colName: 'rowValue3'}
])

View File

@ -0,0 +1,11 @@
provide: seed
seed = (knex) ->
;; Deletes ALL existing entries
knex(.table_name).del()
.then(() ->
;; Inserts seed entries
knex(.table_name).insert with [
{ id = 1, col-name = .row-value-1 }
{ id = 2, col-name = .row-value-2 }
{ id = 3, col-name = .row-value-3 }
]

View File

@ -0,0 +1,13 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.seed = async function(knex) {
// Deletes ALL existing entries
await knex('table_name').del()
await knex('table_name').insert([
{id: 1, colName: 'rowValue1'},
{id: 2, colName: 'rowValue2'},
{id: 3, colName: 'rowValue3'}
]);
};

View File

@ -0,0 +1,11 @@
exports.seed = (knex) ->
# Deletes ALL existing entries
knex('table_name').del()
.then(() ->
# Inserts seed entries
knex('table_name').insert([
{id: 1, colName: 'rowValue1'},
{id: 2, colName: 'rowValue2'},
{id: 3, colName: 'rowValue3'}
])
)

View File

@ -0,0 +1,12 @@
export const seed = async (knex) => {
// Deletes ALL existing entries
await knex('table_name').del();
// Inserts seed entries
await knex('table_name').insert([
{id: 1, colName: 'rowValue1'},
{id: 2, colName: 'rowValue2'},
{id: 3, colName: 'rowValue3'}
]);
};

View File

@ -0,0 +1,13 @@
import { Knex } from "knex";
export async function seed(knex: Knex): Promise<void> {
// Deletes ALL existing entries
await knex("table_name").del();
// Inserts seed entries
await knex("table_name").insert([
{ id: 1, colName: "rowValue1" },
{ id: 2, colName: "rowValue2" },
{ id: 3, colName: "rowValue3" }
]);
};