Skip to content

Commit

Permalink
feat: support browser and support sql.js driver (#386)
Browse files Browse the repository at this point in the history
* feat: support browser

* chore: update

* chore: update

* chore: export Raw directly

* fix: remove useless import

* feat: sql.js driver

* feat: update tsconfig

* test: 增加 sql.js 相关测试用例

* feat: implements for association

* feat: follow sqlite condition

* feat: test cases

* fix: import performance from perf_hooks

* test: add mocha -R dot

* chore: update

* fix: update after review

* fix: lint-staged for .js and .ts

* Update src/drivers/sqljs/sqljs-connection.ts

Co-authored-by: JimmyDaddy <[email protected]>

* chore: update gitignore

* chore: update

* feat: compile browser to dist

* fix: update files

* feat: remove assert

* fix: 修复单测并增加 vscode 调试配置

---------

Co-authored-by: JimmyDaddy <[email protected]>
  • Loading branch information
vagusX and JimmyDaddy authored May 11, 2023
1 parent 7bb5cf2 commit ddcefbc
Show file tree
Hide file tree
Showing 26 changed files with 932 additions and 100 deletions.
35 changes: 0 additions & 35 deletions .eslintrc

This file was deleted.

66 changes: 66 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict';

const eslintConfig = {
overrides: [],
'parser': '@babel/eslint-parser',
'parserOptions': {
'sourceType': 'script',
'ecmaVersion': 2020,
'requireConfigFile': false
},
'env': {
'node': true,
'es6': true
},
'plugins': [
'no-only-tests'
],
'rules': {
'curly': [2, 'multi-line'],
'consistent-return': 0,
'quotes': [2, 'single', { 'avoidEscape': true, 'allowTemplateLiterals': true }],
'semi': [2, 'always'],
'strict': ['error', 'safe'],
'no-const-assign': 'error',
'no-undef': 2,
'no-underscore-dangle': 0,
'no-use-before-define': [2, 'nofunc'],
'no-unused-vars': [2, { 'vars': 'all', 'args': 'none', 'ignoreRestSiblings': true }],
'no-shadow': 2,
'keyword-spacing': 'error',
'eol-last': 'error',
'prefer-const': 'error',
'no-only-tests/no-only-tests': 'error',
'no-trailing-spaces': 'error',
'space-before-blocks': 'error',
'space-in-parens': 'error'
}
};

const tslintConfig = {
extends: [
'plugin:@typescript-eslint/recommended',
],
parser: '@typescript-eslint/parser',
files: ['*.ts'],
plugins: [
'@typescript-eslint',
'no-only-tests',
],
rules: {
...eslintConfig.rules,
'@typescript-eslint/no-var-requires': 0,
'@typescript-eslint/no-use-before-define': ['error'],
strict: 0,
'@typescript-eslint/ban-ts-comment': ['warn'],
'no-shadow': 'off',
'@typescript-eslint/no-shadow': ['warn'],
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'warn',
'@typescript-eslint/no-explicit-any': 'off',
},
};

eslintConfig.overrides.push(tslintConfig);

module.exports = eslintConfig;
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ test/models/photo.js*
src/decorators.js*
src/data_types.js*
src/raw.js*

# Logs
logs
*.log
dist/
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run lint-staged
35 changes: 35 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha Current File",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"${file}"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"preLaunchTask": "npm: pretest",
},
{
"type": "node",
"request": "launch",
"name": "Mocha All",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"preLaunchTask": "npm: pretest",
}
]
}
16 changes: 16 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "pretest",
"problemMatcher": [],
"label": "npm: pretest",
"detail": "tsc && ./test/prepare.sh",
"group": {
"kind": "build",
"isDefault": true
}
},
]
}
28 changes: 26 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@
"version": "2.10.3",
"description": "JavaScript Object-relational mapping alchemy",
"main": "index.js",
"browser": "dist/browser.js",
"types": "index.d.ts",
"files": [
"dist",
"src",
"index.js",
"index.d.ts"
],
"scripts": {
"jsdoc": "rm -rf docs/api && jsdoc -c .jsdoc.json -d docs/api -t node_modules/@cara/minami",
"clean": "tsc -b --clean",
"lint-staged": "lint-staged",
"prepack": "tsc",
"pretest": "tsc && ./test/prepare.sh",
"prepack:browser": "rm -rf dist && tsc -p tsconfig.browser.json",
"prepublishOnly": "npm run prepack && npm run prepack:browser",
"pretest": "npm run prepack && ./test/prepare.sh",
"test": "./test/start.sh",
"test:local": "./test/start.sh",
"test:unit": "./test/start.sh unit",
Expand All @@ -21,12 +27,14 @@
"test:mysql2": "./test/start.sh test/integration/mysql2.test.js",
"test:postgres": "./test/start.sh test/integration/postgres.test.js",
"test:sqlite": "./test/start.sh test/integration/sqlite.test.js",
"test:sqljs": "./test/start.sh test/integration/sqljs.test.js",
"test:custom": "./test/start.sh test/integration/custom.test.js",
"test:sqlcipher": "./test/start.sh test/integration/sqlcipher.test.js",
"test:dts": "./test/start.sh dts",
"test:coverage": "nyc ./test/start.sh && nyc report --reporter=lcov",
"lint": "eslint ./",
"lint:fix": "eslint . --fix"
"lint:fix": "eslint . --fix",
"prepare": "husky install"
},
"repository": {
"type": "git",
Expand All @@ -48,6 +56,11 @@
"engines": {
"node": ">= 12.0.0"
},
"lint-staged": {
"*.{js,ts}": [
"eslint --no-ignore --fix"
]
},
"dependencies": {
"dayjs": "^1.10.3",
"debug": "^3.1.0",
Expand All @@ -56,12 +69,14 @@
"pluralize": "^7.0.0",
"reflect-metadata": "^0.1.13",
"sqlstring": "^2.3.0",
"tslib": "^2.5.0",
"validator": "^13.5.2"
},
"peerDependencies": {
"mysql": "^2.17.1",
"mysql2": "^2.3.0",
"pg": "^8.5.1",
"sql.js": "^1.8.0",
"sqlite3": "^5.0.2"
},
"peerDependenciesMeta": {
Expand All @@ -76,6 +91,9 @@
},
"sqlite3": {
"optional": true
},
"sql.js": {
"optional": true
}
},
"devDependencies": {
Expand All @@ -85,16 +103,22 @@
"@journeyapps/sqlcipher": "^5.2.0",
"@types/mocha": "^9.0.0",
"@types/node": "^16.10.1",
"@types/sql.js": "^1.4.4",
"@typescript-eslint/eslint-plugin": "^5.59.2",
"@typescript-eslint/parser": "^5.59.2",
"eslint": "^7.20.0",
"eslint-plugin-no-only-tests": "^3.0.0",
"expect.js": "^0.3.1",
"husky": "^8.0.3",
"jsdoc": "^3.6.3",
"lint-staged": "^13.2.2",
"mocha": "^8.2.1",
"mysql": "^2.17.1",
"mysql2": "^2.3.0",
"nyc": "^15.1.0",
"pg": "^8.5.1",
"sinon": "^10.0.0",
"sql.js": "^1.8.0",
"sqlite3": "^5.0.2",
"typescript": "^4.6.2"
}
Expand Down
2 changes: 1 addition & 1 deletion src/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,7 @@ class Bone {
}
} else if (unset && affectedRows === instances.length) {
// otherwise, use last insert id to generate bulk inserted ids
if (driver.type === 'sqlite') {
if (['sqlite', 'sqljs'].includes(driver.type)) {
for (let i = instances.length - 1; i >= 0; i--) {
instances[i][primaryKey] = insertId--;
}
Expand Down
60 changes: 60 additions & 0 deletions src/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const Logger = require('./drivers/abstract/logger');
const Spell = require('./spell');
const Bone = require('./bone');
const Collection = require('./collection');
const { invokable: DataTypes, LENGTH_VARIANTS } = require('./data_types');
const sequelize = require('./adapters/sequelize');
const { heresql } = require('./utils/string');

const Realm = require('./realm/base');
const AbstractDriver = require('./drivers/abstract');
const { isBone } = require('./utils');

/**
* @typedef {Object} RawSql
* @property {boolean} __raw
* @property {string} value
* @property {string} type
*/


/**
* Connect models to database. Need to provide both connect options and models.
* @alias module:index.connect
* @param {Object} opts
* @param {string} opts.client - client name
* @param {string|Bone[]} opts.models - an array of models
* @returns {Pool} the connection pool in case we need to perform raw query
*/
export const connect = async function connect(opts) {
opts = { Bone, ...opts };
if (opts.Bone.driver) throw new Error('connected already');
const realm = new Realm(opts);
await realm.connect();
return realm;
};

export const disconnect = async function disconnect(realm, ...args) {
if (realm instanceof Realm && realm.connected) {
return await realm.disconnect(...args);
}
};

Object.assign(Realm.prototype, { DataTypes });
export default Realm;

export { Bone };
export { Collection };
export { DataTypes };
export { Logger };
export { Spell };
export { sequelize };
export { heresql };
export * from './hint';
export * from './decorators';
export { AbstractDriver };
export { default as Raw } from './raw';
export { LENGTH_VARIANTS };
export { isBone };

// TODO: missing migrations and MYSQL, PG, SQLITE drivers
2 changes: 1 addition & 1 deletion src/drivers/abstract/spellbook.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function formatSelectExpr(spell, values) {
const list = map[qualifier];
if (list) {
for (const selectExpr of list) selects.add(selectExpr);
} else if (groups.length === 0 && Model.driver.type !== 'sqlite' && !isAggregate) {
} else if (groups.length === 0 && !['sqlite', 'sqljs'].includes(Model.driver.type) && !isAggregate) {
selects.add(`${escapeId(qualifier)}.*`);
}
}
Expand Down
Loading

0 comments on commit ddcefbc

Please sign in to comment.