@proteinjs/db 1.15.2 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Record.ts CHANGED
@@ -9,6 +9,10 @@ export interface Record {
9
9
  updated: moment.Moment;
10
10
  }
11
11
 
12
+ export function isRecordColumn(column: string): column is keyof Record {
13
+ return column === 'id' || column === 'created' || column === 'updated';
14
+ }
15
+
12
16
  const recordColumns: Columns<Record> = {
13
17
  id: new UuidColumn('id', { ui: { hidden: true } }),
14
18
  created: new DateTimeColumn('created', {
package/src/Table.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Loadable, SourceRepository } from '@proteinjs/reflection';
2
2
  import { CustomSerializableObject } from '@proteinjs/serializer';
3
- import { Record, RecordSerializer } from './Record';
3
+ import { isRecordColumn, Record } from './Record';
4
4
  import { TableSerializerId } from './serializers/TableSerializer';
5
5
  import { QueryBuilder } from '@proteinjs/db-query';
6
6
  import { Identity, TableOperationsAuth } from './auth/TableAuth';
@@ -44,7 +44,10 @@ export const getColumnByName = (table: Table<any>, columnName: string) => {
44
44
  };
45
45
 
46
46
  export const addDefaultFieldValues = async (table: Table<any>, record: any, runAsSystem: boolean) => {
47
- for (const columnPropertyName in table.columns) {
47
+ // Get defaultFieldValue for Record columns first
48
+ const columns = Object.keys(table.columns).sort((a, b) => +!isRecordColumn(a) - +!isRecordColumn(b));
49
+
50
+ for (const columnPropertyName of columns) {
48
51
  const column = (table.columns as any)[columnPropertyName] as Column<any, any>;
49
52
  if (
50
53
  column.options?.defaultValue &&
@@ -144,13 +147,14 @@ export type ColumnOptions = {
144
147
  references?: { table: string };
145
148
  nullable?: boolean;
146
149
  /** Value stored on insert */
147
- defaultValue?: (table: Table<any>, insertObj: any) => Promise<any>;
150
+ defaultValue?: (table: Table<any>, insertObj: any & Record) => Promise<any>;
148
151
  /** If true, the `defaultValue` function will always provide the value and override any existing value */
149
152
  forceDefaultValue?: boolean | ((runAsSystem: boolean) => boolean);
150
153
  /** Value stored on update */
151
154
  updateValue?: (table: Table<any>, updateObj: any) => Promise<any>;
152
155
  /** Add conditions to query; called on every query of this table */
153
156
  addToQuery?: (qb: QueryBuilder, runAsSystem: boolean) => Promise<void>;
157
+ onBeforeInsert?: (insertObj: any & Record, runAsSystem: boolean) => Promise<void>;
154
158
  ui?: {
155
159
  hidden?: boolean;
156
160
  };