Skip to content

Commit 4902767

Browse files
authored
Remove nedb (sqlpad#877)
* Ensure sqlpad is v5 or later for v6 * Remove nedb * clarify comment
1 parent 3f7cc15 commit 4902767

33 files changed

Lines changed: 120 additions & 772 deletions

server/lib/db.js

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,46 @@
11
const path = require('path');
2-
const datastore = require('nedb-promise');
32
const mkdirp = require('mkdirp');
4-
const appLog = require('./app-log');
53
const Models = require('../models');
64
const SequelizeDb = require('../sequelize-db');
75

86
/**
9-
* Whenever possible nedb should be read from the app req object.
7+
* Whenever possible db should be read from the app req object.
108
* Sometimes this isn't possible or convenient at the moment however.
11-
* For those cases, this module provides access by caching the nedb instance.
12-
* Safety measures have been added to ensure only 1 instance of nedb can be initialized per alias
9+
* For those cases, this module provides access by caching the db instance.
10+
* Safety measures have been added to ensure only 1 instance of db can be initialized per alias
1311
*/
1412
let instances = {};
1513

1614
/**
17-
* Get nedb instance for an optional alias.
18-
* Returns promise of nedb instance
15+
* Returns promise of db instance
1916
* @param {string} [instanceAlias]
2017
*/
2118
async function getDb(instanceAlias = 'default') {
2219
const instancePromise = instances[instanceAlias];
2320
if (!instancePromise) {
2421
throw new Error('db instance must be created first');
2522
}
26-
// nedb will already be a promise -- this just makes it explicit
27-
const { nedb, models, sequelizeDb } = await instancePromise;
28-
return { nedb, models, sequelizeDb };
23+
// already be a promise -- this just makes it explicit
24+
const { models, sequelizeDb } = await instancePromise;
25+
return { models, sequelizeDb };
2926
}
3027

3128
/**
32-
* Initialize nedb for a given config
29+
* Initialize db for a given config
3330
* @param {object} config
3431
*/
35-
async function initNedb(config) {
32+
async function initDb(config) {
3633
const dbPath = config.get('dbPath');
37-
const dbInMemory = config.get('dbInMemory');
38-
3934
mkdirp.sync(path.join(dbPath, '/cache'));
4035

41-
function getDatastore(dbName) {
42-
return dbInMemory
43-
? datastore()
44-
: datastore({ filename: path.join(dbPath, dbName) });
45-
}
46-
47-
const nedb = {
48-
users: getDatastore('users.db'),
49-
connections: getDatastore('connections.db'),
50-
connectionAccesses: getDatastore('connectionaccesses.db'),
51-
queries: getDatastore('queries.db'),
52-
queryHistory: getDatastore('queryhistory.db'),
53-
cache: getDatastore('cache.db'),
54-
instances: [
55-
'users',
56-
'connections',
57-
'connectionAccesses',
58-
'queries',
59-
'queryHistory',
60-
'cache',
61-
],
62-
};
63-
64-
// Load dbs, migrate data, and apply indexes
65-
await Promise.all(
66-
nedb.instances.map((dbname) => {
67-
appLog.info('Loading %s', dbname);
68-
return nedb[dbname].loadDatabase();
69-
})
70-
);
71-
7236
const sequelizeDb = new SequelizeDb(config);
7337
const models = new Models(sequelizeDb, config);
7438

75-
return { nedb, models, sequelizeDb };
39+
return { models, sequelizeDb };
7640
}
7741

7842
/**
79-
* Initializes an nedb instance for a given config.
43+
* Initializes a db instance for a given config.
8044
* Ensures that this only happens once for a given alias.
8145
* If this were called multiple times weird things could happen
8246
* @param {object} config
@@ -87,7 +51,7 @@ function makeDb(config, instanceAlias = 'default') {
8751
if (instances[instanceAlias]) {
8852
throw new Error(`db instance ${instanceAlias} already made`);
8953
}
90-
const dbPromise = initNedb(config);
54+
const dbPromise = initDb(config);
9155
instances[instanceAlias] = dbPromise;
9256
return true;
9357
}

server/lib/make-cipher.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

server/lib/make-migrator.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require('path');
22
const Umzug = require('umzug');
33

4-
function makeMigrator(config, appLog, nedb, sequelizeInstance) {
4+
function makeMigrator(config, appLog, sequelizeInstance) {
55
const umzug = new Umzug({
66
storage: 'sequelize',
77
storageOptions: {
@@ -16,7 +16,6 @@ function makeMigrator(config, appLog, nedb, sequelizeInstance) {
1616
sequelizeInstance.queryInterface,
1717
config,
1818
appLog,
19-
nedb,
2019
sequelizeInstance,
2120
],
2221
path: path.join(__dirname, '../migrations'),
@@ -36,6 +35,11 @@ function makeMigrator(config, appLog, nedb, sequelizeInstance) {
3635
return upToDate;
3736
},
3837

38+
/**
39+
* Returns promise containing major version of the database
40+
* If migrations have not yet run, 0 is returned.
41+
* If the version cannot be determined, -1 is returned
42+
*/
3943
async getDbMajorVersion() {
4044
const executed = await umzug.executed();
4145
if (executed.length === 0) {
@@ -61,31 +65,28 @@ function makeMigrator(config, appLog, nedb, sequelizeInstance) {
6165
// but I messed that up pretty eary on 😬
6266
//
6367
// v5 migrations = anything starting with 05, or >= 04-00200
64-
// 04-00200-nedb-sqlite-tables.js
68+
// 04-00200
6569
// ... all the way to
66-
// 05-00100-sessions.js
70+
// 05-00100
6771
//
6872
// v4 migrations = 04-00000 - 04-00199
69-
// 04-00100-query-acl-schema.js
73+
// 04-00100
7074
// ... all the way to
71-
// 04-00129-service-tokens-schema.js
75+
// 04-00129
7276
//
7377
const [majorString, minorString] = lastMigrationFile.split('-');
7478
const major = parseInt(majorString, 10);
7579
const minor = parseInt(minorString, 10);
7680

77-
if (major === 5) {
78-
return 5;
79-
}
80-
8181
if (major === 4) {
8282
if (minor >= 200) {
8383
return 5;
8484
}
8585
return 4;
8686
}
8787

88-
return -1;
88+
// Otherwise returns major as is (it'll be 5 or later)
89+
return major;
8990
},
9091
};
9192
}

server/migrations/04-00100-query-acl-schema.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ const Sequelize = require('sequelize');
44
* @param {import('sequelize').QueryInterface} queryInterface
55
* @param {import('../lib/config')} config
66
* @param {import('../lib/logger')} appLog
7-
* @param {object} nedb - collection of nedb objects created in /lib/db.js
87
* @param {object} sequelizeDb - sequelize instance
98
*/
109
// eslint-disable-next-line no-unused-vars
11-
async function up(queryInterface, config, appLog, nedb, sequelizeDb) {
10+
async function up(queryInterface, config, appLog, sequelizeDb) {
1211
if (config.get('backendDatabaseUri').startsWith('mssql')) {
1312
await sequelizeDb.query(
1413
`

server/migrations/04-00110-query-acl-data.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

server/migrations/04-00120-query-acl-remove-old-constraint.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
* @param {import('sequelize').QueryInterface} queryInterface
33
* @param {import('../lib/config')} config
44
* @param {import('../lib/logger')} appLog
5-
* @param {object} nedb - collection of nedb objects created in /lib/db.js
65
*/
76
// eslint-disable-next-line no-unused-vars
8-
async function up(queryInterface, config, appLog, nedb) {
7+
async function up(queryInterface, config, appLog) {
98
// Remove unique constraint on query_id_user_id (it'll be added again switched around later)
109
await queryInterface.removeConstraint(
1110
'query_acl',

server/migrations/04-00121-query-acl-add-user-email.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ const Sequelize = require('sequelize');
44
* @param {import('sequelize').QueryInterface} queryInterface
55
* @param {import('../lib/config')} config
66
* @param {import('../lib/logger')} appLog
7-
* @param {object} nedb - collection of nedb objects created in /lib/db.js
87
*/
98
// eslint-disable-next-line no-unused-vars
10-
async function up(queryInterface, config, appLog, nedb) {
9+
async function up(queryInterface, config, appLog) {
1110
await queryInterface.addColumn('query_acl', 'user_email', {
1211
type: Sequelize.STRING,
1312
});

server/migrations/04-00122-query-acl-add-group-id.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ const Sequelize = require('sequelize');
44
* @param {import('sequelize').QueryInterface} queryInterface
55
* @param {import('../lib/config')} config
66
* @param {import('../lib/logger')} appLog
7-
* @param {object} nedb - collection of nedb objects created in /lib/db.js
87
*/
98
// eslint-disable-next-line no-unused-vars
10-
async function up(queryInterface, config, appLog, nedb) {
9+
async function up(queryInterface, config, appLog) {
1110
await queryInterface.addColumn('query_acl', 'group_id', {
1211
type: Sequelize.STRING,
1312
});

server/migrations/04-00123-query-acl-remove-user-id-not-null.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ const Sequelize = require('sequelize');
44
* @param {import('sequelize').QueryInterface} queryInterface
55
* @param {import('../lib/config')} config
66
* @param {import('../lib/logger')} appLog
7-
* @param {object} nedb - collection of nedb objects created in /lib/db.js
87
*/
98
// eslint-disable-next-line no-unused-vars
10-
async function up(queryInterface, config, appLog, nedb) {
9+
async function up(queryInterface, config, appLog) {
1110
// remove not-null constraint for user_id
1211
await queryInterface.changeColumn('query_acl', 'user_id', {
1312
type: Sequelize.STRING,

server/migrations/04-00124-query-acl-add-constraint-user-email-query.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
* @param {import('sequelize').QueryInterface} queryInterface
33
* @param {import('../lib/config')} config
44
* @param {import('../lib/logger')} appLog
5-
* @param {object} nedb - collection of nedb objects created in /lib/db.js
65
*/
76
// eslint-disable-next-line no-unused-vars
8-
async function up(queryInterface, config, appLog, nedb) {
7+
async function up(queryInterface, config, appLog) {
98
// Add unique constraint for (user_email, query_id) and (group_id, query_id)
109
await queryInterface.addConstraint('query_acl', {
1110
type: 'unique',

0 commit comments

Comments
 (0)