imean-cassandra-orm 3.0.3 → 3.1.0
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/dist/mod.cjs +37 -17
- package/dist/mod.d.cts +6 -3
- package/dist/mod.d.ts +6 -3
- package/dist/mod.js +37 -17
- package/package.json +1 -1
package/dist/mod.cjs
CHANGED
|
@@ -766,13 +766,14 @@ function convertSchemaToFieldConfigs(schema) {
|
|
|
766
766
|
});
|
|
767
767
|
return configs;
|
|
768
768
|
}
|
|
769
|
-
function buildInsertCql(schema) {
|
|
769
|
+
function buildInsertCql(schema, options) {
|
|
770
770
|
const tableRef = schema.keyspace ? `${schema.keyspace}.${schema.tableName}` : schema.tableName;
|
|
771
771
|
const columns = Object.keys(schema.fields.shape);
|
|
772
772
|
const placeholders = columns.map(() => "?").join(", ");
|
|
773
|
+
const ttlSegment = options?.ttl ? ` USING TTL ${options.ttl}` : "";
|
|
773
774
|
return `INSERT INTO ${tableRef} (${columns.join(
|
|
774
775
|
", "
|
|
775
|
-
)}) VALUES (${placeholders});`;
|
|
776
|
+
)}) VALUES (${placeholders})${ttlSegment};`;
|
|
776
777
|
}
|
|
777
778
|
function buildInsertParams(data, fieldConfigs) {
|
|
778
779
|
const params = [];
|
|
@@ -1278,7 +1279,9 @@ function syncIndexes(schema, existingIndexes) {
|
|
|
1278
1279
|
const normalizedExisting = normalizeOptions(existingOptions);
|
|
1279
1280
|
const normalizedNew = normalizeOptions(newOptions);
|
|
1280
1281
|
if (normalizedExisting !== normalizedNew) {
|
|
1281
|
-
operations.push(
|
|
1282
|
+
operations.push(
|
|
1283
|
+
`DROP INDEX IF EXISTS ${schema.keyspace}.${indexName};`
|
|
1284
|
+
);
|
|
1282
1285
|
let createQuery = `CREATE INDEX ${indexName} ON ${schema.keyspace}.${schema.tableName} (${indexInfo.fieldName}) USING 'sai'`;
|
|
1283
1286
|
if (indexInfo.options) {
|
|
1284
1287
|
createQuery += ` WITH OPTIONS = ${indexInfo.options}`;
|
|
@@ -1380,9 +1383,9 @@ var Model = class _Model {
|
|
|
1380
1383
|
const schema = new TableSchema(definition);
|
|
1381
1384
|
return new _Model(schema, client);
|
|
1382
1385
|
}
|
|
1383
|
-
async insert(data) {
|
|
1386
|
+
async insert(data, options) {
|
|
1384
1387
|
this.schema.fields.parse(data);
|
|
1385
|
-
const cassandraQuery = this.buildInsertQuery(data);
|
|
1388
|
+
const cassandraQuery = this.buildInsertQuery(data, options);
|
|
1386
1389
|
const dualWriteManager = this.client.getDualWriteManager();
|
|
1387
1390
|
await Promise.all([
|
|
1388
1391
|
this.client.execute(cassandraQuery.query, cassandraQuery.params, {
|
|
@@ -1431,10 +1434,10 @@ var Model = class _Model {
|
|
|
1431
1434
|
)
|
|
1432
1435
|
]);
|
|
1433
1436
|
}
|
|
1434
|
-
async batchInsert(dataArray) {
|
|
1437
|
+
async batchInsert(dataArray, options) {
|
|
1435
1438
|
dataArray.forEach((data) => this.schema.fields.parse(data));
|
|
1436
1439
|
const cassandraQueries = dataArray.map(
|
|
1437
|
-
(data) => this.buildInsertQuery(data)
|
|
1440
|
+
(data) => this.buildInsertQuery(data, options)
|
|
1438
1441
|
);
|
|
1439
1442
|
const dualWriteManager = this.client.getDualWriteManager();
|
|
1440
1443
|
const validatedDataArray = dataArray.map(
|
|
@@ -1527,12 +1530,19 @@ var Model = class _Model {
|
|
|
1527
1530
|
// 同步 Schema
|
|
1528
1531
|
async syncSchema(force = false) {
|
|
1529
1532
|
try {
|
|
1530
|
-
console.log(
|
|
1533
|
+
console.log(
|
|
1534
|
+
`\u{1F504} \u5F00\u59CB\u540C\u6B65\u8868: ${this.schema.keyspace}.${this.schema.tableName}`
|
|
1535
|
+
);
|
|
1531
1536
|
const operations = await this.syncTableSchema(force);
|
|
1532
|
-
console.log(
|
|
1537
|
+
console.log(
|
|
1538
|
+
`\u2705 \u8868 ${this.schema.keyspace}.${this.schema.tableName} \u540C\u6B65\u5B8C\u6210`
|
|
1539
|
+
);
|
|
1533
1540
|
return operations.join("\n");
|
|
1534
1541
|
} catch (error) {
|
|
1535
|
-
console.error(
|
|
1542
|
+
console.error(
|
|
1543
|
+
`\u274C \u8868 ${this.schema.keyspace}.${this.schema.tableName} \u540C\u6B65\u5931\u8D25:`,
|
|
1544
|
+
error
|
|
1545
|
+
);
|
|
1536
1546
|
throw error;
|
|
1537
1547
|
}
|
|
1538
1548
|
}
|
|
@@ -1540,12 +1550,18 @@ var Model = class _Model {
|
|
|
1540
1550
|
async syncTableSchema(forceRecreate = false) {
|
|
1541
1551
|
const operations = [];
|
|
1542
1552
|
try {
|
|
1543
|
-
console.log(
|
|
1553
|
+
console.log(
|
|
1554
|
+
` \u{1F4CB} \u8868\u7ED3\u6784: ${this.schema.keyspace}.${this.schema.tableName}`
|
|
1555
|
+
);
|
|
1544
1556
|
console.log(` \u{1F511} \u5206\u533A\u952E: [${this.schema.partitionKey.join(", ")}]`);
|
|
1545
1557
|
if (this.schema.clusteringKey) {
|
|
1546
|
-
console.log(
|
|
1558
|
+
console.log(
|
|
1559
|
+
` \u{1F4CA} \u805A\u7C7B\u952E: ${Object.entries(this.schema.clusteringKey).map(([k, v]) => `${k}:${v}`).join(", ")}`
|
|
1560
|
+
);
|
|
1547
1561
|
}
|
|
1548
|
-
console.log(
|
|
1562
|
+
console.log(
|
|
1563
|
+
` \u{1F4DD} \u5B57\u6BB5\u6570\u91CF: ${Object.keys(this.schema.fields.shape).length}`
|
|
1564
|
+
);
|
|
1549
1565
|
await this.client.execute(
|
|
1550
1566
|
queryHelper.ensureKeyspace(
|
|
1551
1567
|
this.schema.keyspace,
|
|
@@ -1662,7 +1678,9 @@ ${reasons}
|
|
|
1662
1678
|
operations.push(...indexOperations);
|
|
1663
1679
|
return { operations, changes, unalterableReasons };
|
|
1664
1680
|
} catch (error) {
|
|
1665
|
-
throw new Error(
|
|
1681
|
+
throw new Error(
|
|
1682
|
+
`\u5206\u6790\u8868 ${this.schema.keyspace}.${this.schema.tableName} \u7ED3\u6784\u53D8\u66F4\u5931\u8D25: ${error}`
|
|
1683
|
+
);
|
|
1666
1684
|
}
|
|
1667
1685
|
}
|
|
1668
1686
|
// 智能同步索引
|
|
@@ -1761,7 +1779,9 @@ ${reasons}
|
|
|
1761
1779
|
if (!this.isTypeCompatible(existingType, schemaType)) {
|
|
1762
1780
|
return {
|
|
1763
1781
|
hasChanges: true,
|
|
1764
|
-
reasons: [
|
|
1782
|
+
reasons: [
|
|
1783
|
+
`\u5B57\u6BB5\u7C7B\u578B\u4E0D\u517C\u5BB9: ${field} (${existingType} \u2192 ${schemaType})`
|
|
1784
|
+
]
|
|
1765
1785
|
};
|
|
1766
1786
|
}
|
|
1767
1787
|
}
|
|
@@ -1835,9 +1855,9 @@ ${reasons}
|
|
|
1835
1855
|
return false;
|
|
1836
1856
|
}
|
|
1837
1857
|
// 构建插入查询
|
|
1838
|
-
buildInsertQuery(data) {
|
|
1858
|
+
buildInsertQuery(data, options) {
|
|
1839
1859
|
const fieldConfigs = queryHelper.convertSchemaToFieldConfigs(this.schema);
|
|
1840
|
-
const insertQuery = queryHelper.buildInsertCql(this.schema);
|
|
1860
|
+
const insertQuery = queryHelper.buildInsertCql(this.schema, options);
|
|
1841
1861
|
const params = queryHelper.buildInsertParams(data, fieldConfigs);
|
|
1842
1862
|
return { query: insertQuery, params };
|
|
1843
1863
|
}
|
package/dist/mod.d.cts
CHANGED
|
@@ -19,9 +19,12 @@ interface ClickHouseConfig {
|
|
|
19
19
|
}
|
|
20
20
|
type DualWriteConfig = {
|
|
21
21
|
clickhouse?: ClickHouseConfig;
|
|
22
|
-
errorHandling?:
|
|
22
|
+
errorHandling?: "throw" | "ignore" | "log";
|
|
23
23
|
debug?: boolean;
|
|
24
24
|
};
|
|
25
|
+
interface InsertOptions {
|
|
26
|
+
ttl?: number;
|
|
27
|
+
}
|
|
25
28
|
|
|
26
29
|
declare class ClickHouseClientWrapper {
|
|
27
30
|
private client;
|
|
@@ -341,10 +344,10 @@ declare class Model<F extends z.ZodRawShape, PK extends (keyof F)[], CK extends
|
|
|
341
344
|
clusteringKey: CK extends ClusteringKeyConfig ? z.ZodObject<ExtractClusteringKeyShape<F, CK>, z.UnknownKeysParam, z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<ExtractClusteringKeyShape<F, CK>>, any> extends infer T_4 ? { [k_4 in keyof T_4]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<ExtractClusteringKeyShape<F, CK>>, any>[k_4]; } : never, z.baseObjectInputType<ExtractClusteringKeyShape<F, CK>> extends infer T_5 ? { [k_5 in keyof T_5]: z.baseObjectInputType<ExtractClusteringKeyShape<F, CK>>[k_5]; } : never> : never;
|
|
342
345
|
};
|
|
343
346
|
static create<F extends z.ZodRawShape, PK extends (keyof F)[], CK extends ClusteringKeyConfig | undefined = undefined>(definition: SchemaDefinition<F, PK, CK>, client: Client): Model<F, PK, CK>;
|
|
344
|
-
insert(data: z.infer<z.ZodObject<F
|
|
347
|
+
insert(data: z.infer<z.ZodObject<F>>, options?: InsertOptions): Promise<void>;
|
|
345
348
|
update(partitionKey: ExtractPartitionKeyType<F, PK>, clusteringKey: CK extends ClusteringKeyConfig ? ExtractClusteringKeyType<F, CK> : undefined, data: Partial<z.infer<z.ZodObject<F>>>): Promise<void>;
|
|
346
349
|
delete(partitionKey: ExtractPartitionKeyType<F, PK>, clusteringKey?: ExtractClusteringKeyType<F, CK>): Promise<void>;
|
|
347
|
-
batchInsert(dataArray: z.infer<z.ZodObject<F>>[]): Promise<void>;
|
|
350
|
+
batchInsert(dataArray: z.infer<z.ZodObject<F>>[], options?: InsertOptions): Promise<void>;
|
|
348
351
|
find<T extends QueryCondition<F>>(query: T, clusteringKey?: Partial<ExtractClusteringKeyType<F, CK>>): Promise<z.infer<z.ZodObject<F>>[]>;
|
|
349
352
|
findOne<T extends QueryCondition<F>>(query: T): Promise<z.infer<z.ZodObject<F>> | null>;
|
|
350
353
|
syncSchema(force?: boolean): Promise<string>;
|
package/dist/mod.d.ts
CHANGED
|
@@ -19,9 +19,12 @@ interface ClickHouseConfig {
|
|
|
19
19
|
}
|
|
20
20
|
type DualWriteConfig = {
|
|
21
21
|
clickhouse?: ClickHouseConfig;
|
|
22
|
-
errorHandling?:
|
|
22
|
+
errorHandling?: "throw" | "ignore" | "log";
|
|
23
23
|
debug?: boolean;
|
|
24
24
|
};
|
|
25
|
+
interface InsertOptions {
|
|
26
|
+
ttl?: number;
|
|
27
|
+
}
|
|
25
28
|
|
|
26
29
|
declare class ClickHouseClientWrapper {
|
|
27
30
|
private client;
|
|
@@ -341,10 +344,10 @@ declare class Model<F extends z.ZodRawShape, PK extends (keyof F)[], CK extends
|
|
|
341
344
|
clusteringKey: CK extends ClusteringKeyConfig ? z.ZodObject<ExtractClusteringKeyShape<F, CK>, z.UnknownKeysParam, z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<ExtractClusteringKeyShape<F, CK>>, any> extends infer T_4 ? { [k_4 in keyof T_4]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<ExtractClusteringKeyShape<F, CK>>, any>[k_4]; } : never, z.baseObjectInputType<ExtractClusteringKeyShape<F, CK>> extends infer T_5 ? { [k_5 in keyof T_5]: z.baseObjectInputType<ExtractClusteringKeyShape<F, CK>>[k_5]; } : never> : never;
|
|
342
345
|
};
|
|
343
346
|
static create<F extends z.ZodRawShape, PK extends (keyof F)[], CK extends ClusteringKeyConfig | undefined = undefined>(definition: SchemaDefinition<F, PK, CK>, client: Client): Model<F, PK, CK>;
|
|
344
|
-
insert(data: z.infer<z.ZodObject<F
|
|
347
|
+
insert(data: z.infer<z.ZodObject<F>>, options?: InsertOptions): Promise<void>;
|
|
345
348
|
update(partitionKey: ExtractPartitionKeyType<F, PK>, clusteringKey: CK extends ClusteringKeyConfig ? ExtractClusteringKeyType<F, CK> : undefined, data: Partial<z.infer<z.ZodObject<F>>>): Promise<void>;
|
|
346
349
|
delete(partitionKey: ExtractPartitionKeyType<F, PK>, clusteringKey?: ExtractClusteringKeyType<F, CK>): Promise<void>;
|
|
347
|
-
batchInsert(dataArray: z.infer<z.ZodObject<F>>[]): Promise<void>;
|
|
350
|
+
batchInsert(dataArray: z.infer<z.ZodObject<F>>[], options?: InsertOptions): Promise<void>;
|
|
348
351
|
find<T extends QueryCondition<F>>(query: T, clusteringKey?: Partial<ExtractClusteringKeyType<F, CK>>): Promise<z.infer<z.ZodObject<F>>[]>;
|
|
349
352
|
findOne<T extends QueryCondition<F>>(query: T): Promise<z.infer<z.ZodObject<F>> | null>;
|
|
350
353
|
syncSchema(force?: boolean): Promise<string>;
|
package/dist/mod.js
CHANGED
|
@@ -764,13 +764,14 @@ function convertSchemaToFieldConfigs(schema) {
|
|
|
764
764
|
});
|
|
765
765
|
return configs;
|
|
766
766
|
}
|
|
767
|
-
function buildInsertCql(schema) {
|
|
767
|
+
function buildInsertCql(schema, options) {
|
|
768
768
|
const tableRef = schema.keyspace ? `${schema.keyspace}.${schema.tableName}` : schema.tableName;
|
|
769
769
|
const columns = Object.keys(schema.fields.shape);
|
|
770
770
|
const placeholders = columns.map(() => "?").join(", ");
|
|
771
|
+
const ttlSegment = options?.ttl ? ` USING TTL ${options.ttl}` : "";
|
|
771
772
|
return `INSERT INTO ${tableRef} (${columns.join(
|
|
772
773
|
", "
|
|
773
|
-
)}) VALUES (${placeholders});`;
|
|
774
|
+
)}) VALUES (${placeholders})${ttlSegment};`;
|
|
774
775
|
}
|
|
775
776
|
function buildInsertParams(data, fieldConfigs) {
|
|
776
777
|
const params = [];
|
|
@@ -1276,7 +1277,9 @@ function syncIndexes(schema, existingIndexes) {
|
|
|
1276
1277
|
const normalizedExisting = normalizeOptions(existingOptions);
|
|
1277
1278
|
const normalizedNew = normalizeOptions(newOptions);
|
|
1278
1279
|
if (normalizedExisting !== normalizedNew) {
|
|
1279
|
-
operations.push(
|
|
1280
|
+
operations.push(
|
|
1281
|
+
`DROP INDEX IF EXISTS ${schema.keyspace}.${indexName};`
|
|
1282
|
+
);
|
|
1280
1283
|
let createQuery = `CREATE INDEX ${indexName} ON ${schema.keyspace}.${schema.tableName} (${indexInfo.fieldName}) USING 'sai'`;
|
|
1281
1284
|
if (indexInfo.options) {
|
|
1282
1285
|
createQuery += ` WITH OPTIONS = ${indexInfo.options}`;
|
|
@@ -1378,9 +1381,9 @@ var Model = class _Model {
|
|
|
1378
1381
|
const schema = new TableSchema(definition);
|
|
1379
1382
|
return new _Model(schema, client);
|
|
1380
1383
|
}
|
|
1381
|
-
async insert(data) {
|
|
1384
|
+
async insert(data, options) {
|
|
1382
1385
|
this.schema.fields.parse(data);
|
|
1383
|
-
const cassandraQuery = this.buildInsertQuery(data);
|
|
1386
|
+
const cassandraQuery = this.buildInsertQuery(data, options);
|
|
1384
1387
|
const dualWriteManager = this.client.getDualWriteManager();
|
|
1385
1388
|
await Promise.all([
|
|
1386
1389
|
this.client.execute(cassandraQuery.query, cassandraQuery.params, {
|
|
@@ -1429,10 +1432,10 @@ var Model = class _Model {
|
|
|
1429
1432
|
)
|
|
1430
1433
|
]);
|
|
1431
1434
|
}
|
|
1432
|
-
async batchInsert(dataArray) {
|
|
1435
|
+
async batchInsert(dataArray, options) {
|
|
1433
1436
|
dataArray.forEach((data) => this.schema.fields.parse(data));
|
|
1434
1437
|
const cassandraQueries = dataArray.map(
|
|
1435
|
-
(data) => this.buildInsertQuery(data)
|
|
1438
|
+
(data) => this.buildInsertQuery(data, options)
|
|
1436
1439
|
);
|
|
1437
1440
|
const dualWriteManager = this.client.getDualWriteManager();
|
|
1438
1441
|
const validatedDataArray = dataArray.map(
|
|
@@ -1525,12 +1528,19 @@ var Model = class _Model {
|
|
|
1525
1528
|
// 同步 Schema
|
|
1526
1529
|
async syncSchema(force = false) {
|
|
1527
1530
|
try {
|
|
1528
|
-
console.log(
|
|
1531
|
+
console.log(
|
|
1532
|
+
`\u{1F504} \u5F00\u59CB\u540C\u6B65\u8868: ${this.schema.keyspace}.${this.schema.tableName}`
|
|
1533
|
+
);
|
|
1529
1534
|
const operations = await this.syncTableSchema(force);
|
|
1530
|
-
console.log(
|
|
1535
|
+
console.log(
|
|
1536
|
+
`\u2705 \u8868 ${this.schema.keyspace}.${this.schema.tableName} \u540C\u6B65\u5B8C\u6210`
|
|
1537
|
+
);
|
|
1531
1538
|
return operations.join("\n");
|
|
1532
1539
|
} catch (error) {
|
|
1533
|
-
console.error(
|
|
1540
|
+
console.error(
|
|
1541
|
+
`\u274C \u8868 ${this.schema.keyspace}.${this.schema.tableName} \u540C\u6B65\u5931\u8D25:`,
|
|
1542
|
+
error
|
|
1543
|
+
);
|
|
1534
1544
|
throw error;
|
|
1535
1545
|
}
|
|
1536
1546
|
}
|
|
@@ -1538,12 +1548,18 @@ var Model = class _Model {
|
|
|
1538
1548
|
async syncTableSchema(forceRecreate = false) {
|
|
1539
1549
|
const operations = [];
|
|
1540
1550
|
try {
|
|
1541
|
-
console.log(
|
|
1551
|
+
console.log(
|
|
1552
|
+
` \u{1F4CB} \u8868\u7ED3\u6784: ${this.schema.keyspace}.${this.schema.tableName}`
|
|
1553
|
+
);
|
|
1542
1554
|
console.log(` \u{1F511} \u5206\u533A\u952E: [${this.schema.partitionKey.join(", ")}]`);
|
|
1543
1555
|
if (this.schema.clusteringKey) {
|
|
1544
|
-
console.log(
|
|
1556
|
+
console.log(
|
|
1557
|
+
` \u{1F4CA} \u805A\u7C7B\u952E: ${Object.entries(this.schema.clusteringKey).map(([k, v]) => `${k}:${v}`).join(", ")}`
|
|
1558
|
+
);
|
|
1545
1559
|
}
|
|
1546
|
-
console.log(
|
|
1560
|
+
console.log(
|
|
1561
|
+
` \u{1F4DD} \u5B57\u6BB5\u6570\u91CF: ${Object.keys(this.schema.fields.shape).length}`
|
|
1562
|
+
);
|
|
1547
1563
|
await this.client.execute(
|
|
1548
1564
|
queryHelper.ensureKeyspace(
|
|
1549
1565
|
this.schema.keyspace,
|
|
@@ -1660,7 +1676,9 @@ ${reasons}
|
|
|
1660
1676
|
operations.push(...indexOperations);
|
|
1661
1677
|
return { operations, changes, unalterableReasons };
|
|
1662
1678
|
} catch (error) {
|
|
1663
|
-
throw new Error(
|
|
1679
|
+
throw new Error(
|
|
1680
|
+
`\u5206\u6790\u8868 ${this.schema.keyspace}.${this.schema.tableName} \u7ED3\u6784\u53D8\u66F4\u5931\u8D25: ${error}`
|
|
1681
|
+
);
|
|
1664
1682
|
}
|
|
1665
1683
|
}
|
|
1666
1684
|
// 智能同步索引
|
|
@@ -1759,7 +1777,9 @@ ${reasons}
|
|
|
1759
1777
|
if (!this.isTypeCompatible(existingType, schemaType)) {
|
|
1760
1778
|
return {
|
|
1761
1779
|
hasChanges: true,
|
|
1762
|
-
reasons: [
|
|
1780
|
+
reasons: [
|
|
1781
|
+
`\u5B57\u6BB5\u7C7B\u578B\u4E0D\u517C\u5BB9: ${field} (${existingType} \u2192 ${schemaType})`
|
|
1782
|
+
]
|
|
1763
1783
|
};
|
|
1764
1784
|
}
|
|
1765
1785
|
}
|
|
@@ -1833,9 +1853,9 @@ ${reasons}
|
|
|
1833
1853
|
return false;
|
|
1834
1854
|
}
|
|
1835
1855
|
// 构建插入查询
|
|
1836
|
-
buildInsertQuery(data) {
|
|
1856
|
+
buildInsertQuery(data, options) {
|
|
1837
1857
|
const fieldConfigs = queryHelper.convertSchemaToFieldConfigs(this.schema);
|
|
1838
|
-
const insertQuery = queryHelper.buildInsertCql(this.schema);
|
|
1858
|
+
const insertQuery = queryHelper.buildInsertCql(this.schema, options);
|
|
1839
1859
|
const params = queryHelper.buildInsertParams(data, fieldConfigs);
|
|
1840
1860
|
return { query: insertQuery, params };
|
|
1841
1861
|
}
|