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

2
.gitignore vendored
View File

@@ -1,3 +1,5 @@
.DS_Store
# Logs
logs
*.log

4
package-lock.json generated
View File

@@ -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",

View File

@@ -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",

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 });
};