Skip to content

Commit 59b3cba

Browse files
committed
add exempled"
1 parent 6d151c9 commit 59b3cba

4 files changed

Lines changed: 307 additions & 41 deletions

File tree

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

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"scripts": {
1414
"test": "node_modules/jest-cli/bin/jest.js",
1515
"build": "tsc",
16-
"watch": "tsc --watch"
16+
"watch": "tsc --watch",
17+
"data-mapper-basic-example": "node example/basic-example/data-mapper-basic-example.js",
18+
"active-record-basic-example": "node example/basic-example/active-record-basic-example.js"
1719
},
1820
"keywords": [
1921
"couchdb",

0 commit comments

Comments
 (0)