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:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
||||
.DS_Store
|
||||
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
|
||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -3668,10 +3668,6 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"knex-schema-inspector": {
|
||||
"version": "github:knex/knex-schema-inspector#b01d5ff067e0f49b9a6b48e830f016a0bf10d315",
|
||||
"from": "github:knex/knex-schema-inspector"
|
||||
},
|
||||
"levn": {
|
||||
"version": "0.4.1",
|
||||
"resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz",
|
||||
|
||||
@@ -84,7 +84,6 @@
|
||||
"icc": "^2.0.0",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"knex": "^0.21.1",
|
||||
"knex-schema-inspector": "github:knex/knex-schema-inspector",
|
||||
"liquidjs": "^9.12.0",
|
||||
"lodash": "^4.17.15",
|
||||
"mssql": "^6.2.0",
|
||||
|
||||
@@ -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