Skip to content

Commit

Permalink
migration fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wulff007 committed Oct 27, 2024
1 parent 0f69092 commit f1f0e3f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
6 changes: 5 additions & 1 deletion migrations/20241011063712-datasources.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ exports.up = async function(db) {
"deleted_by int REFERENCES users(id) ON DELETE NO ACTION ON UPDATE CASCADE," +
"UNIQUE(account_id, alias)" +
");" +
"DELETE FROM datasource_fields;"+
"ALTER TABLE datasource_fields ADD CONSTRAINT ds_fields FOREIGN KEY (datasource_id) REFERENCES datasource (id);" +
"ALTER TABLE datasource_fields ADD UNIQUE(datasource_id, alias);"+
"ALTER TABLE datasource_fields RENAME COLUMN required TO nullable;"
Expand Down Expand Up @@ -101,10 +102,11 @@ const getDatasource = async(db) => {

const populateFields = async (db) => {
let fields = await getFields(db)
//console.log(fields)


for (let i in fields) {
let field = fields[i]
console.log("field", field)
let query = "INSERT INTO datasource_fields (" +
"datasource_id," +
"datasource_alias," +
Expand Down Expand Up @@ -146,6 +148,8 @@ const populateFields = async (db) => {
`${field.updated_by},` +
`${field.account_id},` +
`${field.version})`

console.log(query)
await db.runSql(query)
}

Expand Down
4 changes: 3 additions & 1 deletion src/data-indexer/data-indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export class DataIndexer {
throw e
}


let dsFields = dataSourceConfig.fields
const allFields = new Map(dsFields.map(i => [i.alias, i]))

Expand Down Expand Up @@ -379,6 +378,9 @@ export class DataIndexer {


private prepareItemForUser(item: any, fields: Map<string, DatasourceField>, fieldsToSelect: string[], timezone = this.timezone):DataItem {
if (!fieldsToSelect || !fieldsToSelect.length)
return item

let o = item
fieldsToSelect.forEach(f => {
if (fields.has(f) && ['datetime', 'date'].includes(fields.get(f).type)) {
Expand Down
2 changes: 2 additions & 0 deletions src/data-indexer/internal-db.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export class InternalDbAdapter extends IndexerDataAdapter {
query += ` WHERE id IN (${ids.join(',')})`
}

console.log(query)

let items = await this.dataSource.query(query)

let docs = []
Expand Down
25 changes: 15 additions & 10 deletions src/datasources/datasourceV2.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class DataSourceV2Service {

// For internal use without context
// Don't use it to response to user
async getConfigByAlias(alias: string): Promise<DataSourceV2Dto> {
async getConfigByAlias(alias: string, withNestedFields = false): Promise<DataSourceV2Dto> {
const rep = this.datasource.getRepository(DatasourceV2Entity)
let item = await rep
.createQueryBuilder('ds')
Expand All @@ -147,7 +147,7 @@ export class DataSourceV2Service {
.getOne()

if (item) {
item.fields = (await this.getFieldsMany({datasource: alias, nested: true})).items
item.fields = (await this.getFieldsMany({datasource: alias, nested: withNestedFields})).items
}

if (item) {
Expand All @@ -157,8 +157,8 @@ export class DataSourceV2Service {
return item ? item : null
}

async getDataSource(alias: string, context: Context) {
let ds = await this.getConfigByAlias(alias)
async getDataSource(alias: string, withNestedFields: boolean, context: Context) {
let ds = await this.getConfigByAlias(alias, withNestedFields)
if (!ds)
throw `Datasource ${alias} not found`

Expand Down Expand Up @@ -392,7 +392,12 @@ export class DataSourceV2Service {
})

await queryRunner.commitTransaction()
await this.upsertIndex(ds.alias, context)

try {
await this.upsertIndex(ds.alias, context)
} catch (e) {
this.logger.error(e)
}

return {
id: data.id
Expand Down Expand Up @@ -661,27 +666,27 @@ export class DataSourceV2Service {
}

async getDataMany(alias: string, params: GetDataManyRequestDto, context: Context) {
let inst = await this.getDataSource(alias, context)
let inst = await this.getDataSource(alias, true, context)
return inst.getMany(params)
}

async getTotals(alias: string, params: GetDataManyRequestDto, context: Context) {
let inst = await this.getDataSource(alias, context)
let inst = await this.getDataSource(alias, false, context)
return inst.getTotals(params)
}

async exportData(alias: string, params: ExportDataRequestDto, context: Context) {
let inst = await this.getDataSource(alias, context)
let inst = await this.getDataSource(alias, true, context)
return inst.exportData(params)
}

async upsertDataSourceItems(alias: string, data: UpsertDataSourceDataRequestDto, context: Context) {
let inst = await this.getDataSource(alias, context)
let inst = await this.getDataSource(alias, false, context)
return inst.upsert(data)
}

async deleteDataSourceItems(alias, data: DeleteDataSourceDataRequestDto, context: Context) {
let inst = await this.getDataSource(alias, context)
let inst = await this.getDataSource(alias, false, context)
if (data.ids) {
return inst.deleteById({ids: data.ids, soft: data.soft !== undefined ? data.soft : true})
} else if (data.where) {
Expand Down
5 changes: 2 additions & 3 deletions src/functions/functions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,8 @@ class DataSourceV2ScriptHelper {
private readonly service: DataSourceV2Service
private readonly context: Context

async getByAlias(alias: string) {
console.log('getByAliasV2', alias)
return await this.service.getDataSource(alias, this.context)
async getByAlias(alias: string, withNestedFields = false) {
return await this.service.getDataSource(alias, withNestedFields, this.context)
}
}

Expand Down

0 comments on commit f1f0e3f

Please sign in to comment.