|
| 1 | + |
| 2 | + |
| 3 | +const { createDataMapperEntity, DataSource, createRepository } = require('nosqlax'); |
| 4 | + |
| 5 | +const schema = { |
| 6 | + "$id": "my-schema", |
| 7 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 8 | + "type": "object", |
| 9 | + "definitions": { |
| 10 | + "address": { |
| 11 | + "type": "object", |
| 12 | + "properties": { |
| 13 | + "city": { "type": "string" } |
| 14 | + } |
| 15 | + } |
| 16 | + }, |
| 17 | + "properties": { |
| 18 | + "name": { "type": "string" }, |
| 19 | + "address": { |
| 20 | + "$ref": "#/definitions/address" |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// Datasource (connexion) |
| 26 | +const ds = new DataSource({ |
| 27 | + url: 'http://localhost:5984', |
| 28 | + database: 'nosqlax-test' |
| 29 | +}) |
| 30 | + |
| 31 | +// Create the User class using the helper |
| 32 | +const User = createDataMapperEntity( |
| 33 | + "User", |
| 34 | + "user", |
| 35 | + schema, |
| 36 | + {}) |
| 37 | + |
| 38 | +// Instanciate a user |
| 39 | +const user = new User({ name: "John" }); |
| 40 | +user.address = { "city": "Lyon" } |
| 41 | + |
| 42 | +// { name: 'John', address: { city: 'Lyon' } } |
| 43 | +console.log(user.toJSON()) |
| 44 | + |
| 45 | +const userRepository = createRepository( |
| 46 | + User, |
| 47 | + ds, |
| 48 | + { |
| 49 | + methods: { |
| 50 | + async findByName(name) { |
| 51 | + return this.findOne({ name: { $eq: name } }); |
| 52 | + }, |
| 53 | + async getViewA(options) { |
| 54 | + return this.dbConnection.view('design', 'view', options) |
| 55 | + // you can also process view results here to return User entities |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +) |
| 60 | + |
| 61 | + |
| 62 | +// Define a service class user your repo |
| 63 | +class UserService { |
| 64 | + constructor(userRepository) { |
| 65 | + this.userRepository = userRepository; |
| 66 | + } |
| 67 | + |
| 68 | + async saveUser(userData) { |
| 69 | + // Create a new user using the UserRepository |
| 70 | + return await this.userRepository.save(userData); |
| 71 | + } |
| 72 | + |
| 73 | + async findUserByName(name) { |
| 74 | + return await this.userRepository.findByName(name); |
| 75 | + } |
| 76 | + |
| 77 | + async findUserByEmailAndName(email, name) { |
| 78 | + return await this.userRepository.findOne({ |
| 79 | + "$and": [ |
| 80 | + { "name": name }, |
| 81 | + { "email": email } |
| 82 | + ] |
| 83 | + } |
| 84 | + ) |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + async getUserById(id) { |
| 89 | + // Fetch a user by ID |
| 90 | + return await this.userRepository.find(id); |
| 91 | + } |
| 92 | + |
| 93 | + async deleteUser(id) { |
| 94 | + // Delete a user by ID |
| 95 | + return await this.userRepository.delete(id); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// instantiate service |
| 100 | + |
| 101 | +const myService = new UserService(userRepository); |
| 102 | + |
| 103 | + |
| 104 | +async function main() { |
| 105 | + await myService.saveUser(user); |
| 106 | + // Document like this will be created in DB: |
| 107 | + /* { |
| 108 | + "_id": "f9d62b31017e03b71fb0a84a5e000a08", |
| 109 | + "_rev": "1-8d43f9216da5ae302f393886daa52765", |
| 110 | + "name": "John", |
| 111 | + "address": { |
| 112 | + "city": "Lyon" |
| 113 | + }, |
| 114 | + "type": "user" |
| 115 | + } */ |
| 116 | + |
| 117 | + const found = await myService.findUserByName("John"); |
| 118 | + console.log(found.toJSON()); |
| 119 | + /* { |
| 120 | + name: 'John', |
| 121 | + address: { city: 'Lyon' }, |
| 122 | + _id: 'f9d62b31017e03b71fb0a84a5e000a08', |
| 123 | + _rev: '1-8d43f9216da5ae302f393886daa52765' |
| 124 | + } */ |
| 125 | +} |
| 126 | + |
| 127 | +main(); |
| 128 | + |
| 129 | + |
0 commit comments