Add initial version of dijkstra backend cloudron image
This commit is contained in:
163
node_modules/knex/lib/seed/Seeder.js
generated
vendored
Normal file
163
node_modules/knex/lib/seed/Seeder.js
generated
vendored
Normal file
@@ -0,0 +1,163 @@
|
||||
// Seeder
|
||||
// -------
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { promisify } = require('util');
|
||||
const mkdirp = require('mkdirp');
|
||||
const { filter, includes, extend } = require('lodash');
|
||||
const { writeJsFileUsingTemplate } = require('../util/template');
|
||||
|
||||
// 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.setConfig(knex.client.config.seeds);
|
||||
}
|
||||
|
||||
// Runs seed files for the given environment.
|
||||
async run(config) {
|
||||
this.config = this.setConfig(config);
|
||||
const all = await this._listAll();
|
||||
const files =
|
||||
config && config.specific
|
||||
? all.filter((file) => file === config.specific)
|
||||
: all;
|
||||
return this._runSeeds(files);
|
||||
}
|
||||
|
||||
// Creates a new seed file, with a given name.
|
||||
async make(name, config) {
|
||||
this.config = this.setConfig(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;
|
||||
}
|
||||
|
||||
// Lists all available seed files as a sorted array.
|
||||
async _listAll(config) {
|
||||
this.config = this.setConfig(config);
|
||||
const loadExtensions = this.config.loadExtensions;
|
||||
return promisify(fs.readdir)(this._absoluteConfigDir()).then((seeds) =>
|
||||
filter(seeds, (value) => {
|
||||
const extension = path.extname(value);
|
||||
return includes(loadExtensions, extension);
|
||||
}).sort()
|
||||
);
|
||||
}
|
||||
|
||||
// Ensures a folder for the seeds exist, dependent on the
|
||||
// seed config settings.
|
||||
async _ensureFolder() {
|
||||
const dir = this._absoluteConfigDir();
|
||||
try {
|
||||
await promisify(fs.stat)(dir);
|
||||
} catch (e) {
|
||||
await promisify(mkdirp)(dir);
|
||||
}
|
||||
}
|
||||
|
||||
// Run seed files, in sequence.
|
||||
_runSeeds(seeds) {
|
||||
seeds.forEach((seed) => this._validateSeedStructure(seed));
|
||||
return this._waterfallBatch(seeds);
|
||||
}
|
||||
|
||||
// Validates seed files by requiring and checking for a `seed` function.
|
||||
_validateSeedStructure(name) {
|
||||
const seed = require(path.join(this._absoluteConfigDir(), name));
|
||||
if (typeof seed.seed !== 'function') {
|
||||
throw new Error(`Invalid seed file: ${name} must have a seed function`);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
_getStubPath() {
|
||||
return (
|
||||
this.config.stub ||
|
||||
path.join(__dirname, 'stub', this.config.extension + '.stub')
|
||||
);
|
||||
}
|
||||
|
||||
_getNewStubFileName(name) {
|
||||
if (name[0] === '-') name = name.slice(1);
|
||||
return name + '.' + this.config.extension;
|
||||
}
|
||||
|
||||
_getNewStubFilePath(name) {
|
||||
return path.join(this._absoluteConfigDir(), this._getNewStubFileName(name));
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Runs a batch of seed files.
|
||||
async _waterfallBatch(seeds) {
|
||||
const { knex } = this;
|
||||
const seedDirectory = this._absoluteConfigDir();
|
||||
const log = [];
|
||||
for (const seedName of seeds) {
|
||||
const seedPath = path.join(seedDirectory, seedName);
|
||||
const seed = require(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];
|
||||
}
|
||||
|
||||
_absoluteConfigDir() {
|
||||
return path.resolve(process.cwd(), this.config.directory);
|
||||
}
|
||||
|
||||
setConfig(config) {
|
||||
return extend(
|
||||
{
|
||||
extension: 'js',
|
||||
directory: './seeds',
|
||||
loadExtensions: [
|
||||
'.co',
|
||||
'.coffee',
|
||||
'.eg',
|
||||
'.iced',
|
||||
'.js',
|
||||
'.litcoffee',
|
||||
'.ls',
|
||||
'.ts',
|
||||
],
|
||||
},
|
||||
this.config || {},
|
||||
config
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Seeder;
|
||||
13
node_modules/knex/lib/seed/seed-stub.js
generated
vendored
Normal file
13
node_modules/knex/lib/seed/seed-stub.js
generated
vendored
Normal 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,
|
||||
};
|
||||
9
node_modules/knex/lib/seed/stub/coffee.stub
generated
vendored
Normal file
9
node_modules/knex/lib/seed/stub/coffee.stub
generated
vendored
Normal 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'}
|
||||
])
|
||||
11
node_modules/knex/lib/seed/stub/eg.stub
generated
vendored
Normal file
11
node_modules/knex/lib/seed/stub/eg.stub
generated
vendored
Normal 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 }
|
||||
]
|
||||
13
node_modules/knex/lib/seed/stub/js.stub
generated
vendored
Normal file
13
node_modules/knex/lib/seed/stub/js.stub
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
exports.seed = function(knex) {
|
||||
// Deletes ALL existing entries
|
||||
return knex('table_name').del()
|
||||
.then(function () {
|
||||
// Inserts seed entries
|
||||
return knex('table_name').insert([
|
||||
{id: 1, colName: 'rowValue1'},
|
||||
{id: 2, colName: 'rowValue2'},
|
||||
{id: 3, colName: 'rowValue3'}
|
||||
]);
|
||||
});
|
||||
};
|
||||
11
node_modules/knex/lib/seed/stub/ls.stub
generated
vendored
Normal file
11
node_modules/knex/lib/seed/stub/ls.stub
generated
vendored
Normal 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'}
|
||||
])
|
||||
)
|
||||
14
node_modules/knex/lib/seed/stub/ts.stub
generated
vendored
Normal file
14
node_modules/knex/lib/seed/stub/ts.stub
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
import * as Knex from "knex";
|
||||
|
||||
export async function seed(knex: Knex): Promise<any> {
|
||||
// Deletes ALL existing entries
|
||||
return knex("table_name").del()
|
||||
.then(() => {
|
||||
// Inserts seed entries
|
||||
return knex("table_name").insert([
|
||||
{ id: 1, colName: "rowValue1" },
|
||||
{ id: 2, colName: "rowValue2" },
|
||||
{ id: 3, colName: "rowValue3" }
|
||||
]);
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user