Skip to content

Commit

Permalink
Added default export object for adapters to enhance modularity.
Browse files Browse the repository at this point in the history
- Refactored data adapter retrieval in Collection class.
- Removed deprecated methods and classes, including MarkdownSourceAdapter and related tests, to streamline the codebase.
- Updated tests to reflect changes in adapter structure and functionality, ensuring reliability across the codebase.
  • Loading branch information
Brian Joseph Petro committed Dec 19, 2024
1 parent 5f50fde commit b03d200
Show file tree
Hide file tree
Showing 20 changed files with 89 additions and 945 deletions.
7 changes: 6 additions & 1 deletion smart-blocks/adapters/_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,9 @@ export class BlockContentAdapter {
async create_hash(content) {
return await create_hash(content);
}
}
}

export default {
collection: null, // No collection adapter for this base file
item: BlockContentAdapter
};
5 changes: 5 additions & 0 deletions smart-blocks/adapters/markdown_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,8 @@ export class MarkdownBlockContentAdapter extends BlockContentAdapter {
await this.item.source.import();
}
}

export default {
collection: null, // No collection adapter needed for markdown blocks
item: MarkdownBlockContentAdapter
};
5 changes: 5 additions & 0 deletions smart-collections/adapters/_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,8 @@ export class ItemDataAdapter {
return this.item.collection.data_adapter;
}
}

export default {
collection: CollectionDataAdapter,
item: ItemDataAdapter
};
5 changes: 5 additions & 0 deletions smart-collections/adapters/_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ export class FileItemDataAdapter extends ItemDataAdapter {
return this.item.collection.data_fs || this.item.collection.env.data_fs;
}
}

export default {
collection: FileCollectionDataAdapter,
item: FileItemDataAdapter
};
7 changes: 6 additions & 1 deletion smart-collections/adapters/ajson_multi_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,9 @@ export class AjsonMultiFileItemDataAdapter extends FileItemDataAdapter {
}
return ajson;
}
}
}

