Skip to content

Commit

Permalink
feat: support ESM (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
sixmen authored Oct 26, 2024
1 parent ca4aab3 commit f7bf980
Show file tree
Hide file tree
Showing 236 changed files with 12,908 additions and 1,286 deletions.
2 changes: 1 addition & 1 deletion packages/cormo/examples/javascript/basic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const cormo = require('../..');
import * as cormo from '../../lib/esm/index.js';

const connection = new cormo.Connection('mysql', {
implicit_apply_schemas: true,
Expand Down
22 changes: 10 additions & 12 deletions packages/cormo/examples/typescript/basic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as util from 'util';
import * as cormo from '../../src';
import * as cormo from '../../src/index.js';

export const connection = new cormo.MySQLConnection({
implicit_apply_schemas: true,
Expand Down Expand Up @@ -121,14 +121,12 @@ async function run() {
await User.drop();
}

if (require.main === module) {
run()
.then(() => {
console.log('Done');
process.exit(0);
})
.catch((error) => {
console.log((error.cause || error).toString());
process.exit(0);
});
}
run()
.then(() => {
console.log('Done');
process.exit(0);
})
.catch((error) => {
console.log((error.cause || error).toString());
process.exit(0);
});
5 changes: 0 additions & 5 deletions packages/cormo/lib/adapters/index.d.ts

This file was deleted.

13 changes: 0 additions & 13 deletions packages/cormo/lib/adapters/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ColumnPropertyInternal } from '../model';
import { Transaction } from '../transaction';
import * as types from '../types';
import { ColumnPropertyInternal } from '../model/index.js';
import { Transaction } from '../transaction.js';
import * as types from '../types.js';
export interface SchemasColumn {
required: boolean;
type: types.ColumnType | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdapterBase = void 0;
const lodash_1 = __importDefault(require("lodash"));
const types = __importStar(require("../types"));
const util = __importStar(require("../util"));
const types = __importStar(require("../types.js"));
const util = __importStar(require("../util/index.js"));
/**
* Base class for adapters
* @namespace adapter
Expand Down
5 changes: 5 additions & 0 deletions packages/cormo/lib/cjs/adapters/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { MongoDBAdapter } from './mongodb.js';
export { MySQLAdapter } from './mysql.js';
export { RedisAdapter } from './redis.js';
export { PostgreSQLAdapter } from './postgresql.js';
export { SQLite3Adapter } from './sqlite3.js';
13 changes: 13 additions & 0 deletions packages/cormo/lib/cjs/adapters/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SQLite3Adapter = exports.PostgreSQLAdapter = exports.RedisAdapter = exports.MySQLAdapter = exports.MongoDBAdapter = void 0;
var mongodb_js_1 = require("./mongodb.js");
Object.defineProperty(exports, "MongoDBAdapter", { enumerable: true, get: function () { return mongodb_js_1.MongoDBAdapter; } });
var mysql_js_1 = require("./mysql.js");
Object.defineProperty(exports, "MySQLAdapter", { enumerable: true, get: function () { return mysql_js_1.MySQLAdapter; } });
var redis_js_1 = require("./redis.js");
Object.defineProperty(exports, "RedisAdapter", { enumerable: true, get: function () { return redis_js_1.RedisAdapter; } });
var postgresql_js_1 = require("./postgresql.js");
Object.defineProperty(exports, "PostgreSQLAdapter", { enumerable: true, get: function () { return postgresql_js_1.PostgreSQLAdapter; } });
var sqlite3_js_1 = require("./sqlite3.js");
Object.defineProperty(exports, "SQLite3Adapter", { enumerable: true, get: function () { return sqlite3_js_1.SQLite3Adapter; } });
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Connection } from '../connection/index.js';
import { AdapterBase } from './base.js';
export interface AdapterSettingsMongoDB {
host?: string;
port?: number;
user?: string | Promise<string>;
password?: string | Promise<string>;
database: string;
}
import { Connection } from '../connection';
import { AdapterBase } from './base';
export declare class MongoDBAdapter extends AdapterBase {
/**
* Exposes mongodb module's a collection object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.MongoDBAdapter = void 0;
exports.createAdapter = createAdapter;
const stream_1 = __importDefault(require("stream"));
const lodash_1 = __importDefault(require("lodash"));
const types = __importStar(require("../types.js"));
const base_js_1 = require("./base.js");
let mongodb;
try {
mongodb = require('mongodb');
}
catch {
const module_promise = Promise.resolve().then(() => __importStar(require('mongodb'))).then((m) => {
mongodb = m;
})
.catch(() => {
//
}
});
class CormoTypesObjectId {
}
const stream_1 = __importDefault(require("stream"));
const lodash_1 = __importDefault(require("lodash"));
const types = __importStar(require("../types"));
const base_1 = require("./base");
function _convertValueToObjectID(value, key) {
if (value == null) {
return null;
Expand Down Expand Up @@ -292,7 +292,7 @@ function _processSaveError(error) {
return new Error('duplicated ' + (key && key[1]));
}
else {
return base_1.AdapterBase.wrapError('unknown error', error);
return base_js_1.AdapterBase.wrapError('unknown error', error);
}
}
function _getMongoDBColName(name) {
Expand All @@ -306,7 +306,7 @@ function _getMongoDBColName(name) {
}
// Adapter for MongoDB
// @namespace adapter
class MongoDBAdapter extends base_1.AdapterBase {
class MongoDBAdapter extends base_js_1.AdapterBase {
// Creates a MongoDB adapter
/** @internal */
constructor(connection) {
Expand Down Expand Up @@ -771,6 +771,11 @@ class MongoDBAdapter extends base_1.AdapterBase {
* @internal
*/
async connect(settings) {
await module_promise;
if (!mongodb) {
console.log('Install mongodb module to use this adapter');
process.exit(1);
}
let url;
const host = settings.host || 'localhost';
const port = settings.port || 27017;
Expand Down Expand Up @@ -995,9 +1000,5 @@ class MongoDBAdapter extends base_1.AdapterBase {
}
exports.MongoDBAdapter = MongoDBAdapter;
function createAdapter(connection) {
if (!mongodb) {
console.log('Install mongodb module to use this adapter');
process.exit(1);
}
return new MongoDBAdapter(connection);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import tls = require('tls');
import { Connection } from '../connection';
import { Transaction } from '../transaction';
import { SQLAdapterBase } from './sql_base';
import tls from 'tls';
import { Connection } from '../connection/index.js';
import { Transaction } from '../transaction.js';
import { SQLAdapterBase } from './sql_base.js';
export interface AdapterSettingsMySQL {
host?: string;
port?: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,26 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.MySQLAdapter = void 0;
exports.createAdapter = createAdapter;
let mysql;
let is_mysql2 = false;
const stream_1 = __importDefault(require("stream"));
const util_1 = __importDefault(require("util"));
const lodash_1 = __importDefault(require("lodash"));
const types = __importStar(require("../types"));
const sql_base_1 = require("./sql_base");
try {
mysql = require('mysql2');
const types = __importStar(require("../types.js"));
const sql_base_js_1 = require("./sql_base.js");
let mysql;
let is_mysql2 = false;
const module_promise = Promise.resolve().then(() => __importStar(require('mysql2'))).then((m) => {
mysql = m;
is_mysql2 = true;
}
catch {
try {
mysql = require('mysql');
}
catch {
})
.catch(() => {
// @ts-expect-error no type definitions
return Promise.resolve().then(() => __importStar(require('mysql'))).then((m) => {
mysql = m;
})
.catch(() => {
//
}
}
});
});
function _typeToSQL(property, support_fractional_seconds, major_version) {
if (property.array) {
return 'TEXT';
Expand Down Expand Up @@ -124,7 +125,7 @@ async function _tryCreateConnection(config, count = 0) {
}
// Adapter for MySQL
// @namespace adapter
class MySQLAdapter extends sql_base_1.SQLAdapterBase {
class MySQLAdapter extends sql_base_js_1.SQLAdapterBase {
// Creates a MySQL adapter
/** @internal */
constructor(connection) {
Expand Down Expand Up @@ -746,6 +747,11 @@ class MySQLAdapter extends sql_base_1.SQLAdapterBase {
* @internal
*/
async connect(settings) {
await module_promise;
if (!mysql) {
console.log('Install mysql module to use this adapter');
process.exit(1);
}
// connect
let client;
this._database = settings.database;
Expand Down Expand Up @@ -1305,9 +1311,5 @@ class MySQLAdapter extends sql_base_1.SQLAdapterBase {
}
exports.MySQLAdapter = MySQLAdapter;
function createAdapter(connection) {
if (!mysql) {
console.log('Install mysql module to use this adapter');
process.exit(1);
}
return new MySQLAdapter(connection);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Connection } from '../connection/index.js';
import { Transaction } from '../transaction.js';
import { SQLAdapterBase } from './sql_base.js';
export interface AdapterSettingsPostgreSQL {
host?: string;
port?: number;
user?: string | Promise<string>;
password?: string | Promise<string>;
database: string;
}
import { Connection } from '../connection';
import { Transaction } from '../transaction';
import { SQLAdapterBase } from './sql_base';
export declare class PostgreSQLAdapter extends SQLAdapterBase {
/**
* Exposes pg module's query method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,26 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.PostgreSQLAdapter = void 0;
exports.createAdapter = createAdapter;
const stream_1 = __importDefault(require("stream"));
const lodash_1 = __importDefault(require("lodash"));
const types = __importStar(require("../types.js"));
const base_js_1 = require("./base.js");
const sql_base_js_1 = require("./sql_base.js");
let pg;
let QueryStream;
try {
pg = require('pg');
}
catch {
// @ts-expect-error no type definitions
const module_promise = Promise.resolve().then(() => __importStar(require('pg'))).then((m) => {
pg = m.default;
return Promise.resolve().then(() => __importStar(require('pg-query-stream'))).then((m2) => {
QueryStream = m2.default;
})
.catch(() => {
//
});
})
.catch(() => {
//
}
try {
QueryStream = require('pg-query-stream');
}
catch {
/**/
}
const stream_1 = __importDefault(require("stream"));
const lodash_1 = __importDefault(require("lodash"));
const types = __importStar(require("../types"));
const base_1 = require("./base");
const sql_base_1 = require("./sql_base");
});
function _typeToSQL(property) {
if (property.array) {
return 'JSON';
Expand Down Expand Up @@ -104,12 +105,12 @@ function _processSaveError(table_name, error) {
return new Error('duplicated' + column);
}
else {
return base_1.AdapterBase.wrapError('unknown error', error);
return base_js_1.AdapterBase.wrapError('unknown error', error);
}
}
// Adapter for PostgreSQL
// @namespace adapter
class PostgreSQLAdapter extends sql_base_1.SQLAdapterBase {
class PostgreSQLAdapter extends sql_base_js_1.SQLAdapterBase {
// Creates a PostgreSQL adapter
/** @internal */
constructor(connection) {
Expand Down Expand Up @@ -582,6 +583,11 @@ class PostgreSQLAdapter extends sql_base_1.SQLAdapterBase {
* @internal
*/
async connect(settings) {
await module_promise;
if (!pg) {
console.log('Install pg module to use this adapter');
process.exit(1);
}
// connect
const pool = new pg.Pool({
database: settings.database,
Expand Down Expand Up @@ -944,9 +950,5 @@ class PostgreSQLAdapter extends sql_base_1.SQLAdapterBase {
}
exports.PostgreSQLAdapter = PostgreSQLAdapter;
function createAdapter(connection) {
if (!pg) {
console.log('Install pg module to use this adapter');
process.exit(1);
}
return new PostgreSQLAdapter(connection);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Connection } from '../connection/index.js';
import { AdapterBase } from './base.js';
export interface AdapterSettingsRedis {
host?: string;
port?: number;
database: string;
}
import { Connection } from '../connection';
import { AdapterBase } from './base';
export declare class RedisAdapter extends AdapterBase {
}
export declare function createAdapter(connection: Connection): RedisAdapter;
Loading

0 comments on commit f7bf980

Please sign in to comment.