feat(core): Fix types for Data Store endpoints (no-changelog) (#18335)

This commit is contained in:
Daria
2025-08-14 14:23:19 +03:00
committed by GitHub
parent 8259b5f5c6
commit 087509371b
5 changed files with 58 additions and 56 deletions

View File

@@ -99,8 +99,8 @@ describe('dataStoreAggregate', () => {
// ASSERT // ASSERT
expect(result.data).toEqual( expect(result.data).toEqual(
expect.arrayContaining([ expect.arrayContaining([
expect.objectContaining({ id: ds1!.id, name: ds1!.name }), expect.objectContaining({ id: ds1.id, name: ds1.name }),
expect.objectContaining({ id: ds2!.id, name: ds2!.name }), expect.objectContaining({ id: ds2.id, name: ds2.name }),
]), ]),
); );
expect(result.count).toBe(2); expect(result.count).toBe(2);
@@ -162,13 +162,13 @@ describe('dataStoreAggregate', () => {
// ACT // ACT
const result = await dataStoreAggregateService.getManyAndCount(user, { const result = await dataStoreAggregateService.getManyAndCount(user, {
filter: { id: ds2!.id }, filter: { id: ds2.id },
skip: 0, skip: 0,
take: 10, take: 10,
}); });
// ASSERT // ASSERT
expect(result.data).toEqual([expect.objectContaining({ id: ds2!.id, name: ds2!.name })]); expect(result.data).toEqual([expect.objectContaining({ id: ds2.id, name: ds2.name })]);
expect(result.count).toBe(1); expect(result.count).toBe(1);
}); });
@@ -208,7 +208,7 @@ describe('dataStoreAggregate', () => {
// ASSERT // ASSERT
expect(result.data.length).toBe(1); expect(result.data.length).toBe(1);
expect([ds1!.id, ds2!.id, ds3!.id]).toContain(result.data[0].id); expect([ds1.id, ds2.id, ds3.id]).toContain(result.data[0].id);
expect(result.count).toBe(3); expect(result.count).toBe(3);
}); });
}); });

View File

