@aigne/afs 1.1.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +135 -111
  3. package/lib/cjs/afs.d.ts +9 -9
  4. package/lib/cjs/afs.js +31 -27
  5. package/lib/cjs/index.d.ts +0 -2
  6. package/lib/cjs/index.js +0 -2
  7. package/lib/cjs/type.d.ts +16 -5
  8. package/lib/dts/afs.d.ts +9 -9
  9. package/lib/dts/index.d.ts +0 -2
  10. package/lib/dts/type.d.ts +16 -5
  11. package/lib/esm/afs.d.ts +9 -9
  12. package/lib/esm/afs.js +31 -27
  13. package/lib/esm/index.d.ts +0 -2
  14. package/lib/esm/index.js +0 -2
  15. package/lib/esm/type.d.ts +16 -5
  16. package/package.json +3 -5
  17. package/lib/cjs/history/index.d.ts +0 -20
  18. package/lib/cjs/history/index.js +0 -47
  19. package/lib/cjs/storage/index.d.ts +0 -24
  20. package/lib/cjs/storage/index.js +0 -72
  21. package/lib/cjs/storage/migrate.d.ts +0 -3
  22. package/lib/cjs/storage/migrate.js +0 -31
  23. package/lib/cjs/storage/migrations/001-init.d.ts +0 -2
  24. package/lib/cjs/storage/migrations/001-init.js +0 -25
  25. package/lib/cjs/storage/models/entries.d.ts +0 -199
  26. package/lib/cjs/storage/models/entries.js +0 -28
  27. package/lib/cjs/storage/type.d.ts +0 -23
  28. package/lib/cjs/storage/type.js +0 -2
  29. package/lib/dts/history/index.d.ts +0 -20
  30. package/lib/dts/storage/index.d.ts +0 -24
  31. package/lib/dts/storage/migrate.d.ts +0 -3
  32. package/lib/dts/storage/migrations/001-init.d.ts +0 -2
  33. package/lib/dts/storage/models/entries.d.ts +0 -199
  34. package/lib/dts/storage/type.d.ts +0 -23
  35. package/lib/esm/history/index.d.ts +0 -20
  36. package/lib/esm/history/index.js +0 -43
  37. package/lib/esm/storage/index.d.ts +0 -24
  38. package/lib/esm/storage/index.js +0 -67
  39. package/lib/esm/storage/migrate.d.ts +0 -3
  40. package/lib/esm/storage/migrate.js +0 -28
  41. package/lib/esm/storage/migrations/001-init.d.ts +0 -2
  42. package/lib/esm/storage/migrations/001-init.js +0 -22
  43. package/lib/esm/storage/models/entries.d.ts +0 -199
  44. package/lib/esm/storage/models/entries.js +0 -23
  45. package/lib/esm/storage/type.d.ts +0 -23
  46. package/lib/esm/storage/type.js +0 -1
