Browse Source

feat : csv(wip)

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/714/head
Pranav C 3 years ago
parent
commit
d7e51016c1
  1. 7
      packages/nocodb/package-lock.json
  2. 1
      packages/nocodb/package.json
  3. 158
      packages/nocodb/src/lib/dataMapper/lib/sql/BaseModelSql.ts

7
packages/nocodb/package-lock.json generated

@ -1,6 +1,6 @@
{
"name": "nocodb",
"version": "0.11.46",
"version": "0.80.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -13770,6 +13770,11 @@
"integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==",
"dev": true
},
"papaparse": {
"version": "5.3.1",
"resolved": "https://registry.npmjs.org/papaparse/-/papaparse-5.3.1.tgz",
"integrity": "sha512-Dbt2yjLJrCwH2sRqKFFJaN5XgIASO9YOFeFP8rIBRG2Ain8mqk5r1M6DkfvqEVozVcz3r3HaUGw253hA1nLIcA=="
},
"parallel-transform": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/parallel-transform/-/parallel-transform-1.2.0.tgz",

1
packages/nocodb/package.json

@ -153,6 +153,7 @@
"nodemailer": "^6.4.10",
"ora": "^4.0.4",
"os-locale": "^5.0.0",
"papaparse": "^5.3.1",
"parse-database-url": "^0.3.0",
"passport": "^0.4.1",
"passport-auth-token": "^1.0.1",

158
packages/nocodb/src/lib/dataMapper/lib/sql/BaseModelSql.ts

@ -5,6 +5,7 @@ import Validator from 'validator';
import BaseModel, { XcFilter, XcFilterWithAlias } from '../BaseModel';
import formulaQueryBuilder from './formulaQueryBuilderFromString';
import genRollupSelect from './genRollupSelect';
import Papaparse from 'papaparse';
/**
* Base class for models
@ -22,6 +23,7 @@ class BaseModelSql extends BaseModel {
private _selectFormulasObj: any;
private _defaultNestedQueryParams: any;
private _nestedProps: { [prop: string]: any };
private readonly _primaryCol: any;
/**
*
@ -66,6 +68,7 @@ class BaseModelSql extends BaseModel {
this.columns = columns;
this.pks = columns.filter(c => c.pk === true);
this.primaryCol = columns.find(c => c.pv)?._cn;
this.hasManyRelations = hasMany;
this.belongsToRelations = belongsTo;
this.manyToManyRelations = manyToMany;
@ -2403,6 +2406,161 @@ class BaseModelSql extends BaseModel {
return arr;
}, []);
}
public async extractCsvData(args) {
const rows = await this.nestedList(args);
const csvRows = [];
for (const row of rows) {
const csvRow = {};
for (const column of this.columns) {
if (column._cn in row) {
csvRow[column._cn] = this.serializeCellValue({
value: row[column._cn],
column
});
}
}
for (const vColumn of this.virtualColumns) {
if (vColumn._cn in row && !vColumn.bt && !vColumn.hm && !vColumn.mm) {
if (vColumn.lk) {
} else {
csvRow[vColumn._cn] = row[vColumn._cn];
}
}
}
for (const [prop, vColumn] of Object.entries(this.nestedProps)) {
if (prop in row) {
if (Array.is) csvRow[prop] = row.map;
}
}
csvRows.push(csvRow);
}
return Papaparse.unparse(csvRows);
//
//
// return Promise.all(this.data.map(async (r) => {
// const row = {}
// for (const col of this.availableColumns) {
// if (col.virtual) {
// let prop, cn
// if (col.mm || (col.lk && col.lk.type === 'mm')) {
// const tn = col.mm ? col.mm.rtn : col.lk.ltn
// const _tn = col.mm ? col.mm._rtn : col.lk._ltn
// await this.$store.dispatch('meta/ActLoadMeta', {
// env: this.nodes.env,
// dbAlias: this.nodes.dbAlias,
// tn
// })
//
// prop = `${_tn}MMList`
// cn = col.lk
// ? col.lk._lcn
// : (this.$store.state.meta.metas[tn].columns.find(c => c.pv) || this.$store.state.meta.metas[tn].columns.find(c => c.pk) || {})._cn
//
// row[col._cn] = r.row[prop] && r.row[prop].map(r => cn && r[cn])
// } else if (col.hm || (col.lk && col.lk.type === 'hm')) {
// const tn = col.hm ? col.hm.tn : col.lk.ltn
// const _tn = col.hm ? col.hm._tn : col.lk._ltn
//
// await this.$store.dispatch('meta/ActLoadMeta', {
// env: this.nodes.env,
// dbAlias: this.nodes.dbAlias,
// tn
// })
//
// prop = `${_tn}List`
// cn = col.lk
// ? col.lk._lcn
// : (this.$store.state.meta.metas[tn].columns.find(c => c.pv) ||
// this.$store.state.meta.metas[tn].columns.find(c => c.pk))._cn
// row[col._cn] = r.row[prop] && r.row[prop].map(r => cn && r[cn])
// } else if (col.bt || (col.lk && col.lk.type === 'bt')) {
// const tn = col.bt ? col.bt.rtn : col.lk.ltn
// const _tn = col.bt ? col.bt._rtn : col.lk._ltn
// await this.$store.dispatch('meta/ActLoadMeta', {
// env: this.nodes.env,
// dbAlias: this.nodes.dbAlias,
// tn
// })
//
// prop = `${_tn}Read`
// cn = col.lk
// ? col.lk._lcn
// : (this.$store.state.meta.metas[tn].columns.find(c => c.pv) ||
// this.$store.state.meta.metas[tn].columns.find(c => c.pk) || {})._cn
// row[col._cn] = r.row[prop] &&
// r.row[prop] && cn && r.row[prop][cn]
// } else {
// row[col._cn] = r.row[col._cn]
// }
// } else if (col.uidt === 'Attachment') {
// let data = []
// try {
// if (typeof r.row[col._cn] === 'string') {
// data = JSON.parse(r.row[col._cn])
// } else if (r.row[col._cn]) {
// data = r.row[col._cn]
// }
// } catch {
// }
// row[col._cn] = (data || []).map(a => `${a.title}(${a.url})`)
// } else {
// row[col._cn] = r.row[col._cn]
// }
// }
// return row
// }))
}
// async exportCsv() {
// const blob = new Blob([Papaparse.unparse(await this.extractCsvData())], {type: 'text/plain;charset=utf-8'})
// FileSaver.saveAs(blob, `${this.meta._tn}_exported.csv`)
// }
private serializeCellValue({
value,
...args
}: {
column?: any;
value: any;
columnName?: string;
}) {
if (!args.column || !args.columnName) {
return value;
}
const column =
args.column || this.columns.find(c => c._cn === this.columnToAlias);
switch (column?.uidt) {
case 'Attachment': {
let data = value;
try {
if (typeof value === 'string') {
data = JSON.parse(value);
}
} catch {}
return (data || []).map(
attachment => `${attachment.title}(${encodeURI(attachment.url)})`
);
}
default:
return value;
}
}
public get primaryCol(): string {
return this._primaryCol?.cn;
}
public get primaryColAlias(): string {
return this._primaryCol?._cn;
}
}
export { BaseModelSql };

Loading…
Cancel
Save