-
Hi I'm interested to know how I might create a collection of columns that can be reused across multiple tables. For example, this is what I'm trying - though its not working:
The idea being, I could create columns that are commonly used, such as |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I'm just learning Here is syntax I've used: const { PgLiteral } = require('node-pg-migrate')
let current_user_id = 123456; // <-- use an environment variable or perform a lookup against the `users` table
exports.shorthands = {
':created_by': {type: 'integer', notNull: true, references: 'users', default: current_user_id},
':created_at': {type: 'timestamp', notNull: true, default: new PgLiteral('current_timestamp')}
':updated_by': {type: 'integer', references: 'users'},
':updated_at': {type: 'timestamp}
}
pgm.createTable('entries', {
id: 'id',
createdBy: ':created_by',
createdAt: ':created_at',
updatedBy: ':updated_by',
updatedAt: ':updated_at'
})
pgm.createTable('tasks', {
id: 'id',
createdBy: ':created_by',
createdAt: ':created_at',
updatedBy: ':updated_by',
updatedAt: ':updated_at'
})
Using this technique, you could place your shorthands in another file like For me, there are certain shorthands I never expect to change (e.g., those timestamps, |
Beta Was this translation helpful? Give feedback.
I'm just learning
node-pg-migrate
, so take this for what it's worth. It sounds like you could make use of shorthands.Here is syntax I've used: