neopg 2.0.7 → 2.0.8

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.cn.md CHANGED
@@ -196,6 +196,8 @@ db.define({
196
196
  }
197
197
  })
198
198
 
199
+ console.log(db.has('User'))
200
+
199
201
  ```
200
202
 
201
203
  ### 同步数据库
@@ -228,6 +230,10 @@ const db = new NeoPG(config)
228
230
  // 注意:这是一个异步方法,因为它兼容 ESM (.mjs) 的动态导入
229
231
  await db.loadModels('./models')
230
232
 
233
+
234
+ // 加载文件列表
235
+ await db.loadFiles(['./models2/WxUser.js', './models2/Role.js'])
236
+
231
237
  // 加载完成后即可同步或使用
232
238
  await db.sync()
233
239
  ```
package/README.md CHANGED
@@ -195,6 +195,8 @@ db.define({
195
195
  }
196
196
  })
197
197
 
198
+ console.log(db.has('User'))
199
+
198
200
  ```
199
201
 
200
202
  ### Syncing Database
@@ -227,6 +229,9 @@ const db = new NeoPG(config)
227
229
  // This is asynchronous because it supports .mjs dynamic imports
228
230
  await db.loadModels('./models')
229
231
 
232
+ //load files
233
+ await db.loadFiles(['./models2/WxUser.js', './models2/Role.js'])
234
+
230
235
  // Now you can sync and use them
231
236
  await db.sync()
232
237
  ```
package/lib/NeoPG.js CHANGED
@@ -268,6 +268,43 @@ class NeoPG {
268
268
  return this
269
269
  }
270
270
 
271
+ /**
272
+ *
273
+ * @param {string} mname
274
+ */
275
+ has(mname) {
276
+ return this.registry.has(mname)
277
+ }
278
+
279
+ /**
280
+ *
281
+ * @param {array} mlist
282
+ */
283
+ async loadFiles(mlist) {
284
+ if (typeof mlist === 'string') {
285
+ mlist = [mlist]
286
+ }
287
+
288
+ if (!Array.isArray(mlist)) {
289
+ throw new Error(`[NeoPG] mlist is not [string]`)
290
+ }
291
+
292
+ let modelExport
293
+
294
+ for (let m of mlist) {
295
+ if (m.endsWith('.mjs')) {
296
+ const imported = await import(m)
297
+ modelExport = imported.default
298
+ } else if (m.endsWith('.js')) {
299
+ modelExport = require(m)
300
+ } else {
301
+ continue
302
+ }
303
+
304
+ this.add(modelExport)
305
+ }
306
+ }
307
+
271
308
  }
272
309
 
273
310
  NeoPG.dataTypes = dataTypes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neopg",
3
- "version": "2.0.7",
3
+ "version": "2.0.8",
4
4
  "description": "orm for postgres",
5
5
  "keywords": [
6
6
  "neopg",
package/test/test-db.js CHANGED
@@ -197,6 +197,8 @@ db.add(User)
197
197
 
198
198
  db.sync({force: true, debug: true, model: 'ShopOrder'})
199
199
 
200
+ console.log('test has', db.has('User'))
201
+
200
202
  await db.model('ShopOrder').where('1=1').delete()
201
203
  await db.model('ShopOrder').insert([
202
204
  {