Start on collections endpoint

This commit is contained in:
rijkvanzanten
2020-06-30 17:06:01 -04:00
parent 6f264eb9a6
commit 67202260f7
6 changed files with 20 additions and 11 deletions

View File

@@ -1,7 +1,7 @@
import knex from 'knex';
import logger from './logger';
import SchemaInspector from '../../../knex-schema-inspector/lib/index';
import SchemaInspector from './knex-schema-inspector/lib/index';
const log = logger.child({ module: 'sql' });

View File

@@ -1,6 +1,6 @@
import { schemaInspector } from '../database';
import * as ItemsService from '../services/items';
import { Table } from '../../../../knex-schema-inspector/lib/types/table';
import { Table } from '../knex-schema-inspector/lib/types/table';
import { Collection } from '../types/collection';
import { Query } from '../types/query';

View File

@@ -1,4 +1,4 @@
import database from '../database';
import database, { schemaInspector } from '../database';
import { Query } from '../types/query';
export const createItem = async (
@@ -70,7 +70,12 @@ export const readItem = async <T = any>(
pk: number | string,
query: Query = {}
): Promise<T> => {
return await database.select('*').from(collection).where({ id: pk }).first();
const primaryKeyField = await schemaInspector.primary(collection);
return await database
.select('*')
.from(collection)
.where({ [primaryKeyField]: pk })
.first();
};
export const updateItem = async (
@@ -79,10 +84,17 @@ export const updateItem = async (
data: Record<string, any>,
query: Query = {}
) => {
const result = await database(collection).update(data).where({ id: pk }).returning('id');
const primaryKeyField = await schemaInspector.primary(collection);
const result = await database(collection)
.update(data)
.where({ [primaryKeyField]: pk })
.returning('id');
return readItem(collection, result[0], query);
};
export const deleteItem = async (collection: string, pk: number | string) => {
return await database(collection).delete().where({ id: pk });
const primaryKeyField = await schemaInspector.primary(collection);
return await database(collection)
.delete()
.where({ [primaryKeyField]: pk });
};