Add ability to auto-generate PK before insert
All checks were successful
continuous-integration/drone Build is passing

This commit is contained in:
Garrett Mills 2022-12-09 04:42:35 -06:00
parent 30e31b2dd3
commit 53087f08a5
3 changed files with 7 additions and 0 deletions

View File

@ -58,6 +58,7 @@ export interface ResourceConfiguration {
key: string, key: string,
collection: string, collection: string,
primaryKey: string, primaryKey: string,
generateKeyOnInsert?: () => string|number,
display: { display: {
field?: string, field?: string,
singular: string, singular: string,

View File

@ -1,3 +1,4 @@
import {uuid4} from '@extollo/lib'
import {allResourceActions, FieldType, Renderer, ResourceAction, ResourceConfiguration} from '../cobalt' import {allResourceActions, FieldType, Renderer, ResourceAction, ResourceConfiguration} from '../cobalt'
export default { export default {
@ -178,6 +179,7 @@ export default {
{ {
key: 'feedPost', key: 'feedPost',
primaryKey: 'feed_post_id', primaryKey: 'feed_post_id',
generateKeyOnInsert: uuid4,
collection: 'feed_posts', collection: 'feed_posts',
display: { display: {
// field: '', // field: '',

View File

@ -83,6 +83,10 @@ export class ResourceAPI extends Controller {
queryRow[field.key] = this.castValue(field, value) queryRow[field.key] = this.castValue(field, value)
} }
if ( config.generateKeyOnInsert ) {
queryRow[config.primaryKey] = config.generateKeyOnInsert()
}
// Create insert query // Create insert query
const result = await this.make<Builder>(Builder) const result = await this.make<Builder>(Builder)
.table(config.collection) .table(config.collection)