Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/reset-interactidons-alias.md
Original file line number Diff line number Diff line change
@@ -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()`.
11 changes: 11 additions & 0 deletions .changeset/store-mysql-migrations.md
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 17 additions & 0 deletions packages/app/src/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
30 changes: 30 additions & 0 deletions packages/app/src/Application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
47 changes: 47 additions & 0 deletions packages/store-mysql/README.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 2 additions & 1 deletion packages/store-mysql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
Expand Down
28 changes: 27 additions & 1 deletion test/packages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down Expand Up @@ -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' });
});

Expand All @@ -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(
Expand Down