mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Start on collections endpoint
This commit is contained in:
@@ -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' });
|
||||
|
||||
|
||||
@@ -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';
|
||||
|
||||
|
||||
@@ -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 });
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user