π₯ π₯ π₯ A Free & Self-hostable Airtable Alternative
TypeScript airtableairtable-alternativeautomatic-apihacktoberfestlow-codeno-codeno-code-databaseno-code-platformpostgresqlrest-apirestful-apispreadsheetsqliteswagger
fix: review comments
Ramesh Manefe1ba3185edb
diff --git a/packages/nc-gui/composables/useRecordTemplate.ts b/packages/nc-gui/composables/useRecordTemplate.tsindex 1f7815b2b7..765c25ff85 100644--- a/packages/nc-gui/composables/useRecordTemplate.ts+++ b/packages/nc-gui/composables/useRecordTemplate.ts@@ -1,27 +1,23 @@-import type { ColumnType, LinkToAnotherRecordType } from 'nocodb-sdk'+import type { ColumnType, LinkToAnotherRecordType, RecordTemplateType } from 'nocodb-sdk'// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ-// Module-level shared state (singleton across the app)+// Composable (shared singleton via createSharedComposable)// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ-/** Controls visibility of the "Manage Templates" modal */-const showRecordTemplateManager = ref(false)+export const useRecordTemplate = createSharedComposable(() => {+ /** Controls visibility of the "Manage Templates" modal */+ const showRecordTemplateManager = ref(false)-/**- * Shared reactive template list.- * Populated by RecordTemplatesButton (manager), consumed by AddNewRowMenu and canvas grid.- * Contains all templates across all tables in the current base.- */-const templates = ref<any[]>([])--/** Tracks the last-used template ID for the "+" button's default action */-const selectedTemplateId = ref<string | null>(null)+ /**+ * Shared reactive template list.+ * Populated by RecordTemplatesButton (manager), consumed by AddNewRowMenu and canvas grid.+ * Contains all templates across all tables in the current base.+ */+ const templates = ref<RecordTemplateType[]>([])-// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ-// Composable-// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ+ /** Tracks the last-used template ID for the "+" button's default action */+ const selectedTemplateId = ref<string | null>(null)-export function useRecordTemplate() {const openManager = () => {showRecordTemplateManager.value = true}@@ -44,7 +40,7 @@ export function useRecordTemplate() {selectedTemplateId,setSelectedTemplate,}-}+})// ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ// Pure utility functions (no Vue reactivity dependencies)diff --git a/packages/nocodb-sdk/src/lib/index.ts b/packages/nocodb-sdk/src/lib/index.tsindex bd6e259607..457a633eda 100644--- a/packages/nocodb-sdk/src/lib/index.ts+++ b/packages/nocodb-sdk/src/lib/index.ts@@ -88,3 +88,4 @@ export * from '~/lib/CircularRefContext';export * from '~/lib/viewUtils';export * from '~/lib/workflow';export * from '~/lib/uniqueConstraintHelpers';+export * from '~/lib/recordTemplate';diff --git a/packages/nocodb-sdk/src/lib/recordTemplate.ts b/packages/nocodb-sdk/src/lib/recordTemplate.tsnew file mode 100644index 0000000000..29e7a012c0--- /dev/null+++ b/packages/nocodb-sdk/src/lib/recordTemplate.ts@@ -0,0 +1,43 @@+/**+ * Record template types for the record templates feature.+ *+ * Template data format:+ * { fields: Record<string, any>, ltarState?: Record<string, any> }+ *+ * - `fields`: column title β default value mapping for regular fields+ * - `ltarState`: LTAR column title β linked record(s) or blueprint(s)+ * - Blueprint items have `_isBlueprint: true` and optionally `_ltarState` for nesting+ */++export interface RecordTemplateType {+ id?: string;+ base_id?: string;+ fk_workspace_id?: string;+ source_id?: string;+ title?: string;+ description?: string;+ template_data?: RecordTemplateDataType | string;+ usage_count?: number;+ enabled?: boolean;+ created_by?: string;+ created_at?: string;+ updated_at?: string;+}++export interface RecordTemplateDataType {+ fields: Record<string, any>;+ ltarState?: Record<string, any>;+}++export interface RecordTemplateReqType {+ title: string;+ description?: string;+ template_data: RecordTemplateDataType;+}++export interface RecordTemplateUpdateReqType {+ title?: string;+ description?: string;+ template_data?: RecordTemplateDataType;+ enabled?: boolean;+}diff --git a/packages/nocodb/src/meta/migrations/v2/nc_099_record_templates.ts b/packages/nocodb/src/meta/migrations/v2/nc_099_record_templates.tsindex 395fce7edc..96aebc8f37 100644--- a/packages/nocodb/src/meta/migrations/v2/nc_099_record_templates.ts+++ b/packages/nocodb/src/meta/migrations/v2/nc_099_record_templates.ts@@ -2,32 +2,31 @@ import type { Knex } from 'knex';import { MetaTable } from '~/utils/globals';const up = async (knex: Knex) => {- await knex.schema.createTable(MetaTable.RECORD_TEMPLATES, (table) => {- table.string('id', 20).primary();- table.string('base_id', 20).notNullable();- table.string('source_id', 20).notNullable();- table.string('title', 255).notNullable();- table.text('description');- table.text('template_data', 'longtext').notNullable();- table.integer('usage_count').defaultTo(0);- table.boolean('enabled').defaultTo(true);- table.string('created_by', 20);- table.timestamps(true, true);+ if (!(await knex.schema.hasTable(MetaTable.RECORD_TEMPLATES))) {+ await knex.schema.createTable(MetaTable.RECORD_TEMPLATES, (table) => {+ table.string('id', 20).primary();+ table.string('base_id', 20).notNullable();+ table.string('fk_workspace_id', 20);+ table.string('source_id', 20).notNullable();+ table.string('title', 255).notNullable();+ table.text('description');+ table.text('template_data', 'longtext').notNullable();+ table.integer('usage_count').defaultTo(0);+ table.boolean('enabled').defaultTo(true);+ table.string('created_by', 20);+ table.timestamps(true, true);- // Indexes- table.index('base_id');- table.index('source_id');- table.index(['base_id', 'source_id']);-- // Foreign keys- table.foreign('base_id').references('id').inTable(MetaTable.PROJECT).onDelete('CASCADE');- table.foreign('source_id').references('id').inTable(MetaTable.SOURCES).onDelete('CASCADE');- table.foreign('created_by').references('id').inTable(MetaTable.USERS).onDelete('SET NULL');- });+ // Indexes+ table.index('base_id');+ table.index('source_id');+ table.index(['base_id', 'source_id']);+ table.index('fk_workspace_id');+ });+ }};const down = async (knex: Knex) => {- await knex.schema.dropTable(MetaTable.RECORD_TEMPLATES);+ await knex.schema.dropTableIfExists(MetaTable.RECORD_TEMPLATES);};export { up, down };diff --git a/packages/nocodb/src/models/RecordTemplate.ts b/packages/nocodb/src/models/RecordTemplate.tsindex b1b0390099..961ff75748 100644--- a/packages/nocodb/src/models/RecordTemplate.ts+++ b/packages/nocodb/src/models/RecordTemplate.ts@@ -13,6 +13,7 @@ import { extractProps } from '~/helpers/extractProps';export default class RecordTemplate {id?: string;base_id: string;+ fk_workspace_id?: string;source_id: string;title: string;description?: string;@@ -35,6 +36,7 @@ export default class RecordTemplate {const insertObj = extractProps(template, ['id','base_id',+ 'fk_workspace_id','source_id','title','description',@@ -267,7 +269,7 @@ export default class RecordTemplate {return this.get(context, id, ncMeta);}- public static castType(template: any): RecordTemplate {+ public static castType(template: any): RecordTemplate | null {if (!template) return null;// Parse template_data if it's a stringdiff --git a/packages/nocodb/src/services/record-templates/record-templates.service.ts b/packages/nocodb/src/services/record-templates/record-templates.service.tsindex 6938d83058..9e3a50e8a9 100644--- a/packages/nocodb/src/services/record-templates/record-templates.service.ts+++ b/packages/nocodb/src/services/record-templates/record-templates.service.ts@@ -71,6 +71,7 @@ export class RecordTemplatesService {const template = await RecordTemplate.insert(param.context, {base_id: param.baseId,+ fk_workspace_id: param.context.workspace_id,source_id: param.sourceId,title: param.body.title,description: param.body.description,@@ -222,13 +223,24 @@ export class RecordTemplatesService {}/**- * Validates template data structure.+ * Validates template data structure and size.* Template data format: { fields: Record<string, any>, ltarState?: Record<string, any> }+ *+ * Size limit: 256 KB (generous for deeply nested templates with sub-records).*/private validateTemplateData(templateData: Record<string, any>) {if (!templateData || typeof templateData !== 'object') {NcError.badRequest('Template data is required and must be an object');}++ // Enforce size limit to prevent oversized payloads+ const serialized = JSON.stringify(templateData);+ const MAX_TEMPLATE_DATA_SIZE = 256 * 1024; // 256 KB+ if (serialized.length > MAX_TEMPLATE_DATA_SIZE) {+ NcError.badRequest(+ `Template data exceeds maximum size of ${MAX_TEMPLATE_DATA_SIZE / 1024} KB`,+ );+ }}/**