Nessie
A modular database migration tool for Deno inspired by Laravel. Supports PostgreSQL, MySQL and SQLite.
See documentation for the query builder.
See documentation for the clients.
Supported databases
- PostgreSQL
- MySQL - Currently it works with password for 5.*, but for >=8 you have to send a blank password, see Deno MySQL for version support
- SQLite
If you have a database system you would like to see in this list, feel free to make an issue or create a pr with your implementation.
You can see examples of how to make a client plugin in the clients folder or in the section How to make a client.
Usage
init
: Generates anessie.config.ts
filedeno run --allow-net --allow-read --allow-write https://deno.land/x/nessie/cli.ts init
make [name]
: Create migrationdeno run --allow-net --allow-read --allow-write https://deno.land/x/nessie/cli.ts make create_users
migrate [amount?]
: Run migration - will migrate your migrations in your migration folder (sorted by timestamp) newer than the latest migration in your db. Amount defines how many migrations, defaults to all available if not set.deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts migrate
deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts migrate 1
deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts migrate -c ./nessie.config.ts
rollback [amount?]
: Rollback - will rollback your migrations. Amount defines how many migrations, defaults to 1 if not set.deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts rollback
deno run --allow-net --allow-read https://deno.land/x/nessie/cli.ts rollback 2
Flags
-c, --config
: Path to config file, will default to ./nessie.config.ts-d, --debug
: Enables verbose output
Contributing
All contributions are welcome, make sure to read the contributing guideline.
Uses
Examples
nessie.config.ts
import { ClientPostgreSQL, nessieConfig } from "https://deno.land/x/nessie/mod.ts";
const migrationFolder = "./migrations";
const config: nessieConfig = {
client: new ClientPostgreSQL(migrationFolder, {
database: "nessie",
hostname: "localhost",
port: 5432,
user: "root",
password: "pwd",
}),
};
export default config;
Minimal example of a migration file
export const up = (): string => {
return "CREATE TABLE table1 (id int);";
};
export const down = (): string => {
return "DROP TABLE table1";
};
Using the native query builder
import { Schema, dbDialects } from "https://deno.land/x/nessie/qb.ts";
const dialect: dbDialects = "mysql"
export const up = (): string => {
let query = new Schema(dialect).create("users", (table) => {
table.id();
table.string("name", 100).nullable();
table.boolean("is_true").default("false");
table.custom("custom_column int default 1");
table.timestamps();
});
query += new Schema(dialect).queryString(
"INSERT INTO users VALUES (DEFAULT, 'Deno', true, 2, DEFAULT, DEFAULT);",
);
return query
};
export const down = (schema: Schema): void => {
return new Schema(dialect).drop("users");
};
See example folder for more
How to make a client
A client needs to extend AbstractClient and implement the ClientI interface.
query
: Takes a query string or array of query strings and sends them of to the batabase for execution. Should return whatever the database responds.
prepare
: Will be run when the migration or rollback commands are executed. This should create the connection, set up the nessie_migrations
table and prepare the database for incoming migrations.
migrate
: Takes a number as an optional input, will default to all files if not set. Will run Math.min(amount, numberOfFiles)
migration files. Only handles the up
method.
rollback
: Takes a number as an optional input, will default to 1 if not set. Will run Math.min(amount, numberOfFiles)
migration files. Only handles the down
method.
close
: Will be the last method run before the program is finished. This should close the database connection.