diff --git a/.changeset/reset-interactidons-alias.md b/.changeset/reset-interactidons-alias.md new file mode 100644 index 0000000..cadacee --- /dev/null +++ b/.changeset/reset-interactidons-alias.md @@ -0,0 +1,5 @@ +--- +'@cuecue/app': patch +--- + +`resetInteractidonsByIds()`, the misspelled name that 0.0.45 renamed to `resetInteractionsByIds()`, works again for the apps that call it. It is deprecated: it warns once with `logger.warn()`, and calls `resetInteractionsByIds()`. diff --git a/.changeset/store-mysql-migrations.md b/.changeset/store-mysql-migrations.md new file mode 100644 index 0000000..5198b2a --- /dev/null +++ b/.changeset/store-mysql-migrations.md @@ -0,0 +1,11 @@ +--- +'@cuecue/store-mysql': patch +--- + +The package ships its migrations: 0.0.45 left them out, so apps couldn't add the `extra` column from `node_modules`. An app runs them with its `MYSQL_*` variables (from the environment or its `.env`): + +```bash +npx db-migrate up --migrations-dir node_modules/@cuecue/store-mysql/migrations --config node_modules/@cuecue/store-mysql/migrations/config.json -e prod +``` + +The package's README gives its settings and how to run the migrations. diff --git a/packages/app/src/Application.js b/packages/app/src/Application.js index ea20b8f..4230146 100644 --- a/packages/app/src/Application.js +++ b/packages/app/src/Application.js @@ -260,6 +260,23 @@ class Application extends EventEmitter { ); } + /** + * @deprecated The misspelled name of `resetInteractionsByIds()`, until 0.0.45. Apps still call + * it. + */ + async resetInteractidonsByIds(ids) { + if (!this.warnedResetInteractidonsByIds) { + this.warnedResetInteractidonsByIds = true; + const { logger } = this.options; + if (typeof logger.warn === 'function') { + logger.warn( + '[cuecue] resetInteractidonsByIds() is deprecated: use resetInteractionsByIds().', + ); + } + } + return this.resetInteractionsByIds(ids); + } + getInteractions() { const { id: sessionId = null } = this.session || {}; return this.store.getItems('interactions', { diff --git a/packages/app/src/Application.test.js b/packages/app/src/Application.test.js index ba0fe15..6f99529 100644 --- a/packages/app/src/Application.test.js +++ b/packages/app/src/Application.test.js @@ -487,6 +487,36 @@ describe('Application interactions', () => { expect((await app.getInteractions()).map(({ id }) => id)).toEqual(['interaction-2']); expect(recorder.calls).toContainEqual(['uninteractions', ['interaction-1']]); }); + + it('keeps resetInteractidonsByIds() as a deprecated name of resetInteractionsByIds()', async () => { + const app = createApp({}, { logger: { error: vi.fn(), warn: vi.fn() } }); + await app.start(); + await app.interact({ vote: 'a' }, 'interaction-1'); + await app.interact({ vote: 'b' }, 'interaction-2'); + await app.interact({ vote: 'c' }, 'interaction-3'); + const resetInteractionsByIds = vi.spyOn(app, 'resetInteractionsByIds'); + + await app.resetInteractidonsByIds(['interaction-1']); + await app.resetInteractidonsByIds(['interaction-2']); + + expect(resetInteractionsByIds).toHaveBeenCalledWith(['interaction-1']); + expect(resetInteractionsByIds).toHaveBeenCalledWith(['interaction-2']); + expect((await app.getInteractions()).map(({ id }) => id)).toEqual(['interaction-3']); + expect(app.options.logger.warn).toHaveBeenCalledTimes(1); + expect(app.options.logger.warn).toHaveBeenCalledWith( + '[cuecue] resetInteractidonsByIds() is deprecated: use resetInteractionsByIds().', + ); + }); + + it('calls resetInteractidonsByIds() without a logger.warn()', async () => { + const app = createApp(); + await app.start(); + await app.interact({ vote: 'a' }, 'interaction-1'); + + await app.resetInteractidonsByIds(['interaction-1']); + + expect(await app.getInteractions()).toEqual([]); + }); }); describe('Application inputs', () => { diff --git a/packages/store-mysql/README.md b/packages/store-mysql/README.md new file mode 100644 index 0000000..9f9101d --- /dev/null +++ b/packages/store-mysql/README.md @@ -0,0 +1,47 @@ +# @cuecue/store-mysql + +A Cuecue store that keeps the sessions, cues and interactions in MySQL, instead of the app's memory store. + +```js +import Application from '@cuecue/app'; +import MysqlStore from '@cuecue/store-mysql'; + +const app = new Application(definition, { store: new MysqlStore() }); +app.start(); +``` + +## Settings + +| Option | Environment variable | Default | | +| ----------------- | -------------------- | ----------- | ---------------------------------------------------------------- | +| `host` | `MYSQL_HOST` | `localhost` | The MySQL server | +| `port` | `MYSQL_PORT` | `3306` | | +| `user` | `MYSQL_USER` | | | +| `password` | `MYSQL_PASSWORD` | | | +| `database` | `MYSQL_DB` | | | +| `connectionLimit` | | `1` | The connections of the pool. One keeps the queries in order | +| `autoConnect` | | `false` | Checks that the database answers when the app starts, or rejects | + +The options passed to `new MysqlStore({ … })` win over the environment variables. + +## Migrations + +The tables (`sessions`, `cues`, `interactions`) come from the [db-migrate](https://db-migrate.readthedocs.io) migrations in `migrations/`, which the package ships. An app runs them from `node_modules`, with the same `MYSQL_*` variables as the store (from the environment or a `.env` file in the current directory): + +```bash +npx db-migrate up --migrations-dir node_modules/@cuecue/store-mysql/migrations --config node_modules/@cuecue/store-mysql/migrations/config.json -e prod +``` + +Run it again after each update of the package: it only runs the new migrations. `db-migrate down` (one migration) and `db-migrate reset` (all of them) take the same options. An app can keep it as a script in its `package.json`: + +```json +{ + "scripts": { + "migrate": "db-migrate up --migrations-dir node_modules/@cuecue/store-mysql/migrations --config node_modules/@cuecue/store-mysql/migrations/config.json -e prod" + } +} +``` + +The fields of an item that have no column of their own (a cue's `duration`, an interaction's `cueId`…) go in the `extra` JSON column, added in 0.0.45 by `20261117120000-extra.js`. Without that migration, the store throws when an item has such a field. + +In this repository, `npm run migrate:up -w @cuecue/store-mysql` runs them with the `.env` at the root. diff --git a/packages/store-mysql/package.json b/packages/store-mysql/package.json index f36bc6c..a7e4cfe 100644 --- a/packages/store-mysql/package.json +++ b/packages/store-mysql/package.json @@ -34,7 +34,8 @@ }, "main": "dist/index.js", "files": [ - "dist" + "dist", + "migrations" ], "scripts": { "migrate:up": "db-migrate up --migrations-dir=./migrations --config=./migrations/config.json -e prod --dotenvCustomPath '../../.env'", diff --git a/test/packages.test.js b/test/packages.test.js index b8f2608..dd50231 100644 --- a/test/packages.test.js +++ b/test/packages.test.js @@ -29,6 +29,12 @@ const expectedExports = { '@cuecue/store-mysql': [], }; +// What each package publishes: its build, and the MySQL store's migrations, which apps run from +// node_modules. +const expectedFiles = { + '@cuecue/store-mysql': ['dist', 'migrations'], +}; + const packages = readdirSync(path.join(root, 'packages')).map((directory) => { const packagePath = path.join(root, 'packages', directory); const manifest = JSON.parse(readFileSync(path.join(packagePath, 'package.json'), 'utf8')); @@ -58,7 +64,7 @@ describe('packages', () => { }); it('publishes only what it builds', () => { - expect(manifest.files).toEqual(['dist']); + expect(manifest.files).toEqual(expectedFiles[manifest.name] ?? ['dist']); expect(manifest.publishConfig).toEqual({ access: 'public' }); }); @@ -71,6 +77,26 @@ describe('packages', () => { expect(require(path.join(root, 'packages/store-mysql'))).toBeTypeOf('function'); }); + it('@cuecue/store-mysql publishes its migrations', () => { + const packed = JSON.parse( + execFileSync( + 'npm', + ['pack', '--dry-run', '--json', '--ignore-scripts', '-w', '@cuecue/store-mysql'], + { cwd: root, encoding: 'utf8' }, + ), + ); + // npm 10 and 11 give an array, npm 12 an object by package name. + const [{ files }] = Array.isArray(packed) ? packed : Object.values(packed); + const published = files.map(({ path: file }) => file); + const migrations = readdirSync(path.join(root, 'packages/store-mysql/migrations')); + expect(migrations).toContain('config.json'); + expect(migrations).toContain('20261117120000-extra.js'); + migrations.forEach((file) => { + expect(published).toContain(`migrations/${file}`); + }); + expect(published).toContain('README.md'); + }); + it('the cuecue command prints its version', () => { const cli = path.join(root, 'packages/cli/dist/cli.js'); const { version } = JSON.parse(