From d9a3fcd84b71b499ef585090871b9a8c7828cc9e Mon Sep 17 00:00:00 2001 From: Liran Cohen Date: Tue, 23 Apr 2024 09:35:30 -0400 Subject: [PATCH] renaming for clarity after review comments --- src/dialect/dialect.ts | 14 ++++++-------- src/dialect/mysql-dialect.ts | 8 ++++---- src/dialect/postgres-dialect.ts | 8 ++++---- src/dialect/sqlite-dialect.ts | 6 +++--- src/event-log-sql.ts | 4 ++-- src/message-store-sql.ts | 8 ++++---- src/types.ts | 2 +- src/utils/tags.ts | 8 ++++---- 8 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src/dialect/dialect.ts b/src/dialect/dialect.ts index 8e2bc09..3aeaebe 100644 --- a/src/dialect/dialect.ts +++ b/src/dialect/dialect.ts @@ -39,9 +39,9 @@ export interface Dialect extends KyselyDialect { * @param onDeleteAction the action to take when the referenced row is deleted. * * The ForeignKey name it creates in `mysql` will be in the following format: - * `${referenceTable}${referenceColumnName}_${tableName}${columnName}` + * `${referenceTable}_${referenceColumnName}__${tableName}_${columnName}` * ex: if the reference table is `users` and the reference column is `id` and the table is `profiles` and the column is `userId`, - * the resulting name for the foreign key is: `usersid_profilesuserId` + * the resulting name for the foreign key is: `users_id__profiles_userId` * * @returns {CreateTableBuilder} the CreateTableBuilder with the added column. */ @@ -49,17 +49,15 @@ export interface Dialect extends KyselyDialect { builder: CreateTableBuilder, tableName: TB, columnName: string, - targetType: ColumnDataType, + columnType: ColumnDataType, referenceTable: string, referenceColumnName: string, onDeleteAction: 'cascade' | 'no action' | 'restrict' | 'set null' | 'set default', ): CreateTableBuilder; /** - * This is a helper method to return an `insertId` for each dialect. - * We need this method because Postgres does not return an `insertId` with insert an insert. - * - * Since `returning` is supported on both `sqlite` and `postgres`, it will be used on those and ignored on `mysql`. + * This is a helper method to return an `insertId` across all dialects after inserting values. + * `postgres` and `sqlite` both support the `returning` clause, however `mysql` does not and instead returns the last inserted id. * * @param db the Kysely DB object or a DB Transaction. * @param table the table to insert into. @@ -72,7 +70,7 @@ export interface Dialect extends KyselyDialect { * * @returns {InsertQueryBuilder} object to further modify the query or execute it. */ - insertIntoReturning = any>( + insertThenReturnId = any>( db: Transaction | Kysely, table: TB & string, values: InsertObject, diff --git a/src/dialect/mysql-dialect.ts b/src/dialect/mysql-dialect.ts index 2bff354..37bed6b 100644 --- a/src/dialect/mysql-dialect.ts +++ b/src/dialect/mysql-dialect.ts @@ -42,15 +42,15 @@ export class MysqlDialect extends KyselyMysqlDialect implements Dialect { builder: CreateTableBuilder, tableName: TB, columnName: string, - targetType: ColumnDataType, + columnType: ColumnDataType, referenceTable: string, referenceColumnName: string, onDeleteAction: 'cascade' | 'no action' | 'restrict' | 'set null' | 'set default', ): CreateTableBuilder { return builder - .addColumn(columnName, targetType, (col) => col.notNull()) + .addColumn(columnName, columnType, (col) => col.notNull()) .addForeignKeyConstraint( - `${referenceTable}${referenceColumnName}_${tableName}${columnName}`, + `${referenceTable}_${referenceColumnName}__${tableName}_${columnName}`, [columnName], referenceTable, [referenceColumnName], @@ -58,7 +58,7 @@ export class MysqlDialect extends KyselyMysqlDialect implements Dialect { ); } - insertIntoReturning = AnyColumn>( + insertThenReturnId = AnyColumn>( db: Transaction | Kysely, table: TB & string, values: InsertObject, diff --git a/src/dialect/postgres-dialect.ts b/src/dialect/postgres-dialect.ts index d7f011d..80aea61 100644 --- a/src/dialect/postgres-dialect.ts +++ b/src/dialect/postgres-dialect.ts @@ -33,17 +33,17 @@ export class PostgresDialect extends KyselyPostgresDialect implements Dialect { addReferencedColumn( builder: CreateTableBuilder, - tableName: TB, + _tableName: TB, columnName: string, - targetType: ColumnDataType, + columnType: ColumnDataType, referenceTable: string, referenceColumnName: string, onDeleteAction: 'cascade' | 'no action' | 'restrict' | 'set null' | 'set default', ): CreateTableBuilder { - return builder.addColumn(columnName, targetType, (col) => col.notNull().references(`${referenceTable}.${referenceColumnName}`).onDelete(onDeleteAction)); + return builder.addColumn(columnName, columnType, (col) => col.notNull().references(`${referenceTable}.${referenceColumnName}`).onDelete(onDeleteAction)); } - insertIntoReturning = any>( + insertThenReturnId = any>( db: Transaction | Kysely, table: TB & string, values: InsertObject, diff --git a/src/dialect/sqlite-dialect.ts b/src/dialect/sqlite-dialect.ts index 0c8d347..ccc1e48 100644 --- a/src/dialect/sqlite-dialect.ts +++ b/src/dialect/sqlite-dialect.ts @@ -41,15 +41,15 @@ export class SqliteDialect extends KyselySqliteDialect implements Dialect { builder: CreateTableBuilder, _tableName: TB, columnName: string, - targetType: ColumnDataType, + columnType: ColumnDataType, referenceTable: string, referenceColumnName: string, onDeleteAction: 'cascade' | 'no action' | 'restrict' | 'set null' | 'set default', ): CreateTableBuilder { - return builder.addColumn(columnName, targetType, (col) => col.notNull().references(`${referenceTable}.${referenceColumnName}`).onDelete(onDeleteAction)); + return builder.addColumn(columnName, columnType, (col) => col.notNull().references(`${referenceTable}.${referenceColumnName}`).onDelete(onDeleteAction)); } - insertIntoReturning = any>( + insertThenReturnId = any>( db: Transaction | Kysely, table: TB & string, values: InsertObject, diff --git a/src/event-log-sql.ts b/src/event-log-sql.ts index c0d9a00..be35dc3 100644 --- a/src/event-log-sql.ts +++ b/src/event-log-sql.ts @@ -125,9 +125,9 @@ export class EventLogSql implements EventLog { ...appendIndexes, }; - // we use the dialect-specific `insertIntoReturning` in order to be able to extract the `insertId` + // we use the dialect-specific `insertThenReturnId` in order to be able to extract the `insertId` const result = await this.#dialect - .insertIntoReturning(tx, 'eventLog', eventIndexValues, 'watermark as insertId') + .insertThenReturnId(tx, 'eventLog', eventIndexValues, 'watermark as insertId') .executeTakeFirstOrThrow(); // if tags exist, we execute those within the transaction associating them with the `insertId`. diff --git a/src/message-store-sql.ts b/src/message-store-sql.ts index 0a06b22..db89afa 100644 --- a/src/message-store-sql.ts +++ b/src/message-store-sql.ts @@ -93,7 +93,7 @@ export class MessageStoreSql implements MessageStore { createTable = this.#dialect.addAutoIncrementingColumn(createTable, 'id', (col) => col.primaryKey()); createTable = this.#dialect.addBlobColumn(createTable, 'encodedMessageBytes', (col) => col.notNull()); createRecordsTagsTable = this.#dialect.addAutoIncrementingColumn(createRecordsTagsTable, 'id', (col) => col.primaryKey()); - createRecordsTagsTable = this.#dialect.addReferencedColumn(createRecordsTagsTable, 'messageStoreRecordsTags', 'messageStoreId', 'integer', 'messageStore', 'id', 'cascade'); + createRecordsTagsTable = this.#dialect.addReferencedColumn(createRecordsTagsTable, 'messageStoreRecordsTags', 'messageInsertId', 'integer', 'messageStore', 'id', 'cascade'); createRecordsTagValuesTable = this.#dialect.addReferencedColumn(createRecordsTagValuesTable, 'messageStoreRecordsTagValues', 'tagId', 'integer', 'messageStoreRecordsTags', 'id', 'cascade'); await createTable.execute(); @@ -176,9 +176,9 @@ export class MessageStoreSql implements MessageStore { ...putIndexes }; - // we use the dialect-specific `insertIntoReturning` in order to be able to extract the `insertId` + // we use the dialect-specific `insertThenReturnId` in order to be able to extract the `insertId` const result = await this.#dialect - .insertIntoReturning(tx, 'messageStore', messageIndexValues, 'id as insertId') + .insertThenReturnId(tx, 'messageStore', messageIndexValues, 'id as insertId') .executeTakeFirstOrThrow(); // if tags exist, we execute those within the transaction associating them with the `insertId`. @@ -239,7 +239,7 @@ export class MessageStoreSql implements MessageStore { let query = this.#db .selectFrom('messageStore') - .leftJoin('messageStoreRecordsTags', 'messageStoreRecordsTags.messageStoreId', 'messageStore.id') + .leftJoin('messageStoreRecordsTags', 'messageStoreRecordsTags.messageInsertId', 'messageStore.id') .leftJoin('messageStoreRecordsTagValues', 'messageStoreRecordsTagValues.tagId', 'messageStoreRecordsTags.id') .select('messageCid') .distinct() diff --git a/src/types.ts b/src/types.ts index d3334fe..8f3aedf 100644 --- a/src/types.ts +++ b/src/types.ts @@ -80,7 +80,7 @@ type MessageStoreTable = { type MessageStoreRecordsTagsTable = { id: Generated; tag: string; - messageStoreId: number; + messageInsertId: number; }; type MessageStoreRecordsTagValuesTable = { diff --git a/src/utils/tags.ts b/src/utils/tags.ts index 71778e1..6de67a7 100644 --- a/src/utils/tags.ts +++ b/src/utils/tags.ts @@ -17,18 +17,18 @@ export class TagTables { constructor(private dialect: Dialect, private table: 'messageStore' | 'eventLog'){} /** - * Inserts the given tag and value associates to the parent Id. + * Inserts the given tag and value associates to the foreign column's `insertId` it is associated with. */ async executeTagsInsert( - id: number, + foreignInsertId: number, tags: KeyValues, tx: Transaction, ):Promise { const tagTable = this.table === 'messageStore' ? 'messageStoreRecordsTags' : 'eventLogRecordsTags'; - const tagValue = tagTable === 'messageStoreRecordsTags' ? { messageStoreId: id } : { eventLogWatermark: id }; + const tagValue = tagTable === 'messageStoreRecordsTags' ? { messageInsertId: foreignInsertId } : { eventLogWatermark: foreignInsertId }; for (const tag in tags) { - const { insertId } = await this.dialect.insertIntoReturning(tx, tagTable, { ...tagValue, tag }, 'id as insertId').executeTakeFirstOrThrow(); + const { insertId } = await this.dialect.insertThenReturnId(tx, tagTable, { ...tagValue, tag }, 'id as insertId').executeTakeFirstOrThrow(); const tagId = Number(insertId); const tagValues = tags[tag]; const values = Array.isArray(tagValues) ? tagValues : [ tagValues ];