Skip to content

Commit 3922170

Browse files
added parameters to set custom created and updated date column names
1 parent a9f9292 commit 3922170

6 files changed

Lines changed: 18 additions & 12 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ Configuration options are set in the file [config.js](https://github.com/evolute
8181
| consoleLog | Log SQL statements to console.|
8282
| wComments | Allow for user comments (not implemented yet). |
8383
| wRating | Allow for user ratings (not implemented yet). |
84-
| wTimestamp | Timestamp columns u_date and c_date w/ date of record creation and last update. |
84+
| wTimestamp | Timestamp columns w/ date of record creation and last update. |
85+
| createdDateColumn | Column containing created date (default `c_date`) |
86+
| updatedDateColumn | Column containing last update date (default `u_date`) |
8587
| schemaQueries | Enables endpoints to query for lists of tables and columns in the database schema. |
8688
| GraphQL | Set GraphQL = true to unable GraphQL (Work In Progress) |
8789

config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ module.exports = {
2727
wComments: false,
2828
wRating: false,
2929

30+
// - Columns containing created and last updated dates
31+
createdDateColumn: 'c_date',
32+
updatedDateColumn: 'u_date',
33+
3034
// - API discovery
3135
apiInfo: true,
3236

js/graphql-schema.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ const model2gqlObjectType = m => {
8181

8282
// - "timestamp" columns to track creation and last modification.
8383
if(config.wTimestamp){
84-
fields['c_date'] = gqlField({type: ft.datetime})
85-
fields['u_date'] = gqlField({type: ft.datetime})
84+
fields[config.createdDateColumn] = gqlField({type: ft.datetime})
85+
fields[config.updatedDateColumn] = gqlField({type: ft.datetime})
8686
}
8787
/*
8888
// - "who-is" columns to track user who created and last modified the record.
@@ -195,4 +195,4 @@ const RootQuery = new GraphQLObjectType({
195195

196196
module.exports = new GraphQLSchema({
197197
query: RootQuery
198-
})
198+
})

js/setup/database.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function sqlSchemaWithData(){
175175
sql += 'CREATE OR REPLACE FUNCTION '+schema+'.u_date() RETURNS trigger\n'+
176176
' LANGUAGE plpgsql\n'+
177177
' AS $$\n'+
178-
' BEGIN\n NEW.u_date = now();\n RETURN NEW;\n END;\n$$;\n\n';
178+
' BEGIN\n NEW.'+config.updatedDateColumn+' = now();\n RETURN NEW;\n END;\n$$;\n\n';
179179
}
180180
for(let mid in models){
181181
const sqls = sqlModel(mid);
@@ -250,8 +250,8 @@ function sqlModel(mid){
250250

251251
// - "timestamp" columns to track creation and last modification.
252252
if(config.wTimestamp){
253-
fs.push(' c_date timestamp'+noTZ+' DEFAULT timezone(\'utc\'::text, now())');
254-
fs.push(' u_date timestamp'+noTZ+' DEFAULT timezone(\'utc\'::text, now())');
253+
fs.push(config.createdDateColumn+' timestamp'+noTZ+' DEFAULT timezone(\'utc\'::text, now())');
254+
fs.push(config.updatedDateColumn+' timestamp'+noTZ+' DEFAULT timezone(\'utc\'::text, now())');
255255
}
256256
// - "who-is" columns to track user who created and last modified the record.
257257
if(config.wWhoIs){

js/stats.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ function numbers(req, res) {
5757
})
5858
if(config.wTimestamp){
5959
// - last update
60-
sql += ', max(u_date) AS u_date_max' +
60+
sql += ', max('+config.updatedDateColumn+') AS u_date_max' +
6161
// - number of insert & updates this week
6262
', (SELECT count('+m.pKey+')::integer '+sqlFROM+
63-
' WHERE u_date > NOW() - interval \'7 days\')'+
63+
' WHERE '+config.updatedDateColumn+' > NOW() - interval \'7 days\')'+
6464
' AS u_date_week_count' +
6565
// - first insert
66-
', min(c_date) AS c_date_min'
66+
', min('+config.createdDateColumn+') AS c_date_min'
6767
}
6868
if(config.wComments){
6969
// - number of comments

js/utils/dico.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ if(config.wTimestamp){
4646
systemFields.push({
4747
// - record creation date
4848
type: 'datetime',
49-
column:'c_date',
49+
column: config.createdDateColumn,
5050
},
5151
{
5252
// - record last update date
5353
type: 'datetime',
54-
column:'u_date',
54+
column: config.updatedDateColumn,
5555
})
5656
}
5757
if(config.wWhoIs){

0 commit comments

Comments
 (0)