From 00c30d44492ef9051a56f99c5335c53573bf252a Mon Sep 17 00:00:00 2001 From: BinarySandia04 Date: Sun, 6 Oct 2024 17:43:44 +0200 Subject: [PATCH] Mes encara mes coses --- backend/services/api.js | 95 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-) diff --git a/backend/services/api.js b/backend/services/api.js index 860ec427..b2dee7fc 100644 --- a/backend/services/api.js +++ b/backend/services/api.js @@ -30,10 +30,17 @@ class BackendApi { * Returns a new database model for the plugin * @param {String} name * @param {Object} schema - * @returns {Model} + * @returns {BackendModel} + * @example + * let itemModel = Api.createModel('Item', { + * name: {type: String, required: true, default: "New Item"}, + * description: {type: String} + * type: {type: String, required: true, default: "Misc Item"}, + * image: { type: String } + * }); */ createModel(name, schema){ - return new Model(name, this.#_plugin, schema); + return new BackendModel(name, this.#_plugin, schema); } }; @@ -79,6 +86,21 @@ class BackendRouter { delete(route){ } + + /** + * Creates backend REST routes for the specified model. These routes are located + * relative to the plugin base route: + * - `//` (POST) - Creates the element + * - `//` (GET) - Gets a list with elements + * - `?id=` return the document with that id + * - `?=` returns all documents that match this criteria + * - `//` (UPDATE) - Updates a element by id + * - `//` (DELETE) - Deletes an element + * @param {BackendModel} model + */ + createModelRoutes(model, path){ + + } } /** @@ -88,14 +110,83 @@ class BackendModel { #_name; #_plugin; #_schema; + #_mongoSchema; constructor(name, plugin, schema){ this.#_name = name; this.#_plugin = plugin; this.#_schema = schema; + this.#_mongoSchema = mongoose.model(`${plugin}/${name}`, new Schema(schema)); + } + + /** + * Finds and returns a promise with the result of the query + * @param {Object} params + * @returns {Promise} + */ + find(params){ + return this.#_mongoSchema.find(params); + } + + /** + * Finds one element and returns a promise with the result of the query + * @param {Object} params + * @returns {Promise} + */ + findOne(params){ + return this.#_mongoSchema.findOne(params); + } + + /** + * Finds an element by id and returns a promise with the result of the query + * @param {String} id + * @returns {Promise} + */ + findById(id){ + return this.#_mongoSchema.findById(id); + } + + /** + * Creates an element given the data and returns a promise with the result of the operation + * @param {Object} data + * @returns {Promise} + */ + create(data){ + return this.#_mongoSchema.create(data); + } + + /** + * Updates an element given the parameters with the given data and returns a promise with the result of the operation + * @param {Object} params + * @param {Object} data + * @returns {Promise} + */ + updateOne(params, data){ + return this.#_mongoSchema.updateOne(params, data); + } + + /** + * Updates elements given the parameters with the given data and returns a promise with the result of the operation + * @param {Object} params + * @param {Object} data + * @returns {Promise} + */ + updateMany(params, data){ + return this.#_mongoSchema.updateMany(params, data); } }; +/** + * @hideconstructor + */ +class BackendModelDocument { + + + save(){ + + } +} + module.exports = {