mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-01-22 04:18:10 +00:00
Merge pull request #2030 from balena-os/knex
Update dependencies to fix NPM build
This commit is contained in:
commit
4d515fe51f
3321
package-lock.json
generated
3321
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -75,7 +75,9 @@
|
||||
"@types/tmp": "^0.1.0",
|
||||
"@types/webpack": "^5.28.0",
|
||||
"@types/yargs": "^15.0.12",
|
||||
"balena-auth": "^4.1.2",
|
||||
"balena-register-device": "^8.0.0",
|
||||
"balena-request": "^11.5.9",
|
||||
"blinking": "0.0.5",
|
||||
"bluebird": "^3.7.2",
|
||||
"chai": "^4.3.4",
|
||||
@ -98,7 +100,7 @@
|
||||
"io-ts": "^2.2.10",
|
||||
"io-ts-reporters": "^1.2.2",
|
||||
"json-mask": "^0.3.9",
|
||||
"knex": "^0.20.13",
|
||||
"knex": "^0.95.15",
|
||||
"lint-staged": "^13.0.3",
|
||||
"livepush": "^3.5.1",
|
||||
"lodash": "^4.17.21",
|
||||
@ -120,7 +122,7 @@
|
||||
"rwlock": "^5.0.0",
|
||||
"shell-quote": "^1.7.2",
|
||||
"sinon": "^11.1.2",
|
||||
"sinon-chai": "^3.6.0",
|
||||
"sinon-chai": "^3.7.0",
|
||||
"strict-event-emitter-types": "^2.0.0",
|
||||
"supertest": "^6.1.3",
|
||||
"tar-stream": "^2.1.3",
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { EventEmitter } from 'events';
|
||||
import type { Transaction } from 'knex';
|
||||
import type { Knex } from 'knex';
|
||||
import * as _ from 'lodash';
|
||||
import StrictEventEmitter from 'strict-event-emitter-types';
|
||||
import { inspect } from 'util';
|
||||
@ -50,7 +50,7 @@ export const removeListener: typeof events['removeListener'] =
|
||||
|
||||
export async function get<T extends SchemaTypeKey>(
|
||||
key: T,
|
||||
trx?: Transaction,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<SchemaReturn<T>> {
|
||||
const $db = trx || db.models;
|
||||
|
||||
@ -115,7 +115,7 @@ export async function get<T extends SchemaTypeKey>(
|
||||
|
||||
export async function getMany<T extends SchemaTypeKey>(
|
||||
keys: T[],
|
||||
trx?: Transaction,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<{ [key in T]: SchemaReturn<key> }> {
|
||||
const values = await Promise.all(keys.map((k) => get(k, trx)));
|
||||
return _.zipObject(keys, values) as unknown as Promise<{
|
||||
@ -125,9 +125,9 @@ export async function getMany<T extends SchemaTypeKey>(
|
||||
|
||||
export async function set<T extends SchemaTypeKey>(
|
||||
keyValues: ConfigMap<T>,
|
||||
trx?: Transaction,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> {
|
||||
const setValuesInTransaction = async (tx: Transaction) => {
|
||||
const setValuesInTransaction = async (tx: Knex.Transaction) => {
|
||||
const configJsonVals: Dictionary<unknown> = {};
|
||||
const dbVals: Dictionary<unknown> = {};
|
||||
|
||||
|
14
src/db.ts
14
src/db.ts
@ -1,4 +1,4 @@
|
||||
import * as Knex from 'knex';
|
||||
import { knex, Knex } from 'knex';
|
||||
import * as path from 'path';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
@ -9,7 +9,7 @@ type DBTransactionCallback = (trx: Knex.Transaction) => void;
|
||||
export type Transaction = Knex.Transaction;
|
||||
|
||||
const databasePath = constants.databasePath;
|
||||
const knex = Knex({
|
||||
const db = knex({
|
||||
client: 'sqlite3',
|
||||
connection: {
|
||||
filename: databasePath,
|
||||
@ -19,17 +19,17 @@ const knex = Knex({
|
||||
|
||||
export const initialized = _.once(async () => {
|
||||
try {
|
||||
await knex('knex_migrations_lock').update({ is_locked: 0 });
|
||||
await db('knex_migrations_lock').update({ is_locked: 0 });
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
return knex.migrate.latest({
|
||||
return db.migrate.latest({
|
||||
directory: path.join(__dirname, 'migrations'),
|
||||
});
|
||||
});
|
||||
|
||||
export function models(modelName: string): Knex.QueryBuilder {
|
||||
return knex(modelName);
|
||||
return db(modelName);
|
||||
}
|
||||
|
||||
export async function upsertModel(
|
||||
@ -38,7 +38,7 @@ export async function upsertModel(
|
||||
id: Dictionary<unknown>,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<any> {
|
||||
const k = trx || knex;
|
||||
const k = trx || db;
|
||||
|
||||
const n = await k(modelName).update(obj).where(id);
|
||||
if (n === 0) {
|
||||
@ -49,5 +49,5 @@ export async function upsertModel(
|
||||
export function transaction(
|
||||
cb: DBTransactionCallback,
|
||||
): Promise<Knex.Transaction> {
|
||||
return knex.transaction(cb);
|
||||
return db.transaction(cb);
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import * as Bluebird from 'bluebird';
|
||||
import * as Knex from 'knex';
|
||||
import { knex, Knex } from 'knex';
|
||||
|
||||
import { expect } from 'chai';
|
||||
import prepare = require('~/test-lib/prepare');
|
||||
@ -7,7 +7,7 @@ import * as constants from '~/lib/constants';
|
||||
import { exists } from '~/lib/fs-utils';
|
||||
|
||||
async function createOldDatabase(path: string) {
|
||||
const knex = Knex({
|
||||
const db = knex({
|
||||
client: 'sqlite3',
|
||||
connection: {
|
||||
filename: path,
|
||||
@ -19,7 +19,7 @@ async function createOldDatabase(path: string) {
|
||||
name: string,
|
||||
fn: (trx: Knex.CreateTableBuilder) => void,
|
||||
) =>
|
||||
knex.schema.createTable(name, (t) => {
|
||||
db.schema.createTable(name, (t) => {
|
||||
if (fn != null) {
|
||||
return fn(t);
|
||||
}
|
||||
@ -38,7 +38,7 @@ async function createOldDatabase(path: string) {
|
||||
await createEmptyTable('dependentDevice', (t) =>
|
||||
t.increments('id').primary(),
|
||||
);
|
||||
return knex;
|
||||
return db;
|
||||
}
|
||||
|
||||
describe('Database Migrations', () => {
|
||||
|
@ -1,68 +0,0 @@
|
||||
import * as _ from 'lodash';
|
||||
import { SinonStub, stub } from 'sinon';
|
||||
import { QueryBuilder } from 'knex';
|
||||
|
||||
import * as db from '~/src/db';
|
||||
import { Image } from '~/src/compose/images';
|
||||
|
||||
let modelStub: SinonStub | null = null;
|
||||
// MOCKED MODELS
|
||||
const MOCKED_MODELS: Dictionary<QueryBuilder> = {};
|
||||
// MOCKED MODEL TYPES
|
||||
let MOCKED_IMAGES: Image[] = [];
|
||||
|
||||
export function create(): SinonStub {
|
||||
if (modelStub === null) {
|
||||
modelStub = stub(db, 'models');
|
||||
// Stub model requests to return our stubbed models
|
||||
modelStub.callsFake((model: string) => {
|
||||
return MOCKED_MODELS[model];
|
||||
});
|
||||
}
|
||||
return modelStub;
|
||||
}
|
||||
|
||||
export function restore(): void {
|
||||
if (modelStub !== null) {
|
||||
modelStub.restore();
|
||||
}
|
||||
}
|
||||
|
||||
export function setImages(images: Image[]) {
|
||||
MOCKED_IMAGES = images;
|
||||
return {
|
||||
stub: stubImages,
|
||||
};
|
||||
}
|
||||
|
||||
function stubImages() {
|
||||
// Set the functions for this model (add them as you need for your test cases)
|
||||
MOCKED_MODELS['image'] = {
|
||||
select: () => {
|
||||
return {
|
||||
where: async (condition: Partial<Image>) =>
|
||||
_(MOCKED_IMAGES).pickBy(condition).map().value(),
|
||||
};
|
||||
},
|
||||
where: (condition: Partial<Image>) => {
|
||||
return {
|
||||
select: async () => _(MOCKED_IMAGES).pickBy(condition).map().value(),
|
||||
};
|
||||
},
|
||||
del: () => {
|
||||
return {
|
||||
where: async (condition: Partial<Image>) => {
|
||||
return _(MOCKED_IMAGES)
|
||||
.pickBy(condition)
|
||||
.map()
|
||||
.value()
|
||||
.forEach((val) => {
|
||||
MOCKED_IMAGES = _.reject(MOCKED_IMAGES, {
|
||||
id: val.id,
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
||||
},
|
||||
} as unknown as QueryBuilder;
|
||||
}
|
Loading…
Reference in New Issue
Block a user