export default {
collection: AjsonMultiFileCollectionDataAdapter,
item: AjsonMultiFileItemDataAdapter
};
19 changes: 12 additions & 7 deletions smart-collections/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,21 @@ export class Collection {
*/
get data_adapter() {
if (!this._data_adapter) {
const config = this.env.opts.collections?.[this.collection_key];
const data_adapter_class = config?.data_adapter
?? this.env.opts.collections?.smart_collections?.data_adapter;
if (!data_adapter_class) {
throw new Error(`No data adapter class found for ${this.collection_key} or smart_collections`);
}
this._data_adapter = new data_adapter_class(this);
const AdapterClass = this.get_adapter_class('data');
this._data_adapter = new AdapterClass(this);
}
return this._data_adapter;
}
get_adapter_class(type) {
const config = this.env.opts.collections?.[this.collection_key];
const adapter_key = type + '_adapter';
const adapter_module = config?.[adapter_key]
?? this.env.opts.collections?.smart_collections?.[adapter_key]
;
if(typeof adapter_module === 'function') return adapter_module; // backward compatibility
if(typeof adapter_module?.collection === 'function') return adapter_module.collection;
throw new Error(`No adapter class found for ${this.collection_key} or smart_collections`);
}

/**
* Data directory strategy for this collection. Defaults to 'multi'.
Expand Down
12 changes: 1 addition & 11 deletions smart-collections/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,6 @@ export class CollectionItem {
return create_uid(this.data);
}

/**
* Ensures the item is fully loaded if it was queued for loading.
* @returns {Promise<void>}
*/
async ensure_loaded() {
if (this._queue_load) {
await this.load();
}
}

/**
* Updates the item data and returns true if changed.
* @param {Object} data
Expand Down Expand Up @@ -159,7 +149,7 @@ export class CollectionItem {
*/
async load() {
try {
await this.data_adapter.load(this);
await this.data_adapter.load_item(this);
this.init();
} catch (err) {
this._load_error = err;
Expand Down
7 changes: 5 additions & 2 deletions smart-collections/tests/collection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TestItem extends CollectionItem {
}

// A mock data adapter to ensure data_adapter is always available.
class MockDataAdapter {
class MockCollectionDataAdapter {
constructor(collection) {
this.collection = collection;
}
Expand All @@ -19,6 +19,9 @@ class MockDataAdapter {
async save_item(item) {}
async load(item) {}
}
const default_adapter_export = {
collection: MockCollectionDataAdapter
};

// Create an environment with the specified item type and mock data adapter
function create_env_and_collection(itemType = CollectionItem) {
Expand All @@ -31,7 +34,7 @@ function create_env_and_collection(itemType = CollectionItem) {
opts: {
collections: {
test_items: {
data_adapter: MockDataAdapter
data_adapter: default_adapter_export
}
}
},
Expand Down
5 changes: 5 additions & 0 deletions smart-entities/adapters/_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,8 @@ export class EntityVectorAdapter {
throw new Error('EntityVectorAdapter.delete_vec() not implemented');
}
}

export default {
collection: EntitiesVectorAdapter,
item: EntityVectorAdapter
};
7 changes: 6 additions & 1 deletion smart-entities/adapters/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,9 @@ export class DefaultEntityVectorAdapter extends EntityVectorAdapter {
this.item.data.embeddings[this.item.embed_model_key].vec = vec;
}

}
}

export default {
collection: DefaultEntitiesVectorAdapter,
item: DefaultEntityVectorAdapter
};
47 changes: 5 additions & 42 deletions smart-sources/adapters/_adapter.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,4 @@
import { create_hash } from "../utils/create_hash.js";
/**
* @deprecated
*/
export class SourceAdapter {
constructor(item, opts = {}) {
this.item = item;
this.opts = opts;
}
get collection() { return this.item.collection; }
get env() { return this.collection.env; }
get smart_change() { return this.collection.smart_change; }
get block_collection() { return this.env.smart_blocks; }
get source_collection() { return this.env.smart_sources; }

// override these methods in the adapter class
throw_not_implemented(method_name) {
throw new Error(`Method "${method_name}" is not implemented for file type "${this.item.file_type}" in "${this.constructor.name}".`);
}
// source methods
async import() { this.throw_not_implemented('import'); }
async append(content) { this.throw_not_implemented('append'); }
async update(full_content, opts = {}) { this.throw_not_implemented('update'); }
// async _update(content) { this.throw_not_implemented('_update'); }
async read(opts = {}) { this.throw_not_implemented('read'); }
// async _read() { this.throw_not_implemented('_read'); }
async remove() { this.throw_not_implemented('remove'); }
async move_to(entity_ref) { this.throw_not_implemented('move_to'); }
async merge(content, opts = {}) { this.throw_not_implemented('merge'); }

// block methods
async block_append(content) { this.throw_not_implemented('block_append'); }
async block_update(full_content, opts = {}) { this.throw_not_implemented('block_update'); }
async _block_update(content) { this.throw_not_implemented('_block_update'); }
async block_read(opts = {}) { this.throw_not_implemented('block_read'); }
async _block_read() { this.throw_not_implemented('_block_read'); }
async block_remove() { this.throw_not_implemented('block_remove'); }
async block_move_to(entity_ref) { this.throw_not_implemented('block_move_to'); }
async block_merge(content, opts = {}) { this.throw_not_implemented('block_merge'); }
// HELPER METHODS
async create_hash(content) { return await create_hash(content); }
}

export class SourceContentAdapter {
constructor(item) {
Expand All @@ -53,4 +12,8 @@ export class SourceContentAdapter {
// HELPER METHODS
get data() { return this.item.data; }
async create_hash(content) { return await create_hash(content); }
}
}

export default {
item: SourceContentAdapter
};
7 changes: 6 additions & 1 deletion smart-sources/adapters/_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,9 @@ export class FileSourceContentAdapter extends SourceContentAdapter {
await this.update(new_content);
}

}
}

export default {
collection: null, // No collection adapter for this base file
item: FileSourceContentAdapter
};
27 changes: 0 additions & 27 deletions smart-sources/adapters/_test.js

This file was deleted.

5 changes: 5 additions & 0 deletions smart-sources/adapters/data/ajson_multi_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,8 @@ export class AjsonMultiFileSourceDataAdapter extends AjsonMultiFileItemDataAdapt
return 'smart_sources';
}
}

export default {
collection: AjsonMultiFileSourcesDataAdapter,
item: AjsonMultiFileSourceDataAdapter
};
5 changes: 5 additions & 0 deletions smart-sources/adapters/data/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,8 @@ export class SqliteSourceDataAdapter extends SqliteCollectionDataAdapter {
}
}
}

export default {
collection: SqliteSourceDataAdapter,
item: SqliteSourceDataAdapter
};
10 changes: 0 additions & 10 deletions smart-sources/adapters/file.js

This file was deleted.

Loading

0 comments on commit b03d200

Please sign in to comment.