Skip to content

Commit 34ab429

Browse files
committed
tests; readme; migrator.status() improvements
1 parent cf0599c commit 34ab429

File tree

9 files changed

+598
-267
lines changed

9 files changed

+598
-267
lines changed

.badges/lines-of-code.svg

Lines changed: 4 additions & 4 deletions
Loading

README.md

Lines changed: 260 additions & 40 deletions
Large diffs are not rendered by default.

lib/cli.js

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,10 @@ async function main(argv) {
2020

2121
/** @type {MigrationOptions} */
2222
const options = {};
23-
if (args.has('--dbPath')) {
24-
options.dbPath = args.get('--dbPath');
25-
}
26-
if (args.has('--schemaPath')) {
27-
options.schemaPath = args.get('--schemaPath');
28-
}
29-
if (args.has('--migrationsPath')) {
30-
options.migrationsPath = args.get('--migrationsPath');
31-
}
32-
if (args.has('--migrationTable')) {
33-
options.migration = args.get('--migrationTable');
34-
}
23+
options.dbPath = args.get('--dbPath');
24+
options.schemaPath = args.get('--schemaPath');
25+
options.migrationsPath = args.get('--migrationsPath');
26+
options.migration = args.get('--migrationTable');
3527
const migrator = new Migrator(options);
3628

3729
if (cmd === 'status') {
@@ -60,6 +52,12 @@ async function main(argv) {
6052
console.log(`${symbols.success} No schema changes detected.`);
6153
}
6254

55+
if (status.has_tampered_data) {
56+
console.log(
57+
`${symbols.error} The database schema has been modified by other means than 'sam migrate'. Please undo any DDL changes you've made and instead change the schema file and/or create a migration file`,
58+
);
59+
}
60+
6361
if (status.missing_migrations.length > 0) {
6462
console.log(colors.FgCyan('\nUnapplied migrations:'));
6563
for (const migration of status.missing_migrations) {
@@ -81,24 +79,12 @@ async function main(argv) {
8179
} else if (cmd === 'make') {
8280
/** @type {MakeOptions} */
8381
const makeOptions = {};
84-
if (args.has('--onRename')) {
85-
makeOptions.onRename = args.get('--onRename');
86-
}
87-
if (args.has('--onDestructiveChange')) {
88-
makeOptions.onDestructiveChange = args.get('--onDestructiveChange');
89-
}
90-
if (args.has('--onChangedIndex')) {
91-
makeOptions.onChangedIndex = args.get('--onChangedIndex');
92-
}
93-
if (args.has('--onChangedView')) {
94-
makeOptions.onChangedView = args.get('--onChangedView');
95-
}
96-
if (args.has('--onChangedTrigger')) {
97-
makeOptions.onChangedTrigger = args.get('--onChangedTrigger');
98-
}
99-
if (target) {
100-
makeOptions.createIfNoChanges = false;
101-
} else {
82+
makeOptions.onRename = args.get('--onRename');
83+
makeOptions.onDestructiveChange = args.get('--onDestructiveChange');
84+
makeOptions.onChangedIndex = args.get('--onChangedIndex');
85+
makeOptions.onChangedView = args.get('--onChangedView');
86+
makeOptions.onChangedTrigger = args.get('--onChangedTrigger');
87+
if (target !== '--onlyCreateIfChanges') {
10288
makeOptions.createIfNoChanges = true;
10389
}
10490

lib/database.mjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,9 @@ export class Database {
264264
/**
265265
* Prepares a SQL query with parameters and returns a prepared statement.
266266
* @param {string} sql the SQL query to prepare
267-
* @param {Params} params the parameters to bind to the query. Supports a dictionary with `:name`, `@name` and `$name` style parameters, or an array with `?` position based parameters.
267+
* @param {Params | Params[]} params the parameters to bind to the query. Supports a dictionary with `:name`, `@name` and `$name` style parameters, or an array with `?` position based parameters.
268268
* @template [Row=any] - the type of the row object that is returned
269-
* @template {any[]} [Params=any[]] - the type of the parameters that can be bound to the prepared statement
269+
* @template {any[] | Object} [Params=any] - the type of the parameters that can be bound to the prepared statement
270270
* @returns {Promise<Statement<Row, Params>} a promise that resolves to a {@link Statement} object representing the prepared statement
271271
* @throws {Error} if the query cannot be prepared
272272
* @see {@link sqlite3.Database#prepare}
@@ -309,7 +309,7 @@ export class Database {
309309
/**
310310
* Represents a prepared statement. Async wrapper around the sqlite3.Statement class.
311311
* @template [Row=any] - the type of the row object that is returned
312-
* @template {any[]} [Params=any[]] - the type of the parameters that can be bound to the prepared statement
312+
* @template {any[] | Object} [Params=any[]] - the type of the parameters that can be bound to the prepared statement
313313
*/
314314
export class Statement {
315315
/**
@@ -330,7 +330,7 @@ export class Statement {
330330

331331
/**
332332
* Binds parameters to the prepared statement. Completely resets the row cursor and removes any previously bound parameters.
333-
* @param {Params} params the parameters to bind to the prepared statement. Supports a dictionary with `:name`, `@name` and `$name` style parameters, or an array with `?` position based parameters.
333+
* @param {Params | Params[]} params the parameters to bind to the prepared statement. Supports a dictionary with `:name`, `@name` and `$name` style parameters, or an array with `?` position based parameters.
334334
* @returns {Promise<void>} a promise that resolves when the parameters have been bound
335335
* @throws {Error} if the parameters cannot be bound
336336
* @see {@link sqlite3.Statement#bind}
@@ -391,7 +391,7 @@ export class Statement {
391391

392392
/**
393393
* Runs the prepared statement with the optional bound parameters (overwriting any previously bound parameters when supplied).
394-
* @param {Params} params the parameters to bind to the prepared statement. Supports a dictionary with `:name`, `@name` and `$name` style parameters, or an array with `?` position based parameters.
394+
* @param {Params | Params[]} params the parameters to bind to the prepared statement. Supports a dictionary with `:name`, `@name` and `$name` style parameters, or an array with `?` position based parameters.
395395
* @returns {Promise<RunResult>} a promise that resolves to a {@link RunResult} object with a `lastID` property and a `changes` property
396396
* @throws {Error} if the prepared statement cannot be run
397397
* @see {@link sqlite3.Statement#run}
@@ -413,7 +413,7 @@ export class Statement {
413413

414414
/**
415415
* Gets a single row from the prepared statement with the optional bound parameters (overwriting any previously bound parameters when supplied).
416-
* @param {Params} params the parameters to bind to the prepared statement. Supports a dictionary with `:name`, `@name` and `$name` style parameters, or an array with `?` position based parameters.
416+
* @param {Params | Params[]} params the parameters to bind to the prepared statement. Supports a dictionary with `:name`, `@name` and `$name` style parameters, or an array with `?` position based parameters.
417417
* @returns {Promise<Row | undefined>} a promise that resolves to the first row returned by the prepared statement or undefined if no rows are returned
418418
* @throws {Error} if the prepared statement cannot be run
419419
* @see {@link sqlite3.Statement#get}
@@ -434,7 +434,7 @@ export class Statement {
434434

435435
/**
436436
* Gets all rows from the prepared statement with the optional bound parameters (overwriting any previously bound parameters when supplied).
437-
* @param {Params} params the parameters to bind to the prepared statement. Supports a dictionary with `:name`, `@name` and `$name` style parameters, or an array with `?` position based parameters.
437+
* @param {Params | Params[]} params the parameters to bind to the prepared statement. Supports a dictionary with `:name`, `@name` and `$name` style parameters, or an array with `?` position based parameters.
438438
* @returns {Promise<Row[]>} a promise that resolves to all rows returned by the prepared statement
439439
* @throws {Error} if the prepared statement cannot be run
440440
* @see {@link sqlite3.Statement#all}
@@ -455,7 +455,7 @@ export class Statement {
455455

456456
/**
457457
* Runs the prepared statement with the optional bound parameters (overwriting any previously bound parameters when supplied) and returns the rows one by one as an async generator (useful for saving memory with large query results).
458-
* @param {Params} params the parameters to bind to the prepared statement. Supports a dictionary with `:name`, `@name` and `$name` style parameters, or an array with `?` position based parameters.
458+
* @param {Params | Params[]} params the parameters to bind to the prepared statement. Supports a dictionary with `:name`, `@name` and `$name` style parameters, or an array with `?` position based parameters.
459459
* @returns {AsyncGenerator<Row, void, undefined>} an async generator that yields each row returned by the prepared statement
460460
* @throws {Error} if the prepared statement cannot be run
461461
* @example <caption>Using the async generator</caption>

0 commit comments

Comments
 (0)