Merge pull request #810 from spotify/freben/inventory1

Implement first knex database basics
This commit is contained in:
Fredrik Adelöw
2020-05-11 14:05:09 +02:00
committed by GitHub
7 changed files with 403 additions and 14 deletions
+1
View File
@@ -0,0 +1 @@
dev.sqlite3
+58
View File
@@ -0,0 +1,58 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
module.exports = {
development: {
client: 'sqlite3',
connection: {
filename: './dev.sqlite3',
},
},
/*
staging: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password',
},
pool: {
min: 2,
max: 10,
},
migrations: {
tableName: 'knex_migrations',
},
},
production: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password',
},
pool: {
min: 2,
max: 10,
},
migrations: {
tableName: 'knex_migrations',
},
},
*/
};
+6 -2
View File
@@ -12,7 +12,8 @@
"start": "backstage-cli watch-deps --build -- tsc-watch --onFirstSuccess nodemon",
"lint": "backstage-cli lint",
"test": "backstage-cli test",
"clean": "backstage-cli clean"
"clean": "backstage-cli clean",
"migrate:create": "knex migrate:make -x ts"
},
"dependencies": {
"@backstage/backend-common": "0.1.1-alpha.4",
@@ -21,7 +22,10 @@
"compression": "^1.7.4",
"cors": "^2.8.5",
"express": "^4.17.1",
"helmet": "^3.22.0"
"helmet": "^3.22.0",
"knex": "^0.21.1",
"sqlite3": "^4.2.0",
"winston": "^3.2.1"
},
"devDependencies": {
"@backstage/cli": "^0.1.1-alpha.4",
+39
View File
@@ -0,0 +1,39 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import knex from 'knex';
import path from 'path';
import { Logger } from 'winston';
export async function buildDatabase(logger: Logger) {
logger.info('Connecting to database');
const db = knex({
client: 'sqlite3',
connection: {
filename: './dev.sqlite3',
},
useNullAsDefault: true,
});
logger.info('Running database migrations');
await db.migrate.latest({
directory: path.resolve(__dirname, 'migrations'),
loadExtensions: ['.js'],
});
logger.info('Database initialization complete');
return db;
}
+6 -2
View File
@@ -28,12 +28,13 @@ import {
notFoundHandler,
requestLoggingHandler,
} from '@backstage/backend-common';
import inventory from './plugins/inventory';
import scaffolder from './plugins/scaffolder';
import compression from 'compression';
import cors from 'cors';
import express from 'express';
import helmet from 'helmet';
import { buildDatabase } from './database';
import inventory from './plugins/inventory';
import scaffolder from './plugins/scaffolder';
import { PluginEnvironment } from './types';
const DEFAULT_PORT = 7000;
@@ -43,6 +44,9 @@ const pluginEnvironment: PluginEnvironment = {
};
async function main() {
const database = await buildDatabase(getRootLogger());
console.log(await database.select().from('locations'));
const app = express();
app.use(helmet());
@@ -0,0 +1,45 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as Knex from 'knex';
export async function up(knex: Knex): Promise<any> {
return knex.schema
.createTable('locations', table => {
table
.uuid('id')
.primary()
.comment('Auto-generated ID of the location');
table
.string('type')
.notNullable()
.comment('The type of location');
table
.string('target')
.notNullable()
.comment('The actual target of the location');
})
.createTable('components', table => {
table
.string('name')
.primary()
.comment('The name of the component');
});
}
export async function down(knex: Knex): Promise<any> {
return knex.schema.dropTable('components').dropTable('locations');
}