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
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"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user