@@ -54,7 +54,7 @@ describe('dataStore', () => {
name, name,
columns: [], columns: [],
}); });
const { id: dataStoreId } = result!; const { id: dataStoreId } = result;
// ASSERT // ASSERT
expect(result).toEqual({ expect(result).toEqual({
@@ -104,7 +104,7 @@ describe('dataStore', () => {
name, name,
columns: [], columns: [],
}); });
const { id: dataStoreId } = result!; const { id: dataStoreId } = result;
const created = await dataStoreRepository.findOneBy({ name, projectId: project1.id }); const created = await dataStoreRepository.findOneBy({ name, projectId: project1.id });
expect(created?.id).toBe(dataStoreId); expect(created?.id).toBe(dataStoreId);
@@ -115,7 +115,7 @@ describe('dataStore', () => {
name: 'dataStoreWithColumns', name: 'dataStoreWithColumns',
columns: [{ name: 'foo', type: 'string' }], columns: [{ name: 'foo', type: 'string' }],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
await expect(dataStoreService.getColumns(dataStoreId)).resolves.toEqual([ await expect(dataStoreService.getColumns(dataStoreId)).resolves.toEqual([
{ {
@@ -154,7 +154,7 @@ describe('dataStore', () => {
}); });
// ASSERT // ASSERT
const { project } = result!; const { project } = result;
expect(project.id).toBe(project1.id); expect(project.id).toBe(project1.id);
expect(project.name).toBe(project1.name); expect(project.name).toBe(project1.name);
}); });
@@ -188,7 +188,7 @@ describe('dataStore', () => {
name: 'myDataStore1', name: 'myDataStore1',
columns: [], columns: [],
}); });
const { id: dataStoreId, updatedAt } = dataStore!; const { id: dataStoreId, updatedAt } = dataStore;
// ACT // ACT
// Wait to get second difference // Wait to get second difference
@@ -222,7 +222,7 @@ describe('dataStore', () => {
name: 'myDataStore', name: 'myDataStore',
columns: [], columns: [],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
// ACT // ACT
const result = dataStoreService.updateDataStore(dataStoreId, { name: '' }); const result = dataStoreService.updateDataStore(dataStoreId, { name: '' });
@@ -237,7 +237,7 @@ describe('dataStore', () => {
name: 'myDataStore1', name: 'myDataStore1',
columns: [], columns: [],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
// ACT // ACT
const result = dataStoreService.updateDataStore(dataStoreId, { name: ' aNewName ' }); const result = dataStoreService.updateDataStore(dataStoreId, { name: ' aNewName ' });
@@ -261,7 +261,7 @@ describe('dataStore', () => {
name: 'myDataStoreNew', name: 'myDataStoreNew',
columns: [], columns: [],
}); });
const { id: dataStoreNewId } = dataStoreNew!; const { id: dataStoreNewId } = dataStoreNew;
// ACT // ACT
const result = dataStoreService.updateDataStore(dataStoreNewId, { name }); const result = dataStoreService.updateDataStore(dataStoreNewId, { name });
@@ -280,7 +280,7 @@ describe('dataStore', () => {
name: 'myDataStore1', name: 'myDataStore1',
columns: [], columns: [],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
// ACT // ACT
const result = await dataStoreService.deleteDataStore(dataStoreId); const result = await dataStoreService.deleteDataStore(dataStoreId);
@@ -319,7 +319,7 @@ describe('dataStore', () => {
name: 'dataStoreWithColumns', name: 'dataStoreWithColumns',
columns: existingColumns, columns: existingColumns,
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
const columns: AddDataStoreColumnDto[] = [ const columns: AddDataStoreColumnDto[] = [
{ name: 'myColumn1', type: 'string' }, { name: 'myColumn1', type: 'string' },
@@ -404,7 +404,7 @@ describe('dataStore', () => {
name: 'dataStore', name: 'dataStore',
columns: [], columns: [],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
// ACT // ACT
const result = await dataStoreService.addColumn(dataStoreId, { const result = await dataStoreService.addColumn(dataStoreId, {
@@ -437,7 +437,7 @@ describe('dataStore', () => {
}, },
], ],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
// ACT // ACT
const result = dataStoreService.addColumn(dataStoreId, { const result = dataStoreService.addColumn(dataStoreId, {
@@ -472,7 +472,7 @@ describe('dataStore', () => {
name: 'dataStore', name: 'dataStore',
columns: [], columns: [],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
const c1 = await dataStoreService.addColumn(dataStoreId, { const c1 = await dataStoreService.addColumn(dataStoreId, {
name: 'myColumn1', name: 'myColumn1',
@@ -515,7 +515,7 @@ describe('dataStore', () => {
}, },
], ],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
// ACT // ACT
const result = dataStoreService.deleteColumn(dataStoreId, 'thisIsNotAnId'); const result = dataStoreService.deleteColumn(dataStoreId, 'thisIsNotAnId');
@@ -532,7 +532,7 @@ describe('dataStore', () => {
name: 'dataStore', name: 'dataStore',
columns: [], columns: [],
}); });
const c1 = await dataStoreService.addColumn(dataStore!.id, { const c1 = await dataStoreService.addColumn(dataStore.id, {
name: 'myColumn1', name: 'myColumn1',
type: 'string', type: 'string',
}); });
@@ -554,7 +554,7 @@ describe('dataStore', () => {
name: 'dataStore', name: 'dataStore',
columns: [], columns: [],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
const c1 = await dataStoreService.addColumn(dataStoreId, { const c1 = await dataStoreService.addColumn(dataStoreId, {
name: 'myColumn1', name: 'myColumn1',
@@ -600,7 +600,7 @@ describe('dataStore', () => {
name: 'dataStore', name: 'dataStore',
columns: [], columns: [],
}); });
const { name } = dataStore!; const { name } = dataStore;
// ACT // ACT
const result = await dataStoreService.getManyAndCount({ const result = await dataStoreService.getManyAndCount({
@@ -629,13 +629,13 @@ describe('dataStore', () => {
name: 'myDataStore1', name: 'myDataStore1',
columns: [], columns: [],
}); });
const { id: dataStoreId1 } = dataStore1!; const { id: dataStoreId1 } = dataStore1;
const dataStore2 = await dataStoreService.createDataStore(project1.id, { const dataStore2 = await dataStoreService.createDataStore(project1.id, {
name: 'myDataStore2', name: 'myDataStore2',
columns: [], columns: [],
}); });
const { id: dataStoreId2 } = dataStore2!; const { id: dataStoreId2 } = dataStore2;
// ACT // ACT
const result = await dataStoreService.getManyAndCount({ const result = await dataStoreService.getManyAndCount({
@@ -663,14 +663,14 @@ describe('dataStore', () => {
name: 'myDataStore', name: 'myDataStore',
columns: [], columns: [],
}); });
const { name } = dataStore!; const { name } = dataStore;
const names = [name]; const names = [name];
for (let i = 0; i < 10; ++i) { for (let i = 0; i < 10; ++i) {
const ds = await dataStoreService.createDataStore(project1.id, { const ds = await dataStoreService.createDataStore(project1.id, {
name: `anotherDataStore${i}`, name: `anotherDataStore${i}`,
columns: [], columns: [],
}); });
names.push(ds!.name); names.push(ds.name);
} }
// ACT // ACT
@@ -689,7 +689,7 @@ describe('dataStore', () => {
name: 'myDataStore', name: 'myDataStore',
columns: [], columns: [],
}); });
const { name } = dataStore!; const { name } = dataStore;
const names = [name]; const names = [name];
for (let i = 0; i < 10; ++i) { for (let i = 0; i < 10; ++i) {
@@ -697,7 +697,7 @@ describe('dataStore', () => {
name: `anotherDataStore${i}`, name: `anotherDataStore${i}`,
columns: [], columns: [],
}); });
names.push(ds!.name); names.push(ds.name);
} }
// ACT // ACT
@@ -748,7 +748,7 @@ describe('dataStore', () => {
name: 'myDataStore', name: 'myDataStore',
columns, columns,
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
// ACT // ACT
const result = await dataStoreService.getManyAndCount({ const result = await dataStoreService.getManyAndCount({
@@ -887,7 +887,7 @@ describe('dataStore', () => {
name: 'ds1', name: 'ds1',
columns: [], columns: [],
}); });
const { id: ds1Id } = ds1!; const { id: ds1Id } = ds1;
// Wait to get seconds difference // Wait to get seconds difference
await new Promise((resolve) => setTimeout(resolve, 1001)); await new Promise((resolve) => setTimeout(resolve, 1001));
@@ -930,7 +930,7 @@ describe('dataStore', () => {
{ name: 'c4', type: 'string' }, { name: 'c4', type: 'string' },
], ],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
// ACT // ACT
const rows = [ const rows = [
@@ -966,7 +966,7 @@ describe('dataStore', () => {
{ name: 'c2', type: 'string' }, { name: 'c2', type: 'string' },
], ],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
// Insert initial row // Insert initial row
await dataStoreService.insertRows(dataStoreId, [{ c1: 1, c2: 'foo' }]); await dataStoreService.insertRows(dataStoreId, [{ c1: 1, c2: 'foo' }]);
@@ -1000,7 +1000,7 @@ describe('dataStore', () => {
{ name: 'c4', type: 'string' }, { name: 'c4', type: 'string' },
], ],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
// ACT // ACT
const result = dataStoreService.insertRows(dataStoreId, [ const result = dataStoreService.insertRows(dataStoreId, [
@@ -1023,7 +1023,7 @@ describe('dataStore', () => {
{ name: 'c4', type: 'string' }, { name: 'c4', type: 'string' },
], ],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
// ACT // ACT
const result = dataStoreService.insertRows(dataStoreId, [ const result = dataStoreService.insertRows(dataStoreId, [
@@ -1046,7 +1046,7 @@ describe('dataStore', () => {
{ name: 'c4', type: 'string' }, { name: 'c4', type: 'string' },
], ],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
// ACT // ACT
const result = dataStoreService.insertRows(dataStoreId, [ const result = dataStoreService.insertRows(dataStoreId, [
@@ -1098,7 +1098,7 @@ describe('dataStore', () => {
name: 'dataStore', name: 'dataStore',
columns: [{ name: 'c1', type: 'number' }], columns: [{ name: 'c1', type: 'number' }],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
// ACT // ACT
const result = dataStoreService.insertRows(dataStoreId, [{ c1: 3 }, { c1: true }]); const result = dataStoreService.insertRows(dataStoreId, [{ c1: 3 }, { c1: true }]);
@@ -1119,7 +1119,7 @@ describe('dataStore', () => {
{ name: 'age', type: 'number' }, { name: 'age', type: 'number' },
], ],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
// Insert initial row // Insert initial row
await dataStoreService.insertRows(dataStoreId, [ await dataStoreService.insertRows(dataStoreId, [
@@ -1154,7 +1154,7 @@ describe('dataStore', () => {
{ name: 'age', type: 'number' }, { name: 'age', type: 'number' },
], ],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
// Insert initial row // Insert initial row
await dataStoreService.insertRows(dataStoreId, [ await dataStoreService.insertRows(dataStoreId, [
@@ -1195,7 +1195,7 @@ describe('dataStore', () => {
{ name: 'c4', type: 'string' }, { name: 'c4', type: 'string' },
], ],
}); });
const { id: dataStoreId } = dataStore!; const { id: dataStoreId } = dataStore;
const rows = [ const rows = [
{ c1: 3, c2: true, c3: new Date(0), c4: 'hello?' }, { c1: 3, c2: true, c3: new Date(0), c4: 'hello?' },

View File

@@ -135,7 +135,7 @@ export class DataStoreRowsRepository {
async getManyAndCount(dataStoreId: DataStoreUserTableName, dto: ListDataStoreContentQueryDto) { async getManyAndCount(dataStoreId: DataStoreUserTableName, dto: ListDataStoreContentQueryDto) {
const [countQuery, query] = this.getManyQuery(dataStoreId, dto); const [countQuery, query] = this.getManyQuery(dataStoreId, dto);
const data: Array<Record<string, unknown>> = await query.select('*').getRawMany(); const data: DataStoreRows = await query.select('*').getRawMany();
const countResult = await countQuery.select('COUNT(*) as count').getRawOne<{ const countResult = await countQuery.select('COUNT(*) as count').getRawOne<{
count: number | string | null; count: number | string | null;
}>(); }>();

View File

@@ -65,10 +65,12 @@ export class DataStoreRepository extends Repository<DataStore> {
throw new UnexpectedError('Data store creation failed'); throw new UnexpectedError('Data store creation failed');
} }
return await this.findOne({ const createdDataStore = await this.findOneOrFail({
where: { id: dataStoreId }, where: { id: dataStoreId },
relations: ['project', 'columns'], relations: ['project', 'columns'],
}); });
return createdDataStore;
} }
async deleteDataStore(dataStoreId: string, entityManager?: EntityManager) { async deleteDataStore(dataStoreId: string, entityManager?: EntityManager) {

View File

@@ -157,8 +157,22 @@ export class DataStoreService {
return await this.dataStoreColumnRepository.getColumns(dataStoreId); return await this.dataStoreColumnRepository.getColumns(dataStoreId);
} }
async insertRows(dataStoreId: string, rows: DataStoreRows) {
await this.validateRows(dataStoreId, rows);
const columns = await this.dataStoreColumnRepository.getColumns(dataStoreId);
return await this.dataStoreRowsRepository.insertRows(toTableName(dataStoreId), rows, columns);
}
async upsertRows(dataStoreId: string, dto: UpsertDataStoreRowsDto) {
await this.validateRows(dataStoreId, dto.rows);
const columns = await this.dataStoreColumnRepository.getColumns(dataStoreId);
return await this.dataStoreRowsRepository.upsertRows(toTableName(dataStoreId), dto, columns);
}
// TODO: move to utils and test // TODO: move to utils and test
private normalizeRows(rows: Array<Record<string, unknown>>, columns: DataStoreColumn[]) { private normalizeRows(rows: DataStoreRows, columns: DataStoreColumn[]): DataStoreRows {
const typeMap = new Map(columns.map((col) => [col.name, col.type])); const typeMap = new Map(columns.map((col) => [col.name, col.type]));
return rows.map((row) => { return rows.map((row) => {
const normalized = { ...row }; const normalized = { ...row };
@@ -239,20 +253,6 @@ export class DataStoreService {
} }
} }
async insertRows(dataStoreId: string, rows: DataStoreRows) {
await this.validateRows(dataStoreId, rows);
const columns = await this.dataStoreColumnRepository.getColumns(dataStoreId);
return await this.dataStoreRowsRepository.insertRows(toTableName(dataStoreId), rows, columns);
}
async upsertRows(dataStoreId: string, dto: UpsertDataStoreRowsDto) {
await this.validateRows(dataStoreId, dto.rows);
const columns = await this.dataStoreColumnRepository.getColumns(dataStoreId);
return await this.dataStoreRowsRepository.upsertRows(toTableName(dataStoreId), dto, columns);
}
private async validateDataStoreExists(dataStoreId: string, msg?: string) { private async validateDataStoreExists(dataStoreId: string, msg?: string) {
const existingTable = await this.dataStoreRepository.findOneBy({ const existingTable = await this.dataStoreRepository.findOneBy({
id: dataStoreId, id: dataStoreId,