The default loader plugin is responsible for importing migration files written in JavaScript or TypeScript.
Migration files can be written using either CommonJS or ES Modules.
Supported extensions
The default loader plugin supports the following extensions:
.js
- either CommonJS or ES Modules depending on your package.json’s type
field
.cjs
- CommonJS
.mjs
- ES Modules
.ts
- either CommonJS or ES Modules written in TypeScript
.cts
- CommonJS written in TypeScript
.mts
- ES Modules written in TypeScript
Supported exports
The default loader plugin supports the following exports:
ES Modules
Default export
Exporting a function as the default export.
import { database } from ' some-database ' ;
export default async function someMigration () {
await database . query ( ... );
await database . query ( ... );
}
import { database } from ' some-database ' ;
export default function someMigration () {
// Remember to return the promise
return database . query ( ... )
. then ( () => {
return database . query ( ... );
})
}
import { database } from ' some-database ' ;
export default function someMigration ( done ) {
database . query ( ... , ( err ) => {
if ( err ) {
return done ( err );
}
database . query ( ... , ( err ) => {
if ( err ) {
return done ( err );
}
done ();
});
});
}
Named export
Exporting a function named up
.
import { database } from ' some-database ' ;
export const up = async () => {
await database . query ( ... ) ;
await database . query ( ... ) ;
} ;
import { database } from ' some-database ' ;
export const up = () => {
// Remember to return the promise
return database . query ( ... )
. then ( () => {
return database . query ( ... ) ;
} )
} ;
import { database } from ' some-database ' ;
export const up = ( done ) => {
database . query ( ... , ( err ) => {
if ( err ) {
return done ( err ) ;
}
database . query ( ... , ( err ) => {
if ( err ) {
return done ( err ) ;
}
done () ;
} ) ;
} ) ;
}
CommonJS
module.exports
Exporting a function as the module.
const { database } = require ( ' some-database ' );
module . exports = async function someMigration () {
await database . query ( ... );
await database . query ( ... );
}
const { database } = require ( ' some-database ' );
module . exports = function someMigration () {
// Remember to return the promise
return database . query ( ... )
. then ( () => {
return database . query ( ... );
})
}
const { database } = require ( ' some-database ' );
module . exports = function someMigration ( done ) {
database . query ( ... , ( err ) => {
if ( err ) {
return done ( err );
}
database . query ( ... , ( err ) => {
if ( err ) {
return done ( err );
}
done ();
});
});
}
exports.up
Exporting an up
function.
const { database } = require ( ' some-database ' );
exports . up = async () => {
await database . query ( ... );
await database . query ( ... );
};
const { database } = require ( ' some-database ' );
exports . up = () => {
// Remember to return the promise
return database . query ( ... )
. then ( () => {
return database . query ( ... );
})
};
const { database } = require ( ' some-database ' );
exports . up = ( done ) => {
database . query ( ... , ( err ) => {
if ( err ) {
return done ( err );
}
database . query ( ... , ( err ) => {
if ( err ) {
return done ( err );
}
done ();
});
});
}