Skip to content

Prefixes for IDs #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,23 @@ API along with your API token.
}
```

- When importing multiple agencies their IDs may overlap - specify prefix added to every ID in feed to maintain uniqueness. Provided string will be prepended to every ID in feed, number will be added (number IDs required, otherwise error will be thrown)

```json
{
"agencies": [
{
"path": "/path/to/the/gtfs.zip",
"prefix": "A"
},
{
"path": "/path/to/the/othergtfs.zip",
"prefix": 10000
}
]
}
```

### csvOptions

{Object} Add options to be passed to [`csv-parse`](https://csv.js.org/parse/) with the key `csvOptions`. This is an optional parameter.
Expand Down
33 changes: 29 additions & 4 deletions lib/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,11 +452,35 @@ const importLines = (task, lines, model, totalLineCount) => {
const placeholders = [];
const values = [];

while (lines.length > 0) {
const line = lines.pop();
placeholders.push(`(${fieldNames.map(() => '?').join(', ')})`);
values.push(...fieldNames.map((fieldName) => line[fieldName]));
//Let's check, if we will use prefixes at all - if not, don't repeat ifs in loop and do it once
if(task.prefix)
{
const prefixes = (() => {
if(typeof task.prefix === "number") {
return model.schema
.filter((column) => column.name !== 'id')
.map((column) => (column.prefix ? `${task.prefix}+` : ''));
} else {
return model.schema
.filter((column) => column.name !== 'id')
.map((column) => (column.prefix ? `'${task.prefix}'||` : ''));
}})();

while (lines.length > 0) {
const line = lines.pop();
placeholders.push(`(${prefixes.map((pref) => pref + '?').join(', ')})`);
values.push(...fieldNames.map((fieldName) => line[fieldName]));
}
}
else
{
while (lines.length > 0) {
const line = lines.pop();
placeholders.push(`(${fieldNames.map(() => '?').join(', ')})`);
values.push(...fieldNames.map((fieldName) => line[fieldName]));
}
}


try {
db.prepare(
Expand Down Expand Up @@ -588,6 +612,7 @@ export async function importGtfs(initialConfig) {
csvOptions: config.csvOptions || {},
ignoreDuplicates: config.ignoreDuplicates,
sqlitePath: config.sqlitePath,
prefix: (agency.prefix ? agency.prefix : false),
log,
warn: logWarning,
error: logError,
Expand Down
1 change: 1 addition & 0 deletions models/gtfs-plus/calendar-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const model = {
name: 'service_id',
type: 'varchar(255)',
primary: true,
prefix: true,
},
{
name: 'service_description',
Expand Down
1 change: 1 addition & 0 deletions models/gtfs-plus/directions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const model = {
type: 'varchar(255)',
required: true,
primary: true,
prefix: true,
},
{
name: 'direction_id',
Expand Down
1 change: 1 addition & 0 deletions models/gtfs-plus/route-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const model = {
name: 'route_id',
type: 'varchar(255)',
primary: true,
prefix: true,
},
{
name: 'category',
Expand Down
1 change: 1 addition & 0 deletions models/gtfs-plus/stop-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const model = {
type: 'varchar(255)',
required: true,
primary: true,
prefix: true,
},
{
name: 'accessibility_id',
Expand Down
2 changes: 2 additions & 0 deletions models/gtfs-ride/board-alight.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ const model = {
type: 'varchar(255)',
required: true,
index: true,
prefix: true,
},
{
name: 'stop_id',
type: 'varchar(255)',
required: true,
index: true,
prefix: true,
},
{
name: 'stop_sequence',
Expand Down
5 changes: 5 additions & 0 deletions models/gtfs-ride/rider-trip.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@ const model = {
name: 'rider_id',
type: 'varchar(255)',
primary: true,
prefix: true,
},
{
name: 'agency_id',
type: 'varchar(255)',
index: true,
prefix: true,
},
{
name: 'trip_id',
type: 'varchar(255)',
index: true,
prefix: true,
},
{
name: 'boarding_stop_id',
type: 'varchar(255)',
index: true,
prefix: true,
},
{
name: 'boarding_stop_sequence',
Expand All @@ -33,6 +37,7 @@ const model = {
name: 'alighting_stop_id',
type: 'varchar(255)',
index: true,
prefix: true,
},
{
name: 'alighting_stop_sequence',
Expand Down
5 changes: 5 additions & 0 deletions models/gtfs-ride/ridership.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const model = {
name: 'service_id',
type: 'varchar(255)',
index: true,
prefix: true,
},
{
name: 'monday',
Expand Down Expand Up @@ -94,11 +95,13 @@ const model = {
name: 'agency_id',
type: 'varchar(255)',
index: true,
prefix: true,
},
{
name: 'route_id',
type: 'varchar(255)',
index: true,
prefix: true,
},
{
name: 'direction_id',
Expand All @@ -110,10 +113,12 @@ const model = {
{
name: 'trip_id',
type: 'varchar(255)',
prefix: true,
},
{
name: 'stop_id',
type: 'varchar(255)',
prefix: true,
},
],
};
Expand Down
2 changes: 2 additions & 0 deletions models/gtfs-ride/trip-capacity.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ const model = {
name: 'agency_id',
type: 'varchar(255)',
index: true,
prefix: true,
},
{
name: 'trip_id',
type: 'varchar(255)',
index: true,
prefix: true,
},
{
name: 'service_date',
Expand Down
1 change: 1 addition & 0 deletions models/gtfs/agency.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const model = {
name: 'agency_id',
type: 'varchar(255)',
primary: true,
prefix: true,
},
{
name: 'agency_name',
Expand Down
1 change: 1 addition & 0 deletions models/gtfs/areas.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const model = {
type: 'varchar(255)',
required: true,
primary: true,
prefix: true,
},
{
name: 'area_name',
Expand Down
6 changes: 5 additions & 1 deletion models/gtfs/attributions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ const model = {
type: 'varchar(255)',
primary: true,
required: true,
prefix: true,
},
{
name: 'agency_id',
type: 'varchar(255)',
type: 'varchar(255)',
prefix: true,
},
{
name: 'route_id',
type: 'varchar(255)',
prefix: true,
},
{
name: 'trip_id',
type: 'varchar(255)',
prefix: true,
},
{
name: 'organization_name',
Expand Down
1 change: 1 addition & 0 deletions models/gtfs/calendar-dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const model = {
type: 'varchar(255)',
required: true,
primary: true,
prefix: true,
},
{
name: 'date',
Expand Down
1 change: 1 addition & 0 deletions models/gtfs/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const model = {
type: 'varchar(255)',
required: true,
primary: true,
prefix: true,
},
{
name: 'monday',
Expand Down
2 changes: 2 additions & 0 deletions models/gtfs/fare-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const model = {
type: 'varchar(255)',
required: true,
primary: true,
prefix: true,
},
{
name: 'price',
Expand Down Expand Up @@ -33,6 +34,7 @@ const model = {
{
name: 'agency_id',
type: 'varchar(255)',
prefix: true,
},
{
name: 'transfer_duration',
Expand Down
5 changes: 5 additions & 0 deletions models/gtfs/fare-leg-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@ const model = {
{
name: 'leg_group_id',
type: 'varchar(255)',
prefix: true,
},
{
name: 'network_id',
type: 'varchar(255)',
primary: true,
prefix: true,
},
{
name: 'from_area_id',
type: 'varchar(255)',
primary: true,
prefix: true,
},
{
name: 'to_area_id',
type: 'varchar(255)',
primary: true,
prefix: true,
},
{
name: 'fare_product_id',
type: 'varchar(255)',
required: true,
primary: true,
prefix: true,
},
],
};
Expand Down
2 changes: 2 additions & 0 deletions models/gtfs/fare-products.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const model = {
type: 'varchar(255)',
required: true,
primary: true,
prefix: true,
},
{
name: 'fare_product_name',
Expand All @@ -15,6 +16,7 @@ const model = {
name: 'fare_media_id',
type: 'varchar(255)',
primary: true,
prefix: true,
},
{
name: 'amount',
Expand Down
5 changes: 5 additions & 0 deletions models/gtfs/fare-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,27 @@ const model = {
name: 'fare_id',
type: 'varchar(255)',
required: true,
prefix: true,
},
{
name: 'route_id',
type: 'varchar(255)',
prefix: true,
},
{
name: 'origin_id',
type: 'varchar(255)',
prefix: true,
},
{
name: 'destination_id',
type: 'varchar(255)',
prefix: true,
},
{
name: 'contains_id',
type: 'varchar(255)',
prefix: true,
},
],
};
Expand Down
4 changes: 4 additions & 0 deletions models/gtfs/fare-transfer-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ const model = {
name: 'from_leg_group_id',
type: 'varchar(255)',
primary: true,
prefix: true,
},
{
name: 'to_leg_group_id',
type: 'varchar(255)',
primary: true,
prefix: true,
},
{
name: 'transfer_count',
Expand All @@ -20,6 +22,7 @@ const model = {
{
name: 'transfer_id',
type: 'varchar(255)',
prefix: true,
},
{
name: 'duration_limit',
Expand All @@ -44,6 +47,7 @@ const model = {
name: 'fare_product_id',
type: 'varchar(255)',
primary: true,
prefix: true,
},
],
};
Expand Down
1 change: 1 addition & 0 deletions models/gtfs/frequencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const model = {
type: 'varchar(255)',
required: true,
primary: true,
prefix: true,
},
{
name: 'start_time',
Expand Down
1 change: 1 addition & 0 deletions models/gtfs/levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const model = {
type: 'varchar(255)',
primary: true,
required: true,
prefix: true,
},
{
name: 'level_index',
Expand Down
Loading