-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for timeframes.txt (#158)
- Loading branch information
1 parent
fed951e
commit 2d17e80
Showing
7 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import sqlString from 'sqlstring-sqlite'; | ||
|
||
import { openDb } from '../db.js'; | ||
|
||
import { | ||
formatOrderByClause, | ||
formatSelectClause, | ||
formatWhereClauses, | ||
} from '../utils.js'; | ||
import timeframes from '../../models/gtfs/timesframes.js'; | ||
|
||
/* | ||
* Returns an array of all timeframes that match the query | ||
*/ | ||
export function getTimeframes(query = {}, fields = [], orderBy = [], options = {}) { | ||
const db = options.db ?? openDb(); | ||
const tableName = sqlString.escapeId(timeframes.filenameBase); | ||
const selectClause = formatSelectClause(fields); | ||
const whereClause = formatWhereClauses(query); | ||
const orderByClause = formatOrderByClause(orderBy); | ||
|
||
return db | ||
.prepare( | ||
`${selectClause} FROM ${tableName} ${whereClause} ${orderByClause};` | ||
) | ||
.all(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const model = { | ||
filenameBase: 'timesframes', | ||
schema: [ | ||
{ | ||
name: 'timeframe_group_id', | ||
type: 'varchar(255)', | ||
required: true, | ||
primary: true, | ||
prefix: true, | ||
}, | ||
{ | ||
name: 'start_time', | ||
type: 'varchar(255)', | ||
default: null, | ||
}, | ||
{ | ||
name: 'end_time', | ||
type: 'varchar(255)', | ||
default: null, | ||
}, | ||
{ | ||
name: 'service_id', | ||
type: 'varchar(255)', | ||
}, | ||
], | ||
}; | ||
|
||
export default model; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* eslint-env mocha */ | ||
|
||
import should from 'should'; | ||
|
||
import config from '../test-config.js'; | ||
import { openDb, closeDb, importGtfs, getTimeframes } from '../../index.js'; | ||
|
||
describe('getTimeframes():', () => { | ||
before(async () => { | ||
openDb(config); | ||
await importGtfs(config); | ||
}); | ||
|
||
after(() => { | ||
const db = openDb(config); | ||
closeDb(db); | ||
}); | ||
|
||
it('should return empty array if no timeframes', () => { | ||
const timeframeGroupId = 'not_real'; | ||
|
||
const results = getTimeframes({ | ||
timeframe_group_id: timeframeGroupId, | ||
}); | ||
should.exists(results); | ||
results.should.have.length(0); | ||
}); | ||
}) |