@@ -1,67 +0,0 @@
1
- import { and, asc, desc, eq, initDatabase, sql } from "@aigne/sqlite";
2
- import { migrate } from "./migrate.js";
3
- import { entriesTable } from "./models/entries.js";
4
- const DEFAULT_AFS_STORAGE_LIST_LIMIT = 10;
5
- export class SharedAFSStorage {
6
- options;
7
- constructor(options) {
8
- this.options = options;
9
- }
10
- _db;
11
- get db() {
12
- this._db ??= initDatabase({ url: this.options?.url });
13
- return this._db;
14
- }
15
- withModule(module) {
16
- return new AFSStorageWithModule(this.db, migrate(this.db, module).then(() => entriesTable(module)));
17
- }
18
- }
19
- export class AFSStorageWithModule {
20
- db;
21
- table;
22
- constructor(db, table) {
23
- this.db = db;
24
- this.table = table;
25
- }
26
- async list(options = {}) {
27
- const { filter, limit = DEFAULT_AFS_STORAGE_LIST_LIMIT } = options;
28
- const db = await this.db;
29
- const table = await this.table;
30
- const list = await db
31
- .select()
32
- .from(table)
33
- .where(and(filter?.userId ? eq(table.userId, filter.userId) : undefined, filter?.sessionId ? eq(table.sessionId, filter.sessionId) : undefined))
34
- .orderBy(...(options.orderBy ?? []).map((item) => (item[1] === "asc" ? asc : desc)(sql.identifier(item[0]))))
35
- .limit(limit)
36
- .execute();
37
- return { list };
38
- }
39
- async read(path) {
40
- const db = await this.db;
41
- const table = await this.table;
42
- return db
43
- .select()
44
- .from(table)
45
- .where(eq(table.path, path))
46
- .limit(1)
47
- .execute()
48
- .then((memory) => memory.at(0));
49
- }
50
- async create(entry) {
51
- const db = await this.db;
52
- const table = await this.table;
53
- let result = await db
54
- .update(table)
55
- .set(entry)
56
- .where(eq(table.path, entry.path))
57
- .returning()
58
- .execute();
59
- if (!result.length) {
60
- result = await db.insert(table).values(entry).returning().execute();
61
- }
62
- const [res] = result;
63
- if (!res)
64
- throw new Error("Failed to create AFS entry, no result");
65
- return res;
66
- }
67
- }
@@ -1,3 +0,0 @@
1
- import { type initDatabase } from "@aigne/sqlite";
2
- import type { AFSModule } from "../type.js";
3
- export declare function migrate(db: ReturnType<typeof initDatabase>, module: AFSModule): Promise<void>;
@@ -1,28 +0,0 @@
1
- import { sql } from "@aigne/sqlite";
2
- import { v7 } from "@aigne/uuid";
3
- import { init } from "./migrations/001-init.js";
4
- export async function migrate(db, module) {
5
- const migrations = [init];
6
- const migrationsTable = "__drizzle_migrations";
7
- const migrationTableCreate = sql `
8
- CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
9
- "id" SERIAL PRIMARY KEY,
10
- "moduleId" TEXT NOT NULL,
11
- "hash" TEXT NOT NULL
12
- )
13
- `;
14
- await (await db).run(migrationTableCreate).execute();
15
- const dbMigrations = await (await db)
16
- .values(sql `SELECT "id", "moduleId", "hash" FROM ${sql.identifier(migrationsTable)} WHERE "moduleId" = ${sql.param(module.moduleId)} ORDER BY id DESC LIMIT 1`)
17
- .execute();
18
- const lastDbMigration = dbMigrations[0];
19
- const queriesToRun = [];
20
- for (const migration of migrations) {
21
- if (!lastDbMigration || lastDbMigration[1] < migration.hash) {
22
- queriesToRun.push(...migration.sql(module), sql `INSERT INTO ${sql.identifier(migrationsTable)} ("id", "moduleId", "hash") VALUES(${sql.param(v7())}, ${sql.param(module.moduleId)}, ${sql.param(migration.hash)})`);
23
- }
24
- }
25
- for (const query of queriesToRun) {
26
- await (await db).run(query).execute();
27
- }
28
- }
@@ -1,2 +0,0 @@
1
- import type { AFSStorageMigrations } from "../type.js";
2
- export declare const init: AFSStorageMigrations;
@@ -1,22 +0,0 @@
1
- import { sql } from "@aigne/sqlite";
2
- import { entriesTableName } from "../models/entries.js";
3
- export const init = {
4
- hash: "001-init",
5
- sql: (module) => [
6
- sql `\
7
- CREATE TABLE ${sql.identifier(entriesTableName(module))} (
8
- "id" TEXT NOT NULL PRIMARY KEY,
9
- "createdAt" DATETIME NOT NULL,
10
- "updatedAt" DATETIME NOT NULL,
11
- "path" TEXT NOT NULL,
12
- "userId" TEXT,
13
- "sessionId" TEXT,
14
- "summary" TEXT,
15
- "metadata" JSON,
16
- "linkTo" TEXT,
17
- "content" JSON,
18
- UNIQUE (path)
19
- )
20
- `,
21
- ],
22
- };
@@ -1,199 +0,0 @@
1
- import type { AFSModule } from "../../type.js";
2
- export declare const entriesTableName: (module: AFSModule) => string;
3
- export declare const entriesTable: (module: AFSModule) => import("@aigne/sqlite").SQLiteTableWithColumns<{
4
- name: string;
5
- schema: undefined;
6
- columns: {
7
- id: import("@aigne/sqlite").SQLiteColumn<{
8
- name: "id";
9
- tableName: string;
10
- dataType: "string";
11
- columnType: "SQLiteText";
12
- data: string;
13
- driverParam: string;
14
- notNull: true;
15
- hasDefault: true;
16
- isPrimaryKey: true;
17
- isAutoincrement: false;
18
- hasRuntimeDefault: true;
19
- enumValues: [string, ...string[]];
20
- baseColumn: never;
21
- identity: undefined;
22
- generated: undefined;
23
- }, {}, {
24
- length: number | undefined;
25
- }>;
26
- createdAt: import("@aigne/sqlite").SQLiteColumn<{
27
- name: "createdAt";
28
- tableName: string;
29
- dataType: "custom";
30
- columnType: "SQLiteCustomColumn";
31
- data: Date;
32
- driverParam: string;
33
- notNull: true;
34
- hasDefault: true;
35
- isPrimaryKey: false;
36
- isAutoincrement: false;
37
- hasRuntimeDefault: true;
38
- enumValues: undefined;
39
- baseColumn: never;
40
- identity: undefined;
41
- generated: undefined;
42
- }, {}, {
43
- sqliteColumnBuilderBrand: "SQLiteCustomColumnBuilderBrand";
44
- }>;
45
- updatedAt: import("@aigne/sqlite").SQLiteColumn<{
46
- name: "updatedAt";
47
- tableName: string;
48
- dataType: "custom";
49
- columnType: "SQLiteCustomColumn";
50
- data: Date;
51
- driverParam: string;
52
- notNull: true;
53
- hasDefault: true;
54
- isPrimaryKey: false;
55
- isAutoincrement: false;
56
- hasRuntimeDefault: true;
57
- enumValues: undefined;
58
- baseColumn: never;
59
- identity: undefined;
60
- generated: undefined;
61
- }, {}, {
62
- sqliteColumnBuilderBrand: "SQLiteCustomColumnBuilderBrand";
63
- }>;
64
- path: import("@aigne/sqlite").SQLiteColumn<{
65
- name: "path";
66
- tableName: string;
67
- dataType: "string";
68
- columnType: "SQLiteText";
69
- data: string;
70
- driverParam: string;
71
- notNull: true;
72
- hasDefault: false;
73
- isPrimaryKey: false;
74
- isAutoincrement: false;
75
- hasRuntimeDefault: false;
76
- enumValues: [string, ...string[]];
77
- baseColumn: never;
78
- identity: undefined;
79
- generated: undefined;
80
- }, {}, {
81
- length: number | undefined;
82
- }>;
83
- userId: import("@aigne/sqlite").SQLiteColumn<{
84
- name: "userId";
85
- tableName: string;
86
- dataType: "string";
87
- columnType: "SQLiteText";
88
- data: string;
89
- driverParam: string;
90
- notNull: false;
91
- hasDefault: false;
92
- isPrimaryKey: false;
93
- isAutoincrement: false;
94
- hasRuntimeDefault: false;
95
- enumValues: [string, ...string[]];
96
- baseColumn: never;
97
- identity: undefined;
98
- generated: undefined;
99
- }, {}, {
100
- length: number | undefined;
101
- }>;
102
- sessionId: import("@aigne/sqlite").SQLiteColumn<{
103
- name: "sessionId";
104
- tableName: string;
105
- dataType: "string";
106
- columnType: "SQLiteText";
107
- data: string;
108
- driverParam: string;
109
- notNull: false;
110
- hasDefault: false;
111
- isPrimaryKey: false;
112
- isAutoincrement: false;
113
- hasRuntimeDefault: false;
114
- enumValues: [string, ...string[]];
115
- baseColumn: never;
116
- identity: undefined;
117
- generated: undefined;
118
- }, {}, {
119
- length: number | undefined;
120
- }>;
121
- summary: import("@aigne/sqlite").SQLiteColumn<{
122
- name: "summary";
123
- tableName: string;
124
- dataType: "string";
125
- columnType: "SQLiteText";
126
- data: string;
127
- driverParam: string;
128
- notNull: false;
129
- hasDefault: false;
130
- isPrimaryKey: false;
131
- isAutoincrement: false;
132
- hasRuntimeDefault: false;
133
- enumValues: [string, ...string[]];
134
- baseColumn: never;
135
- identity: undefined;
136
- generated: undefined;
137
- }, {}, {
138
- length: number | undefined;
139
- }>;
140
- metadata: import("@aigne/sqlite").SQLiteColumn<{
141
- name: string;
142
- tableName: string;
143
- dataType: "custom";
144
- columnType: "SQLiteCustomColumn";
145
- data: Record<string, unknown>;
146
- driverParam: string;
147
- notNull: false;
148
- hasDefault: false;
149
- isPrimaryKey: false;
150
- isAutoincrement: false;
151
- hasRuntimeDefault: false;
152
- enumValues: undefined;
153
- baseColumn: never;
154
- identity: undefined;
155
- generated: undefined;
156
- }, {}, {
157
- sqliteColumnBuilderBrand: "SQLiteCustomColumnBuilderBrand";
158
- }>;
159
- linkTo: import("@aigne/sqlite").SQLiteColumn<{
160
- name: "linkTo";
161
- tableName: string;
162
- dataType: "string";
163
- columnType: "SQLiteText";
164
- data: string;
165
- driverParam: string;
166
- notNull: false;
167
- hasDefault: false;
168
- isPrimaryKey: false;
169
- isAutoincrement: false;
170
- hasRuntimeDefault: false;
171
- enumValues: [string, ...string[]];
172
- baseColumn: never;
173
- identity: undefined;
174
- generated: undefined;
175
- }, {}, {
176
- length: number | undefined;
177
- }>;
178
- content: import("@aigne/sqlite").SQLiteColumn<{
179
- name: string;
180
- tableName: string;
181
- dataType: "custom";
182
- columnType: "SQLiteCustomColumn";
183
- data: any;
184
- driverParam: string;
185
- notNull: false;
186
- hasDefault: false;
187
- isPrimaryKey: false;
188
- isAutoincrement: false;
189
- hasRuntimeDefault: false;
190
- enumValues: undefined;
191
- baseColumn: never;
192
- identity: undefined;
193
- generated: undefined;
194
- }, {}, {
195
- sqliteColumnBuilderBrand: "SQLiteCustomColumnBuilderBrand";
196
- }>;
197
- };
198
- dialect: "sqlite";
199
- }>;
@@ -1,23 +0,0 @@
1
- import { datetime, json, sqliteTable, text } from "@aigne/sqlite";
2
- import { v7 } from "@aigne/uuid";
3
- export const entriesTableName = (module) => `Entries_${module.moduleId}`;
4
- export const entriesTable = (module) => sqliteTable(entriesTableName(module), {
5
- id: text("id")
6
- .notNull()
7
- .primaryKey()
8
- .$defaultFn(() => v7()),
9
- createdAt: datetime("createdAt")
10
- .notNull()
11
- .$defaultFn(() => new Date()),
12
- updatedAt: datetime("updatedAt")
13
- .notNull()
14
- .$defaultFn(() => new Date())
15
- .$onUpdateFn(() => new Date()),
16
- path: text("path").notNull(),
17
- userId: text("userId"),
18
- sessionId: text("sessionId"),
19
- summary: text("summary"),
20
- metadata: json("metadata"),
21
- linkTo: text("linkTo"),
22
- content: json("content"),
23
- });
@@ -1,23 +0,0 @@
1
- import type { SQL } from "@aigne/sqlite";
2
- import type { AFSEntry, AFSModule } from "../type.js";
3
- export interface AFSStorageCreatePayload extends Omit<AFSEntry, "id"> {
4
- }
5
- export interface AFSStorageListOptions {
6
- filter?: {
7
- userId?: string;
8
- sessionId?: string;
9
- };
10
- limit?: number;
11
- orderBy?: [string, "asc" | "desc"][];
12
- }
13
- export interface AFSStorage {
14
- create(entry: AFSStorageCreatePayload): Promise<AFSEntry>;
15
- list(options?: AFSStorageListOptions): Promise<{
16
- list: AFSEntry[];
17
- }>;
18
- read(path: string): Promise<AFSEntry | undefined>;
19
- }
20
- export type AFSStorageMigrations = {
21
- hash: string;
22
- sql: (module: AFSModule) => SQL[];
23
- };
@@ -1 +0,0 @@
1
- export {};