remult 0.23.0-exp.3 → 0.23.0-exp.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -27
- package/esm/src/entity.js +1 -0
- package/package.json +1 -1
- package/src/entity.js +1 -0
package/README.md
CHANGED
|
@@ -22,7 +22,6 @@
|
|
|
22
22
|
<a href="https://discord.gg/GXHk7ZfuG5" rel="nofollow">
|
|
23
23
|
<img alt="Join Discord" src="https://badgen.net/discord/online-members/GXHk7ZfuG5?icon=discord&label=Discord"/>
|
|
24
24
|
</a>
|
|
25
|
-
</a>
|
|
26
25
|
</div>
|
|
27
26
|
|
|
28
27
|
<br/>
|
|
@@ -101,14 +100,14 @@ npm i remult
|
|
|
101
100
|
```ts
|
|
102
101
|
// shared/product.ts
|
|
103
102
|
|
|
104
|
-
import { Entity, Fields } from
|
|
103
|
+
import { Entity, Fields } from 'remult'
|
|
105
104
|
|
|
106
|
-
@Entity(
|
|
107
|
-
allowApiCrud: true
|
|
105
|
+
@Entity('products', {
|
|
106
|
+
allowApiCrud: true,
|
|
108
107
|
})
|
|
109
108
|
export class Product {
|
|
110
109
|
@Fields.string()
|
|
111
|
-
name =
|
|
110
|
+
name = ''
|
|
112
111
|
|
|
113
112
|
@Fields.number()
|
|
114
113
|
unitPrice = 0
|
|
@@ -120,17 +119,17 @@ export class Product {
|
|
|
120
119
|
```ts
|
|
121
120
|
// backend/index.ts
|
|
122
121
|
|
|
123
|
-
import express from
|
|
124
|
-
import { remultExpress } from
|
|
125
|
-
import { Product } from
|
|
122
|
+
import express from 'express'
|
|
123
|
+
import { remultExpress } from 'remult/remult-express'
|
|
124
|
+
import { Product } from '../shared/product'
|
|
126
125
|
|
|
127
126
|
const port = 3001
|
|
128
127
|
const app = express()
|
|
129
128
|
|
|
130
129
|
app.use(
|
|
131
130
|
remultExpress({
|
|
132
|
-
entities: [Product]
|
|
133
|
-
})
|
|
131
|
+
entities: [Product],
|
|
132
|
+
}),
|
|
134
133
|
)
|
|
135
134
|
|
|
136
135
|
app.listen(port, () => {
|
|
@@ -151,13 +150,13 @@ app.listen(port, () => {
|
|
|
151
150
|
```ts
|
|
152
151
|
// frontend/code.ts
|
|
153
152
|
|
|
154
|
-
import { remult } from
|
|
155
|
-
import { Product } from
|
|
153
|
+
import { remult } from 'remult'
|
|
154
|
+
import { Product } from '../shared/product'
|
|
156
155
|
|
|
157
156
|
async function increasePriceOfTofu(priceIncrease: number) {
|
|
158
157
|
const productsRepo = remult.repo(Product)
|
|
159
158
|
|
|
160
|
-
const product = await productsRepo.findFirst({ name:
|
|
159
|
+
const product = await productsRepo.findFirst({ name: 'Tofu' }) // filter is passed through API request all the way to the db
|
|
161
160
|
product.unitPrice += priceIncrease
|
|
162
161
|
productsRepo.save(product) // mutation request updates the db with no boilerplate code
|
|
163
162
|
}
|
|
@@ -179,32 +178,32 @@ static async increasePriceOfTofu(priceIncrease: number) {
|
|
|
179
178
|
### :ballot_box_with_check: Data validation and constraints - defined once
|
|
180
179
|
|
|
181
180
|
```ts
|
|
182
|
-
import { Entity, Fields, Validators } from
|
|
181
|
+
import { Entity, Fields, Validators } from 'remult'
|
|
183
182
|
|
|
184
|
-
@Entity(
|
|
185
|
-
allowApiCrud: true
|
|
183
|
+
@Entity('products', {
|
|
184
|
+
allowApiCrud: true,
|
|
186
185
|
})
|
|
187
186
|
export class Product {
|
|
188
187
|
@Fields.string({
|
|
189
|
-
validate: Validators.required
|
|
188
|
+
validate: Validators.required,
|
|
190
189
|
})
|
|
191
|
-
name =
|
|
190
|
+
name = ''
|
|
192
191
|
|
|
193
192
|
@Fields.string<Product>({
|
|
194
193
|
validate: (product) => {
|
|
195
194
|
if (product.description.trim().length < 50) {
|
|
196
|
-
throw
|
|
195
|
+
throw 'too short'
|
|
197
196
|
}
|
|
198
|
-
}
|
|
197
|
+
},
|
|
199
198
|
})
|
|
200
|
-
description =
|
|
199
|
+
description = ''
|
|
201
200
|
|
|
202
201
|
@Fields.number({
|
|
203
202
|
validate: (_, field) => {
|
|
204
203
|
if (field.value < 0) {
|
|
205
|
-
field.error =
|
|
204
|
+
field.error = 'must not be less than 0' // or: throw "must not be less than 0";
|
|
206
205
|
}
|
|
207
|
-
}
|
|
206
|
+
},
|
|
208
207
|
})
|
|
209
208
|
unitPrice = 0
|
|
210
209
|
}
|
|
@@ -233,20 +232,20 @@ try {
|
|
|
233
232
|
### :lock: Secure the API with fine-grained authorization
|
|
234
233
|
|
|
235
234
|
```ts
|
|
236
|
-
@Entity<Article>(
|
|
235
|
+
@Entity<Article>('Articles', {
|
|
237
236
|
allowApiRead: true,
|
|
238
237
|
allowApiInsert: (_, remult) => remult.authenticated(),
|
|
239
|
-
allowApiUpdate: (article, remult) => article.author.id == remult.user.id
|
|
238
|
+
allowApiUpdate: (article, remult) => article.author.id == remult.user.id,
|
|
240
239
|
})
|
|
241
240
|
export class Article {
|
|
242
241
|
@Fields.string({ allowApiUpdate: false })
|
|
243
|
-
slug =
|
|
242
|
+
slug = ''
|
|
244
243
|
|
|
245
244
|
@Field(() => Profile, { allowApiUpdate: false })
|
|
246
245
|
author!: Profile
|
|
247
246
|
|
|
248
247
|
@Fields.string()
|
|
249
|
-
content =
|
|
248
|
+
content = ''
|
|
250
249
|
}
|
|
251
250
|
```
|
|
252
251
|
|
package/esm/src/entity.js
CHANGED
package/package.json
CHANGED
package/src/entity.js
CHANGED