imodel 0.3.0 → 0.3.1

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/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * imodel v0.3.0
2
+ * imodel v0.3.1
3
3
  * (c) 2019-2025 undefined
4
4
  * @license undefined
5
5
  */
@@ -679,11 +679,11 @@ interface IndexInfo extends IndexOptions {
679
679
  interface Index extends IndexInfo {
680
680
  name: string;
681
681
  }
682
- interface TableDefine<T extends Fields = Fields> {
682
+ interface TableDefine<T extends Fields = Fields, TT extends string | VirtualTable | void = string | VirtualTable | void> {
683
683
  /** 字段信息 */
684
684
  readonly fields: T;
685
685
  /** 表名 */
686
- readonly table?: string | VirtualTable;
686
+ readonly table: TT;
687
687
  /** 是否启用软删除 */
688
688
  readonly pseudo?: string;
689
689
  /** 索引信息 */
@@ -800,23 +800,23 @@ interface MatchArg extends Omit<TableDefine<Fields<keyof FieldType>>, 'table'> {
800
800
  type BuildValue<T extends TableDefine> = T extends Build<infer R> ? R : object;
801
801
  interface DBField {
802
802
  type: string;
803
- size: number;
804
- scale: number;
803
+ size?: number;
804
+ scale?: number;
805
805
  default?: any;
806
- nullable: boolean;
807
- array: boolean;
806
+ nullable?: boolean;
807
+ array?: boolean;
808
808
  primary?: number;
809
809
  }
810
810
  interface DBIndex {
811
811
  fields: string[];
812
- includes: string[];
813
- unique: boolean;
812
+ includes?: string[];
813
+ unique?: boolean;
814
814
  name?: string;
815
815
  }
816
816
  interface DBTable {
817
817
  table: string;
818
818
  fields: Record<string, DBField>;
819
- indexes: DBIndex[];
819
+ indexes?: DBIndex[];
820
820
  primary?: string;
821
821
  }
822
822
  interface IConnection<E extends object = object> {
@@ -1192,10 +1192,15 @@ declare class Connection<E extends {} = {}> {
1192
1192
  */
1193
1193
  loadTables(tables: string[]): Promise<DBTable[]>;
1194
1194
  /**
1195
- * @param {DBTable[]} tables
1195
+ * @param {TableDefine | DBTable} table
1196
+ * @returns {Promise<void>}
1197
+ */
1198
+ syncTable(table: TableDefine | DBTable): Promise<void>;
1199
+ /**
1200
+ * @param {(TableDefine | DBTable)[]} tables
1196
1201
  * @returns {Promise<void>}
1197
1202
  */
1198
- syncTables(tables: DBTable[]): Promise<void>;
1203
+ syncTables(tables: (TableDefine | DBTable)[]): Promise<void>;
1199
1204
  /**
1200
1205
  * @template T
1201
1206
  * @overload
package/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * imodel v0.3.0
2
+ * imodel v0.3.1
3
3
  * (c) 2019-2025 undefined
4
4
  * @license undefined
5
5
  */
@@ -1389,15 +1389,116 @@ async function pseudoDeleteMany(conn, queryable, update, env) {
1389
1389
  return list.length;
1390
1390
  }
1391
1391
 
1392
+ /** @import { TableDefine } from '../types/table' */
1393
+ /** @import { DBIndex, DBTable } from '../types/connection' */
1394
+ /**
1395
+ *
1396
+ * @param {(TableDefine | DBTable)[]} tables
1397
+ */
1398
+ function table2db(tables) {
1399
+ /** @type {DBTable[]} */
1400
+ const allTables = [];
1401
+ /** @type {Map<string, DBTable & {indexes: DBIndex[]}>} */
1402
+ const map = new Map();
1403
+ const saved = new Set();
1404
+ /** @type {(TableDefine | DBTable)[]} */
1405
+ const list = [...tables];
1406
+ for (let define = list.pop(); define; define = list.pop()) {
1407
+ if (saved.has(define)) {
1408
+ continue;
1409
+ }
1410
+ saved.add(define);
1411
+ const {
1412
+ table,
1413
+ fields,
1414
+ indexes
1415
+ } = define;
1416
+ if (table && typeof table === 'string') {
1417
+ let t = map.get(table);
1418
+ if (!t) {
1419
+ t = {
1420
+ table,
1421
+ fields: Object.create(null),
1422
+ indexes: []
1423
+ };
1424
+ map.set(table, t);
1425
+ allTables.push(t);
1426
+ }
1427
+ const dbFields = t.fields;
1428
+ for (const [name, field] of Object.entries(fields)) {
1429
+ const {
1430
+ type,
1431
+ size,
1432
+ scale,
1433
+ default: value,
1434
+ nullable,
1435
+ array,
1436
+ primary
1437
+ } = field;
1438
+ if (!type) {
1439
+ continue;
1440
+ }
1441
+ const old = dbFields[name];
1442
+ if (typeof type === 'string') {
1443
+ if (!old) {
1444
+ t.fields[name] = {
1445
+ type,
1446
+ size,
1447
+ scale,
1448
+ default: value,
1449
+ nullable,
1450
+ array,
1451
+ primary
1452
+ };
1453
+ } else {
1454
+ t.fields[name] = {
1455
+ type,
1456
+ size: size || old.size,
1457
+ scale: scale || old.scale,
1458
+ default: value ?? old.default,
1459
+ nullable: nullable ?? old.nullable,
1460
+ array: array ?? old.array,
1461
+ primary: primary || old.primary
1462
+ };
1463
+ }
1464
+ continue;
1465
+ }
1466
+ if (typeof type !== 'object') {
1467
+ continue;
1468
+ }
1469
+ if (type.table) {
1470
+ continue;
1471
+ }
1472
+ if (!old) {
1473
+ t.fields[name] = {
1474
+ type: 'object',
1475
+ nullable,
1476
+ array
1477
+ };
1478
+ } else {
1479
+ old.type = 'object';
1480
+ old.array = array ?? old.array;
1481
+ old.nullable = nullable ?? old.nullable;
1482
+ }
1483
+ }
1484
+ for (const index of indexes || []) {
1485
+ t.indexes.push(index);
1486
+ }
1487
+ }
1488
+ for (const {
1489
+ type
1490
+ } of Object.values(fields)) {
1491
+ if (!type || typeof type !== 'object') {
1492
+ continue;
1493
+ }
1494
+ list.push(type);
1495
+ }
1496
+ }
1497
+ return allTables;
1498
+ }
1499
+
1392
1500
  /* eslint-disable max-lines */
1393
1501
  /* eslint-disable max-len */
1394
- /** @import { WhereValue } from '../Where.mjs' */
1395
- /** @import { SetValue } from '../set.mjs' */
1396
- /** @import { Environment, IConnection } from '../types' */
1397
- /** @import { Fields, FieldType, FieldValue, IndexOptions, MainFieldType, TableDefine } from '../types/table' */
1398
- /** @import { Queryable } from '../types/options' */
1399
- /** @import { Column, ColumnOptions } from '../types/column' */
1400
- /** @import { DBTable, GetName, Skip, TableType } from '../types/connection' */
1401
1502
  /**
1402
1503
  *
1403
1504
  * @param {Fields<keyof FieldType>} fields
@@ -2164,12 +2265,20 @@ class Connection {
2164
2265
  return conn.loadTables(this.#env, tables);
2165
2266
  }
2166
2267
  /**
2167
- * @param {DBTable[]} tables
2268
+ * @param {TableDefine | DBTable} table
2269
+ * @returns {Promise<void>}
2270
+ */
2271
+ async syncTable(table) {
2272
+ const conn = await this.#getConnection();
2273
+ return conn.syncTables(this.#env, table2db([table]));
2274
+ }
2275
+ /**
2276
+ * @param {(TableDefine | DBTable)[]} tables
2168
2277
  * @returns {Promise<void>}
2169
2278
  */
2170
2279
  async syncTables(tables) {
2171
2280
  const conn = await this.#getConnection();
2172
- return conn.syncTables(this.#env, tables);
2281
+ return conn.syncTables(this.#env, table2db(tables));
2173
2282
  }
2174
2283
  /**
2175
2284
  * @template T
package/migrate.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * imodel v0.3.0
2
+ * imodel v0.3.1
3
3
  * (c) 2019-2025 undefined
4
4
  * @license undefined
5
5
  */
package/migrate.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * imodel v0.3.0
2
+ * imodel v0.3.1
3
3
  * (c) 2019-2025 undefined
4
4
  * @license undefined
5
5
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imodel",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "main": "index.mjs",
5
5
  "type": "module",
6
6
  "repository": {