mirror of
https://github.com/morten-olsen/plainidx.git
synced 2026-02-08 01:06:24 +01:00
init
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/node_modules/
|
||||
/coverage/
|
||||
18
.prettierrc.json
Normal file
18
.prettierrc.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"arrowParens": "always",
|
||||
"bracketSpacing": true,
|
||||
"htmlWhitespaceSensitivity": "css",
|
||||
"insertPragma": false,
|
||||
"bracketSameLine": false,
|
||||
"jsxSingleQuote": false,
|
||||
"printWidth": 120,
|
||||
"proseWrap": "preserve",
|
||||
"quoteProps": "as-needed",
|
||||
"requirePragma": false,
|
||||
"semi": true,
|
||||
"singleQuote": true,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "all",
|
||||
"useTabs": false,
|
||||
"singleAttributePerLine": false
|
||||
}
|
||||
22
eslint.config.js
Normal file
22
eslint.config.js
Normal file
@@ -0,0 +1,22 @@
|
||||
import { FlatCompat } from '@eslint/eslintrc';
|
||||
import eslint from '@eslint/js';
|
||||
import tseslint from 'typescript-eslint';
|
||||
import eslintConfigPrettier from 'eslint-config-prettier';
|
||||
|
||||
const compat = new FlatCompat({
|
||||
baseDirectory: import.meta.__dirname,
|
||||
resolvePluginsRelativeTo: import.meta.__dirname,
|
||||
});
|
||||
|
||||
export default tseslint.config(
|
||||
eslint.configs.recommended,
|
||||
...tseslint.configs.strict,
|
||||
...tseslint.configs.stylistic,
|
||||
eslintConfigPrettier,
|
||||
{
|
||||
rules: {
|
||||
'@typescript-eslint/consistent-type-definitions': ['error', 'type'],
|
||||
},
|
||||
},
|
||||
...compat.extends('plugin:prettier/recommended'),
|
||||
);
|
||||
27
package.json
Normal file
27
package.json
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "plaindb",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "vitest --coverage",
|
||||
"lint": "eslint packages/*/src",
|
||||
"build:dev": "tsc --build --watch"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@eslint/eslintrc": "^3.2.0",
|
||||
"@eslint/js": "^9.16.0",
|
||||
"@vitest/coverage-v8": "2.1.8",
|
||||
"eslint": "^9.16.0",
|
||||
"eslint-config-prettier": "^9.1.0",
|
||||
"eslint-plugin-prettier": "^5.2.1",
|
||||
"prettier": "^3.4.2",
|
||||
"typescript": "^5.7.2",
|
||||
"typescript-eslint": "^8.18.0",
|
||||
"vitest": "^2.1.8"
|
||||
}
|
||||
}
|
||||
2
packages/fs-memory/.gitignore
vendored
Normal file
2
packages/fs-memory/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/node_modules/
|
||||
/dist/
|
||||
19
packages/fs-memory/package.json
Normal file
19
packages/fs-memory/package.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "@plaindb/fs-memory",
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"main": "dist/exports.js",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "tsc --build"
|
||||
},
|
||||
"dependencies": {
|
||||
"@plaindb/plaindb": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.10.1",
|
||||
"typescript": "^5.7.2"
|
||||
}
|
||||
}
|
||||
36
packages/fs-memory/src/exports.ts
Normal file
36
packages/fs-memory/src/exports.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { FileSystem } from '@plaindb/plaindb';
|
||||
|
||||
type MemoryFileSystemOptions = {
|
||||
documents?: Record<string, Buffer>;
|
||||
};
|
||||
|
||||
class MemoryFileSystem extends FileSystem {
|
||||
#options: MemoryFileSystemOptions;
|
||||
#files: Record<string, Buffer | undefined>;
|
||||
|
||||
constructor(options: MemoryFileSystemOptions = {}) {
|
||||
super();
|
||||
this.#options = options;
|
||||
this.#files = {
|
||||
...this.#options.documents,
|
||||
};
|
||||
}
|
||||
|
||||
public get = async (location: string): Promise<Buffer | undefined> => {
|
||||
return this.#files[location];
|
||||
};
|
||||
|
||||
public set = async (location: string, data: Buffer): Promise<void> => {
|
||||
this.#files[location] = data;
|
||||
};
|
||||
|
||||
public update = async (files: Record<string, Buffer | undefined>) => {
|
||||
this.#files = {
|
||||
...this.#files,
|
||||
...files,
|
||||
};
|
||||
this.emit('changed', Object.keys(files));
|
||||
};
|
||||
}
|
||||
|
||||
export { MemoryFileSystem };
|
||||
9
packages/fs-memory/tsconfig.json
Normal file
9
packages/fs-memory/tsconfig.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
]
|
||||
}
|
||||
2
packages/fs-system/.gitignore
vendored
Normal file
2
packages/fs-system/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/node_modules/
|
||||
/dist/
|
||||
20
packages/fs-system/package.json
Normal file
20
packages/fs-system/package.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "@plaindb/fs-system",
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"main": "dist/exports.js",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "tsc --build"
|
||||
},
|
||||
"dependencies": {
|
||||
"@plaindb/plaindb": "workspace:*",
|
||||
"glob": "^11.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.10.1",
|
||||
"typescript": "^5.7.2"
|
||||
}
|
||||
}
|
||||
48
packages/fs-system/src/exports.ts
Normal file
48
packages/fs-system/src/exports.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { FileSystem } from '@plaindb/plaindb';
|
||||
import { existsSync } from 'fs';
|
||||
import { mkdir, readFile, writeFile } from 'fs/promises';
|
||||
import { dirname, resolve } from 'path';
|
||||
import { glob } from 'glob';
|
||||
|
||||
type SystemFileSystemOptions = {
|
||||
cwd: string;
|
||||
};
|
||||
|
||||
class SystemFileSystem extends FileSystem {
|
||||
#options: SystemFileSystemOptions;
|
||||
|
||||
constructor(options: SystemFileSystemOptions) {
|
||||
super();
|
||||
this.#options = options;
|
||||
}
|
||||
|
||||
public get = async (location: string): Promise<Buffer | undefined> => {
|
||||
const { cwd } = this.#options;
|
||||
const actualLocation = resolve(cwd, location);
|
||||
if (!existsSync(actualLocation)) {
|
||||
return undefined;
|
||||
}
|
||||
return readFile(actualLocation);
|
||||
};
|
||||
|
||||
public set = async (location: string, data: Buffer): Promise<void> => {
|
||||
const { cwd } = this.#options;
|
||||
const actualLocation = resolve(cwd, location);
|
||||
const dir = dirname(actualLocation);
|
||||
if (!existsSync(dir)) {
|
||||
await mkdir(dir, { recursive: true });
|
||||
}
|
||||
await writeFile(actualLocation, data);
|
||||
};
|
||||
|
||||
public update = async () => {
|
||||
const { cwd } = this.#options;
|
||||
const files = await glob('**/*', {
|
||||
cwd,
|
||||
nodir: true,
|
||||
});
|
||||
await this.emit('changed', files);
|
||||
};
|
||||
}
|
||||
|
||||
export { SystemFileSystem };
|
||||
9
packages/fs-system/tsconfig.json
Normal file
9
packages/fs-system/tsconfig.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
]
|
||||
}
|
||||
2
packages/plaindb/.gitignore
vendored
Normal file
2
packages/plaindb/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/node_modules/
|
||||
/dist/
|
||||
21
packages/plaindb/package.json
Normal file
21
packages/plaindb/package.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "@plaindb/plaindb",
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"main": "dist/exports.js",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "tsc --build"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.10.1",
|
||||
"typescript": "^5.7.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"knex": "^3.1.0",
|
||||
"nanoid": "^5.0.9",
|
||||
"zod": "^3.24.0"
|
||||
}
|
||||
}
|
||||
56
packages/plaindb/src/databases/databases.ts
Normal file
56
packages/plaindb/src/databases/databases.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import knex, { Knex } from 'knex';
|
||||
|
||||
type DatabaseMigration = {
|
||||
name: string;
|
||||
up: (knex: Knex) => Promise<void>;
|
||||
down: (knex: Knex) => Promise<void>;
|
||||
};
|
||||
|
||||
type DatabaseGetOptions = {
|
||||
name: string;
|
||||
migrations: DatabaseMigration[];
|
||||
};
|
||||
|
||||
type DatabasesOptions = {
|
||||
getConfig: (name: string) => Knex.Config;
|
||||
};
|
||||
|
||||
class Databases {
|
||||
#dbs = new Map<string, Knex>();
|
||||
#options: DatabasesOptions;
|
||||
|
||||
constructor(options: DatabasesOptions) {
|
||||
this.#options = options;
|
||||
}
|
||||
|
||||
public get = async ({ name, migrations }: DatabaseGetOptions) => {
|
||||
if (!this.#dbs.has(name)) {
|
||||
const { getConfig } = this.#options;
|
||||
const migrationSource: Knex.MigrationSource<DatabaseMigration> = {
|
||||
getMigrations: async () => migrations,
|
||||
getMigrationName: (migration) => migration.name,
|
||||
getMigration: async (migration) => migration,
|
||||
};
|
||||
const config: Knex.Config = {
|
||||
...getConfig(name),
|
||||
migrations: {
|
||||
migrationSource,
|
||||
},
|
||||
};
|
||||
const db = knex(config);
|
||||
await db.migrate.latest();
|
||||
this.#dbs.set(name, db);
|
||||
}
|
||||
const value = this.#dbs.get(name);
|
||||
if (!value) {
|
||||
throw new Error('Database not found');
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
public close = async () => {
|
||||
await Promise.all(Array.from(this.#dbs.values()).map((db) => db.destroy()));
|
||||
};
|
||||
}
|
||||
|
||||
export { Databases, type DatabasesOptions, type DatabaseMigration, type DatabaseGetOptions };
|
||||
52
packages/plaindb/src/documents/documents.document.ts
Normal file
52
packages/plaindb/src/documents/documents.document.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { EventEmitter } from '../utils/eventemitter.js';
|
||||
|
||||
type DocumentEvent = {
|
||||
save: (document: Document) => void;
|
||||
change: (document: Document) => void;
|
||||
};
|
||||
|
||||
type DocumentOptions = {
|
||||
id: string;
|
||||
location: string;
|
||||
data: Buffer;
|
||||
};
|
||||
|
||||
class Document extends EventEmitter<DocumentEvent> {
|
||||
#id: string;
|
||||
#location: string;
|
||||
#data: Buffer;
|
||||
|
||||
constructor({ location, data, id }: DocumentOptions) {
|
||||
super();
|
||||
this.#id = id;
|
||||
this.#location = location;
|
||||
this.#data = data;
|
||||
}
|
||||
|
||||
public get id() {
|
||||
return this.#id;
|
||||
}
|
||||
|
||||
public get location() {
|
||||
return this.#location;
|
||||
}
|
||||
|
||||
public get data() {
|
||||
return this.#data;
|
||||
}
|
||||
|
||||
public set data(data: Buffer) {
|
||||
this.#data = data;
|
||||
this.emit('change', this);
|
||||
}
|
||||
|
||||
public replace = (data: Buffer) => {
|
||||
this.#data = data;
|
||||
};
|
||||
|
||||
public save = async () => {
|
||||
await this.emit('save', this);
|
||||
};
|
||||
}
|
||||
|
||||
export { Document };
|
||||
108
packages/plaindb/src/documents/documents.ts
Normal file
108
packages/plaindb/src/documents/documents.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import { Knex } from 'knex';
|
||||
import { nanoid } from 'nanoid';
|
||||
import { createHash } from 'crypto';
|
||||
import { Databases } from '../databases/databases.js';
|
||||
import { FileSystem } from '../filesystem/filesystem.js';
|
||||
import { EventEmitter } from '../utils/eventemitter.js';
|
||||
import { Document } from './documents.document.js';
|
||||
import { migrations } from './migrations/migrations.js';
|
||||
|
||||
type DocumentsEvents = {
|
||||
change: (loctions: string[]) => void;
|
||||
save: (document: Document) => void;
|
||||
};
|
||||
|
||||
type DocumentsOptions = {
|
||||
fs: FileSystem;
|
||||
databases: Databases;
|
||||
};
|
||||
|
||||
class Documents extends EventEmitter<DocumentsEvents> {
|
||||
#options: DocumentsOptions;
|
||||
#documents: Map<string, Document>;
|
||||
#db?: Promise<Knex>;
|
||||
|
||||
constructor(options: DocumentsOptions) {
|
||||
super();
|
||||
this.#options = options;
|
||||
this.#documents = new Map();
|
||||
this.#options.fs.on('changed', this.#onChanges);
|
||||
}
|
||||
|
||||
#onChanges = async (changes: string[]) => {
|
||||
const db = await this.#getDB();
|
||||
const actualChanges: string[] = [];
|
||||
for (const location of changes) {
|
||||
const current = await db('files').where({ location }).first();
|
||||
const document = await this.get(location);
|
||||
const hash = createHash('sha256').update(document.data).digest('hex');
|
||||
if (!current) {
|
||||
await db('files').insert({
|
||||
id: nanoid(),
|
||||
location,
|
||||
hash,
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
} else if (current.hash !== hash) {
|
||||
await db('files').where({ location }).update({
|
||||
hash,
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
await this.emit('save', document);
|
||||
actualChanges.push(location);
|
||||
}
|
||||
if (actualChanges.length) {
|
||||
this.emit('change', actualChanges);
|
||||
}
|
||||
};
|
||||
|
||||
#getDB = async () => {
|
||||
if (!this.#db) {
|
||||
const { databases } = this.#options;
|
||||
this.#db = databases.get({
|
||||
name: 'documents',
|
||||
migrations,
|
||||
});
|
||||
}
|
||||
return this.#db;
|
||||
};
|
||||
|
||||
#onSave = async (document: Document) => {
|
||||
const { fs } = this.#options;
|
||||
await this.emit('save', document);
|
||||
await fs.set(document.location, document.data);
|
||||
};
|
||||
|
||||
public all = async () => {
|
||||
const db = await this.#getDB();
|
||||
const files = await db('files').select('*');
|
||||
return files.map((file: { location: string }) => file.location as string);
|
||||
};
|
||||
|
||||
public get = async (location: string) => {
|
||||
if (!this.#documents.has(location)) {
|
||||
const db = await this.#getDB();
|
||||
const { fs } = this.#options;
|
||||
const data = await fs.get(location);
|
||||
const current = await db('files').select('*').where({ location }).first();
|
||||
const document = new Document({
|
||||
id: current?.id || nanoid(),
|
||||
location,
|
||||
data: data || Buffer.from(''),
|
||||
});
|
||||
document.on('save', this.#onSave);
|
||||
this.#documents.set(location, document);
|
||||
}
|
||||
const value = this.#documents.get(location);
|
||||
if (!value) {
|
||||
throw new Error('Document not found');
|
||||
}
|
||||
return value;
|
||||
};
|
||||
}
|
||||
|
||||
export { Documents };
|
||||
export { Document } from './documents.document.js';
|
||||
24
packages/plaindb/src/documents/migrations/migrations.ts
Normal file
24
packages/plaindb/src/documents/migrations/migrations.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { DatabaseMigration } from '../../databases/databases.js';
|
||||
|
||||
const migrations: DatabaseMigration[] = [
|
||||
{
|
||||
name: 'init',
|
||||
up: async (knex) => {
|
||||
await knex.schema.createTable('files', (table) => {
|
||||
table.string('id').primary();
|
||||
table.string('location').notNullable();
|
||||
table.string('hash').notNullable();
|
||||
table.dateTime('updatedAt').notNullable();
|
||||
table.dateTime('syncedAt').nullable();
|
||||
table.unique('location');
|
||||
table.index('updatedAt');
|
||||
table.index('hash');
|
||||
});
|
||||
},
|
||||
down: async (knex) => {
|
||||
await knex.schema.dropTable('files');
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
export { migrations };
|
||||
10
packages/plaindb/src/exports.ts
Normal file
10
packages/plaindb/src/exports.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export { PlainDB } from './plaindb/plaindb.js';
|
||||
export { FileSystem } from './filesystem/filesystem.js';
|
||||
export { Documents } from './documents/documents.js';
|
||||
export { Document } from './documents/documents.document.js';
|
||||
export { createActionApiRoute } from './plugins/plugin/plugin.api.js';
|
||||
export { Plugins } from './plugins/plugins.js';
|
||||
export { Plugin } from './plugins/plugin/plugin.js';
|
||||
export { Databases, DatabaseMigration } from './databases/databases.js';
|
||||
export * from 'zod';
|
||||
export { type Knex as Database } from 'knex';
|
||||
12
packages/plaindb/src/filesystem/filesystem.ts
Normal file
12
packages/plaindb/src/filesystem/filesystem.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { EventEmitter } from '../utils/eventemitter.js';
|
||||
|
||||
type FileSystemEvents = {
|
||||
changed: (locations: string[]) => void;
|
||||
};
|
||||
|
||||
abstract class FileSystem extends EventEmitter<FileSystemEvents> {
|
||||
abstract get: (location: string) => Promise<Buffer | undefined>;
|
||||
abstract set: (location: string, data: Buffer) => Promise<void>;
|
||||
}
|
||||
|
||||
export { FileSystem };
|
||||
50
packages/plaindb/src/plaindb/plaindb.ts
Normal file
50
packages/plaindb/src/plaindb/plaindb.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Documents } from '../documents/documents.js';
|
||||
import { FileSystem } from '../filesystem/filesystem.js';
|
||||
import { Plugins } from '../plugins/plugins.js';
|
||||
import { Databases } from '../databases/databases.js';
|
||||
|
||||
type PlainDBOptions = {
|
||||
fs: FileSystem;
|
||||
getDBConfig: (name: string) => Knex.Config;
|
||||
};
|
||||
|
||||
class PlainDB {
|
||||
#options: PlainDBOptions;
|
||||
#documents: Documents;
|
||||
#plugins: Plugins;
|
||||
#databases: Databases;
|
||||
|
||||
constructor(options: PlainDBOptions) {
|
||||
this.#options = options;
|
||||
this.#databases = new Databases({
|
||||
getConfig: this.#options.getDBConfig,
|
||||
});
|
||||
this.#documents = new Documents({
|
||||
fs: this.#options.fs,
|
||||
databases: this.#databases,
|
||||
});
|
||||
this.#plugins = new Plugins({
|
||||
documents: this.#documents,
|
||||
databases: this.#databases,
|
||||
});
|
||||
}
|
||||
|
||||
public get documents() {
|
||||
return this.#documents;
|
||||
}
|
||||
|
||||
public get plugins() {
|
||||
return this.#plugins;
|
||||
}
|
||||
|
||||
public get databases() {
|
||||
return this.#databases;
|
||||
}
|
||||
|
||||
public close = async () => {
|
||||
await this.#databases.close();
|
||||
};
|
||||
}
|
||||
|
||||
export { PlainDB };
|
||||
18
packages/plaindb/src/plugins/plugin/plugin.api.ts
Normal file
18
packages/plaindb/src/plugins/plugin/plugin.api.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { z, ZodSchema } from 'zod';
|
||||
|
||||
type PluginActionApi = Record<
|
||||
string,
|
||||
{
|
||||
input?: ZodSchema;
|
||||
output?: ZodSchema;
|
||||
handle?: (input: any) => Promise<any>;
|
||||
}
|
||||
>;
|
||||
|
||||
const createActionApiRoute = <TInput extends ZodSchema = ZodSchema, TOutput extends ZodSchema = ZodSchema>(options: {
|
||||
input?: TInput;
|
||||
output?: TOutput;
|
||||
handle?: (input: z.infer<TInput>) => Promise<z.infer<TOutput>>;
|
||||
}) => options satisfies PluginActionApi[string];
|
||||
|
||||
export { type PluginActionApi, createActionApiRoute };
|
||||
108
packages/plaindb/src/plugins/plugin/plugin.ts
Normal file
108
packages/plaindb/src/plugins/plugin/plugin.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import { Document, Documents } from '../../documents/documents.js';
|
||||
import { DatabaseMigration, Databases } from '../../databases/databases.js';
|
||||
import { EventEmitter } from '../../utils/eventemitter.js';
|
||||
import { Plugins } from '../plugins.js';
|
||||
import { z, ZodSchema } from 'zod';
|
||||
import { PluginActionApi } from './plugin.api.js';
|
||||
|
||||
type PluginOptions<TLocalConfig extends ZodSchema = ZodSchema, TSharedConfig extends ZodSchema = ZodSchema> = {
|
||||
plugins: Plugins;
|
||||
documents: Documents;
|
||||
databases: Databases;
|
||||
configs: {
|
||||
local?: TLocalConfig;
|
||||
shared?: TSharedConfig;
|
||||
};
|
||||
};
|
||||
|
||||
type PluginEvents = {
|
||||
configChange: (config: unknown) => void;
|
||||
};
|
||||
|
||||
abstract class Plugin<
|
||||
TLocalConfig extends ZodSchema = ZodSchema,
|
||||
TSharedConfig extends ZodSchema = ZodSchema,
|
||||
TActions extends PluginActionApi = PluginActionApi,
|
||||
> extends EventEmitter<PluginEvents> {
|
||||
#options: PluginOptions<TLocalConfig, TSharedConfig>;
|
||||
|
||||
constructor(options: PluginOptions<TLocalConfig, TSharedConfig>) {
|
||||
super();
|
||||
this.#options = options;
|
||||
}
|
||||
|
||||
public get documents(): Documents {
|
||||
return this.#options.documents;
|
||||
}
|
||||
|
||||
public readonly configSchemas?: {
|
||||
local?: TLocalConfig;
|
||||
shared?: TSharedConfig;
|
||||
};
|
||||
|
||||
public getDB = async (name: string, migrations: DatabaseMigration[]) => {
|
||||
const { databases } = this.#options;
|
||||
const scopedName = `plugins:${this.name}:${name}`;
|
||||
return databases.get({ name: scopedName, migrations });
|
||||
};
|
||||
|
||||
public get configs(): {
|
||||
local?: z.infer<TLocalConfig>;
|
||||
shared?: z.infer<TSharedConfig>;
|
||||
} {
|
||||
return this.#options.configs;
|
||||
}
|
||||
|
||||
public setConfigs = async (configs: { local?: z.infer<TLocalConfig>; shared?: z.infer<TSharedConfig> }) => {
|
||||
this.#options.configs = configs;
|
||||
await this.emit('configChange', configs);
|
||||
};
|
||||
|
||||
public abstract readonly name: string;
|
||||
public actions?: TActions;
|
||||
public onLoad?: () => Promise<void>;
|
||||
public onUnload?: () => Promise<void>;
|
||||
public onLoaded?: () => Promise<void>;
|
||||
public process?: (document: Document) => Promise<void>;
|
||||
|
||||
/*public getPlugin = async <T extends Plugin>(plugin: new (...args: any) => T): Promise<
|
||||
T['api'] extends (...args: any[]) => infer R ? R : never
|
||||
> => {
|
||||
const { plugins } = this.#options;
|
||||
const instance = await plugins.get(plugin);
|
||||
return instance.api?.() as any;
|
||||
}*/
|
||||
|
||||
public action = async <TPlugin extends Plugin<any, any, any>, TAction extends keyof TPlugin['actions']>(
|
||||
plugin: new (...args: any[]) => TPlugin,
|
||||
action: TAction,
|
||||
input: Exclude<Exclude<TPlugin['actions'], undefined>[TAction]['input'], undefined> extends ZodSchema
|
||||
? z.infer<Exclude<Exclude<TPlugin['actions'], undefined>[TAction]['input'], undefined>>
|
||||
: undefined,
|
||||
): Promise<
|
||||
Exclude<Exclude<TPlugin['actions'], undefined>[TAction]['output'], undefined> extends ZodSchema
|
||||
? z.infer<Exclude<Exclude<TPlugin['actions'], undefined>[TAction]['output'], undefined>>
|
||||
: undefined
|
||||
> => {
|
||||
const { plugins } = this.#options;
|
||||
const instance = await plugins.get(plugin);
|
||||
const { actions } = instance;
|
||||
if (!actions) {
|
||||
throw new Error(`Plugin ${plugin.name} does not have actions`);
|
||||
}
|
||||
const actionDef = actions[action];
|
||||
if (!actionDef) {
|
||||
throw new Error(`Plugin ${plugin.name} does not have action ${String(action)}`);
|
||||
}
|
||||
actionDef.input?.parse(input);
|
||||
return (await actionDef.handle?.(input)) as any;
|
||||
};
|
||||
}
|
||||
|
||||
type PluginConstructor<
|
||||
TLocalConfig extends ZodSchema = ZodSchema,
|
||||
TSharedConfig extends ZodSchema = ZodSchema,
|
||||
T extends Plugin = Plugin,
|
||||
> = new (options: PluginOptions<TLocalConfig, TSharedConfig>) => T;
|
||||
|
||||
export { Plugin, type PluginOptions, type PluginConstructor };
|
||||
108
packages/plaindb/src/plugins/plugins.ts
Normal file
108
packages/plaindb/src/plugins/plugins.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import { Document } from '../documents/documents.document.js';
|
||||
import { Documents } from '../documents/documents.js';
|
||||
import { Databases } from '../databases/databases.js';
|
||||
import { Plugin, PluginConstructor } from './plugin/plugin.js';
|
||||
import { z, ZodSchema } from 'zod';
|
||||
|
||||
type PluginsOptions = {
|
||||
documents: Documents;
|
||||
databases: Databases;
|
||||
};
|
||||
|
||||
class Plugins {
|
||||
#options: PluginsOptions;
|
||||
#plugins: Map<PluginConstructor, Plugin>;
|
||||
|
||||
constructor(options: PluginsOptions) {
|
||||
this.#options = options;
|
||||
this.#plugins = new Map();
|
||||
options.documents.on('save', this.#onSave);
|
||||
}
|
||||
|
||||
#onSave = async (document: Document) => {
|
||||
for (const plugin of this.#plugins.values()) {
|
||||
await plugin.process?.(document);
|
||||
}
|
||||
};
|
||||
|
||||
#load = async (plugins: Plugin[]) => {
|
||||
await Promise.all(plugins.map((plugin) => plugin.onLoad?.()));
|
||||
plugins.forEach((plugin) => plugin.onLoaded?.());
|
||||
};
|
||||
|
||||
#saveConfig = async (plugin: Plugin) => {
|
||||
const document = await this.#options.documents.get(`.db/plugins/${plugin.name}/config.json`);
|
||||
document.data = Buffer.from(JSON.stringify(plugin.configs));
|
||||
await document.save();
|
||||
};
|
||||
|
||||
public get = async <T extends Plugin>(plugin: PluginConstructor<any, any, T>): Promise<T> => {
|
||||
if (!this.#plugins.has(plugin)) {
|
||||
await this.add([plugin]);
|
||||
}
|
||||
return this.#plugins.get(plugin) as T;
|
||||
};
|
||||
|
||||
public add = async (plugins: PluginConstructor[]) => {
|
||||
const { documents, databases } = this.#options;
|
||||
const configs = await Promise.all(
|
||||
plugins.map(async (plugin) => {
|
||||
const document = await documents.get(`.db/plugins/${plugin.name}/config.json`);
|
||||
return JSON.parse(document.data.toString() || '{}');
|
||||
}),
|
||||
);
|
||||
const instances = plugins.map(
|
||||
(Plugin, i) =>
|
||||
new Plugin({
|
||||
plugins: this,
|
||||
documents,
|
||||
databases,
|
||||
configs: configs[i],
|
||||
}),
|
||||
);
|
||||
await this.#load(instances);
|
||||
for (let i = 0; i < plugins.length; i++) {
|
||||
const instance = instances[i];
|
||||
const plugin = plugins[i];
|
||||
instance.on('configChange', this.#saveConfig.bind(null, instance));
|
||||
this.#plugins.set(plugin, instance);
|
||||
}
|
||||
};
|
||||
|
||||
public process = async (document: Document) => {
|
||||
for (const plugin of this.#plugins.values()) {
|
||||
await plugin.process?.(document);
|
||||
}
|
||||
};
|
||||
|
||||
public unload = async () => {
|
||||
await Promise.all(this.#plugins.values().map((plugin) => plugin.onUnload?.()));
|
||||
this.#plugins = new Map();
|
||||
};
|
||||
|
||||
public action = async <TPlugin extends Plugin<any, any, any>, TAction extends keyof TPlugin['actions']>(
|
||||
plugin: new (...args: any[]) => TPlugin,
|
||||
action: TAction,
|
||||
input: Exclude<Exclude<TPlugin['actions'], undefined>[TAction]['input'], undefined> extends ZodSchema
|
||||
? z.infer<Exclude<Exclude<TPlugin['actions'], undefined>[TAction]['input'], undefined>>
|
||||
: undefined,
|
||||
): Promise<
|
||||
Exclude<Exclude<TPlugin['actions'], undefined>[TAction]['output'], undefined> extends ZodSchema
|
||||
? z.infer<Exclude<Exclude<TPlugin['actions'], undefined>[TAction]['output'], undefined>>
|
||||
: undefined
|
||||
> => {
|
||||
const instance = await this.get(plugin);
|
||||
const { actions } = instance;
|
||||
if (!actions) {
|
||||
throw new Error(`Plugin ${plugin.name} does not have actions`);
|
||||
}
|
||||
const actionDef = actions[action];
|
||||
if (!actionDef) {
|
||||
throw new Error(`Plugin ${plugin.name} does not have action ${String(action)}`);
|
||||
}
|
||||
actionDef.input?.parse(input);
|
||||
return actionDef.handle?.(input) as any;
|
||||
};
|
||||
}
|
||||
|
||||
export { Plugins };
|
||||
28
packages/plaindb/src/utils/eventemitter.ts
Normal file
28
packages/plaindb/src/utils/eventemitter.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
class EventEmitter<TEvents extends Record<string, (...args: any[]) => any>> {
|
||||
#listeners: Record<keyof TEvents, TEvents[keyof TEvents][]> = {} as any;
|
||||
|
||||
public on = <K extends keyof TEvents>(event: K, listener: TEvents[K]) => {
|
||||
if (!this.#listeners[event]) {
|
||||
this.#listeners[event] = [];
|
||||
}
|
||||
this.#listeners[event].push(listener);
|
||||
return () => this.off(event, listener);
|
||||
};
|
||||
|
||||
public off = <K extends keyof TEvents>(event: K, listener: TEvents[K]) => {
|
||||
if (!this.#listeners[event]) {
|
||||
return;
|
||||
}
|
||||
this.#listeners[event] = this.#listeners[event].filter((l) => l !== listener);
|
||||
};
|
||||
|
||||
public emit = async <K extends keyof TEvents>(event: K, ...args: Parameters<TEvents[K]>) => {
|
||||
if (!this.#listeners[event]) {
|
||||
return;
|
||||
}
|
||||
const actions = this.#listeners[event].map((listener) => listener(...args));
|
||||
await Promise.all(actions);
|
||||
};
|
||||
}
|
||||
|
||||
export { EventEmitter };
|
||||
9
packages/plaindb/tsconfig.json
Normal file
9
packages/plaindb/tsconfig.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
]
|
||||
}
|
||||
2
packages/plugin-core/.gitignore
vendored
Normal file
2
packages/plugin-core/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/node_modules/
|
||||
/dist/
|
||||
19
packages/plugin-core/package.json
Normal file
19
packages/plugin-core/package.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "@plaindb/plugin-core",
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"main": "dist/exports.js",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "tsc --build"
|
||||
},
|
||||
"dependencies": {
|
||||
"@plaindb/plaindb": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.10.1",
|
||||
"typescript": "^5.7.2"
|
||||
}
|
||||
}
|
||||
89
packages/plugin-core/src/core.ts
Normal file
89
packages/plugin-core/src/core.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import { createActionApiRoute, type Database, Plugin, z } from '@plaindb/plaindb';
|
||||
import { migrations } from './migrations/migrations.js';
|
||||
|
||||
class CorePlugin extends Plugin {
|
||||
#db?: Promise<Database>;
|
||||
|
||||
public readonly name = '@builtin/core';
|
||||
|
||||
public actions = {
|
||||
setTags: createActionApiRoute({
|
||||
input: z.object({
|
||||
document: z.string(),
|
||||
tags: z.array(z.string()),
|
||||
}),
|
||||
output: z.undefined(),
|
||||
handle: async ({ document, tags }) => {
|
||||
await this.setTags(document, tags);
|
||||
return undefined;
|
||||
},
|
||||
}),
|
||||
getTags: createActionApiRoute({
|
||||
output: z.array(
|
||||
z.object({
|
||||
name: z.string(),
|
||||
count: z.number(),
|
||||
}),
|
||||
),
|
||||
handle: async () => {
|
||||
return this.getTags();
|
||||
},
|
||||
}),
|
||||
setTitle: createActionApiRoute({
|
||||
input: z.object({
|
||||
document: z.string(),
|
||||
title: z.string(),
|
||||
}),
|
||||
output: z.undefined(),
|
||||
handle: async ({ document, title }) => {
|
||||
await this.setTitle(document, title);
|
||||
return undefined;
|
||||
},
|
||||
}),
|
||||
getTitles: createActionApiRoute({
|
||||
output: z.array(
|
||||
z.object({
|
||||
document: z.string(),
|
||||
title: z.string(),
|
||||
}),
|
||||
),
|
||||
handle: async () => {
|
||||
return this.getTitles();
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
#getDatabase = async () => {
|
||||
if (!this.#db) {
|
||||
this.#db = this.getDB('data', migrations);
|
||||
}
|
||||
return this.#db;
|
||||
};
|
||||
|
||||
public getTags = async () => {
|
||||
const db = await this.#getDatabase();
|
||||
return db('tags')
|
||||
.select([db.raw('tag as name'), db.raw('count(document) as count')])
|
||||
.groupBy('tag');
|
||||
};
|
||||
|
||||
public setTags = async (document: string, tags: string[]) => {
|
||||
const db = await this.#getDatabase();
|
||||
await db('tags').where({ document }).delete();
|
||||
if (tags.length) {
|
||||
await db('tags').insert(tags.map((tag) => ({ tag, document })));
|
||||
}
|
||||
};
|
||||
|
||||
public setTitle = async (document: string, title: string) => {
|
||||
const db = await this.#getDatabase();
|
||||
await db('titles').where({ document }).insert({ document, title }).onConflict('document').merge();
|
||||
};
|
||||
|
||||
public getTitles = async () => {
|
||||
const db = await this.#getDatabase();
|
||||
return await db('titles').select('*');
|
||||
};
|
||||
}
|
||||
|
||||
export { CorePlugin };
|
||||
1
packages/plugin-core/src/exports.ts
Normal file
1
packages/plugin-core/src/exports.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { CorePlugin } from './core.js';
|
||||
27
packages/plugin-core/src/migrations/migrations.ts
Normal file
27
packages/plugin-core/src/migrations/migrations.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { DatabaseMigration } from '@plaindb/plaindb';
|
||||
|
||||
const migrations: DatabaseMigration[] = [
|
||||
{
|
||||
name: 'init',
|
||||
up: async (db) => {
|
||||
await db.schema.createTable('tags', (table) => {
|
||||
table.string('tag').notNullable();
|
||||
table.string('document').notNullable();
|
||||
table.primary(['tag', 'document']);
|
||||
table.index('document');
|
||||
table.index('tag');
|
||||
});
|
||||
|
||||
await db.schema.createTable('titles', (table) => {
|
||||
table.string('document').primary().notNullable();
|
||||
table.string('title').notNullable();
|
||||
});
|
||||
},
|
||||
down: async (db) => {
|
||||
await db.schema.dropTable('tags');
|
||||
await db.schema.dropTable('titles');
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
export { migrations };
|
||||
9
packages/plugin-core/tsconfig.json
Normal file
9
packages/plugin-core/tsconfig.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
]
|
||||
}
|
||||
2
packages/plugin-markdown/.gitignore
vendored
Normal file
2
packages/plugin-markdown/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/node_modules/
|
||||
/dist/
|
||||
34
packages/plugin-markdown/package.json
Normal file
34
packages/plugin-markdown/package.json
Normal file
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "@plaindb/plugin-markdown",
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"main": "dist/exports.js",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "tsc --build"
|
||||
},
|
||||
"dependencies": {
|
||||
"@plaindb/plaindb": "workspace:*",
|
||||
"@plaindb/plugin-core": "workspace:*",
|
||||
"lodash": "^4.17.21",
|
||||
"mdast-util-to-string": "^4.0.0",
|
||||
"remark-behead": "^3.1.0",
|
||||
"remark-directive": "^3.0.0",
|
||||
"remark-frontmatter": "^5.0.0",
|
||||
"remark-gfm": "^4.0.0",
|
||||
"remark-math": "^6.0.0",
|
||||
"remark-parse": "^11.0.0",
|
||||
"remark-stringify": "^11.0.0",
|
||||
"unified": "^11.0.5",
|
||||
"unist-util-remove-position": "^5.0.0",
|
||||
"unist-util-visit": "^5.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/lodash": "^4.17.13",
|
||||
"@types/mdast": "^4.0.4",
|
||||
"@types/node": "^22.10.1",
|
||||
"typescript": "^5.7.2"
|
||||
}
|
||||
}
|
||||
1
packages/plugin-markdown/src/exports.ts
Normal file
1
packages/plugin-markdown/src/exports.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { MarkdownPlugin } from './markdown.js';
|
||||
71
packages/plugin-markdown/src/markdown.ts
Normal file
71
packages/plugin-markdown/src/markdown.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { createActionApiRoute, Document, Plugin, z } from '@plaindb/plaindb';
|
||||
import { MarkdownAst } from './utils/markdown-ast.js';
|
||||
import { CorePlugin } from '@plaindb/plugin-core';
|
||||
|
||||
type MarkdownSubPlugin = {
|
||||
process: (ast: MarkdownAst) => Promise<void>;
|
||||
};
|
||||
|
||||
class MarkdownPlugin extends Plugin {
|
||||
#subPlugins = new Set<MarkdownSubPlugin>();
|
||||
|
||||
public readonly name = '@builtin/markdown';
|
||||
|
||||
public actions = {
|
||||
register: createActionApiRoute({
|
||||
input: z.object({
|
||||
plugin: z.custom<MarkdownSubPlugin>(),
|
||||
}),
|
||||
handle: async ({ plugin }) => {
|
||||
this.#subPlugins.add(plugin);
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
public process = async (document: Document) => {
|
||||
if (!document.location.endsWith('.md')) {
|
||||
return;
|
||||
}
|
||||
const ast = new MarkdownAst(document.data);
|
||||
|
||||
for (const plugin of this.#subPlugins) {
|
||||
await plugin.process(ast);
|
||||
}
|
||||
|
||||
const tags: string[] = [];
|
||||
let topHead = Number.MAX_SAFE_INTEGER;
|
||||
let title = 'Untitles';
|
||||
|
||||
ast.visit((node) => {
|
||||
if (node.type !== 'heading') {
|
||||
return;
|
||||
}
|
||||
if (node.depth === undefined) {
|
||||
return;
|
||||
}
|
||||
if (node.depth < topHead) {
|
||||
topHead = node.depth;
|
||||
title = ast.nodeToString(node.children);
|
||||
}
|
||||
});
|
||||
|
||||
await this.action(CorePlugin, 'setTitle', {
|
||||
title,
|
||||
document: document.location,
|
||||
});
|
||||
|
||||
ast.visit((node) => {
|
||||
if (node.type === 'textDirective' && node.name === 'tag') {
|
||||
const body = ast.nodeToString(node.children);
|
||||
tags.push(body);
|
||||
}
|
||||
});
|
||||
document.replace(ast.toBuffer());
|
||||
await this.action(CorePlugin, 'setTags', {
|
||||
tags,
|
||||
document: document.location,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export { MarkdownPlugin };
|
||||
76
packages/plugin-markdown/src/utils/markdown-ast.ts
Normal file
76
packages/plugin-markdown/src/utils/markdown-ast.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { Root, Node as BaseNode } from 'mdast';
|
||||
import { type PropertyName } from 'lodash';
|
||||
import get from 'lodash/get.js';
|
||||
import set from 'lodash/set.js';
|
||||
import remarkDirective from 'remark-directive';
|
||||
import remarkFrontmatter from 'remark-frontmatter';
|
||||
import remarkGfm from 'remark-gfm';
|
||||
import remarkMath from 'remark-math';
|
||||
import remarkParse from 'remark-parse';
|
||||
import remarkStringify from 'remark-stringify';
|
||||
import { unified } from 'unified';
|
||||
import { toString } from 'mdast-util-to-string';
|
||||
|
||||
type Node = {
|
||||
children?: Node[];
|
||||
attributes?: Record<string, unknown>;
|
||||
name?: string;
|
||||
depth?: number;
|
||||
value?: string;
|
||||
} & BaseNode;
|
||||
|
||||
const compiler = unified()
|
||||
.use(remarkParse)
|
||||
.use(remarkStringify)
|
||||
.use(remarkGfm)
|
||||
.use(remarkFrontmatter, ['yaml'])
|
||||
.use(remarkDirective)
|
||||
.use(remarkMath);
|
||||
|
||||
class MarkdownAst {
|
||||
content: Root;
|
||||
|
||||
constructor(content: Buffer) {
|
||||
this.content = this.#parse(content.toString());
|
||||
}
|
||||
|
||||
public set = (path: PropertyName[], value: unknown) => {
|
||||
set(this.content, path, value);
|
||||
};
|
||||
|
||||
public get = (path: PropertyName[]) => {
|
||||
return get(this.content, path);
|
||||
};
|
||||
|
||||
#parse = (input: string | null) => {
|
||||
const ast = compiler.parse(input || '');
|
||||
return ast;
|
||||
};
|
||||
|
||||
public toBuffer = () => {
|
||||
const output = compiler.stringify(this.content);
|
||||
return Buffer.from(output);
|
||||
};
|
||||
|
||||
public visit = (visitor: (node: Node, path: PropertyName[]) => void) => {
|
||||
const visit = (node: Node, path: PropertyName[]) => {
|
||||
visitor(node, path);
|
||||
if (node.children) {
|
||||
node.children.forEach((child, index) => {
|
||||
visit(child, [...path, 'children', index]);
|
||||
});
|
||||
}
|
||||
};
|
||||
visit(this.content, []);
|
||||
};
|
||||
|
||||
public nodeToString = (node: Node[] = []) => {
|
||||
return toString(node);
|
||||
};
|
||||
|
||||
public patch = (visitor: (node: Node, path: PropertyName[]) => void) => {
|
||||
this.visit(visitor);
|
||||
};
|
||||
}
|
||||
|
||||
export { MarkdownAst, type Node };
|
||||
9
packages/plugin-markdown/tsconfig.json
Normal file
9
packages/plugin-markdown/tsconfig.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
]
|
||||
}
|
||||
2
packages/tests/.gitignore
vendored
Normal file
2
packages/tests/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/node_modules/
|
||||
/dist/
|
||||
20
packages/tests/package.json
Normal file
20
packages/tests/package.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "@plaindb/tests",
|
||||
"private": true,
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@plaindb/fs-memory": "workspace:*",
|
||||
"@plaindb/plaindb": "workspace:*",
|
||||
"@plaindb/plugin-core": "workspace:*",
|
||||
"@plaindb/plugin-markdown": "workspace:*",
|
||||
"@pnpm/find-workspace-dir": "^900.0.1",
|
||||
"sqlite3": "^5.1.7",
|
||||
"vitest": "^2.1.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@pnpm/find-workspace-packages": "^6.0.9",
|
||||
"@types/node": "^22.10.1",
|
||||
"typescript": "^5.7.2"
|
||||
}
|
||||
}
|
||||
53
packages/tests/src/documents.test.ts
Normal file
53
packages/tests/src/documents.test.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { describe, it } from 'vitest';
|
||||
import { CorePlugin } from '@plaindb/plugin-core';
|
||||
import { MarkdownPlugin } from '@plaindb/plugin-markdown';
|
||||
import { PlainDB } from '../../plaindb/dist/exports.js';
|
||||
import { MemoryFileSystem } from '@plaindb/fs-memory';
|
||||
|
||||
describe('documents', () => {
|
||||
it('should be able to create a document', async () => {
|
||||
const fs = new MemoryFileSystem({});
|
||||
|
||||
const { plugins, documents, close } = new PlainDB({
|
||||
fs,
|
||||
getDBConfig: () => ({
|
||||
client: 'sqlite',
|
||||
useNullAsDefault: false,
|
||||
connection: {
|
||||
filename: ':memory:',
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
await plugins.add([MarkdownPlugin]);
|
||||
|
||||
const tags1 = await plugins.action(CorePlugin, 'getTags', undefined);
|
||||
|
||||
console.log('Done', tags1);
|
||||
|
||||
const demoDocument = await documents.get('hello/world.md');
|
||||
|
||||
demoDocument.data = Buffer.from(`
|
||||
# Hello World
|
||||
|
||||
:tag[hello]
|
||||
`);
|
||||
|
||||
await demoDocument.save();
|
||||
|
||||
const tags2 = await plugins.action(CorePlugin, 'getTags', undefined);
|
||||
console.log('Done', tags2);
|
||||
|
||||
demoDocument.data = Buffer.from(`
|
||||
# Hello World
|
||||
|
||||
:tag[world]
|
||||
`);
|
||||
await demoDocument.save();
|
||||
|
||||
const tags3 = await plugins.action(CorePlugin, 'getTags', undefined);
|
||||
console.log('Done', tags3);
|
||||
|
||||
await close();
|
||||
});
|
||||
});
|
||||
9
packages/tests/tsconfig.json
Normal file
9
packages/tests/tsconfig.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
]
|
||||
}
|
||||
25
packages/tests/vitest.config.ts
Normal file
25
packages/tests/vitest.config.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
/// <reference types="vitest" />
|
||||
|
||||
import { resolve } from 'path';
|
||||
import { defineConfig } from 'vitest/config';
|
||||
import { findWorkspaceDir } from '@pnpm/find-workspace-dir';
|
||||
import { findWorkspacePackages } from '@pnpm/find-workspace-packages';
|
||||
|
||||
const root = await findWorkspaceDir(process.cwd());
|
||||
if (!root) {
|
||||
throw new Error('No workspace found');
|
||||
}
|
||||
const packages = await findWorkspacePackages(root);
|
||||
|
||||
const alias = Object.fromEntries(
|
||||
packages.map(({ dir, manifest }) => [
|
||||
manifest.name!,
|
||||
resolve(dir, 'src', 'exports.ts'),
|
||||
]),
|
||||
);
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
alias,
|
||||
},
|
||||
});
|
||||
2
packages/tools-cli/.gitignore
vendored
Normal file
2
packages/tools-cli/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/node_modules/
|
||||
/dist/
|
||||
2
packages/tools-cli/bin/cli.mjs
Executable file
2
packages/tools-cli/bin/cli.mjs
Executable file
@@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env node
|
||||
import '../dist/cli.js';
|
||||
27
packages/tools-cli/package.json
Normal file
27
packages/tools-cli/package.json
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "@plaindb/tools-cli",
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"main": "dist/exports.js",
|
||||
"bin": {
|
||||
"plaindb": "bin/cli.mjs"
|
||||
},
|
||||
"files": [
|
||||
"bin",
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "tsc --build"
|
||||
},
|
||||
"dependencies": {
|
||||
"@plaindb/fs-system": "workspace:*",
|
||||
"@plaindb/plaindb": "workspace:*",
|
||||
"@plaindb/plugin-core": "workspace:*",
|
||||
"@plaindb/plugin-markdown": "workspace:*",
|
||||
"sqlite3": "^5.1.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.10.1",
|
||||
"typescript": "^5.7.2"
|
||||
}
|
||||
}
|
||||
53
packages/tools-cli/src/cli.ts
Normal file
53
packages/tools-cli/src/cli.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { SystemFileSystem } from '@plaindb/fs-system';
|
||||
import { CorePlugin } from '@plaindb/plugin-core';
|
||||
import { MarkdownPlugin } from '@plaindb/plugin-markdown';
|
||||
import { resolve } from 'path';
|
||||
import { PlainDB } from '../../plaindb/dist/exports.js';
|
||||
|
||||
const fs = new SystemFileSystem({
|
||||
cwd: resolve('/Users/alice/Documents/Exo'),
|
||||
});
|
||||
|
||||
const { plugins, documents, close } = new PlainDB({
|
||||
fs,
|
||||
getDBConfig: () => ({
|
||||
client: 'sqlite',
|
||||
useNullAsDefault: false,
|
||||
connection: {
|
||||
filename: ':memory:',
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
await plugins.add([MarkdownPlugin]);
|
||||
|
||||
await fs.update();
|
||||
|
||||
const tags1 = await plugins.action(CorePlugin, 'getTags', undefined);
|
||||
|
||||
console.log('Done', tags1);
|
||||
|
||||
const demoDocument = await documents.get('hello/world.md');
|
||||
|
||||
demoDocument.data = Buffer.from(`
|
||||
# Hello World
|
||||
|
||||
:tag[hello]
|
||||
`);
|
||||
|
||||
await demoDocument.save();
|
||||
|
||||
const tags2 = await plugins.action(CorePlugin, 'getTags', undefined);
|
||||
console.log('Done', tags2);
|
||||
|
||||
demoDocument.data = Buffer.from(`
|
||||
# Hello World
|
||||
|
||||
:tag[world]
|
||||
`);
|
||||
await demoDocument.save();
|
||||
|
||||
const tags3 = await plugins.action(CorePlugin, 'getTags', undefined);
|
||||
console.log('Done', tags3);
|
||||
|
||||
await close();
|
||||
9
packages/tools-cli/tsconfig.json
Normal file
9
packages/tools-cli/tsconfig.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist"
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
]
|
||||
}
|
||||
5539
pnpm-lock.yaml
generated
Normal file
5539
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
2
pnpm-workspace.yaml
Normal file
2
pnpm-workspace.yaml
Normal file
@@ -0,0 +1,2 @@
|
||||
packages:
|
||||
- packages/*
|
||||
14
tsconfig.base.json
Normal file
14
tsconfig.base.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"sourceMap": true,
|
||||
"strict": true,
|
||||
"resolveJsonModule": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"skipLibCheck": true
|
||||
}
|
||||
}
|
||||
23
tsconfig.json
Normal file
23
tsconfig.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"include": [],
|
||||
"references": [
|
||||
{
|
||||
"path": "./packages/plaindb"
|
||||
},
|
||||
{
|
||||
"path": "./packages/fs-system"
|
||||
},
|
||||
{
|
||||
"path": "./packages/fs-memory"
|
||||
},
|
||||
{
|
||||
"path": "./packages/tools-cli"
|
||||
},
|
||||
{
|
||||
"path": "./packages/plugin-core"
|
||||
},
|
||||
{
|
||||
"path": "./packages/plugin-markdown"
|
||||
}
|
||||
]
|
||||
}
|
||||
14
vitest.config.ts
Normal file
14
vitest.config.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
/// <reference types="vitest" />
|
||||
|
||||
import { defineConfig } from 'vitest/config';
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
coverage: {
|
||||
reporter: ['text', 'json-summary', 'json', 'html'],
|
||||
reportOnFailure: true,
|
||||
include: ['packages/*/src/**/*.ts'],
|
||||
exclude: ['packages/tests/**/*'],
|
||||
},
|
||||
},
|
||||
});
|
||||
1
vitest.workspace.ts
Normal file
1
vitest.workspace.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default ['packages/*'];
|
||||
Reference in New Issue
Block a user