Skip to content

Commit

Permalink
feat: compability with sequelize/sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
cusspvz committed Oct 23, 2023
1 parent 37963e3 commit 3e0b8cc
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 50 deletions.
1 change: 1 addition & 0 deletions .github/workflows/lint-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
check-latest: true
cache: yarn

- name: Install dependencies
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: 18
check-latest: true
cache: yarn
scope: '@abmf'

Expand Down
9 changes: 6 additions & 3 deletions packages/cli/__tests__/fixtures/run-command.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { exec } from 'node:child_process';
import { join } from 'node:path';
import pkg from '../../package.json';

export async function runCliCommand(cmdAndArgs: string) {
const distBinPath = join(__dirname, '../../dist/cjs/cli.min.js');
const distBinPath = join(__dirname, '../../', pkg.bin.abmt);
const migrationsPath = join(
__dirname,
'../../../../__tests__/test-data/ts-project/migrations',
'../../', // cli root
'../../', // monorepo root
'__tests__/test-data/ts-project/migrations',
);
const abmtCommand = `node "${distBinPath}" --migrations-path="${migrationsPath}"`;
const abmtCommand = `"${distBinPath}" --migrations-path="${migrationsPath}" --orm sequelize --sequelize-uri="sqlite::memory:"`;

const command = cmdAndArgs.replace('abmt', abmtCommand);
return await new Promise<{
Expand Down
File renamed without changes.
16 changes: 12 additions & 4 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "Mosano Dev Team <dev@mosano.eu>",
"license": "MIT",
"bin": {
"abmt": "dist/cjs/cli.min.js"
"abmt": "dist/cli.js"
},
"scripts": {
"dev": "rollup --config --sourcemap --watch",
Expand All @@ -21,9 +21,17 @@
"peerDependencies": {
"@abmt/core": "0.0.1",
"@abmt/orm-mongoose": "0.0.1",
"mongoose": "^7.6.3"
"@abmt/orm-sequelize": "0.0.1",
"mongoose": "^7.6.3",
"sequelize": "^6.33.0"
},
"peerDependenciesMeta": {
"@abmt/orm-sequelize": {
"optional": true
},
"sequelize": {
"optional": true
},
"@abmt/orm-mongoose": {
"optional": true
},
Expand All @@ -33,10 +41,10 @@
},
"dependencies": {
"@abmt/migrations-fs": "^0.0.1",
"@swc/register": "^0.1.10",
"chalk": "4",
"cli-table": "^0.3.11",
"commander": "^11.1.0",
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"tsx": "^3.14.0"
}
}
6 changes: 3 additions & 3 deletions packages/cli/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default [
{
file: pkg.bin.abmt,
format: 'cjs',
},
}
],
plugins: [
globals(),
Expand All @@ -24,12 +24,12 @@ export default [
swc(
defineRollupSwcOption({
sourceMaps: true,
minify: true,
minify: false,
jsc: {
parser: {
syntax: 'typescript',
},
target: 'es2016',
target: 'es5',
},
}),
),
Expand Down
21 changes: 19 additions & 2 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/bin/env node
#!/usr/bin/env node --loader tsx

import './utils/swc-registration';
import { Command } from 'commander';
import dotenv from 'dotenv';

Expand All @@ -11,6 +10,24 @@ import { setupCmdToOwnMigrations } from './options/migrations';
import { checkoutCmd } from './commands/checkout';
import { createCmd } from './commands/create';

// import register from '@swc/register';
// register();
// register({
// module: {
// type: 'commonjs',
// },
// jsc: {
// target: 'es5',
// keepClassNames: true,
// loose: true,
// parser: {
// syntax: 'typescript',
// decorators: true,
// dynamicImport: true,
// },
// },
// });

(async function main() {
dotenv.config({ path: '.env.local' });

Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/external.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Swcrc } from '@swc/core';

declare module '@swc/register' {
export default function register(options?: Swcrc): void;
}
37 changes: 34 additions & 3 deletions packages/cli/src/options/orm.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { IContextProvider, IStorageProvider } from '@abmt/core';
import { Command, Option, createOption } from 'commander';
import { Command, createOption } from 'commander';
import { EventEmitter } from 'node:events';
import { notifyOnTerminal } from '../utils/cli';

export enum PlatformKey {
Mongoose = 'mongoose',
Sequelize = 'sequelize',
}

const emitter = new EventEmitter();
Expand All @@ -14,14 +15,20 @@ export function setupCmdToOwnORM(cmd: Command) {
// Handle options
const options = [
createOption('-o, --orm <orm>')
.choices(['mongoose'])
.choices(Object.values(PlatformKey))
.default('mongoose')
.makeOptionMandatory(true),

// PlatformKey.Sequelize
createOption('--sequelize-uri <sequelizeUri>')
.default('sqlite::memory:')
.implies({ orm: PlatformKey.Mongoose })
.env('SEQUELIZE_URI'),

// PlatformKey.Mongoose
createOption('--mongoose-uri <mongooseUri>')
.default('mongodb://127.0.0.1:27017/abmt')
.implies({ orm: 'mongoose' })
.implies({ orm: PlatformKey.Mongoose })
.env('MONGOOSE_URI'),

createOption(
Expand Down Expand Up @@ -51,6 +58,30 @@ export async function getORMProviders<Context>(cmd: Command): Promise<{
const options = cmd.optsWithGlobals();

switch (options.orm) {
case PlatformKey.Sequelize: {
const { SequelizeORM } = require('@abmt/orm-sequelize');
const { Sequelize } = require('sequelize');

const sequelize = new Sequelize(options.sequelizeUri);
const orm = new SequelizeORM({
sequelize,
collection: options.mongooseMigrationsCollection,
});

// handle hooks
// wait for the connection to be established
await notifyOnTerminal(cmd, 'Connecting to orm', () => sequelize.sync());

emitter.once('post-action', () => {
sequelize.close();
});

return {
storageProvider: orm,
contextProvider: orm,
};
}

case PlatformKey.Mongoose: {
const { MongooseORM } = require('@abmt/orm-mongoose');
const { createConnection } = require('mongoose');
Expand Down
17 changes: 0 additions & 17 deletions packages/cli/src/utils/swc-registration.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/orm-sequelize/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@abmt/orm-sqlite",
"name": "@abmt/orm-sequelize",
"version": "0.0.1",
"description": "Allows ABMT to store and execute migrations with SQLite",
"repository": {
Expand Down
Loading

0 comments on commit 3e0b8cc

Please sign in to comment.