mirror of
https://github.com/placeholder-soft/sui-data-sync.git
synced 2026-01-12 22:30:28 +08:00
feat: annotate sql tag for building
This commit is contained in:
3
nx.json
3
nx.json
@@ -29,6 +29,5 @@
|
||||
"targetName": "lint"
|
||||
}
|
||||
}
|
||||
],
|
||||
"nxCloudAccessToken": "OTM1NjljZGItOTgyYi00MDlhLTg0NjQtZjBjOWM4Njg3ODVlfHJlYWQtd3JpdGU="
|
||||
]
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
33
src/index.ts
33
src/index.ts
@@ -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';
|
||||
|
||||
@@ -1,9 +1,17 @@
|
||||
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({
|
||||
type SqlTagType = ReturnType<typeof createSqlTag>;
|
||||
|
||||
const SqlTag: SqlTagType = createSqlTag({
|
||||
typeAliases: {
|
||||
id: z.object({
|
||||
id: z.number(),
|
||||
@@ -11,17 +19,19 @@ export const SQL = {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
"declaration": false,
|
||||
"moduleResolution": "node",
|
||||
"strict": true,
|
||||
"noEmit": true,
|
||||
"noImplicitReturns": true,
|
||||
"noUnusedLocals": false,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist/out-tsc",
|
||||
"declaration": true,
|
||||
"types": ["node"]
|
||||
},
|
||||
"include": ["src/**/*.ts"],
|
||||
|
||||
Reference in New Issue
Block a user