Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/insomnia-inso/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { initRuntime } from 'insomnia/src/runtimes';
import { nodeRuntime } from 'insomnia/src/runtimes/runtime.node';
import { configureFetch } from 'insomnia-api';
import type {
BaseModel,
Environment,
Request,
RequestGroup,
Expand All @@ -41,7 +42,6 @@ import { isFile, loadDb } from './db';
import { insomniaExportAdapter } from './db/adapters/insomnia-adapter';
import { loadApiSpec, promptApiSpec } from './db/models/api-spec';
import { loadEnvironment, promptEnvironment } from './db/models/environment';
import type { BaseModel } from './db/models/types';
import { loadTestSuites, promptTestSuites } from './db/models/unit-test-suite';
import { matchIdIsh } from './db/models/util';
import { loadWorkspace, promptWorkspace } from './db/models/workspace';
Expand Down Expand Up @@ -641,11 +641,15 @@ export const go = (args?: string[]) => {
// attach this global env to the workspace
db.WorkspaceMeta = [
{
...models.workspaceMeta.init(),
activeGlobalEnvironmentId: globalEnv._id,
_id: `wrkm_${uuidv4().replace(/-/g, '')}`,
type: 'WorkspaceMeta',
parentId: workspaceId,
name: '',
modified: Date.now(),
created: Date.now(),
isPrivate: false,
},
];
}
Expand All @@ -665,11 +669,15 @@ export const go = (args?: string[]) => {
// attach this global env to the workspace
db.WorkspaceMeta = [
{
...models.workspaceMeta.init(),
activeGlobalEnvironmentId: firstGlobalEnv._id,
_id: `wrkm_${uuidv4().replace(/-/g, '')}`,
type: 'WorkspaceMeta',
parentId: workspaceId,
name: '',
modified: Date.now(),
created: Date.now(),
isPrivate: false,
},
];
}
Expand Down
7 changes: 3 additions & 4 deletions packages/insomnia-inso/src/db/adapters/insomnia-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { importInsomniaV5Data } from 'insomnia/src/common/insomnia-v5';
import YAML from 'yaml';

import { InsoError } from '../../errors';
import type { BaseModel } from '../models/types';
import type { DbAdapter } from '../types';
import type { Database, DbAdapter } from '../types';
import { emptyDb } from '../types';

/**
Expand Down Expand Up @@ -39,7 +38,7 @@ type RawTypeKey =
| 'unit_test_suite'
| 'unit_test';

const rawTypeToParsedTypeMap: Record<RawTypeKey, BaseModel['type']> = {
const rawTypeToParsedTypeMap: Record<RawTypeKey, keyof Database> = {
api_spec: 'ApiSpec',
environment: 'Environment',
request: 'Request',
Expand All @@ -55,7 +54,7 @@ type RawTypeModel = {
_type: RawTypeKey;
} & ExtraProperties;

type ParsedTypeModel = Pick<BaseModel, 'type'> & ExtraProperties;
type ParsedTypeModel = { type: keyof Database } & ExtraProperties;

const parseRawType = (type: RawTypeModel['_type']): ParsedTypeModel['type'] => rawTypeToParsedTypeMap[type];

Expand Down
18 changes: 18 additions & 0 deletions packages/insomnia-inso/src/db/adapters/ne-db-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,22 @@ describe('neDbAdapter()', () => {
const db = await neDbAdapter(workingDir);
expect(db).toBe(null);
});

it('should load all types from a real nedb data directory', async () => {
const workingDir = path.join(fixturesPath, 'nedb');
const db = await neDbAdapter(workingDir);
expect(db).not.toBeNull();
expect(db!.Workspace).toHaveLength(2);
expect(db!.Workspace.map(w => w.name).sort()).toEqual(['Insomnia Designer', 'Sample Spec 1.2']);
expect(db!.ApiSpec).toHaveLength(2);
expect(db!.ApiSpec.map(s => s.fileName).sort()).toEqual(['Insomnia Designer', 'Sample Specification']);
});

it('should only load the requested types when filterTypes is given', async () => {
const workingDir = path.join(fixturesPath, 'nedb');
const db = await neDbAdapter(workingDir, ['ApiSpec']);
expect(db).not.toBeNull();
expect(db!.ApiSpec).toHaveLength(2);
expect(db!.Workspace).toHaveLength(0);
});
});
54 changes: 34 additions & 20 deletions packages/insomnia-inso/src/db/adapters/ne-db-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@ import { stat } from 'node:fs/promises';
import path from 'node:path';

import NeDB from '@seald-io/nedb';
import type { BaseModel } from 'insomnia-data';
import { database, initDatabase } from 'insomnia-data';
import { createNedbDatabase } from 'insomnia-data/node';

import type { BaseModel } from '../models/types';
import type { Database, DbAdapter } from '../types';
import { emptyDb } from '../types';

/** Reads a single on-disk NeDB collection file as-is, without touching the file (no repair/migration). */
const readNedbFile = (filePath: string): Promise<BaseModel[]> =>
new Promise((resolve, reject) => {
const collection = new NeDB({
autoload: true,
filename: filePath,
corruptAlertThreshold: 0.9,
});
collection.find({}, (err: Error, docs: BaseModel[]) => {
if (err) {
return reject(err);
}

resolve(docs);
});
});

const neDbAdapter: DbAdapter = async (dir, filterTypes) => {
// Confirm if db files exist
try {
Expand All @@ -17,26 +36,21 @@ const neDbAdapter: DbAdapter = async (dir, filterTypes) => {

const db = emptyDb();
const types = filterTypes?.length ? filterTypes : (Object.keys(db) as (keyof Database)[]);
const promises = types.map(
t =>
new Promise((resolve, reject) => {
const filePath = path.join(dir, `insomnia.${t}.db`);
const collection = new NeDB({
autoload: true,
filename: filePath,
corruptAlertThreshold: 0.9,
});
collection.find({}, (err: Error, docs: BaseModel[]) => {
if (err) {
return reject(err);
}

(db[t] as {}[]).push(...docs);
resolve(null);
});
}),

const docsByType = await Promise.all(types.map(t => readNedbFile(path.join(dir, `insomnia.${t}.db`))));
const allDocs = docsByType.flat();

// Load the raw docs into an in-memory database so they go through insomnia-data's real model
// init/migration logic, the same way the desktop app and CLI network path already do.
await initDatabase(createNedbDatabase(), { inMemoryOnly: true }, true);
await database.batchModifyDocs({ upsert: allDocs });

await Promise.all(
types.map(async t => {
(db[t] as BaseModel[]).push(...(await database.find(t)));
}),
);
await Promise.all(promises);

return db;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/insomnia-inso/src/db/models/api-spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// @ts-expect-error the enquirer types are incomplete https://github.com/enquirer/enquirer/pull/307
import { AutoComplete } from 'enquirer';
import type { ApiSpec } from 'insomnia-data';

import { logger } from '../../logger';
import type { Database } from '../types';
import type { ApiSpec } from './types';
import { ensureSingleOrNone, generateIdIsh, getDbChoice, matchIdIsh } from './util';
const entity = 'api specification';

Expand Down
2 changes: 1 addition & 1 deletion packages/insomnia-inso/src/db/models/environment.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// @ts-expect-error the enquirer types are incomplete https://github.com/enquirer/enquirer/pull/307
import { AutoComplete } from 'enquirer';
import type { Environment } from 'insomnia-data';

import { logger } from '../../logger';
import type { Database } from '../types';
import type { Environment } from './types';
import { ensureSingle, generateIdIsh, getDbChoice, matchIdIsh } from './util';

const loadBaseEnvironmentForWorkspace = (db: Database, workspaceId: string): Environment => {
Expand Down
59 changes: 0 additions & 59 deletions packages/insomnia-inso/src/db/models/types.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/insomnia-inso/src/db/models/unit-test-suite.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @ts-expect-error the enquirer types are incomplete https://github.com/enquirer/enquirer/pull/307
import { AutoComplete } from 'enquirer';
import type { UnitTestSuite } from 'insomnia-data';

import { logger } from '../../logger';
import type { Database } from '../types';
import { loadApiSpec } from './api-spec';
import type { UnitTestSuite } from './types';
import { ensureSingleOrNone, generateIdIsh, getDbChoice, matchIdIsh } from './util';
import { loadWorkspace } from './workspace';

Expand Down
3 changes: 2 additions & 1 deletion packages/insomnia-inso/src/db/models/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { BaseModel } from 'insomnia-data';

import { InsoError } from '../../errors';
import type { BaseModel } from './types';

export const matchIdIsh = ({ _id }: BaseModel, identifier: string) => _id.startsWith(identifier);

Expand Down
2 changes: 1 addition & 1 deletion packages/insomnia-inso/src/db/models/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// @ts-expect-error the enquirer types are incomplete https://github.com/enquirer/enquirer/pull/307
import { AutoComplete } from 'enquirer';
import type { Workspace } from 'insomnia-data';

import { logger } from '../../logger';
import type { Database } from '../types';
import type { Workspace } from './types';
import { ensureSingleOrNone, generateIdIsh, getDbChoice, matchIdIsh } from './util';
const entity = 'workspace';
export const loadWorkspace = (db: Database, identifier: string) => {
Expand Down
9 changes: 6 additions & 3 deletions packages/insomnia-inso/src/db/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import type { CaCertificate, ClientCertificate, CloudProviderCredential, CookieJar, Settings } from 'insomnia-data';

import type {
ApiSpec,
BaseModel,
CaCertificate,
ClientCertificate,
CloudProviderCredential,
CookieJar,
Environment,
Settings,
UnitTest,
UnitTestSuite,
Workspace,
WorkspaceMeta,
} from './models/types';
} from 'insomnia-data';

export interface Database {
ApiSpec: ApiSpec[];
Expand Down
Loading