Skip to content

Commit 060a184

Browse files
committed
v1.1é
1 parent 2d35f59 commit 060a184

9 files changed

Lines changed: 448 additions & 274 deletions

File tree

README.md

Lines changed: 261 additions & 131 deletions
Large diffs are not rendered by default.

example/basic-example/active-record-basic-example.js

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,7 @@ const ds = new DataSource({
2626
database: 'nosqlax-test'
2727
})
2828

29-
// Create the User class using the helper
30-
const User = createActiveRecordEntity(
31-
"User", // class name
32-
"user", // type
33-
schema, // Schema or schema ID
34-
ds, // Data source
35-
{ // additional query methods
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-
})
4829

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())
5530

5631

5732
// Define a service class using your entity
@@ -88,12 +63,38 @@ class UserService {
8863
}
8964
}
9065

91-
// instantiate service
9266

93-
const myService = new UserService(User);
9467

9568

9669
async function main() {
70+
71+
// Create the User class using the helper
72+
const User = await createActiveRecordEntity(
73+
"User", // class name
74+
"user", // type
75+
schema, // Schema or schema ID
76+
ds, // Data source
77+
{ // additional query methods
78+
79+
methods: {
80+
async findByName(name) {
81+
return this.findOne({ name: { $eq: name } });
82+
},
83+
async getViewA(options) {
84+
return this.dbConnection.view('design', 'view', options)
85+
// you can also process view results here to return User entities
86+
}
87+
}
88+
89+
})
90+
91+
// Instanciate a user
92+
const user = new User({ name: "John Active" });
93+
user.address = { "city": "Lyon" }
94+
95+
// { name: 'John', address: { city: 'Lyon' } }
96+
console.log(user.toJSON())
97+
9798
await user.save();
9899
// Document like this will be created in DB:
99100
/* {
@@ -105,7 +106,9 @@ async function main() {
105106
},
106107
"type": "user"
107108
} */
109+
// instantiate service
108110

111+
const myService = new UserService(User);
109112
const found = await myService.findUserByName("John Active");
110113
console.log(found.toJSON());
111114
/* {

example/basic-example/data-mapper-basic-example.js

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,7 @@ const ds = new DataSource({
2828
database: 'nosqlax-test'
2929
})
3030

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-
)
31+
6032

6133

6234
// Define a service class user your repo
@@ -96,12 +68,42 @@ class UserService {
9668
}
9769
}
9870

99-
// instantiate service
10071

101-
const myService = new UserService(userRepository);
72+
async function main() {
73+
// Create the User class using the helper
74+
const User = await createDataMapperEntity(
75+
"User",
76+
"user",
77+
schema,
78+
{})
79+
80+
// Instanciate a user
81+
const user = new User({ name: "John" });
82+
user.address = { "city": "Lyon" }
83+
84+
// { name: 'John', address: { city: 'Lyon' } }
85+
console.log(user.toJSON())
86+
87+
const userRepository = createRepository(
88+
User,
89+
ds,
90+
{
91+
methods: {
92+
async findByName(name) {
93+
return this.findOne({ name: { $eq: name } });
94+
},
95+
async getViewA(options) {
96+
return this.dbConnection.view('design', 'view', options)
97+
// you can also process view results here to return User entities
98+
}
99+
}
100+
}
101+
)
102102

103+
// instantiate service
104+
105+
const myService = new UserService(userRepository);
103106

104-
async function main() {
105107
await myService.saveUser(user);
106108
// Document like this will be created in DB:
107109
/* {

example/complex-schema/complex-schema-example.js

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -75,36 +75,7 @@ const ds = new DataSource({
7575
database: 'nosqlax-test'
7676
})
7777

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-
}
9978

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())
10879

10980

11081
// Define a service class using your entity
@@ -141,12 +112,41 @@ class UserService {
141112
}
142113
}
143114

144-
// instantiate service
145115

146-
const myService = new UserService(User);
147116

148117

149118
async function main() {
119+
// Create the User class using the helper
120+
const User = await createActiveRecordEntity(
121+
"User",
122+
"user",
123+
"workflow.schema.json", // passing id of schema and passing schema in the ajvoption with dependencies
124+
ds,
125+
{
126+
ajvOptions,
127+
methods: {
128+
async findByType(t) {
129+
return this.findOne({ inputType: { $eq: t } });
130+
},
131+
async getViewA(options) {
132+
return this.dbConnection.view('design', 'view', options)
133+
// you can also process view results here to return User entities
134+
}
135+
},
136+
fieldMap: {
137+
type: "doctype",
138+
inputType: "input.type"
139+
}
140+
141+
})
142+
143+
// Instanciate a user
144+
const user = new User({ name: "John Custom Field" });
145+
user.inputType = "TypeA"
146+
147+
// { name: 'John Custom Field', inputType: 'TypeA' }
148+
console.log(user.toJSON())
149+
150150
await user.save();
151151
// Document like this will be created in DB:
152152
/* {
@@ -158,7 +158,9 @@ async function main() {
158158
},
159159
"name": "John Custom Field"
160160
} */
161+
// instantiate service
161162

163+
const myService = new UserService(User);
162164
const found = await myService.findUserByType("TypeA");
163165
console.log(found.toJSON());
164166
/* {

example/custom-field-mapping/custom-field-mapping-example.js

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,8 @@ const ds = new DataSource({
2626
database: 'nosqlax-test'
2727
})
2828

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 findByCity(city) {
39-
return this.findOne({ city: { $eq: city } });
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-
fieldMap: {
47-
type: "doctype",
48-
city: "address.city"
49-
}
5029

51-
})
5230

53-
// Instanciate a user
54-
const user = new User({ name: "John Custom Field" });
55-
user.city = "Lyon"
56-
57-
// { name: 'John Custom Field', city: 'Lyon' }
58-
console.log(user.toJSON())
5931

6032

6133
// Define a service class using your entity
@@ -92,12 +64,47 @@ class UserService {
9264
}
9365
}
9466

95-
// instantiate service
9667

97-
const myService = new UserService(User);
9868

9969

10070
async function main() {
71+
72+
// Create the User class using the helper
73+
const User = await createActiveRecordEntity(
74+
"User",
75+
"user",
76+
schema,
77+
ds,
78+
{
79+
80+
methods: {
81+
async findByCity(city) {
82+
return this.findOne({ city: { $eq: city } });
83+
},
84+
async getViewA(options) {
85+
return this.dbConnection.view('design', 'view', options)
86+
// you can also process view results here to return User entities
87+
}
88+
},
89+
fieldMap: {
90+
type: "doctype",
91+
city: "address.city"
92+
}
93+
94+
})
95+
96+
// Instanciate a user
97+
const user = new User({ name: "John Custom Field" });
98+
user.city = "Lyon"
99+
100+
// { name: 'John Custom Field', city: 'Lyon' }
101+
console.log(user.toJSON())
102+
103+
104+
// instantiate service
105+
106+
const myService = new UserService(User);
107+
101108
await user.save();
102109
// Document like this will be created in DB:
103110
/* {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nosqlax",
3-
"version": "1.0.3",
3+
"version": "1.1.0",
44
"description": "NoSQLax is a lightweight JavaScript library designed to simplify and streamline CRUD operations with CouchDB. NoSQLax provides a unified and intuitive repository pattern to handle your data effortlessly.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/core/BaseEntity.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ abstract class BaseEntity implements IBaseEntity {
159159
this.initializeData(data);
160160
}
161161

162-
static initialize(): void {
162+
static async initialize(): Promise<void> {
163163
if (initializedClasses.has(this)) return;
164164

165165
const ctor = this as typeof BaseEntity;
@@ -196,7 +196,8 @@ abstract class BaseEntity implements IBaseEntity {
196196
// compile to dereference the schema
197197
const validator = ajv.compile(rawSchema);
198198
const resolvedSchema = validator.schema as AnySchemaObject;
199-
const restructured = restructureSchemaFromFieldMap(resolvedSchema, fieldMap);
199+
const dereferencedSchema = await $RefParser.dereference(rawSchema);
200+
const restructured = restructureSchemaFromFieldMap(dereferencedSchema, fieldMap);
200201

201202
const schemaProperties = restructured.properties || {};
202203
const schemaDefs: any = restructured.definitions;

0 commit comments

Comments
 (0)