Browse Source

feat: populate created and updated time whils inserting / updating , programmatically

pull/7304/head
Pranav C 10 months ago
parent
commit
d4c9cbf74a
  1. 44
      packages/nocodb/src/db/BaseModelSqlv2.ts
  2. 18
      packages/nocodb/src/services/columns.service.ts

44
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -18,6 +18,7 @@ import Validator from 'validator';
import { customAlphabet } from 'nanoid'; import { customAlphabet } from 'nanoid';
import DOMPurify from 'isomorphic-dompurify'; import DOMPurify from 'isomorphic-dompurify';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import type { SortType } from 'nocodb-sdk';
import type { Knex } from 'knex'; import type { Knex } from 'knex';
import type LookupColumn from '~/models/LookupColumn'; import type LookupColumn from '~/models/LookupColumn';
import type { XKnex } from '~/db/CustomKnex'; import type { XKnex } from '~/db/CustomKnex';
@ -35,15 +36,6 @@ import type {
SelectOption, SelectOption,
User, User,
} from '~/models'; } from '~/models';
import type { SortType } from 'nocodb-sdk';
import formulaQueryBuilderv2 from '~/db/formulav2/formulaQueryBuilderv2';
import genRollupSelectv2 from '~/db/genRollupSelectv2';
import conditionV2 from '~/db/conditionV2';
import sortV2 from '~/db/sortV2';
import { customValidators } from '~/db/util/customValidators';
import { extractLimitAndOffset } from '~/helpers';
import { NcError } from '~/helpers/catchError';
import getAst from '~/helpers/getAst';
import { import {
Audit, Audit,
BaseUser, BaseUser,
@ -55,6 +47,14 @@ import {
Source, Source,
View, View,
} from '~/models'; } from '~/models';
import formulaQueryBuilderv2 from '~/db/formulav2/formulaQueryBuilderv2';
import genRollupSelectv2 from '~/db/genRollupSelectv2';
import conditionV2 from '~/db/conditionV2';
import sortV2 from '~/db/sortV2';
import { customValidators } from '~/db/util/customValidators';
import { extractLimitAndOffset } from '~/helpers';
import { NcError } from '~/helpers/catchError';
import getAst from '~/helpers/getAst';
import { sanitize, unsanitize } from '~/helpers/sqlSanitize'; import { sanitize, unsanitize } from '~/helpers/sqlSanitize';
import Noco from '~/Noco'; import Noco from '~/Noco';
import { HANDLE_WEBHOOK } from '~/services/hook-handler.service'; import { HANDLE_WEBHOOK } from '~/services/hook-handler.service';
@ -2358,7 +2358,7 @@ class BaseModelSqlv2 {
await this.beforeInsert(insertObj, trx, cookie); await this.beforeInsert(insertObj, trx, cookie);
} }
await this.prepareNocoData(insertObj); await this.prepareNocoData(insertObj, true);
let response; let response;
// const driver = trx ? trx : this.dbDriver; // const driver = trx ? trx : this.dbDriver;
@ -2709,7 +2709,6 @@ class BaseModelSqlv2 {
this.clientMeta, this.clientMeta,
this.dbDriver, this.dbDriver,
); );
let rowId = null; let rowId = null;
const nestedCols = (await this.model.getColumns()).filter((c) => const nestedCols = (await this.model.getColumns()).filter((c) =>
@ -2725,6 +2724,8 @@ class BaseModelSqlv2 {
await this.beforeInsert(insertObj, this.dbDriver, cookie); await this.beforeInsert(insertObj, this.dbDriver, cookie);
await this.prepareNocoData(insertObj, true);
let response; let response;
const query = this.dbDriver(this.tnPath).insert(insertObj); const query = this.dbDriver(this.tnPath).insert(insertObj);
@ -3068,7 +3069,7 @@ class BaseModelSqlv2 {
} }
} }
await this.prepareNocoData(insertObj); await this.prepareNocoData(insertObj, true);
// prepare nested link data for insert only if it is single record insertion // prepare nested link data for insert only if it is single record insertion
if (isSingleRecordInsertion) { if (isSingleRecordInsertion) {
@ -5632,13 +5633,28 @@ class BaseModelSqlv2 {
await this.execAndParse(qb, null, { raw: true }); await this.execAndParse(qb, null, { raw: true });
} }
async prepareNocoData(data) { async prepareNocoData(data, isInsertData = false) {
if ( if (
this.model.columns.some((c) => this.model.columns.some((c) =>
[UITypes.Attachment, UITypes.User].includes(c.uidt), [
UITypes.Attachment,
UITypes.User,
UITypes.CreateTime,
UITypes.LastModifiedTime,
].includes(c.uidt),
) )
) { ) {
for (const column of this.model.columns) { for (const column of this.model.columns) {
if (
isInsertData &&
column.uidt === UITypes.CreateTime &&
column.system
) {
data[column.column_name] = Noco.ncMeta.now();
}
if (column.uidt === UITypes.LastModifiedTime && column.system) {
data[column.column_name] = Noco.ncMeta.now();
}
if (column.uidt === UITypes.Attachment) { if (column.uidt === UITypes.Attachment) {
if (data[column.column_name]) { if (data[column.column_name]) {
if (Array.isArray(data[column.column_name])) { if (Array.isArray(data[column.column_name])) {

18
packages/nocodb/src/services/columns.service.ts

@ -1688,6 +1688,7 @@ export class ColumnsService {
case UITypes.CreateTime: case UITypes.CreateTime:
case UITypes.LastModifiedTime: case UITypes.LastModifiedTime:
{ {
let columnName: string;
const columns = await table.getColumns(); const columns = await table.getColumns();
// check if column already exists, then just create a new column in meta // check if column already exists, then just create a new column in meta
// else create a new column in meta and db // else create a new column in meta and db
@ -1699,7 +1700,7 @@ export class ColumnsService {
); );
if (!existingColumn) { if (!existingColumn) {
let columnName = columnName =
colBody.uidt === UITypes.CreateTime ? 'created_at' : 'updated_at'; colBody.uidt === UITypes.CreateTime ? 'created_at' : 'updated_at';
// const sqlClient = await reuseOrSave('sqlClient', reuse, async () => // const sqlClient = await reuseOrSave('sqlClient', reuse, async () =>
// NcConnectionMgrv2.getSqlClient(source), // NcConnectionMgrv2.getSqlClient(source),
@ -1718,9 +1719,14 @@ export class ColumnsService {
columnName = getUniqueColumnName(columns, columnName); columnName = getUniqueColumnName(columns, columnName);
} }
if (!dbColumn) { {
// create column in db // remove default value for SQLite since it doesn't support default value as function when adding column
// only support default value as constant value
if (source.type === 'sqlite3') {
colBody.cdf = null;
}
// create column in db
const tableUpdateBody = { const tableUpdateBody = {
...table, ...table,
tn: table.table_name, tn: table.table_name,
@ -1732,7 +1738,7 @@ export class ColumnsService {
...table.columns.map((c) => ({ ...c, cn: c.column_name })), ...table.columns.map((c) => ({ ...c, cn: c.column_name })),
{ {
...colBody, ...colBody,
cn: UITypes.CreateTime ? 'created_at' : 'updated_at', cn: columnName,
altered: Altered.NEW_COLUMN, altered: Altered.NEW_COLUMN,
}, },
], ],
@ -1753,11 +1759,15 @@ export class ColumnsService {
title, title,
system: 1, system: 1,
fk_model_id: table.id, fk_model_id: table.id,
column_name: columnName,
}); });
} else {
columnName = existingColumn.column_name;
} }
await Column.insert({ await Column.insert({
...colBody, ...colBody,
fk_model_id: table.id, fk_model_id: table.id,
column_name: columnName,
}); });
} }
break; break;

Loading…
Cancel
Save