added support for sqlite
Signed-off-by: Lykke Axlin <lykkeaxlin@hotmail.com> Co-authored-by: klaraab <klarabroman@live.se>
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* Copyright 2021 The Backstage Authors
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
exports.up = function up(knex) {
|
||||
return knex.raw(`
|
||||
CREATE OR REPLACE FUNCTION update_timestamp() RETURNS TRIGGER
|
||||
LANGUAGE plpgsql
|
||||
AS
|
||||
$$
|
||||
BEGIN
|
||||
NEW.updated_at = CURRENT_TIMESTAMP;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
`);
|
||||
};
|
||||
|
||||
exports.down = function down(knex) {
|
||||
return knex.raw(`
|
||||
DROP FUNCTION IF EXISTS update_timestamp() CASCADE;
|
||||
`);
|
||||
};
|
||||
+15
-11
@@ -17,8 +17,12 @@
|
||||
exports.up = async function up(knex) {
|
||||
await knex.schema.createTable('metadata', table => {
|
||||
table.comment('The table of Bazaar metadata');
|
||||
table
|
||||
.text('entity_ref')
|
||||
.notNullable()
|
||||
.unique()
|
||||
.comment('The ref of the entity');
|
||||
table.text('name').notNullable().comment('The name of the entity');
|
||||
table.text('entity_ref').notNullable().comment('The ref of the entity');
|
||||
table
|
||||
.text('community')
|
||||
.comment('Link to where the community can discuss ideas');
|
||||
@@ -31,20 +35,20 @@ exports.up = async function up(knex) {
|
||||
.defaultTo('proposed')
|
||||
.notNullable()
|
||||
.comment('The status of the Bazaar project');
|
||||
table.timestamps(true, true);
|
||||
table
|
||||
.text('updated_at')
|
||||
.notNullable()
|
||||
.comment('Timestamp on ISO 8601 format when entity was last updated');
|
||||
});
|
||||
|
||||
await knex.raw(`
|
||||
CREATE TRIGGER update_timestamp
|
||||
BEFORE UPDATE
|
||||
ON metadata
|
||||
FOR EACH ROW
|
||||
EXECUTE PROCEDURE update_timestamp();
|
||||
`);
|
||||
|
||||
await knex.schema.createTable('members', table => {
|
||||
table.comment('The table of Bazaar members');
|
||||
table.text('entity_ref').notNullable().comment('The ref of the entity');
|
||||
table
|
||||
.text('entity_ref')
|
||||
.notNullable()
|
||||
.references('metadata.entity_ref')
|
||||
.onDelete('CASCADE')
|
||||
.comment('The ref of the entity');
|
||||
table.text('user_id').notNullable().comment('The user id of the member');
|
||||
table
|
||||
.dateTime('join_date')
|
||||
@@ -17,12 +17,14 @@
|
||||
import { DatabaseHandler } from './DatabaseHandler';
|
||||
import { TestDatabaseId, TestDatabases } from '@backstage/backend-test-utils';
|
||||
|
||||
const members: Array<any> = [
|
||||
{
|
||||
entity_ref: 'project1',
|
||||
user_id: 'member1',
|
||||
},
|
||||
];
|
||||
const bazaarProject: any = {
|
||||
name: 'name',
|
||||
entityRef: 'ref',
|
||||
community: '',
|
||||
status: 'proposed',
|
||||
announcement: 'a',
|
||||
membersCount: 0,
|
||||
};
|
||||
|
||||
describe('DatabaseHandler', () => {
|
||||
const databases = TestDatabases.create({
|
||||
@@ -38,10 +40,11 @@ describe('DatabaseHandler', () => {
|
||||
'should do a full sync with the locations on connect, %p',
|
||||
async databaseId => {
|
||||
const db = await createDatabaseHandler(databaseId);
|
||||
await db.addMember(members[0].user_id, members[0].entity_ref);
|
||||
const cov: any[] = await db.getMembers('project1');
|
||||
expect(cov[0].entity_ref).toEqual(members[0].entity_ref);
|
||||
expect(cov[0].user_id).toEqual(members[0].user_id);
|
||||
await db.insertMetadata(bazaarProject);
|
||||
|
||||
const entities = await db.getEntities();
|
||||
expect(entities.length).toEqual(1);
|
||||
expect(entities[0].entity_ref).toEqual(bazaarProject.entityRef);
|
||||
},
|
||||
60_000,
|
||||
);
|
||||
|
||||
@@ -43,20 +43,103 @@ export class DatabaseHandler {
|
||||
this.database = options.database;
|
||||
}
|
||||
|
||||
async getMembers(entityRef: any) {
|
||||
async getMembers(entityRef: string) {
|
||||
return await this.database
|
||||
.select('*')
|
||||
.from('public.members')
|
||||
.from('members')
|
||||
.where({ entity_ref: entityRef });
|
||||
}
|
||||
|
||||
async addMember(userId: any, entityRef: any, picture?: string) {
|
||||
async addMember(userId: string, entityRef: string, picture?: string) {
|
||||
await this.database
|
||||
.insert({
|
||||
entity_ref: entityRef,
|
||||
user_id: userId,
|
||||
picture: picture,
|
||||
})
|
||||
.into('public.members');
|
||||
.into('members');
|
||||
}
|
||||
|
||||
async deleteMember(userId: string, entityRef: string) {
|
||||
return await this.database('members')
|
||||
.where({ entity_ref: decodeURIComponent(entityRef) })
|
||||
.andWhere('user_id', userId)
|
||||
.del();
|
||||
}
|
||||
|
||||
async getMetadata(entityRef: string) {
|
||||
const coalesce = this.database.raw(
|
||||
'coalesce(count(members.entity_ref), 0) as members_count',
|
||||
);
|
||||
|
||||
const columns = [
|
||||
'members.entity_ref',
|
||||
'metadata.entity_ref',
|
||||
'metadata.name',
|
||||
'metadata.announcement',
|
||||
'metadata.status',
|
||||
'metadata.updated_at',
|
||||
'metadata.community',
|
||||
];
|
||||
|
||||
return await this.database('metadata')
|
||||
.select([...columns, coalesce])
|
||||
.where({ 'metadata.entity_ref': entityRef })
|
||||
.groupBy(columns)
|
||||
.leftJoin('members', 'metadata.entity_ref', '=', 'members.entity_ref');
|
||||
}
|
||||
|
||||
async insertMetadata(bazaarProject: any) {
|
||||
const { name, entityRef, community, announcement, status } = bazaarProject;
|
||||
|
||||
await this.database
|
||||
.insert({
|
||||
name: name,
|
||||
entity_ref: entityRef,
|
||||
community: community,
|
||||
announcement: announcement,
|
||||
status: status,
|
||||
updated_at: new Date().toISOString(),
|
||||
})
|
||||
.into('metadata');
|
||||
}
|
||||
|
||||
async updateMetadata(bazaarProject: any) {
|
||||
const { entityRef, community, announcement, status } = bazaarProject;
|
||||
|
||||
return await this.database('metadata')
|
||||
.where({ entity_ref: entityRef })
|
||||
.update({
|
||||
announcement: announcement,
|
||||
community: community,
|
||||
status: status,
|
||||
updated_at: new Date().toISOString(),
|
||||
});
|
||||
}
|
||||
|
||||
async deleteMetadata(entityRef: string) {
|
||||
return await this.database('metadata')
|
||||
.where({ entity_ref: entityRef })
|
||||
.del();
|
||||
}
|
||||
|
||||
async getEntities() {
|
||||
const coalesce = this.database.raw(
|
||||
'coalesce(count(members.entity_ref), 0) as members_count',
|
||||
);
|
||||
|
||||
const columns = [
|
||||
'members.entity_ref',
|
||||
'metadata.entity_ref',
|
||||
'metadata.name',
|
||||
'metadata.announcement',
|
||||
'metadata.status',
|
||||
'metadata.updated_at',
|
||||
'metadata.community',
|
||||
];
|
||||
return await this.database('metadata')
|
||||
.select([...columns, coalesce])
|
||||
.groupBy(columns)
|
||||
.leftJoin('members', 'metadata.entity_ref', '=', 'members.entity_ref');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,21 +61,7 @@ export async function createRouter(
|
||||
router.delete('/member/:ref/:id', async (request, response) => {
|
||||
const { ref, id } = request.params;
|
||||
|
||||
const count = await db('public.members')
|
||||
.where({ entity_ref: decodeURIComponent(ref) })
|
||||
.andWhere('user_id', id)
|
||||
.del();
|
||||
|
||||
if (count) {
|
||||
response.json({ status: 'ok' });
|
||||
} else {
|
||||
response.status(404).json({ message: 'Record not found' });
|
||||
}
|
||||
});
|
||||
|
||||
router.delete('/members/:ref', async (request, response) => {
|
||||
const ref = decodeURIComponent(request.params.ref);
|
||||
const count = await db('public.members').where({ entity_ref: ref }).del();
|
||||
const count = await dbHandler.deleteMember(id, ref);
|
||||
|
||||
if (count) {
|
||||
response.json({ status: 'ok' });
|
||||
@@ -85,97 +71,35 @@ export async function createRouter(
|
||||
});
|
||||
|
||||
router.get('/metadata/:ref', async (request, response) => {
|
||||
const entity_ref = decodeURIComponent(request.params.ref);
|
||||
|
||||
const coalesce = db.raw(
|
||||
'coalesce(count(members.entity_ref), 0) as members_count',
|
||||
);
|
||||
|
||||
const columns = [
|
||||
'members.entity_ref',
|
||||
'metadata.entity_ref',
|
||||
'metadata.name',
|
||||
'metadata.announcement',
|
||||
'metadata.status',
|
||||
'metadata.updated_at',
|
||||
'metadata.community',
|
||||
];
|
||||
|
||||
const data = await db('public.members as members')
|
||||
.select([...columns, coalesce])
|
||||
.where({ 'metadata.entity_ref': entity_ref })
|
||||
.groupBy(columns)
|
||||
.rightJoin(
|
||||
'public.metadata as metadata',
|
||||
'metadata.entity_ref',
|
||||
'=',
|
||||
'members.entity_ref',
|
||||
);
|
||||
const ref = decodeURIComponent(request.params.ref);
|
||||
|
||||
const data = await dbHandler.getMetadata(ref);
|
||||
response.json({ status: 'ok', data: data });
|
||||
});
|
||||
|
||||
router.get('/entities', async (_, response) => {
|
||||
const coalesce = db.raw(
|
||||
'coalesce(count(members.entity_ref), 0) as members_count',
|
||||
);
|
||||
|
||||
const columns = [
|
||||
'members.entity_ref',
|
||||
'metadata.entity_ref',
|
||||
'metadata.name',
|
||||
'metadata.announcement',
|
||||
'metadata.status',
|
||||
'metadata.updated_at',
|
||||
'metadata.community',
|
||||
];
|
||||
|
||||
const data = await db('public.members as members')
|
||||
.select([...columns, coalesce])
|
||||
.groupBy(columns)
|
||||
.rightJoin(
|
||||
'public.metadata as metadata',
|
||||
'metadata.entity_ref',
|
||||
'=',
|
||||
'members.entity_ref',
|
||||
);
|
||||
const data = await dbHandler.getEntities();
|
||||
|
||||
response.json({ status: 'ok', data: data });
|
||||
});
|
||||
|
||||
router.put('/metadata', async (request, response) => {
|
||||
const { name, announcement, status, community, entity_ref } = request.body;
|
||||
const bazaarProject = request.body;
|
||||
|
||||
const count = await db('public.metadata')
|
||||
.where({ entity_ref: entity_ref })
|
||||
.update({
|
||||
announcement: announcement,
|
||||
community: community,
|
||||
status: status,
|
||||
});
|
||||
const count = await dbHandler.updateMetadata(bazaarProject);
|
||||
|
||||
if (count) {
|
||||
response.json({ status: 'ok' });
|
||||
} else {
|
||||
await db
|
||||
.insert({
|
||||
name: name,
|
||||
entity_ref: entity_ref,
|
||||
community: community,
|
||||
announcement: announcement,
|
||||
status: status,
|
||||
})
|
||||
.into('public.metadata');
|
||||
await dbHandler.insertMetadata(bazaarProject);
|
||||
response.json({ status: 'ok' });
|
||||
}
|
||||
});
|
||||
|
||||
router.delete('/metadata/:ref', async (request, response) => {
|
||||
const entity_ref = decodeURIComponent(request.params.ref);
|
||||
const ref = decodeURIComponent(request.params.ref);
|
||||
|
||||
const count = await db('public.metadata')
|
||||
.where({ entity_ref: entity_ref })
|
||||
.del();
|
||||
const count = await dbHandler.deleteMetadata(ref);
|
||||
|
||||
if (count) {
|
||||
response.json({ status: 'ok' });
|
||||
|
||||
Reference in New Issue
Block a user