This commit is contained in:
Morten Olsen
2024-12-10 20:59:29 +01:00
commit ede2d56b7c
54 changed files with 6955 additions and 0 deletions

2
packages/plugin-core/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/node_modules/
/dist/

View 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"
}
}

View 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 };

View File

@@ -0,0 +1 @@
export { CorePlugin } from './core.js';

View 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 };

View File

@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "dist"
},
"include": [
"src"
]
}