Skip to content

Commit 8fd30ef

Browse files
authored
NocoDB - New record in view (#14483)
* new-record-in-view * pnpm-lock.yaml
1 parent 34c316d commit 8fd30ef

File tree

12 files changed

+182
-118
lines changed

12 files changed

+182
-118
lines changed

components/nocodb/actions/add-record/add-record.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "nocodb-add-record",
66
name: "Add Record",
77
description: "This action adds a record in a table. [See the documentation](https://data-apis-v2.nocodb.com/#tag/Table-Records/operation/db-data-table-row-create)",
8-
version: "0.0.4",
8+
version: "0.0.5",
99
type: "action",
1010
props: {
1111
...common.props,

components/nocodb/actions/delete-record/delete-record.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "nocodb-delete-record",
66
name: "Delete Record",
77
description: "This action deletes a row in a table. [See the documentation](https://data-apis-v2.nocodb.com/#tag/Table-Records/operation/db-data-table-row-delete)",
8-
version: "0.0.4",
8+
version: "0.0.5",
99
type: "action",
1010
props: {
1111
...common.props,

components/nocodb/actions/get-record/get-record.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "nocodb-get-record",
66
name: "Get Record (from row number)",
77
description: "This action gets a row by row Id. [See the documentation](https://data-apis-v2.nocodb.com/#tag/Table-Records/operation/db-data-table-row-read)",
8-
version: "0.0.4",
8+
version: "0.0.5",
99
type: "action",
1010
props: {
1111
...common.props,

components/nocodb/actions/list-records-matching-criteria/list-records-matching-criteria.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "nocodb-list-records-matching-criteria",
66
name: "List Records in Table Matching Criteria",
77
description: "This action lists all rows in a table. [See the documentation](https://data-apis-v2.nocodb.com/#tag/Table-Records/operation/db-data-table-row-list)",
8-
version: "0.0.5",
8+
version: "0.0.6",
99
type: "action",
1010
props: {
1111
...common.props,

components/nocodb/actions/update-record/update-record.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "nocodb-update-record",
66
name: "Update Record",
77
description: "This action updates a record in a table. [See the documentation](https://data-apis-v2.nocodb.com/#tag/Table-Records/operation/db-data-table-row-update)",
8-
version: "0.0.4",
8+
version: "0.0.5",
99
type: "action",
1010
props: {
1111
...common.props,

components/nocodb/nocodb.app.mjs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,27 @@ export default {
6969
return rows?.map(({ Id }) => `${Id}` ) || [];
7070
},
7171
},
72+
viewId: {
73+
type: "string",
74+
label: "View ID",
75+
description: "The ID of a view",
76+
async options({
77+
tableId, page,
78+
}) {
79+
const { list } = await this.listViews({
80+
tableId,
81+
params: {
82+
offset: page * DEFAULT_LIMIT,
83+
},
84+
});
85+
return list?.map(({
86+
id: value, title: label,
87+
}) => ({
88+
label,
89+
value,
90+
})) || [];
91+
},
92+
},
7293
data: {
7394
type: "any",
7495
label: "data",
@@ -233,5 +254,13 @@ export default {
233254
...opts,
234255
});
235256
},
257+
listViews({
258+
tableId, ...opts
259+
}) {
260+
return this._makeRequest({
261+
path: `/meta/tables/${tableId}/views`,
262+
...opts,
263+
});
264+
},
236265
},
237266
};

components/nocodb/package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/nocodb",
3-
"version": "0.0.7",
3+
"version": "0.1.0",
44
"description": "Pipedream Nocodb Components",
55
"main": "nocodb.app.mjs",
66
"keywords": [
@@ -9,11 +9,10 @@
99
],
1010
"homepage": "https://pipedream.com/apps/nocodb",
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
12-
"dependencies": {
13-
"@pipedream/platform": "^1.6.0",
14-
"moment": "^2.29.4"
15-
},
1612
"publishConfig": {
1713
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
1817
}
1918
}

components/nocodb/sources/common/base.mjs

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import moment from "moment";
21
import nocodb from "../../nocodb.app.mjs";
32
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
43

@@ -46,59 +45,53 @@ export default {
4645
_setLastTime(lastTime) {
4746
this.db.set("lastTime", lastTime);
4847
},
49-
async processEvent({
50-
params, lastTime,
51-
}) {
48+
getParams(timeField) {
49+
return {
50+
sort: `-${timeField}`,
51+
};
52+
},
53+
async getRows(records, timeField, lastTime) {
54+
const rows = [];
55+
for await (const row of records) {
56+
if (!lastTime || Date.parse(row[timeField]) >= Date.parse(lastTime)) {
57+
rows.push(row);
58+
} else {
59+
break;
60+
}
61+
}
62+
return rows.reverse();
63+
},
64+
async processEvent(max) {
5265
const timeField = this.getTimeField();
66+
const lastTime = this._getLastTime();
5367

5468
const records = this.nocodb.paginate({
5569
fn: this.nocodb.listTableRow,
5670
args: {
5771
tableId: this.tableId.value,
58-
params,
72+
params: this.getParams(timeField),
5973
},
74+
max,
6075
});
6176

62-
for await (const record of records) {
63-
if (moment(record[timeField]).isAfter(lastTime)) this._setLastTime(record[timeField]);
64-
this.$emit(record, this.getDataToEmit(record));
77+
const rows = await this.getRows(records, timeField, lastTime);
78+
79+
if (!rows.length) {
80+
return;
6581
}
82+
83+
this._setLastTime(rows[rows.length - 1][timeField]);
84+
85+
rows.forEach((row) => this.$emit(row, this.getDataToEmit(row)));
6686
},
6787
},
6888
hooks: {
69-
async activate() {
70-
const timeField = this.getTimeField();
71-
const lastTime = this._getLastTime();
72-
const { list } = await this.nocodb.listTableRow({
73-
tableId: this.tableId.value,
74-
params: {
75-
sort: `-${timeField}`,
76-
},
77-
});
78-
79-
list.reverse();
80-
81-
for (const row of list) {
82-
if (!lastTime || moment(lastTime).isAfter(row[timeField])) {
83-
this._setLastTime(row[timeField]);
84-
}
85-
this.$emit(row, this.getDataToEmit(row));
86-
}
89+
async deploy() {
90+
await this.processEvent(25);
8791
},
8892
},
8993
async run() {
90-
const timeField = this.getTimeField();
91-
const lastTime = this._getLastTime();
92-
const params = {
93-
sort: timeField,
94-
};
95-
// moment is necessary because nocodb query doesn't filter equal datetime in 'greater than'
96-
if (lastTime) params.where = `(${timeField},gte,${moment(lastTime).add(1, "ms")
97-
.toISOString()})`;
98-
return this.processEvent({
99-
params,
100-
lastTime,
101-
});
94+
await this.processEvent();
10295
},
10396
};
10497

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import common from "../common/base.mjs";
2+
3+
export default {
4+
...common,
5+
key: "nocodb-new-record-in-view",
6+
name: "New Record in View",
7+
description: "Emit new event for each new record in a view. [See the documentation](https://data-apis-v2.nocodb.com/#tag/Table-Records/operation/db-data-table-row-list)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
props: {
12+
...common.props,
13+
viewId: {
14+
propDefinition: [
15+
common.props.nocodb,
16+
"viewId",
17+
(c) => ({
18+
tableId: c.tableId.value || c.tableId,
19+
}),
20+
],
21+
},
22+
},
23+
methods: {
24+
...common.methods,
25+
getDataToEmit(record) {
26+
return {
27+
id: record.id,
28+
summary: `New record created (${record.id})`,
29+
ts: Date.parse(record[this.getTimeField()]),
30+
};
31+
},
32+
getTimeField() {
33+
return "created_at";
34+
},
35+
getParams(timeField) {
36+
return {
37+
viewId: this.viewId,
38+
fields: timeField,
39+
};
40+
},
41+
async getRows(records, timeField, lastTime) {
42+
const rows = [];
43+
for await (const row of records) {
44+
if (!lastTime || Date.parse(row[timeField]) >= Date.parse(lastTime)) {
45+
rows.push(row);
46+
}
47+
}
48+
return rows;
49+
},
50+
},
51+
};

components/nocodb/sources/new-record/new-record.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
name: "New Record in Table",
77
key: "nocodb-new-record",
88
description: "Emit new event for each new record in table. [See the documentation](https://data-apis-v2.nocodb.com/#tag/Table-Records/operation/db-data-table-row-list)",
9-
version: "0.0.5",
9+
version: "0.0.6",
1010
dedupe: "unique",
1111
props: {
1212
...common.props,

0 commit comments

Comments
 (0)