|
| 1 | +const coreSch = { |
| 2 | + "$id": "core.schema.json", |
| 3 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 4 | + "title": "Core", |
| 5 | + "definitions": { |
| 6 | + "spineTypeInOut": { |
| 7 | + "$id": "#spineTypeInOut", |
| 8 | + "description": "SPINE Type", |
| 9 | + "type": "string" |
| 10 | + } |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | + |
| 15 | +const wfSchema = |
| 16 | +{ |
| 17 | + "$id": "workflow.schema.json", |
| 18 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 19 | + "title": "SPINE Workflow schemas", |
| 20 | + "definitions": { |
| 21 | + "name": { |
| 22 | + "description": "Name", |
| 23 | + "type": "string" |
| 24 | + } |
| 25 | + }, |
| 26 | + "anyOf": [ |
| 27 | + { |
| 28 | + "type": "object", |
| 29 | + "properties": { |
| 30 | + "name": { |
| 31 | + "description": "Workflow name", |
| 32 | + "type": "string" |
| 33 | + }, |
| 34 | + "input": { "type": 'number' } |
| 35 | + } |
| 36 | + }, |
| 37 | + { |
| 38 | + "type": "object", |
| 39 | + "properties": { |
| 40 | + "name": { |
| 41 | + "$ref": "#/definitions/name" |
| 42 | + }, |
| 43 | + "input": { |
| 44 | + "oneOf": [ |
| 45 | + { |
| 46 | + "type": "object", |
| 47 | + "properties": { |
| 48 | + "type": { |
| 49 | + "$ref": "core.schema.json#/definitions/spineTypeInOut" |
| 50 | + } |
| 51 | + } |
| 52 | + }, |
| 53 | + { "type": 'string' } |
| 54 | + |
| 55 | + ] |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + ] |
| 60 | +} |
| 61 | + |
| 62 | +const { createActiveRecordEntity, DataSource } = require('nosqlax'); |
| 63 | + |
| 64 | +const ajvOptions = { |
| 65 | + schemas: [ |
| 66 | + coreSch, |
| 67 | + wfSchema |
| 68 | + ], |
| 69 | + allErrors: true |
| 70 | +}; |
| 71 | + |
| 72 | +// Datasource (connexion) |
| 73 | +const ds = new DataSource({ |
| 74 | + url: 'http://localhost:5984', |
| 75 | + database: 'nosqlax-test' |
| 76 | +}) |
| 77 | + |
| 78 | +// Create the User class using the helper |
| 79 | +const User = createActiveRecordEntity( |
| 80 | + "User", |
| 81 | + "user", |
| 82 | + "workflow.schema.json", // passing id of schema and passing schema in the ajvoption with dependencies |
| 83 | + ds, |
| 84 | + { |
| 85 | + ajvOptions, |
| 86 | + methods: { |
| 87 | + async findByType(t) { |
| 88 | + return this.findOne({ inputType: { $eq: t } }); |
| 89 | + }, |
| 90 | + async getViewA(options) { |
| 91 | + return this.dbConnection.view('design', 'view', options) |
| 92 | + // you can also process view results here to return User entities |
| 93 | + } |
| 94 | + }, |
| 95 | + fieldMap: { |
| 96 | + type: "doctype", |
| 97 | + inputType: "input.type" |
| 98 | + } |
| 99 | + |
| 100 | + }) |
| 101 | + |
| 102 | +// Instanciate a user |
| 103 | +const user = new User({ name: "John Custom Field" }); |
| 104 | +user.inputType = "TypeA" |
| 105 | + |
| 106 | +// { name: 'John Custom Field', inputType: 'TypeA' } |
| 107 | +console.log(user.toJSON()) |
| 108 | + |
| 109 | + |
| 110 | +// Define a service class using your entity |
| 111 | +class UserService { |
| 112 | + |
| 113 | + constructor(UserClass) { |
| 114 | + this.UserClass = UserClass; |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + async findUserByType(t) { |
| 119 | + return await this.UserClass.findByType(t); |
| 120 | + } |
| 121 | + |
| 122 | + async findUserByEmailAndName(email, name) { |
| 123 | + return await this.UserClass.findOne({ |
| 124 | + "$and": [ |
| 125 | + { "name": name }, |
| 126 | + { "email": email } |
| 127 | + ] |
| 128 | + } |
| 129 | + ) |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + async getUserById(id) { |
| 134 | + // Fetch a user by ID |
| 135 | + return await this.UserClass.find(id); |
| 136 | + } |
| 137 | + |
| 138 | + async deleteUser(id) { |
| 139 | + // Delete a user by ID |
| 140 | + return await this.UserClass.delete(id); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// instantiate service |
| 145 | + |
| 146 | +const myService = new UserService(User); |
| 147 | + |
| 148 | + |
| 149 | +async function main() { |
| 150 | + await user.save(); |
| 151 | + // Document like this will be created in DB: |
| 152 | + /* { |
| 153 | + "_id": "f9d62b31017e03b71fb0a84a5e01a8bd", |
| 154 | + "_rev": "1-b570d2de4279b4d9d2432cb0f4036e8c", |
| 155 | + "doctype": "user", |
| 156 | + "input": { |
| 157 | + "type": "TypeA" |
| 158 | + }, |
| 159 | + "name": "John Custom Field" |
| 160 | +} */ |
| 161 | + |
| 162 | + const found = await myService.findUserByType("TypeA"); |
| 163 | + console.log(found.toJSON()); |
| 164 | + /* { |
| 165 | + inputType: 'TypeA', |
| 166 | + name: 'John Custom Field', |
| 167 | + _id: 'f9d62b31017e03b71fb0a84a5e01a8bd', |
| 168 | + _rev: '1-b570d2de4279b4d9d2432cb0f4036e8c' |
| 169 | +} |
| 170 | + */ |
| 171 | +} |
| 172 | + |
| 173 | +main(); |
| 174 | + |
0 commit comments