feat: annotate sql tag for building

This commit is contained in:
Zitao Xiong
2024-05-24 18:15:15 +08:00
parent dea0863fdc
commit c4c6392907
6 changed files with 84 additions and 58 deletions

View File

@@ -29,6 +29,5 @@
"targetName": "lint"
}
}
],
"nxCloudAccessToken": "OTM1NjljZGItOTgyYi00MDlhLTg0NjQtZjBjOWM4Njg3ODVlfHJlYWQtd3JpdGU="
]
}

View File

@@ -1,5 +1,40 @@
import { NestFactory } from '@nestjs/core';
import {
DataSyncModule,
DataSyncService,
EventSyncSchema,
} from './lib/data-sync';
const schema: EventSyncSchema = {
tableSchema: 'gifted',
transactionModule: 'simple_gift_box',
events: [
{
eventName: 'GiftBoxMinted',
fields: { object_id: 'buffer', name: 'string', creator: 'buffer' },
},
],
};
async function main() {
const app = await NestFactory.createMicroservice(DataSyncModule, {
logger: ['log', 'verbose', 'error', 'warn', 'debug', 'fatal'],
});
const syncService = app.get(DataSyncService);
syncService.setSchemas([schema]);
syncService.addSyncMoveEventType(
`0xceba50ec29ada96392373f340fe4eeffab45140ac66acc9459770e5a3c58abf8::simple_gift_box::GiftBoxMinted`,
);
syncService.startSync();
}
describe('main test', () => {
it('should pass', () => {
expect(true).toBe(true);
});
it.skip('should run main', async () => {
await main();
});
});

View File

@@ -1,32 +1 @@
import { NestFactory } from '@nestjs/core';
import {
DataSyncModule,
DataSyncService,
EventSyncSchema,
} from './lib/data-sync';
const schema: EventSyncSchema = {
tableSchema: 'gifted',
transactionModule: 'simple_gift_box',
events: [
{
eventName: 'GiftBoxMinted',
fields: { object_id: 'buffer', name: 'string', creator: 'buffer' },
},
],
};
async function main() {
const app = await NestFactory.createMicroservice(DataSyncModule, {
logger: ['log', 'verbose', 'error', 'warn', 'debug', 'fatal'],
});
const syncService = app.get(DataSyncService);
syncService.setSchemas([schema]);
syncService.addSyncMoveEventType(
`0xceba50ec29ada96392373f340fe4eeffab45140ac66acc9459770e5a3c58abf8::simple_gift_box::GiftBoxMinted`,
);
syncService.startSync();
}
main().catch(console.error);
export * from './lib/data-sync';

View File

@@ -1,27 +1,37 @@
import { createSqlTag, type SerializableValue } from 'slonik';
import {
BinarySqlToken,
createSqlTag,
JsonBinarySqlToken,
SerializableValue,
sql,
SqlTag,
} from 'slonik';
import z from 'zod';
import { BufferSchema } from '../model/base.model';
export const SQL = {
...createSqlTag({
typeAliases: {
id: z.object({
id: z.number(),
}),
void: z.object({}).strict(),
any: z.any(),
},
}),
type SqlTagType = ReturnType<typeof createSqlTag>;
const SqlTag: SqlTagType = createSqlTag({
typeAliases: {
id: z.object({
id: z.number(),
}),
void: z.object({}).strict(),
any: z.any(),
},
});
const SqlHelper = {
bigint: (bigInt: bigint) => bigInt.toString(10),
number: (num: number) => num,
string: (str: string) => str,
boolean: (bool: boolean) => bool,
bool: (bool: boolean) => bool,
buffer: (buffer: string | Buffer) => {
buffer: (buffer: string | Buffer): BinarySqlToken => {
if (typeof buffer === 'string') {
return SQL.binary(BufferSchema.parse(buffer));
return sql.binary(BufferSchema.parse(buffer));
}
return SQL.binary(buffer);
return sql.binary(buffer);
},
get nullish() {
return {
@@ -31,17 +41,21 @@ export const SQL = {
}
return num;
},
date: (date: Date | null | undefined) => {
date: (
date: Date | null | undefined,
): ReturnType<SqlTag['date']> | null => {
if (date == null) {
return null;
}
return SQL.date(date);
return sql.date(date);
},
timestamp: (date: Date | null | undefined) => {
timestamp: (
date: Date | null | undefined,
): ReturnType<SqlTag['timestamp']> | null => {
if (date == null) {
return null;
}
return SQL.timestamp(date);
return sql.timestamp(date);
},
bigint: (bigInt: bigint | null | undefined) => {
if (bigInt == null) {
@@ -63,22 +77,31 @@ export const SQL = {
},
binary: SQL_to_buffer,
buffer: SQL_to_buffer,
jsonb: (json: SerializableValue | null | undefined) => {
jsonb: (
json: SerializableValue | null | undefined,
): null | JsonBinarySqlToken => {
if (json == null) {
return null;
}
return SQL.jsonb(json);
return sql.jsonb(json);
},
};
},
};
function SQL_to_buffer(buffer: string | Buffer | null | undefined) {
export const SQL: SqlTagType & typeof SqlHelper = {
...SqlTag,
...SqlHelper,
};
function SQL_to_buffer(
buffer: string | Buffer | null | undefined,
): BinarySqlToken | null {
if (buffer == null) {
return null;
}
if (typeof buffer === 'string') {
return SQL.binary(BufferSchema.parse(buffer));
return sql.binary(BufferSchema.parse(buffer));
}
return SQL.binary(buffer);
return sql.binary(buffer);
}

View File

@@ -5,7 +5,6 @@
"declaration": false,
"moduleResolution": "node",
"strict": true,
"noEmit": true,
"noImplicitReturns": true,
"noUnusedLocals": false,
"noUncheckedIndexedAccess": true,

View File

@@ -2,6 +2,7 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"include": ["src/**/*.ts"],