Skip to content

Commit

Permalink
Feat: Removed sql-ts
Browse files Browse the repository at this point in the history
queries are built manually by the keyv-sql package instead of depending on external packages

Signed-off-by: Jytesh <44925963+Jytesh@users.noreply.github.com>
  • Loading branch information
Jytesh committed Apr 20, 2021
1 parent 395c540 commit 171bd12
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 62 deletions.
19 changes: 10 additions & 9 deletions package.json
Expand Up @@ -2,19 +2,20 @@
"name": "@keyvhq/mono",
"private": "true",
"devDependencies": {
"@keyvhq/keyv": "./packages/keyv",
"@keyvhq/keyv-mongo": "./packages/keyv-mongo",
"@keyvhq/keyv-mysql": "./packages/keyv-mysql",
"@keyvhq/keyv-postgres": "./packages/keyv-postgres",
"@keyvhq/keyv-redis": "./packages/keyv-redis",
"@keyvhq/keyv-sql": "./packages/keyv-sql",
"@keyvhq/keyv-sqlite": "./packages/keyv-sqlite",
"@keyvhq/keyv-test-suite": "./packages/keyv-test-suite",
"@keyvhq/keyv": "file:./packages/keyv",
"@keyvhq/keyv-mongo": "file:packages/keyv-mongo",
"@keyvhq/keyv-mysql": "file:packages/keyv-mysql",
"@keyvhq/keyv-postgres": "file:packages/keyv-postgres",
"@keyvhq/keyv-redis": "file:packages/keyv-redis",
"@keyvhq/keyv-sql": "file:packages/keyv-sql",
"@keyvhq/keyv-sqlite": "file:packages/keyv-sqlite",
"@keyvhq/keyv-test-suite": "file:packages/keyv-test-suite",
"ava": "^3.15.0",
"coveralls": "^3.0.0",
"delay": "^4.3.0",
"dotenv": "^8.2.0",
"eslint-config-xo-lukechilds": "^1.0.0",
"knex": "^0.95.4",
"lerna": "^4.0.0",
"nyc": "^15.1.0",
"pify": "^5.0.0",
Expand All @@ -31,7 +32,7 @@
},
"repository": {
"type": "git",
"url" : "git+https://github.com/keyvhq/keyv.git"
"url": "git+https://github.com/keyvhq/keyv.git"
},
"keywords": [
"keyv",
Expand Down
3 changes: 0 additions & 3 deletions packages/keyv-mysql/package.json
Expand Up @@ -37,8 +37,5 @@
"@keyvhq/keyv-sql": "file:../keyv-sql",
"mysql2": "2.2.5"
},
"devDependencies": {
"@keyvhq/keyv-test-suite": "file:../keyv-test-suite"
},
"gitHead": "a4e2c1f285236a753de13eb8a42d9a98690526cc"
}
8 changes: 1 addition & 7 deletions packages/keyv-sql/package.json
Expand Up @@ -4,7 +4,7 @@
"description": "Parent class for SQL based Keyv storage adapters",
"main": "src/index.js",
"scripts": {
"test": "xo && nyc --no-clean --temp-dir ../../.nyc_output ava"
"test": "ava"
},
"xo": {
"extends": "xo-lukechilds"
Expand All @@ -31,11 +31,5 @@
"url": "https://github.com/keyvhq/keyv/issues"
},
"homepage": "https://github.com/keyvhq/keyv",
"dependencies": {
"sql-ts": "^5.2.0"
},
"devDependencies": {
"@keyvhq/keyv-test-suite": "file:../keyv-test-suite"
},
"gitHead": "a4e2c1f285236a753de13eb8a42d9a98690526cc"
}
43 changes: 16 additions & 27 deletions packages/keyv-sql/src/index.js
@@ -1,8 +1,6 @@
'use strict';

const EventEmitter = require('events');
const Sql = require('sql-ts').Sql;

class KeyvSql extends EventEmitter {
constructor(options) {
super();
Expand All @@ -13,34 +11,19 @@ class KeyvSql extends EventEmitter {
keySize: 255
}, options);

const sql = new Sql(options.dialect);

this.entry = sql.define({
name: this.options.table,
columns: [
{
name: 'key',
primaryKey: true,
dataType: `VARCHAR(${Number(this.options.keySize)})`
},
{
name: 'value',
dataType: 'TEXT'
}
]
});
const createTable = this.entry.create().ifNotExists().toString();
let createTable = this.options.dialect == 'mysql' ? `CREATE TABLE IF NOT EXISTS \`${this.options.table}\` (\`key\` VARCHAR(${this.options.keySize}) PRIMARY KEY, \`value\` TEXT)` : `CREATE TABLE IF NOT EXISTS "${this.options.table}" ("key" VARCHAR(${this.options.keySize}) PRIMARY KEY, "value" TEXT)`;

const connected = this.options.connect()
.then(query => query(createTable).then(() => query))
.catch(error => this.emit('error', error));

this.query = sqlString => connected
.then(query => query(sqlString));

}

get(key) {
const select = this.entry.select().where({ key }).toString();
const select = this.options.dialect == 'mysql' ? `SELECT \`${this.options.table}\`.* FROM \`${this.options.table}\` WHERE (\`${this.options.table}\`.\`key\` = '${key}')`: `SELECT "${this.options.table}".* FROM "${this.options.table}" WHERE ("${this.options.table}"."key" = '${key}')`;
return this.query(select)
.then(rows => {
const row = rows[0];
Expand All @@ -56,16 +39,18 @@ class KeyvSql extends EventEmitter {
if (this.options.dialect === 'mysql') {
value = value.replace(/\\/g, '\\\\');
}

const upsert = this.options.dialect === 'postgres' ?
this.entry.insert({ key, value }).onConflict({ columns: ['key'], update: ['value'] }).toString() : this.entry.replace({ key, value }).toString();
`INSERT INTO "${this.options.table}" ("key", "value") VALUES ('${key}', '${value}') ON CONFLICT ("key") DO UPDATE SET "value" = EXCLUDED."value"` :
this.options.dialect == 'mysql' ?
`REPLACE INTO \`${this.options.table}\` (\`key\`, \`value\`) VALUES ('${key}', '${value}')`
: `REPLACE INTO "${this.options.table}" ("key", "value") VALUES ('${key}', '${value}')`;

return this.query(upsert);
}

delete(key) {
const select = this.entry.select().where({ key }).toString();
const del = this.entry.delete().where({ key }).toString();
const select = this.options.dialect == 'mysql' ? `SELECT \`${this.options.table}\`.* FROM \`${this.options.table}\` WHERE (\`${this.options.table}\`.\`key\` = '${key}')` : `SELECT "${this.options.table}".* FROM "${this.options.table}" WHERE ("${this.options.table}"."key" = '${key}')`
const del = this.options.dialect == 'mysql' ? `DELETE FROM \`${this.options.table}\` WHERE (\`${this.options.table}\`.\`key\` = '${key}')` : `DELETE FROM "${this.options.table}" WHERE ("${this.options.table}"."key" = '${key}')`
return this.query(select)
.then(rows => {
const row = rows[0];
Expand All @@ -79,10 +64,14 @@ class KeyvSql extends EventEmitter {
}

clear() {
const del = this.entry.delete(this.entry.key.like(`${this.namespace}:%`)).toString();
const del = this.options.dialect == 'mysql' ? `DELETE FROM \`${this.options.table}\` WHERE (\`${this.options.table}\`.\`key\` LIKE '${this.namespace}:%')` : `DELETE FROM "${this.options.table}" WHERE ("${this.options.table}"."key" LIKE '${this.namespace}:%')`;
return this.query(del)
.then(() => undefined);
}
}

module.exports = KeyvSql;
module.exports = KeyvSql
/*
REPLACE INTO "table" ("key", "value") VALUES ('KEY', 'VALUE')
INSERT INTO "table" ("key", "value") VALUES ('KEY', 'VALUE') ON CONFLICT ("key") DO UPDATE SET "value" = EXCLUDED."value"
REPLACE INTO `table` (`key`, `value`) VALUES ('KEY', 'VALUE')
*/
16 changes: 6 additions & 10 deletions packages/keyv-sql/test/test.js
Expand Up @@ -31,13 +31,9 @@ class TestSqlite extends KeyvSql {

const store = () => new TestSqlite();
keyvTestSuite(test, Keyv, store);

test('Default key data type is VARCHAR(255)', t => {
const store = new TestSqlite();
t.is(store.entry.key.dataType, 'VARCHAR(255)');
});

test('keySize option overrides default', t => {
const store = new TestSqlite({ keySize: 100 });
t.is(store.entry.key.dataType, 'VARCHAR(100)');
});
const keyv = store();
test.serial(`basic`, async(t) => {
const keyv = new Keyv({ store: store() });
await keyv.set('foo', 0);
t.is(await keyv.get('foo'), 0)
})
6 changes: 0 additions & 6 deletions packages/keyv-sqlite/package.json
Expand Up @@ -37,11 +37,5 @@
"pify": "5.0.0",
"sqlite3": "^5.0.2"
},
"devDependencies": {
"@keyvhq/keyv-test-suite": "file:../keyv-test-suite"
},
"directories": {
"test": "test"
},
"gitHead": "a4e2c1f285236a753de13eb8a42d9a98690526cc"
}

0 comments on commit 171bd12

Please sign in to comment.